diff --git a/etc/deploy-jndi-config.xml b/etc/deploy-jndi-config.xml index 7d40932..522f3e9 100755 --- a/etc/deploy-jndi-config.xml +++ b/etc/deploy-jndi-config.xml @@ -21,6 +21,13 @@ value="600000" type="java.lang.String" override="false" /> + + + diff --git a/src/org/gcube/vremanagement/vremodeler/consumers/GHNConsumer.java b/src/org/gcube/vremanagement/vremodeler/consumers/GHNConsumer.java index 3ffc002..719c664 100644 --- a/src/org/gcube/vremanagement/vremodeler/consumers/GHNConsumer.java +++ b/src/org/gcube/vremanagement/vremodeler/consumers/GHNConsumer.java @@ -40,8 +40,9 @@ public class GHNConsumer extends BaseNotificationConsumer{ query.addAtomicConditions(new AtomicCondition("/ID",id)); Thread.sleep(60000); new GHNHandler().add(client.execute(query, this.scope).get(0)); - } else if (operation.compareTo("remove")==0){ + } else if (operation.compareTo("destroy")==0){ logger.trace("removing a GHN from DB"); + new GHNHandler().drop(id); } }catch(Exception e){logger.error("error in notification received",e);} diff --git a/src/org/gcube/vremanagement/vremodeler/consumers/FunctionalityConsumer.java b/src/org/gcube/vremanagement/vremodeler/consumers/GenericResourceConsumer.java similarity index 67% rename from src/org/gcube/vremanagement/vremodeler/consumers/FunctionalityConsumer.java rename to src/org/gcube/vremanagement/vremodeler/consumers/GenericResourceConsumer.java index d69fbe6..157349e 100644 --- a/src/org/gcube/vremanagement/vremodeler/consumers/FunctionalityConsumer.java +++ b/src/org/gcube/vremanagement/vremodeler/consumers/GenericResourceConsumer.java @@ -11,25 +11,27 @@ import org.gcube.common.core.informationsystem.client.queries.GCUBEGenericQuery; import org.gcube.common.core.informationsystem.client.queries.GCUBEGenericResourceQuery; import org.gcube.common.core.informationsystem.notifier.ISNotifier.BaseNotificationConsumer; import org.gcube.common.core.informationsystem.notifier.ISNotifier.NotificationEvent; +import org.gcube.common.core.resources.GCUBEGenericResource; import org.gcube.common.core.scope.GCUBEScope; import org.gcube.common.core.utils.logging.GCUBELog; import org.gcube.vremanagement.vremodeler.impl.ServiceContext; import org.gcube.vremanagement.vremodeler.resources.handlers.FunctionalityHandler; +import org.gcube.vremanagement.vremodeler.resources.handlers.GenericResourceHandler; import org.gcube.vremanagement.vremodeler.resources.kxml.KGCUBEGenericFunctionalityResource; -public class FunctionalityConsumer extends BaseNotificationConsumer{ +public class GenericResourceConsumer extends BaseNotificationConsumer{ private GCUBELog logger= new GCUBELog(GHNConsumer.class); public static final QName functionalityQName= new QName("http://gcube-system.org/namespaces/informationsystem/registry","GenericResource"); - private String resourceId; + private String functionalityResourceId; private GCUBEScope scope; - public FunctionalityConsumer(GCUBEScope scope, String resourceId){ + public GenericResourceConsumer(GCUBEScope scope, String resourceId){ super(); this.scope=scope; - this.resourceId= resourceId; + this.functionalityResourceId= resourceId; } public void onNotificationReceived(NotificationEvent event){ @@ -38,7 +40,8 @@ private GCUBELog logger= new GCUBELog(GHNConsumer.class); String id= event.getPayload().getMessage()[0].getChildNodes().item(0).getChildNodes().item(0).getNodeValue(); String operation=event.getPayload().getMessage()[0].getChildNodes().item(1).getChildNodes().item(0).getNodeValue(); logger.info("notification received for genericResource "+id+" and operation "+operation); - if (id.compareTo(this.resourceId)==0 && (operation.compareTo("update")==0 || operation.compareTo("create")==0)){ + + if (id.compareTo(this.functionalityResourceId)==0 && (operation.compareTo("update")==0 || operation.compareTo("create")==0)){ logger.trace("notification received for functionalityResource with id "+id+" in scope "+scope.toString()); KGCUBEGenericFunctionalityResource resource= new KGCUBEGenericFunctionalityResource(); ISClient client=GHNContext.getImplementation(ISClient.class); @@ -51,6 +54,17 @@ private GCUBELog logger= new GCUBELog(GHNConsumer.class); //FunctionalityHandler FunctionalityHandler functionalityHandler= new FunctionalityHandler(); functionalityHandler.add(resource); + }else if (operation.compareTo("create")==0){ + logger.trace("notification received for generic resource with operation create"); + ISClient client=GHNContext.getImplementation(ISClient.class); + GCUBEGenericResourceQuery query= client.getQuery(GCUBEGenericResourceQuery.class); + query.addAtomicConditions(new AtomicCondition("/ID",this.functionalityResourceId)); + GCUBEGenericResource resource =client.execute(query, ServiceContext.getContext().getScope()).get(0); + if (ServiceContext.getContext().getSecondaryTypeGenericResourceRequired().contains(resource.getSecondaryType())) + new GenericResourceHandler().add(resource); + }else if (operation.compareTo("destroy")==0){ + logger.trace("notification received for generic resource with operation destroy"); + new GenericResourceHandler().drop(id); } }catch(Exception e){logger.error("error in functionality notification",e);} diff --git a/src/org/gcube/vremanagement/vremodeler/consumers/RunningInstanceConsumer.java b/src/org/gcube/vremanagement/vremodeler/consumers/RunningInstanceConsumer.java index 23c6c61..f51ef76 100644 --- a/src/org/gcube/vremanagement/vremodeler/consumers/RunningInstanceConsumer.java +++ b/src/org/gcube/vremanagement/vremodeler/consumers/RunningInstanceConsumer.java @@ -43,12 +43,12 @@ public class RunningInstanceConsumer extends BaseNotificationConsumer{ GCUBERunningInstance ri= client.execute(query, this.scope).get(0); new RunningInstancesHandler(ri.getGHNID()).add(ri); - } else if (operation.compareTo("remove")==0){ - logger.trace("removing a GHN from DB"); + } else if (operation.compareTo("destroy")==0){ + logger.trace("removing a RI from DB"); + new RunningInstancesHandler().drop(id); } }catch(Exception e){logger.error("error in notification received",e);} - } } diff --git a/src/org/gcube/vremanagement/vremodeler/db/DBInterface.java b/src/org/gcube/vremanagement/vremodeler/db/DBInterface.java index 0cc3513..a3e983d 100644 --- a/src/org/gcube/vremanagement/vremodeler/db/DBInterface.java +++ b/src/org/gcube/vremanagement/vremodeler/db/DBInterface.java @@ -228,6 +228,14 @@ public class DBInterface { st.close(); } + public static void deleteElement(String table, String whereClause) throws SQLException{ + String query="DELETE FROM "+table.toUpperCase()+" WHERE "+whereClause; + Connection conn= getConnection(); + Statement st=conn.createStatement(); + st.executeUpdate(query); + st.close(); + } + public static void close() throws SQLException{ Connection conn= getConnection(); if (conn!=null){ diff --git a/src/org/gcube/vremanagement/vremodeler/db/IStoDBUtil.java b/src/org/gcube/vremanagement/vremodeler/db/IStoDBUtil.java index 2c72836..a09e083 100644 --- a/src/org/gcube/vremanagement/vremodeler/db/IStoDBUtil.java +++ b/src/org/gcube/vremanagement/vremodeler/db/IStoDBUtil.java @@ -36,6 +36,7 @@ import org.gcube.vremanagement.vremodeler.resources.Service; 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; import org.gcube.vremanagement.vremodeler.resources.handlers.GhnRiRelationHandler; import org.gcube.vremanagement.vremodeler.resources.handlers.MCollectionHandler; import org.gcube.vremanagement.vremodeler.resources.handlers.MetadataFormatHandler; @@ -56,10 +57,6 @@ public class IStoDBUtil { private static GCUBELog logger = new GCUBELog(IStoDBUtil.class); - private static ISClient queryClient; - - - private static void createTables(GCUBEScope scope) throws GCUBEFault{ try{ DBInterface.connect(); @@ -87,7 +84,6 @@ public class IStoDBUtil { if (!DBInterface.dbAlreadyCreated()) createTables(scope); cleanDB(scope); - insertNeededResources(scope); } private static void cleanDB(GCUBEScope scope) throws GCUBEFault @@ -110,7 +106,7 @@ public class IStoDBUtil { DBInterface.deleteAll(RunningInstancesHandler.tableName); DBInterface.deleteAll(GhnRiRelationHandler.tableName); //DBInterface.deleteAll("cs"); - DBInterface.deleteAll("NEEDEDRESOURCES"); + DBInterface.deleteAll(GenericResourceHandler.tableName); DBInterface.deleteAll(FunctionalityHandler.portletTableName); DBInterface.deleteAll(FunctionalityHandler.serviceTableName); DBInterface.deleteAll(FunctionalityHandler.functionalityTableName); @@ -120,6 +116,8 @@ public class IStoDBUtil { } + + /* private static void insertNeededResources(GCUBEScope scope) throws GCUBEFault{ List genericResourcesList= null; try{ @@ -149,6 +147,6 @@ public class IStoDBUtil { } } - + */ } diff --git a/src/org/gcube/vremanagement/vremodeler/impl/ModelFactoryService.java b/src/org/gcube/vremanagement/vremodeler/impl/ModelFactoryService.java index d14994e..0966052 100644 --- a/src/org/gcube/vremanagement/vremodeler/impl/ModelFactoryService.java +++ b/src/org/gcube/vremanagement/vremodeler/impl/ModelFactoryService.java @@ -128,13 +128,11 @@ public class ModelFactoryService{ */ public void removeVRE(String id) throws GCUBEFault{ try{ - ModelerContext pctx= ModelerContext.getPortTypeContext(); logger.trace("Deleting resource with id "+id); //destroy the resource; DBInterface.ExecuteUpdate("DELETE FROM VRE where VRE.id='"+id+"';"); pctx.getWSHome().remove(pctx.makeKey(id)); - }catch(Exception e){ logger.error("VDLModel: "+e.getMessage()); diff --git a/src/org/gcube/vremanagement/vremodeler/impl/ServiceContext.java b/src/org/gcube/vremanagement/vremodeler/impl/ServiceContext.java index 36c7843..4553d43 100644 --- a/src/org/gcube/vremanagement/vremodeler/impl/ServiceContext.java +++ b/src/org/gcube/vremanagement/vremodeler/impl/ServiceContext.java @@ -2,6 +2,7 @@ package org.gcube.vremanagement.vremodeler.impl; import java.util.ArrayList; +import java.util.Collections; import javax.xml.namespace.QName; @@ -13,14 +14,15 @@ 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.FunctionalityConsumer; 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{ @@ -36,6 +38,8 @@ public class ServiceContext extends GCUBEServiceContext{ /** {@inheritDoc} */ protected String getJNDIName() {return "gcube/vremanagement/vremodeler";} + private ArrayList secondaryTypeGenericResourceRequired= new ArrayList(); + protected void onReady() throws Exception{ try{ logger.info("ready event invoked on " + this.getName()); @@ -51,10 +55,12 @@ public class ServiceContext extends GCUBEServiceContext{ ISNotifier notifier= GHNContext.getImplementation(ISNotifier.class); for (GCUBEScope scope : ServiceContext.getContext().getInstance().getScopes().values()){ ServiceContext.getContext().setScope(scope); + + this.setSecondaryTypeGenericResourceRequired(((String)this.getProperty("GenericResourceToAdd", true)).split(",")); + GCUBESecurityManager secMan= new GCUBESecurityManagerImpl(){ @Override public boolean isSecurityEnabled() { - // TODO Auto-generated method stub return false; }}; @@ -63,6 +69,7 @@ public class ServiceContext extends GCUBEServiceContext{ new CollectionHandler().initialize(); FunctionalityHandler functionalityHandler= new FunctionalityHandler(); functionalityHandler.initialize(); + new GenericResourceHandler().initialize(); logger.debug("Service initialized!!"); //GHNNotification @@ -83,10 +90,19 @@ public class ServiceContext extends GCUBEServiceContext{ notifier.registerToISNotification(qnameList, new MCollectionConsumer(scope), secMan, scope); //FunctionalityResource qnameList= new ArrayList(); - qnameList.add(FunctionalityConsumer.functionalityQName); - notifier.registerToISNotification(qnameList, new FunctionalityConsumer(scope,functionalityHandler.getFunctionalityResourceId()), secMan, scope); + qnameList.add(GenericResourceConsumer.functionalityQName); + notifier.registerToISNotification(qnameList, new GenericResourceConsumer(scope,functionalityHandler.getFunctionalityResourceId()), secMan, scope); logger.debug("consumers registered"); } } + + public ArrayList getSecondaryTypeGenericResourceRequired() { + return secondaryTypeGenericResourceRequired; + } + + public void setSecondaryTypeGenericResourceRequired( + String[] secondaryTypeGenericResourceRequired) { + Collections.addAll(this.secondaryTypeGenericResourceRequired,secondaryTypeGenericResourceRequired); + } } diff --git a/src/org/gcube/vremanagement/vremodeler/resources/handlers/CollectionHandler.java b/src/org/gcube/vremanagement/vremodeler/resources/handlers/CollectionHandler.java index f4d036a..836c3fc 100644 --- a/src/org/gcube/vremanagement/vremodeler/resources/handlers/CollectionHandler.java +++ b/src/org/gcube/vremanagement/vremodeler/resources/handlers/CollectionHandler.java @@ -22,8 +22,9 @@ public class CollectionHandler implements ResourceHandler { } - public void drop(GCUBECollection resource) throws Exception { - // TODO Auto-generated method stub + public void drop(String collectionId) throws Exception { + DBInterface.connect(); + DBInterface.deleteElement(tableName, "ID='"+collectionId+"'"); } diff --git a/src/org/gcube/vremanagement/vremodeler/resources/handlers/FunctionalityHandler.java b/src/org/gcube/vremanagement/vremodeler/resources/handlers/FunctionalityHandler.java index d9228b5..354efed 100644 --- a/src/org/gcube/vremanagement/vremodeler/resources/handlers/FunctionalityHandler.java +++ b/src/org/gcube/vremanagement/vremodeler/resources/handlers/FunctionalityHandler.java @@ -53,7 +53,7 @@ public class FunctionalityHandler implements ResourceHandler { @@ -53,12 +54,11 @@ public class GHNHandler implements ResourceHandler { public void add(GCUBEHostingNode resource) throws Exception { this.insert(resource); - } - public void drop(GCUBEHostingNode resource) throws Exception { - // TODO Auto-generated method stub - + public void drop(String ghnId) throws Exception { + DBInterface.connect(); + DBInterface.deleteElement(tableName, "ID='"+ghnId+"'"); } } diff --git a/src/org/gcube/vremanagement/vremodeler/resources/handlers/GenericResourceHandler.java b/src/org/gcube/vremanagement/vremodeler/resources/handlers/GenericResourceHandler.java new file mode 100644 index 0000000..6b35be2 --- /dev/null +++ b/src/org/gcube/vremanagement/vremodeler/resources/handlers/GenericResourceHandler.java @@ -0,0 +1,49 @@ +package org.gcube.vremanagement.vremodeler.resources.handlers; + +import java.util.ArrayList; +import java.util.List; + +import org.gcube.common.core.contexts.GHNContext; +import org.gcube.common.core.informationsystem.client.ISClient; +import org.gcube.common.core.informationsystem.client.queries.GCUBEGenericResourceQuery; +import org.gcube.common.core.resources.GCUBEGenericResource; +import org.gcube.common.core.utils.logging.GCUBELog; +import org.gcube.vremanagement.vremodeler.db.DBInterface; +import org.gcube.vremanagement.vremodeler.impl.ServiceContext; + +public class GenericResourceHandler implements ResourceHandler { + + private static GCUBELog logger= new GCUBELog(GenericResourceHandler.class); + + public static final String tableName="NEEDEDRESOURCES"; + + public void add(GCUBEGenericResource resource) throws Exception { + ArrayList row= new ArrayList(2); + row.add(resource.getID()); + row.add(resource.getType()); + DBInterface.connect(); + DBInterface.insertInto(tableName, row.toArray(new String[0])); + } + + public void drop(String resourceId) throws Exception { + DBInterface.connect(); + try{ + DBInterface.deleteElement(tableName, "ID='"+resourceId+"'"); + }catch(Exception e){logger.warn("the generic resource with id "+resourceId+" is not in the DB");} + } + + public void initialize() throws Exception { + StringBuilder temp= new StringBuilder(); + for (String secTypeResource:ServiceContext.getContext().getSecondaryTypeGenericResourceRequired()) + temp.append(" $result/Profile/SecondaryType/string() eq '"+secTypeResource.trim()+"' or"); + ISClient queryClient= GHNContext.getImplementation(ISClient.class); + temp.delete(temp.length()-2,temp.length()); + logger.trace(temp); + GCUBEGenericResourceQuery query= queryClient.getQuery(GCUBEGenericResourceQuery.class); + query.addGenericCondition(temp.toString()); + List genericResourcesList= queryClient.execute(query, ServiceContext.getContext().getScope()); + for (GCUBEGenericResource genRes:genericResourcesList) + this.add(genRes); + } + +} diff --git a/src/org/gcube/vremanagement/vremodeler/resources/handlers/GhnRiRelationHandler.java b/src/org/gcube/vremanagement/vremodeler/resources/handlers/GhnRiRelationHandler.java index 1f63259..b78b3ab 100644 --- a/src/org/gcube/vremanagement/vremodeler/resources/handlers/GhnRiRelationHandler.java +++ b/src/org/gcube/vremanagement/vremodeler/resources/handlers/GhnRiRelationHandler.java @@ -17,9 +17,9 @@ public class GhnRiRelationHandler implements ResourceHandler { this.insert(resource); } - public void drop(GCUBEMCollection resource) throws Exception { + public void drop(String resourceId) throws Exception { // TODO Auto-generated method stub } diff --git a/src/org/gcube/vremanagement/vremodeler/resources/handlers/MetadataFormatHandler.java b/src/org/gcube/vremanagement/vremodeler/resources/handlers/MetadataFormatHandler.java index 7fccd3d..e01c746 100644 --- a/src/org/gcube/vremanagement/vremodeler/resources/handlers/MetadataFormatHandler.java +++ b/src/org/gcube/vremanagement/vremodeler/resources/handlers/MetadataFormatHandler.java @@ -34,7 +34,7 @@ public class MetadataFormatHandler implements ResourceHandler { } } - public void drop(MetadataFormat resource) throws Exception { + public void drop(String resourceId) throws Exception { // TODO Auto-generated method stub } diff --git a/src/org/gcube/vremanagement/vremodeler/resources/handlers/NativeMetadataFormatHandler.java b/src/org/gcube/vremanagement/vremodeler/resources/handlers/NativeMetadataFormatHandler.java index f4d8546..45a33c5 100644 --- a/src/org/gcube/vremanagement/vremodeler/resources/handlers/NativeMetadataFormatHandler.java +++ b/src/org/gcube/vremanagement/vremodeler/resources/handlers/NativeMetadataFormatHandler.java @@ -15,7 +15,7 @@ public class NativeMetadataFormatHandler implements ResourceHandler { public void add(T resource) throws Exception; - public void drop(T resource) throws Exception; + public void drop(String resourceId) throws Exception; } diff --git a/src/org/gcube/vremanagement/vremodeler/resources/handlers/RunningInstancesHandler.java b/src/org/gcube/vremanagement/vremodeler/resources/handlers/RunningInstancesHandler.java index 68ee107..aebe676 100644 --- a/src/org/gcube/vremanagement/vremodeler/resources/handlers/RunningInstancesHandler.java +++ b/src/org/gcube/vremanagement/vremodeler/resources/handlers/RunningInstancesHandler.java @@ -24,6 +24,11 @@ public class RunningInstancesHandler implements ResourceHandler row= new ArrayList(3); @@ -53,9 +59,10 @@ public class RunningInstancesHandler implements ResourceHandler