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 ;
2023-10-30 18:52:02 +01:00
import java.net.InetSocketAddress ;
import java.util.ArrayList ;
2013-01-10 14:19:53 +01:00
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
2023-10-30 18:52:02 +01:00
* @author Ahmed Salah Tawfik Ibrahim ISTI - CNR
*
* @version 2 . 0 . 0 October 2023
2013-01-10 14:19:53 +01:00
*
* /
@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
* /
2023-10-30 18:52:02 +01:00
private static final String HOST_PROPERTY = " host " ;
2013-01-10 14:19:53 +01:00
private static final String HOST_PORT_PROPERTY = " port " ;
2023-10-30 18:52:02 +01:00
private static final String DATACENTER_NAME_PROPERTY = " datacenter " ;
2013-01-10 14:19:53 +01:00
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
* /
2023-10-30 18:52:02 +01:00
private String datacenterName ;
2013-01-10 14:19:53 +01:00
/ * *
* Keyspace Name
* /
2023-10-30 18:52:02 +01:00
private String keyspaceName ; //to be modified
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 ;
2023-10-30 18:52:02 +01:00
}
2013-01-10 14:19:53 +01:00
/ * *
* private constructor
* /
2016-02-04 16:52:46 +01:00
private RunningCluster ( String infrastructureName ) {
2023-10-30 18:52:02 +01:00
//Query the IS (for the future)
/ * 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 ( ) ;
2023-10-30 18:52:02 +01:00
} * /
host = " 10.1.28.55:9042, 10.1.30.142:9042, 10.1.28.100:9042 " ;
datacenterName = " 1 " ;
keyspaceName = " dev_mig_new_schema_test " ;
2013-01-10 14:19:53 +01:00
}
2023-10-30 18:52:02 +01:00
2013-01-10 14:19:53 +01:00
/ * *
2023-10-30 18:52:02 +01:00
*
2013-01-10 14:19:53 +01:00
* @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 ;
2023-10-30 18:52:02 +01:00
}
2017-05-16 17:43:12 +02:00
private String readInfrastructureName ( ) {
2023-10-30 18:52:02 +01:00
2017-05-16 17:43:12 +02:00
Properties props = new Properties ( ) ;
try {
StringBuilder sb = new StringBuilder ( getCatalinaHome ( ) ) ;
sb . append ( File . separator )
2023-10-30 18:52:02 +01:00
. append ( PortalContext . CONFIGURATION_FOLDER )
. append ( File . separator )
. append ( PortalContext . INFRA_PROPERTY_FILENAME ) ;
2017-05-16 17:43:12 +02:00
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 " ;
2023-10-30 18:52:02 +01:00
}
2017-05-16 17:43:12 +02:00
}
2013-01-10 14:19:53 +01:00
/ * *
2023-10-30 18:52:02 +01:00
*
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 ) ;
2023-10-30 18:52:02 +01:00
datacenterName = props . getProperty ( DATACENTER_NAME_PROPERTY ) ;
2013-01-10 14:19:53 +01:00
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 getKeyspaceName ( ) {
return keyspaceName ;
}
2023-10-30 18:52:02 +01:00
2013-01-10 14:19:53 +01:00
public void setKeyspaceName ( String keyspaceName ) {
this . keyspaceName = keyspaceName ;
}
@Override
public String toString ( ) {
2023-10-30 18:52:02 +01:00
return " RunningCluster [host= " + host + " , datacenterName= " + datacenterName
2013-01-10 14:19:53 +01:00
+ " , keyspaceName= " + keyspaceName + " ] " ;
}
2017-05-16 17:43:12 +02:00
/ * *
2023-10-30 18:52:02 +01:00
*
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 " ) + " / " ) ;
}
2023-10-30 18:52:02 +01:00
public void setDatacenterName ( String datacenterName ) {
this . datacenterName = datacenterName ;
}
public String getDatacenterName ( ) {
return datacenterName ;
}
public List < InetSocketAddress > getHosts ( ) {
List < InetSocketAddress > hosts = new ArrayList < > ( ) ;
String [ ] ips = host . split ( " , " ) ;
for ( String ip : ips ) {
String [ ] ip_port = ip . split ( " : " ) ;
hosts . add ( new InetSocketAddress ( ip_port [ 0 ] , Integer . parseInt ( ip_port [ 1 ] ) ) ) ;
}
return hosts ;
}
}