is-collector/src/org/gcube/informationsystem/collector/impl/xmlstorage/exist/State.java

133 lines
4.1 KiB
Java
Raw Normal View History

package org.gcube.informationsystem.collector.impl.xmlstorage.exist;
import org.gcube.informationsystem.collector.impl.contexts.ICServiceContext;
import org.gcube.informationsystem.collector.impl.persistence.AggregatorPersistentResource;
import org.globus.wsrf.config.ContainerConfig;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Collections;
/**
* The global state of an IC instance
*
* @author Manuele Simi (ISTI-CNR)
*
*/
public class State {
/**
* Connection to eXist used to store data
*/
private static DataManager storageManager;
/**
* Connection to eXist used to query the database
*/
private static QueryManager queryManager;
/**
* Thread that periodically sweeps the database from expired resources
*/
public static Thread sweeperT;
/**
* List of recently deleted resources. It is used to avoid the storage of RPs of a deleted
* resource
*/
public static List<AggregatorPersistentResource> deletedResources = Collections.synchronizedList(new ArrayList<AggregatorPersistentResource>());
private static Log logger = LogFactory.getLog(State.class.getName());
/**
* Initializes the eXist DB connections using during the service life
*
* @param configuration
* the RI configuration loaded from the JNDI resource
* @throws Exception
* if the intialization fails
*/
public static void initialize() throws Exception {
logger.info("starting IC service initialization...");
State.initializeDataManager();
State.initializeQueryManager();
if (Boolean.valueOf((String) ICServiceContext.getContext().getProperty("deleteRPsOnStartup", true))) {
// cleanup the RPs collection
logger.info("deleting all RPs...");
State.storageManager.deleteAllProperties();
} else {
logger.info("all RPs previously stored are keept in the storage");
}
// start the sweeper to periodically cleanup the storage and some data structures
Sweeper sweeper = new Sweeper(Long.valueOf((String) ICServiceContext.getContext().getProperty("sweeperIntervalinMillis", true)), Long.valueOf((String) ICServiceContext.getContext()
.getProperty("resourceExpirationTimeInMillis", true)));
State.sweeperT = new Thread(sweeper);
State.sweeperT.start();
logger.info("IC service initialization completed");
}
private static void initializeDataManager() throws Exception {
// open the connection used to store resources
State.storageManager = new DataManager();
storageManager.initialize();
}
private static void initializeQueryManager() throws Exception {
// open the connection used to query stored resources
State.queryManager = new QueryManager();
queryManager.initialize();
}
/**
*
* @return the container base dir
*/
public static String getBaseDirectory() {
return ContainerConfig.getBaseDirectory();
}
/**
* Releases all the State resources
*
* @throws Exception if the shutdown fails
*/
public static void dispose() throws Exception {
logger.info("Disposing IC service's resources...");
State.storageManager.shutdown();
State.queryManager.shutdown();
}
/**
* @return the storageManager
*/
public static DataManager getDataManager() {
return storageManager;
}
/**
* @return the queryManager
*/
public static QueryManager getQueryManager() {
return queryManager;
}
/**
* Prints the enviromnet variables
*/
public void printEnv() {
java.util.Properties p = System.getProperties();
Enumeration<Object> keys = p.keys();
while (keys.hasMoreElements()) {
logger.debug(keys.nextElement());
}
logger.debug("Exist home: " + System.getProperty("exist.home"));
logger.debug("Class path: " + System.getProperty("java.class.path"));
}
}