2013-09-24 15:26:13 +02:00
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 ;
/ * *
2013-09-24 15:32:30 +02:00
* Clients can obtain the single instance of the { @link PortalContext } by invoking its static method { @link # getConfiguration ( ) } .
2013-09-24 15:26:13 +02:00
* The first invocation of the method triggers the initialisation of the instance .
*
* @author Massimiliano Assante ( ISTI - CNR )
*
* /
2013-09-24 15:32:30 +02:00
public class PortalContext {
private static final Logger _log = LoggerFactory . getLogger ( PortalContext . class ) ;
2013-09-24 15:26:13 +02:00
2013-09-30 15:19:28 +02:00
private static final String INFRASTRUCTURE_NAME = " infrastructure " ;
2015-06-22 15:24:39 +02:00
private static final String SCOPES = " scopes " ;
2015-07-08 12:09:20 +02:00
private static final String SENDER_EMAIL = " notificationSenderEmail " ;
private static final String GATEWAY_NAME = " portalinstancename " ;
2016-02-25 15:55:46 +01:00
private static final String ADMIN_USERNAME = " administratorUsername " ;
2015-07-08 12:09:20 +02:00
2013-09-30 15:19:28 +02:00
private static PortalContext singleton = new PortalContext ( ) ;
2013-09-24 15:26:13 +02:00
private String infra ;
private String vos ;
2013-09-24 15:32:30 +02:00
private PortalContext ( ) {
2013-09-24 15:26:13 +02:00
initialize ( ) ;
}
/ * *
*
* @return the instance
* /
2013-09-24 15:32:30 +02:00
public synchronized static PortalContext getConfiguration ( ) {
return singleton = = null ? new PortalContext ( ) : singleton ;
2013-09-24 15:26:13 +02:00
}
2013-09-24 17:09:28 +02:00
2013-09-24 15:26:13 +02:00
private void initialize ( ) {
Properties props = new Properties ( ) ;
try {
2013-10-01 15:11:45 +02:00
String propertyfile = getCatalinaHome ( ) + File . separator + " conf " + File . separator + " infrastructure.properties " ;
2013-09-24 15:26:13 +02:00
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 " ;
2013-10-01 15:14:42 +02:00
vos = " devsec " ;
2013-10-17 17:46:16 +02:00
_log . error ( " infrastructure.properties file not found under $CATALINA_HOME/conf/ dir, setting default infrastructure Name and devsec as VO Name " + infra ) ;
2013-09-24 15:26:13 +02:00
}
2013-09-24 15:32:30 +02:00
_log . info ( " PortalContext configurator correctly initialized on " + infra ) ;
2013-09-24 15:26:13 +02:00
}
2013-09-24 17:09:28 +02:00
/ * *
*
* @return the infrastructure name in which your client runs
* /
2013-09-24 15:26:13 +02:00
public String getInfrastructureName ( ) {
return this . infra ;
}
2013-09-24 17:09:28 +02:00
/ * *
*
* @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
* /
2013-09-24 15:26:13 +02:00
public List < String > getVOs ( ) {
List < String > toReturn = new ArrayList < String > ( ) ;
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 ;
}
2015-07-08 12:09:20 +02:00
/ * *
2016-02-25 15:55:46 +01:00
*
* @deprecated use getConfiguration ( ) . getGatewayName ( ) method
* read the portal instance name from a property file and returns it
2015-07-08 12:09:20 +02:00
* /
2016-02-25 15:55:46 +01:00
@Deprecated
2015-07-08 12:09:20 +02:00
public static String getPortalInstanceName ( ) {
2016-02-25 15:55:46 +01:00
return getConfiguration ( ) . getGatewayName ( ) ;
}
/ * *
* read the infrastructure gateway name from a property file and returns it
* /
public String getGatewayName ( ) {
2015-07-08 12:09:20 +02:00
//get the portles to look for from the property file
Properties props = new Properties ( ) ;
String toReturn = " " ;
2015-06-22 15:24:39 +02:00
2015-07-08 12:09:20 +02:00
try {
String propertyfile = getCatalinaHome ( ) + File . separator + " conf " + File . separator + " gcube-data.properties " ;
File propsFile = new File ( propertyfile ) ;
FileInputStream fis = new FileInputStream ( propsFile ) ;
props . load ( fis ) ;
toReturn = props . getProperty ( GATEWAY_NAME ) ;
}
//catch exception in case properties file does not exist
catch ( IOException e ) {
toReturn = " D4science Gateway " ;
_log . error ( " gcube-data.properties file not found under $CATALINA_HOME/conf dir, returning default Portal Name " + toReturn ) ;
return toReturn ;
}
_log . debug ( " Returning Gateway Name: " + toReturn ) ;
return toReturn ;
}
2015-06-22 15:24:39 +02:00
/ * *
2015-07-08 12:09:20 +02:00
* read the sender ( from ) email address for notifications name from a property file and returns it
2015-06-22 15:24:39 +02:00
* /
2015-06-22 15:48:38 +02:00
public String getSenderEmail ( ) {
2015-06-22 15:24:39 +02:00
//get the portles to look for from the property file
Properties props = new Properties ( ) ;
String toReturn = " " ;
try {
String propertyfile = getCatalinaHome ( ) + File . separator + " conf " + File . separator + " gcube-data.properties " ;
File propsFile = new File ( propertyfile ) ;
FileInputStream fis = new FileInputStream ( propsFile ) ;
props . load ( fis ) ;
2015-07-08 12:09:20 +02:00
toReturn = props . getProperty ( SENDER_EMAIL ) ;
2015-06-22 15:24:39 +02:00
}
//catch exception in case properties file does not exist
catch ( IOException e ) {
toReturn = " do-not-reply@d4science.org " ;
_log . error ( " gcube-data.properties file not found under $CATALINA_HOME/conf dir, returning default Email " + toReturn ) ;
return toReturn ;
}
_log . debug ( " Returning SENDER_EMAIL: " + toReturn ) ;
return toReturn ;
}
2016-02-25 15:55:46 +01:00
/ * *
2016-03-14 14:33:53 +01:00
* use org . gcube . vomanagement . usermanagement . impl . LiferayUserManager # getAdmin method
2016-02-25 15:55:46 +01:00
* /
2016-03-14 14:33:53 +01:00
@Deprecated
2016-02-25 15:55:46 +01:00
public String getAdministratorUsername ( ) {
//get the portles to look for from the property file
Properties props = new Properties ( ) ;
String toReturn = " " ;
try {
String propertyfile = getCatalinaHome ( ) + File . separator + " conf " + File . separator + " gcube-data.properties " ;
File propsFile = new File ( propertyfile ) ;
FileInputStream fis = new FileInputStream ( propsFile ) ;
props . load ( fis ) ;
toReturn = props . getProperty ( ADMIN_USERNAME ) ;
}
//catch exception in case properties file does not exist
catch ( IOException e ) {
toReturn = " massimiliano.assante " ;
_log . error ( " gcube-data.properties file not found under $CATALINA_HOME/conf dir, returning default administrator " + toReturn ) ;
return toReturn ;
}
_log . debug ( " Returning Administrator username: " + toReturn ) ;
return toReturn ;
}
2015-06-22 15:24:39 +02:00
/ * *
2013-09-24 15:26:13 +02:00
*
* @return $CATALINA_HOME
* /
2013-10-01 15:11:45 +02:00
private static String getCatalinaHome ( ) {
return ( System . getenv ( " CATALINA_HOME " ) . endsWith ( " / " ) ? System . getenv ( " CATALINA_HOME " ) : System . getenv ( " CATALINA_HOME " ) + " / " ) ;
2013-09-24 15:26:13 +02:00
}
}