package org.gcube.common.portal; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Properties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Clients can obtain the single instance of the {@link PortalContext} by invoking its static method {@link #getConfiguration()}. * The first invocation of the method triggers the initialisation of the instance. * * @author Massimiliano Assante (ISTI-CNR) * */ public class PortalContext { private static final Logger _log = LoggerFactory.getLogger(PortalContext.class); public static final String INFRASTRUCTURE_NAME = "infrastructure"; public static final String SCOPES = "scopes"; protected static PortalContext singleton = new PortalContext(); private String infra; private String vos; private PortalContext() { initialize(); } /** * * @return the instance */ public synchronized static PortalContext getConfiguration() { return singleton == null ? new PortalContext() : singleton; } private void initialize() { Properties props = new Properties(); try { String propertyfile = getGCoreFolder() + File.separator + "infrastructure.properties"; File propsFile = new File(propertyfile); FileInputStream fis = new FileInputStream(propsFile); props.load( fis); infra = props.getProperty(INFRASTRUCTURE_NAME); vos = props.getProperty(SCOPES); } catch(IOException e) { infra = "gcube"; vos = ""; _log.error("infrastructure.properties file not found under $CATALINA_HOME/ dir, setting default infrastructure Name and no Scopes" + infra); } _log.info("PortalContext configurator correctly initialized on " + infra); } /** * * @return the infrastructure name in which your client runs */ public String getInfrastructureName() { return this.infra; } /** * * @return the value of the scopes as it is in the property file (a string with comma separated vales) */ public String getVOsAsString() { return this.vos; } /** * * @return the value of the scopes */ public List getVOs() { List toReturn = new ArrayList(); if (vos == null || vos.equals("")) return toReturn; String[] split = vos.split(","); for (int i = 0; i < split.length; i++) { toReturn.add(split[i].trim()); } return toReturn; } /** * * @return $CATALINA_HOME */ private static String getGCoreFolder() { return (System.getenv("GLOBUS_LOCATION").endsWith("/") ? System.getenv("GLOBUS_LOCATION") : System.getenv("GLOBUS_LOCATION")+"/"); } }