2013-01-10 14:19:53 +01:00
package org.gcube.portal.databook.server ;
2013-09-25 15:42:08 +02:00
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor ;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor ;
2017-05-16 17:43:12 +02:00
import java.io.File ;
import java.io.FileInputStream ;
2013-01-10 14:19:53 +01:00
import java.io.IOException ;
import java.io.Serializable ;
import java.util.List ;
import java.util.Properties ;
2017-05-16 17:43:12 +02:00
import org.gcube.common.portal.GCubePortalConstants ;
2013-09-25 15:42:08 +02:00
import org.gcube.common.portal.PortalContext ;
import org.gcube.common.resources.gcore.ServiceEndpoint ;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint ;
import org.gcube.common.scope.api.ScopeProvider ;
2013-01-10 14:19:53 +01:00
import org.gcube.portal.databook.shared.ex.TooManyRunningClustersException ;
2013-09-25 15:42:08 +02:00
import org.gcube.resources.discovery.client.api.DiscoveryClient ;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2013-01-10 14:19:53 +01:00
/ * *
* @author Massimiliano Assante ISTI - CNR
*
* @version 0 . 1 Dec 2012
*
* /
@SuppressWarnings ( " serial " )
public class RunningCluster implements Serializable {
/ * *
* logger
* /
2013-09-25 15:42:08 +02:00
private static final Logger _log = LoggerFactory . getLogger ( RunningCluster . class ) ;
2013-01-10 14:19:53 +01:00
/ * *
* properties to read
* /
private static final String HOST_PROPERTY = " host " ;
private static final String HOST_PORT_PROPERTY = " port " ;
private static final String CLUSTER_NAME_PROPERTY = " cluster " ;
private static final String KEY_SPACE_NAME_PROPERTY = " keyspace " ;
/ * *
* other constants
* /
private final static String RUNTIME_RESOURCE_NAME = " SocialPortalDataStore " ;
private final static String PLATFORM_NAME = " Cassandra " ;
private static final String DEFAULT_CONFIGURATION = " /org/gcube/portal/databook/server/resources/databook.properties " ;
private static RunningCluster singleton ;
/ * *
* Host
* /
private String host ;
/ * *
* Cluster Name
* /
private String clusterName ;
/ * *
* Keyspace Name
* /
private String keyspaceName ;
2016-02-04 16:52:46 +01:00
2013-01-10 14:19:53 +01:00
/ * *
2016-09-15 15:54:48 +02:00
* @param infrastructureName could be null
2013-01-10 14:19:53 +01:00
* @return an instance of the RunningCluster
* /
2016-02-04 16:52:46 +01:00
public static synchronized RunningCluster getInstance ( String infrastructureName ) {
2013-01-10 14:19:53 +01:00
if ( singleton = = null ) {
2016-02-04 16:52:46 +01:00
singleton = new RunningCluster ( infrastructureName ) ;
2013-01-10 14:19:53 +01:00
}
return singleton ;
}
/ * *
* private constructor
* /
2016-02-04 16:52:46 +01:00
private RunningCluster ( String infrastructureName ) {
2013-01-10 14:19:53 +01:00
try {
2016-02-04 16:52:46 +01:00
List < ServiceEndpoint > resources = getConfigurationFromIS ( infrastructureName ) ;
2013-01-10 14:19:53 +01:00
if ( resources . size ( ) > 1 ) {
_log . error ( " Too many Runtime Resource having name " + RUNTIME_RESOURCE_NAME + " in this scope " ) ;
throw new TooManyRunningClustersException ( " There exist more than 1 Runtime Resource in this scope having name "
2016-02-04 16:52:46 +01:00
+ RUNTIME_RESOURCE_NAME + " and Platform " + PLATFORM_NAME + " . Only one allowed per infrasrtucture. " ) ;
2013-01-10 14:19:53 +01:00
}
else if ( resources . size ( ) = = 0 ) {
_log . error ( " There is no Runtime Resource having name " + RUNTIME_RESOURCE_NAME + " and Platform " + PLATFORM_NAME + " in this scope. Using default configuration properties: " + DEFAULT_CONFIGURATION ) ;
2016-02-04 16:52:46 +01:00
loadDefaultConfiguration ( ) ;
2013-01-10 14:19:53 +01:00
}
else {
2013-09-25 15:42:08 +02:00
for ( ServiceEndpoint res : resources ) {
AccessPoint found = res . profile ( ) . accessPoints ( ) . iterator ( ) . next ( ) ;
host = found . address ( ) ;
clusterName = found . description ( ) ;
keyspaceName = found . name ( ) ;
2013-01-10 14:19:53 +01:00
}
}
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
}
/ * *
*
* @return the
* @throws Exception
* /
2016-02-04 16:52:46 +01:00
private List < ServiceEndpoint > getConfigurationFromIS ( String infrastructureName ) throws Exception {
2016-09-15 15:54:48 +02:00
_log . debug ( " getConfigurationFromIS infrastructureName= " + infrastructureName ) ;
2016-02-04 16:52:46 +01:00
String scope = " / " ;
if ( infrastructureName ! = null & & ! infrastructureName . isEmpty ( ) )
scope + = infrastructureName ;
2016-09-15 15:54:48 +02:00
else {
2017-05-16 17:43:12 +02:00
scope + = readInfrastructureName ( ) ;
2016-09-15 15:54:48 +02:00
_log . debug ( " infrastrucute name is null, setting root scope= " + scope ) ;
}
2013-09-25 15:42:08 +02:00
String currScope = ScopeProvider . instance . get ( ) ;
ScopeProvider . instance . set ( scope ) ;
SimpleQuery query = queryFor ( ServiceEndpoint . class ) ;
query . addCondition ( " $resource/Profile/Name/text() eq ' " + RUNTIME_RESOURCE_NAME + " ' " ) ;
query . addCondition ( " $resource/Profile/Platform/Name/text() eq ' " + PLATFORM_NAME + " ' " ) ;
DiscoveryClient < ServiceEndpoint > client = clientFor ( ServiceEndpoint . class ) ;
List < ServiceEndpoint > toReturn = client . submit ( query ) ;
ScopeProvider . instance . set ( currScope ) ;
return toReturn ;
2017-05-16 17:43:12 +02:00
}
private String readInfrastructureName ( ) {
Properties props = new Properties ( ) ;
try {
StringBuilder sb = new StringBuilder ( getCatalinaHome ( ) ) ;
sb . append ( File . separator )
. append ( PortalContext . CONFIGURATION_FOLDER )
. append ( File . separator )
. append ( PortalContext . INFRA_PROPERTY_FILENAME ) ;
String propertyfile = sb . toString ( ) ;
File propsFile = new File ( propertyfile ) ;
FileInputStream fis = new FileInputStream ( propsFile ) ;
props . load ( fis ) ;
return props . getProperty ( GCubePortalConstants . INFRASTRUCTURE_NAME ) ;
}
catch ( IOException e ) {
_log . error ( " infrastructure.properties file not found under $CATALINA_HOME/conf/ dir, setting default infrastructure Name " + " gcube " ) ;
return " gcube " ;
}
}
2013-01-10 14:19:53 +01:00
/ * *
*
* /
private void loadDefaultConfiguration ( ) {
Properties props = new Properties ( ) ;
try {
props . load ( CassandraClusterConnection . class . getResourceAsStream ( DEFAULT_CONFIGURATION ) ) ;
host = props . getProperty ( HOST_PROPERTY ) + " : " + props . getProperty ( HOST_PORT_PROPERTY ) ;
clusterName = props . getProperty ( CLUSTER_NAME_PROPERTY ) ;
keyspaceName = props . getProperty ( KEY_SPACE_NAME_PROPERTY ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
}
public String getHost ( ) {
return host ;
}
public void setHost ( String host ) {
this . host = host ;
}
public String getClusterName ( ) {
return clusterName ;
}
public void setClusterName ( String clusterName ) {
this . clusterName = clusterName ;
}
public String getKeyspaceName ( ) {
return keyspaceName ;
}
public void setKeyspaceName ( String keyspaceName ) {
this . keyspaceName = keyspaceName ;
}
@Override
public String toString ( ) {
return " RunningCluster [host= " + host + " , clusterName= " + clusterName
+ " , keyspaceName= " + keyspaceName + " ] " ;
}
2017-05-16 17:43:12 +02:00
/ * *
*
* @return $CATALINA_HOME
* /
private static String getCatalinaHome ( ) {
return ( System . getenv ( " CATALINA_HOME " ) . endsWith ( " / " ) ? System . getenv ( " CATALINA_HOME " ) : System . getenv ( " CATALINA_HOME " ) + " / " ) ;
}
2013-01-10 14:19:53 +01:00
}