vre-modeler/src/org/gcube/vremanagement/vremodeler/impl/ServiceContext.java

113 lines
4.5 KiB
Java
Raw Normal View History

package org.gcube.vremanagement.vremodeler.impl;
import java.util.ArrayList;
import java.util.Collections;
import javax.xml.namespace.QName;
import org.gcube.common.core.contexts.GCUBEServiceContext;
import org.gcube.common.core.contexts.GHNContext;
import org.gcube.common.core.informationsystem.notifier.ISNotifier;
import org.gcube.common.core.scope.GCUBEScope;
import org.gcube.common.core.security.GCUBESecurityManager;
import org.gcube.common.core.security.GCUBESecurityManagerImpl;
import org.gcube.vremanagement.vremodeler.consumers.CollectionConsumer;
import org.gcube.vremanagement.vremodeler.consumers.GHNConsumer;
import org.gcube.vremanagement.vremodeler.consumers.GenericResourceConsumer;
import org.gcube.vremanagement.vremodeler.consumers.MCollectionConsumer;
import org.gcube.vremanagement.vremodeler.consumers.RunningInstanceConsumer;
import org.gcube.vremanagement.vremodeler.db.IStoDBUtil;
import org.gcube.vremanagement.vremodeler.resources.handlers.CollectionHandler;
import org.gcube.vremanagement.vremodeler.resources.handlers.FunctionalityHandler;
import org.gcube.vremanagement.vremodeler.resources.handlers.GHNHandler;
import org.gcube.vremanagement.vremodeler.resources.handlers.GenericResourceHandler;
public class ServiceContext extends GCUBEServiceContext{
/** Single context instance, created eagerly */
private static ServiceContext cache = new ServiceContext();
/** Returns cached instance */
public static ServiceContext getContext() {return cache;}
/** Prevents accidental creation of more instances */
private ServiceContext(){};
/** {@inheritDoc} */
protected String getJNDIName() {return "gcube/vremanagement/vremodeler";}
private ArrayList<String> secondaryTypeGenericResourceRequired= new ArrayList<String>();
protected void onReady() throws Exception{
try{
logger.info("ready event invoked on " + this.getName());
this.intializeDB();
}catch (Exception e){
logger.error("error initializing VREModeler",e);
this.setStatus(Status.FAILED);
throw e;
}
}
protected void intializeDB() throws Exception{
ISNotifier notifier= GHNContext.getImplementation(ISNotifier.class);
for (GCUBEScope scope : ServiceContext.getContext().getInstance().getScopes().values()){
ServiceContext.getContext().setScope(scope);
ArrayList<String> genResList= new ArrayList<String>();
for (String gen:((String)this.getProperty("GenericResourceToAdd", true)).split(","))
genResList.add(gen.trim());
this.setSecondaryTypeGenericResourceRequired(genResList.toArray(new String[0]));
GCUBESecurityManager secMan= new GCUBESecurityManagerImpl(){
@Override
public boolean isSecurityEnabled() {
return false;
}};
IStoDBUtil.initDB(scope);
new GHNHandler().initialize();
new CollectionHandler().initialize();
FunctionalityHandler functionalityHandler= new FunctionalityHandler();
functionalityHandler.initialize();
new GenericResourceHandler().initialize();
logger.debug("Service initialized!!");
//GHNNotification
ArrayList<QName> qnameList= new ArrayList<QName>();
qnameList.add(GHNConsumer.ghnQName);
notifier.registerToISNotification(qnameList, new GHNConsumer(scope), secMan, scope);
//RINotification
qnameList= new ArrayList<QName>();
qnameList.add(RunningInstanceConsumer.riQName);
notifier.registerToISNotification(qnameList, new RunningInstanceConsumer(scope), secMan, scope);
//CollectionNotification
qnameList= new ArrayList<QName>();
qnameList.add(CollectionConsumer.collectionQName);
notifier.registerToISNotification(qnameList, new CollectionConsumer(scope), secMan, scope);
//MCollectionNotification
qnameList= new ArrayList<QName>();
qnameList.add(MCollectionConsumer.mCollectionQName);
notifier.registerToISNotification(qnameList, new MCollectionConsumer(scope), secMan, scope);
//FunctionalityResource
qnameList= new ArrayList<QName>();
qnameList.add(GenericResourceConsumer.functionalityQName);
notifier.registerToISNotification(qnameList, new GenericResourceConsumer(scope,functionalityHandler.getFunctionalityResourceId()), secMan, scope);
logger.debug("consumers registered");
}
}
public ArrayList<String> getSecondaryTypeGenericResourceRequired() {
return secondaryTypeGenericResourceRequired;
}
public void setSecondaryTypeGenericResourceRequired(
String[] secondaryTypeGenericResourceRequired) {
Collections.addAll(this.secondaryTypeGenericResourceRequired,secondaryTypeGenericResourceRequired);
}
}