Classes of living resources configurable via JNDI

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/information-system/gCubeIS/Registry@25579 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Manuele Simi 2010-07-31 03:18:56 +00:00
parent 7ddab7f37b
commit d8315a8a91
3 changed files with 486 additions and 472 deletions

View File

@ -1,116 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<jndiConfig xmlns="http://wsrf.globus.org/jndi/config"> <jndiConfig xmlns="http://wsrf.globus.org/jndi/config">
<service name="gcube/informationsystem/registry"> <service name="gcube/informationsystem/registry">
<environment <environment name="configDir" value="@config.dir@" type="java.lang.String"
name="configDir" override="false" />
value="@config.dir@"
type="java.lang.String" <!--
override="false" /> GCUBEResources that are not considered temporary, i.e. they are not automatically
destroyed after the 'temporaryResourceLifetimeInMs' period
<!-- Lifetime for temporary resources, after this period temporary resources are destroyed--> -->
<environment <environment name="livingResourceTypes" value="GHN,RunningInstance"
name="temporaryResourceLifetimeInMs" type="java.lang.String" override="false" />
value="480000"
type="java.lang.Long"
override="false" />
<environment
name="temporaryResourceSweeperIntervalInMs"
value="480000"
type="java.lang.Long"
override="false" />
<!--
<environment
name="startScopes"
value=""
type="java.lang.String"
override="false" />
-->
</service>
<service name="gcube/informationsystem/registry/Registry">
<environment
name="name"
value="Registry"
type="java.lang.String"
override="false" />
<environment
name="RPDName"
value="ProfileResourceProperties"
type="java.lang.String"
override="false"/>
<!--
<resource name="publicationProfile" type="org.gcube.common.core.state.GCUBEPublicationProfile"> Lifetime for temporary resources, after this period temporary
<resourceParams> resources are destroyed
<parameter> -->
<name>factory</name> <environment name="temporaryResourceLifetimeInMs" value="480000"
<value>org.globus.wsrf.jndi.BeanFactory</value> type="java.lang.Long" override="false" />
</parameter>
<parameter>
<name>mode</name>
<value>pull</value>
</parameter>
<parameter>
<name>fileName</name>
<value>registration.xml</value>
</parameter>
</resourceParams>
</resource>
<resource name="home" type="org.gcube.informationsystem.registry.impl.state.ProfileResourceHome">
<resourceParams>
<parameter> <environment name="temporaryResourceSweeperIntervalInMs"
<name>factory</name> value="480000" type="java.lang.Long" override="false" />
<value>org.globus.wsrf.jndi.BeanFactory</value>
</parameter>
<parameter>
<name>resourceClass</name>
<value>org.gcube.informationsystem.registry.impl.state.ProfileResource</value>
</parameter>
<parameter>
<name>persistenceDelegateClass</name>
<value>org.gcube.informationsystem.registry.impl.persistence.RegistryPersistenceDelegate</value>
</parameter>
<parameter>
<name>sweeperDelay</name>
<value>10000</value>
</parameter>
</resourceParams>
</resource>
<!--
<environment name="startScopes" value="" type="java.lang.String"
override="false" />
-->
</service> </service>
<service name="gcube/informationsystem/registry/RegistryFactory"> <service name="gcube/informationsystem/registry/Registry">
<environment <environment name="name" value="Registry" type="java.lang.String"
name="name" override="false" />
value="RegistryFactory"
type="java.lang.String" <environment name="RPDName" value="ProfileResourceProperties"
override="false" /> type="java.lang.String" override="false" />
<environment
name="RPDName" <resource name="publicationProfile"
value="RegistryFactoryResourceProperties" type="org.gcube.common.core.state.GCUBEPublicationProfile">
type="java.lang.String"
override="false"/>
<!--
<resource name="publicationProfile" type="org.gcube.common.core.state.GCUBEPublicationProfile">
<resourceParams> <resourceParams>
<parameter> <parameter>
<name>factory</name> <name>factory</name>
@ -122,30 +52,71 @@
</parameter> </parameter>
<parameter> <parameter>
<name>fileName</name> <name>fileName</name>
<value>registrationFactory.xml</value> <value>registration.xml</value>
</parameter> </parameter>
</resourceParams> </resourceParams>
</resource> --> </resource>
<resource name="home" type="org.gcube.informationsystem.registry.impl.state.RegistryFactoryResourceHome"> <resource name="home"
<resourceParams> type="org.gcube.informationsystem.registry.impl.state.ProfileResourceHome">
<parameter>
<name>factory</name> <resourceParams>
<value>org.globus.wsrf.jndi.BeanFactory</value> <parameter>
</parameter> <name>factory</name>
<parameter> <value>org.globus.wsrf.jndi.BeanFactory</value>
<name>resourceClass</name> </parameter>
<value>org.gcube.informationsystem.registry.impl.state.RegistryFactoryResource</value> <parameter>
</parameter> <name>resourceClass</name>
<parameter> <value>org.gcube.informationsystem.registry.impl.state.ProfileResource
<name>persistenceDelegateClass</name> </value>
<value>org.gcube.informationsystem.registry.impl.persistence.RegistryFactoryPersistenceDelegate</value> </parameter>
</parameter> <parameter>
<name>persistenceDelegateClass</name>
</resourceParams> <value>org.gcube.informationsystem.registry.impl.persistence.RegistryPersistenceDelegate
</value>
</resource> </parameter>
<parameter>
</service> <name>sweeperDelay</name>
<value>10000</value>
</parameter>
</resourceParams>
</resource>
</service>
<service name="gcube/informationsystem/registry/RegistryFactory">
<environment name="name" value="RegistryFactory" type="java.lang.String"
override="false" />
<environment name="RPDName" value="RegistryFactoryResourceProperties"
type="java.lang.String" override="false" />
<resource name="home"
type="org.gcube.informationsystem.registry.impl.state.RegistryFactoryResourceHome">
<resourceParams>
<parameter>
<name>factory</name>
<value>org.globus.wsrf.jndi.BeanFactory</value>
</parameter>
<parameter>
<name>resourceClass</name>
<value>org.gcube.informationsystem.registry.impl.state.RegistryFactoryResource
</value>
</parameter>
<parameter>
<name>persistenceDelegateClass</name>
<value>org.gcube.informationsystem.registry.impl.persistence.RegistryFactoryPersistenceDelegate
</value>
</parameter>
</resourceParams>
</resource>
</service>
</jndiConfig> </jndiConfig>

View File

@ -18,7 +18,7 @@
<Main> <Main>
<Description>IS-Registry: validate, register and unregister GCUBE resources to/from the IS. It also publishes Topics for notifications about GCUBE resource events</Description> <Description>IS-Registry: validate, register and unregister GCUBE resources to/from the IS. It also publishes Topics for notifications about GCUBE resource events</Description>
<Name>IS-Registry-service</Name> <Name>IS-Registry-service</Name>
<Version>1.3.3</Version> <Version>1.4.0</Version>
<Mandatory level="VO"/> <Mandatory level="VO"/>
<Shareable level="VO"/> <Shareable level="VO"/>
<GHNRequirements> <GHNRequirements>

View File

@ -7,7 +7,11 @@ import java.io.StringReader;
import java.io.StringWriter; import java.io.StringWriter;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.rmi.RemoteException; import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar; import java.util.Calendar;
import java.util.List;
import org.apache.axis.components.uuid.UUIDGen; import org.apache.axis.components.uuid.UUIDGen;
import org.apache.axis.components.uuid.UUIDGenFactory; import org.apache.axis.components.uuid.UUIDGenFactory;
import org.gcube.common.core.contexts.GCUBEServiceContext; import org.gcube.common.core.contexts.GCUBEServiceContext;
@ -47,195 +51,214 @@ import static org.gcube.informationsystem.registry.impl.core.RegistryConfigurati
*/ */
public class RegistryFactory extends GCUBEPortType { public class RegistryFactory extends GCUBEPortType {
/** Name of temporaryResourceLifetimeInMs JNDI environment. */ /** Name of temporaryResourceLifetimeInMs JNDI environment. */
private static final String LIFETIME_JNDI_NAME = "temporaryResourceLifetimeInMs"; private static final String LIFETIME_JNDI_NAME = "temporaryResourceLifetimeInMs";
/**
* Lifetime of temporary resources
*/
private static long temporaryResourceLifetimeInMs = 120000;
/** The UUIDGen */
private static final UUIDGen uuidgen = UUIDGenFactory.getUUIDGen();
/** Object logger */
protected final GCUBELog logger = new GCUBELog(RegistryFactory.class);
enum OperationType {create, update, destroy};
/** the key used to label the Factory Resource */
public static final String NOTIFICATOR_RESOURCE_KEY = "RegistryResource";
/**
* {@inheritDoc}
*/
@Override
protected void onInitialisation() throws Exception {
temporaryResourceLifetimeInMs = (Long) ServiceContext.getContext().getProperty(LIFETIME_JNDI_NAME);
logger.info("Temporary resources lifetime = " + temporaryResourceLifetimeInMs);
}
/**
*
* Creates a new GCUBEResource's profile manager
*
* @param inputMessage
* @return the profile as string
* @throws SchemaValidationFault
* @throws RemoteException
* @throws ProfileAlreadyRegisteredFault
*/
@SuppressWarnings("unchecked")
public String createResource(CreateResourceMessage mess) throws SchemaValidationFault, RemoteException,
ProfileAlreadyRegisteredFault, ResourceNotAcceptedFault {
/** Name of Living Resource Types JNDI environment. */
private static final String LIVINGTYPES_JNDI_NAME = "livingResourceTypes";
/**
* Lifetime of temporary resources
*/
private static long temporaryResourceLifetimeInMs = 120000;
/**
* Lifetime of temporary resources
*/
private static List<String> livingResourceTypes;
/** The UUIDGen */
private static final UUIDGen uuidgen = UUIDGenFactory.getUUIDGen();
/** Object logger */
protected final GCUBELog logger = new GCUBELog(RegistryFactory.class);
enum OperationType {
create, update, destroy
};
/** the key used to label the Factory Resource */
public static final String NOTIFICATOR_RESOURCE_KEY = "RegistryResource";
/**
* {@inheritDoc}
*/
@Override
protected void onInitialisation() throws Exception {
temporaryResourceLifetimeInMs = (Long) ServiceContext.getContext().getProperty(LIFETIME_JNDI_NAME);
logger.info("Temporary resources lifetime = "+ temporaryResourceLifetimeInMs);
try {
temporaryResourceLifetimeInMs = (Long) ServiceContext.getContext().getProperty(LIFETIME_JNDI_NAME);
livingResourceTypes = Arrays.asList(((String)ServiceContext.getContext().getProperty(LIVINGTYPES_JNDI_NAME)).split(","));
} catch (Exception e) {livingResourceTypes = new ArrayList<String>();}
}
/**
*
* Creates a new {@link ProfileResource} and registers the {@link GCUBEResource} in the IS-IC
*
* @param inputMessage defined in the WSDL
* @return the registered profile
* @throws SchemaValidationException if the string serialization of the resource is not valid
* @throws ResourceNotAcceptedFault it the resource is rejected when evaluating the resources' filters
* @throws RemoteException
* @throws ProfileAlreadyRegisteredFault
*/
@SuppressWarnings("unchecked")
public String createResource(CreateResourceMessage mess)
throws SchemaValidationFault, RemoteException,
ProfileAlreadyRegisteredFault, ResourceNotAcceptedFault {
GCUBEResource resource = null; GCUBEResource resource = null;
logger.info("CreateResource operation invoked"); logger.info("CreateResource operation invoked");
// logSecurityInfo("createResource"); // logSecurityInfo("createResource");
String profile = mess.getProfile(); String profile = mess.getProfile();
if (profile == null || profile.compareTo("") == 0) { if (profile == null || profile.compareTo("") == 0) {
String msg = "Profile file is empty"; String msg = "Profile file is empty";
logger.debug(msg); logger.debug(msg);
throw new RemoteException(msg); throw new RemoteException(msg);
} }
try { try {
resource = ResourceType.valueOf(mess.getType()).getResourceClass(); resource = ResourceType.valueOf(mess.getType()).getResourceClass();
resource.load(new BufferedReader(new InputStreamReader(new ByteArrayInputStream(profile.getBytes("UTF-8")), "UTF-8"))); resource.load(new BufferedReader(new InputStreamReader(
new ByteArrayInputStream(profile.getBytes("UTF-8")),
// the parse Profile class allows to extract from profiles "UTF-8")));
// information about type/SCOPE/UniqueID
// in order to distinguish among different Resource Type // the parse Profile class allows to extract from profiles
// Adding scopes to Profile // information about type/SCOPE/UniqueID
String[] scopes = mess.getScopes(); // in order to distinguish among different Resource Type
if (scopes != null) { // Adding scopes to Profile
for (String scope : scopes) { String[] scopes = mess.getScopes();
logger.debug(resource.getID() if (scopes != null) {
+ " Adding Scopes to Profile " + scope); for (String scope : scopes) {
resource.addScope(GCUBEScope.getScope(scope)); logger.debug(resource.getID()
+ " Adding Scopes to Profile " + scope);
resource.addScope(GCUBEScope.getScope(scope));
}
} }
// check ID
} if (resource.getID() == null || resource.getID().compareTo("") == 0) {
// check ID resource.setID(uuidgen.nextUUID());
if (resource.getID() == null || resource.getID().compareTo("") == 0) { }
resource.setID(uuidgen.nextUUID());
}
} catch (Exception ex) { } catch (Exception ex) {
logger.error("Error trying to load profile", ex); logger.error("Error trying to load profile", ex);
throw new SchemaValidationFault(); throw new SchemaValidationFault();
} }
// apply resource filter // apply resource filter
try { try {
if (!FilterManager.getExecutor(resource.getType()).accept(resource)) { if (!FilterManager.getExecutor(resource.getType()).accept(resource)) {
logger.warn("Resource " + resource.getID() + " NOT accepted "); logger.warn("Resource " + resource.getID() + " NOT accepted ");
throw new ResourceNotAcceptedFault(); throw new ResourceNotAcceptedFault();
} }
logger.trace("Resource " + resource.getID() + " accepted "); logger.trace("Resource " + resource.getID() + " accepted ");
} catch (InvalidFilterException e) { } catch (InvalidFilterException e) {
logger.warn("Invalid filter selected, the resource " logger.warn("Invalid filter selected, the resource "
+ resource.getID() + " CANNOT be filtered "); + resource.getID() + " CANNOT be filtered ");
} }
// check if the Resource already exists // check if the Resource already exists
if (isResourceCreated(resource.getID())) { if (isResourceCreated(resource.getID())) {
// update the existing resource // update the existing resource
logger.warn("A Resource with ID " + resource.getID() logger.debug("A Resource with ID " + resource.getID() + " is already registered");
+ " is already registered"); UpdateResourceMessage upmess = new UpdateResourceMessage();
UpdateResourceMessage upmess = new UpdateResourceMessage(); upmess.setUniqueID(resource.getID());
upmess.setUniqueID(resource.getID()); upmess.setXmlProfile(mess.getProfile());
upmess.setXmlProfile(mess.getProfile()); upmess.setType(mess.getType());
upmess.setType(mess.getType()); this.updateResource(upmess);
this.updateResource(upmess); // throw new ProfileAlreadyRegisteredFault();
// throw new ProfileAlreadyRegisteredFault();
} else { } else {
// try to create resource // try to create resource
try { try {
logger.debug(resource.getID() + " Creating the stateful resource for " + resource.getID()); logger.debug(resource.getID()
+ " Creating the stateful resource for "
ProfileResource presource = (ProfileResource) ProfileContext.getContext().getWSHome().create(ProfileContext.getContext() + resource.getID());
.makeKey(resource.getID()), resource);
presource.store(); ProfileResource presource = (ProfileResource) ProfileContext
.getContext().getWSHome().create(
// Deleting the WSResource created if != from GHN ProfileContext.getContext().makeKey(
if (resource.getType().compareTo(GCUBEHostingNode.TYPE) != 0) resource.getID()), resource);
this.scheduleForLazyDeletion(presource); presource.store();
} catch (Exception ex) { // Deleting the WSResource created if != from GHN || RI
String msg = "Error creating Resource " + resource.getID(); if (! this.isLivingResource(resource))
logger.error(msg, ex); this.scheduleForLazyDeletion(presource);
throw new RemoteException(msg);
} } catch (Exception ex) {
try { String msg = "Error creating Resource " + resource.getID();
updateCounterInfo(resource.getID(), ResourceType.valueOf(mess.getType()), OperationType.create, Calendar.getInstance(), mess.getProfile()); logger.error(msg, ex);
throw new RemoteException(msg);
GCUBEEvent<ServiceContext.RegistryTopic, GCUBEResource> event = new GCUBEEvent<ServiceContext.RegistryTopic, GCUBEResource>(); }
event.setPayload(resource); try {
ServiceContext.getContext().getTopicProducer().notify( updateCounterInfo(resource.getID(), ResourceType.valueOf(
ServiceContext.RegistryTopic.CREATE, event); mess.getType()), OperationType.create,
} catch (Exception e) { Calendar.getInstance(), mess.getProfile());
logger.warn("Error updating Counting info for resource with ID " + resource.getID(), e);
} GCUBEEvent<ServiceContext.RegistryTopic, GCUBEResource> event = new GCUBEEvent<ServiceContext.RegistryTopic, GCUBEResource>();
event.setPayload(resource);
ServiceContext.getContext().getTopicProducer().notify(ServiceContext.RegistryTopic.CREATE, event);
} catch (Exception e) {
logger.warn(
"Error updating Counting info for resource with ID "
+ resource.getID(), e);
}
} }
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();
try { try {
logger.debug("Persisting the stateful resource for " logger.debug("Persisting the stateful resource for " + resource.getID());
+ resource.getID()); resource.store(writer);
resource.store(writer);
} catch (Exception e) { } catch (Exception e) {
logger.error("Persistence failed for " + resource.getID(), e); logger.error("Persistence failed for " + resource.getID(), e);
} }
logger.info("Profile " + resource.getID() + " created "); logger.info("Profile " + resource.getID() + " created ");
return writer.toString(); return writer.toString();
}
/**
* Schedules a resource for a delayed deletion
*
* @param presource the resource to delete
*/
private void scheduleForLazyDeletion(ProfileResource presource) {
long timestamp = System.currentTimeMillis() + temporaryResourceLifetimeInMs;
GCUBEScope scope = ServiceContext.getContext().getScope().getType() == GCUBEScope.Type.VRE ? ServiceContext
.getContext().getScope().getEnclosingScope(): ServiceContext.getContext().getScope();
Pair c = new Pair(timestamp, presource);
if (!ServiceContext.threadTable.get(scope.getName()).getStack().contains(c)) {
ServiceContext.threadTable.get(scope.getName()).getStack().add(c);
logger.trace("Adding resource to EliminatePoolingThread " + presource.getGCubeResource().getID());
} else { //add more life to the resource by further delaying its deletion
int index = ServiceContext.threadTable.get(scope.getName()).getStack().indexOf(c);
ServiceContext.threadTable.get(scope.getName()).getStack().get(index).lifetime = timestamp+ temporaryResourceLifetimeInMs;
} }
}
/** /**
* Updates Resource Profiles (in case of an update of an ID not present it * Schedules a resource for a delayed deletion
* invokes calls the Create resource operation) *
* * @param presource the resource to delete
* @param mess */
* Complex Object that contains a String representing the XML private void scheduleForLazyDeletion(ProfileResource presource) {
* profile to update and the diligentID
* @return UpdateResourceResponse long timestamp = System.currentTimeMillis() + temporaryResourceLifetimeInMs;
* @throws RemoteException GCUBEScope scope = ServiceContext.getContext().getScope().getType() ==
* Exception GCUBEScope.Type.VRE ? ServiceContext.getContext().getScope().getEnclosingScope()
* @throws SchemaValidationException : ServiceContext.getContext().getScope();
* Exception
*/ Pair c = new Pair(timestamp, presource);
@SuppressWarnings("unchecked") if (!ServiceContext.threadTable.get(scope.getName()).getStack().contains(c)) {
public UpdateResourceResponse updateResource(UpdateResourceMessage mess) ServiceContext.threadTable.get(scope.getName()).getStack().add(c);
throws RemoteException, SchemaValidationFault, ResourceNotAcceptedFault, GCUBEFault { logger.trace("Adding resource to EliminatePoolingThread "+ presource.getGCubeResource().getID());
} else { // add more life to the resource by further delaying its
// deletion
int index = ServiceContext.threadTable.get(scope.getName()).getStack().indexOf(c);
ServiceContext.threadTable.get(scope.getName()).getStack().get(index).lifetime = timestamp + temporaryResourceLifetimeInMs;
}
}
/**
* Updates a {@link GCUBEResource}
*
* @param mess Complex Object that contains a String representing the XML profile to update and the diligentID
* @return UpdateResourceResponse
* @throws RemoteException Exception
* @throws SchemaValidationException if the string serialization of the resource is not valid
* @throws ResourceNotAcceptedFault it the resource is rejected when evaluating the resources' filters
*/
@SuppressWarnings("unchecked")
public UpdateResourceResponse updateResource(UpdateResourceMessage mess)
throws RemoteException, SchemaValidationFault,
ResourceNotAcceptedFault, GCUBEFault {
logger.info("UpdateResource operation invoked"); logger.info("UpdateResource operation invoked");
String ID = mess.getUniqueID(); String ID = mess.getUniqueID();
String xmlProfile = mess.getXmlProfile(); String xmlProfile = mess.getXmlProfile();
@ -243,219 +266,239 @@ public class RegistryFactory extends GCUBEPortType {
CreateResourceMessage input = null; CreateResourceMessage input = null;
// validating input parameters // validating input parameters
if (ID == null || ID.compareTo("") == 0) { if (ID == null || ID.compareTo("") == 0) {
logger.debug(" ID missing "); logger.debug(" ID missing ");
throw new RemoteException("Error, ID missing"); throw new RemoteException("Error, ID missing");
} }
if (xmlProfile == null || xmlProfile.compareTo("") == 0) { if (xmlProfile == null || xmlProfile.compareTo("") == 0) {
logger.debug("Profile missing"); logger.debug("Profile missing");
throw new RemoteException("Error, profile missing"); throw new RemoteException("Error, profile missing");
} }
// check if the profile exist // check if the profile exist
if (!isResourceCreated(ID)) { if (!isResourceCreated(ID)) {
input = new CreateResourceMessage(); input = new CreateResourceMessage();
input.setProfile(xmlProfile); input.setProfile(xmlProfile);
input.setType(mess.getType()); input.setType(mess.getType());
this.createResource(input); this.createResource(input);
return new UpdateResourceResponse(); return new UpdateResourceResponse();
} }
try { try {
resource = ResourceType.valueOf(mess.getType()).getResourceClass(); resource = ResourceType.valueOf(mess.getType()).getResourceClass();
resource.load(new StringReader(xmlProfile)); resource.load(new StringReader(xmlProfile));
// apply resource filter // apply resource filter
try { try {
if (!FilterManager.getExecutor(resource.getType()).accept(resource)) { if (!FilterManager.getExecutor(resource.getType()).accept(resource)) {
logger.warn("Resource " + resource.getID() + " NOT accepted "); logger.warn("Resource " + resource.getID() + " NOT accepted ");
throw new ResourceNotAcceptedFault(); throw new ResourceNotAcceptedFault();
} }
logger.trace("Resource " + resource.getID() + " accepted "); logger.trace("Resource " + resource.getID() + " accepted ");
} catch (InvalidFilterException e) { } catch (InvalidFilterException e) {
logger.warn("Invalid filter selected, the resource " + resource.getID() + " CANNOT be filtered "); logger.warn("Invalid filter selected, the resource "
} + resource.getID() + " CANNOT be filtered ");
}
ProfileResource presource = getProfileResource(ID);
presource.updateResource(resource); ProfileResource presource = getProfileResource(ID);
presource.updateResource(resource);
// Deleting the WSResource created
if (resource.getType().compareTo(GCUBEHostingNode.TYPE) != 0) // Deleting the WSResource created if != from GHN || RI
this.scheduleForLazyDeletion(presource); if (! this.isLivingResource(resource))
this.scheduleForLazyDeletion(presource);
} catch (Exception e) { } catch (Exception e) {
logger.error("Error updating profile for ID " + e); logger.error("Error updating profile for ID " + e);
throw new RemoteException(e.toString()); throw new RemoteException(e.toString());
} }
try { try {
this.updateCounterInfo(ID, ResourceType.valueOf(mess.getType()), OperationType.update, Calendar.getInstance(), xmlProfile); this.updateCounterInfo(ID, ResourceType.valueOf(mess.getType()),
GCUBEEvent<ServiceContext.RegistryTopic, GCUBEResource> event = new GCUBEEvent<ServiceContext.RegistryTopic, GCUBEResource>(); OperationType.update, Calendar.getInstance(), xmlProfile);
event.setPayload(resource); GCUBEEvent<ServiceContext.RegistryTopic, GCUBEResource> event = new GCUBEEvent<ServiceContext.RegistryTopic, GCUBEResource>();
ServiceContext.getContext().getTopicProducer().notify(ServiceContext.RegistryTopic.UPDATE, event); event.setPayload(resource);
ServiceContext.getContext().getTopicProducer().notify(ServiceContext.RegistryTopic.UPDATE, event);
} catch (Exception e) { } catch (Exception e) {
logger.warn("Error while updating the counting info for resource with ID " + resource.getID(), e); logger.warn("Error while updating the counting info for resource with ID " + resource.getID(), e);
} }
logger.info("Profile " + mess.getUniqueID() + " updated"); logger.info("Profile " + mess.getUniqueID() + " updated");
return new UpdateResourceResponse(); return new UpdateResourceResponse();
}
/**
* Removes a Resource profile identified by the given the resource ID
*
* @param inputMessage defined into WSDL file
* @return RemoveResourceResponse
* @throws RemoteException
*/
@SuppressWarnings("unchecked")
public RemoveResourceResponse removeResource(RemoveResourceMessage inputMessage) throws RemoteException, GCUBEFault {
//logSecurityInfo("removeResource");
String ID = inputMessage.getUniqueID();
String type = inputMessage.getType();
logger.info("RemoveResource operation invoked on resource ID=" + ID + ", type=" + type);
if (ID == null || ID.compareTo("") == 0) {
logger.warn("Resource ID is missing, cannot manage the resource");
throw new RemoteException("Resource ID is missing, cannot manage the resource");
} }
try { /**
logger.debug("Trying to remove the resource from the IS-IC"); * Removes a Resource profile identified by the given the resource ID
new ProfileManager().removeFromISIC(ID, type, ServiceContext.getContext().getScope(), ServiceContext.getContext()); *
logger.info("Resource removed from the IS-IC"); * @param inputMessage defined into WSDL file
} catch (Exception e) { * @return RemoveResourceResponse
logger.error("Unable to remove the resource", e); * @throws RemoteException
throw ServiceContext.getContext().getDefaultException("", e).toFault( "Unable to remove the resource: the remote IS-IC returned an error"); */
} @SuppressWarnings("unchecked")
// if the resource is a GHN, remove also the related RIs public RemoveResourceResponse removeResource(
try { RemoveResourceMessage inputMessage) throws RemoteException, GCUBEFault {
if (type.compareTo(GCUBEHostingNode.TYPE) == 0) {
logger.debug("Removing the related RIs");
GHN ghn = new GHN(ID);
ghn.unregisterHostedRIs(this);
logger.debug("Related RIs removed");
}
} catch (Exception e) {
logger.error("Error while removing RI profiles related to the GHN", e);
}
if (this.isResourceCreated(ID)) { String ID = inputMessage.getUniqueID();
// destroy the resource String type = inputMessage.getType();
try { logger.info("RemoveResource operation invoked on resource ID=" + ID
logger.debug("Destroying the local stateful Resoruce"); + ", type=" + type);
ProfileContext.getContext().getWSHome().remove(ProfileContext.getContext().makeKey(ID)); if (ID == null || ID.compareTo("") == 0) {
logger.info(" Resource Destroyed"); logger.warn("Resource ID is missing, cannot manage the resource");
} catch (Exception e) { throw new RemoteException(
logger.error("Error removing resource for ID ", e); "Resource ID is missing, cannot manage the resource");
throw new RemoteException(); }
}
try {
GCUBEEvent<ServiceContext.RegistryTopic, GCUBEResource> event = new GCUBEEvent<ServiceContext.RegistryTopic, GCUBEResource>();
GCUBEResource resource = ResourceType.valueOf(type).getResourceClass();
resource.setID(ID);
event.setPayload(resource);
ServiceContext.getContext().getTopicProducer().notify(ServiceContext.RegistryTopic.REMOVE, event);
updateCounterInfo(ID, ResourceType.valueOf(type),OperationType.destroy, Calendar.getInstance(),null);
} catch (Exception e) {
logger.warn("Error updating counting info for resource with ID " + ID, e);
}
}
return new RemoveResourceResponse(); try {
logger.debug("Trying to remove the resource from the IS-IC");
new ProfileManager().removeFromISIC(ID, type, ServiceContext
.getContext().getScope(), ServiceContext.getContext());
logger.info("Resource removed from the IS-IC");
} catch (Exception e) {
logger.error("Unable to remove the resource", e);
throw ServiceContext
.getContext()
.getDefaultException("", e)
.toFault(
"Unable to remove the resource: the remote IS-IC returned an error");
}
// if the resource is a GHN, remove also the related RIs
try {
if (type.compareTo(GCUBEHostingNode.TYPE) == 0) {
logger.debug("Removing the related RIs");
GHN ghn = new GHN(ID);
ghn.unregisterHostedRIs(this);
logger.debug("Related RIs removed");
}
} catch (Exception e) {
logger.error("Error while removing RI profiles related to the GHN",
e);
}
} if (this.isResourceCreated(ID)) {
// destroy the resource
try {
logger.debug("Destroying the local stateful Resoruce");
ProfileContext.getContext().getWSHome().remove(
ProfileContext.getContext().makeKey(ID));
logger.info(" Resource Destroyed");
} catch (Exception e) {
logger.error("Error removing resource for ID ", e);
throw new RemoteException();
}
try {
GCUBEEvent<ServiceContext.RegistryTopic, GCUBEResource> event = new GCUBEEvent<ServiceContext.RegistryTopic, GCUBEResource>();
GCUBEResource resource = ResourceType.valueOf(type)
.getResourceClass();
resource.setID(ID);
event.setPayload(resource);
ServiceContext.getContext().getTopicProducer().notify(
ServiceContext.RegistryTopic.REMOVE, event);
updateCounterInfo(ID, ResourceType.valueOf(type),
OperationType.destroy, Calendar.getInstance(), null);
} catch (Exception e) {
logger.warn(
"Error updating counting info for resource with ID "
+ ID, e);
}
}
return new RemoveResourceResponse();
/**
* Checks if the Resource has been created
*
* @param id
* the Resource ID
* @return the resource ( null if the resource has not been created yet
*/
protected synchronized ProfileResource getProfileResource(String id) {
try {
return (ProfileResource) ProfileContext.getContext().getWSHome()
.find(ProfileContext.getContext().makeKey(id));
} catch (Exception e) {
logger.debug("A profile with the given id " + id
+ " has not been created yet");
}
return null;
}
/**
*
* @param id
* @return
*/
protected synchronized boolean isResourceCreated(String id) {
if (getProfileResource(id) != null)
return true;
else
return false;
}
/**
* Updates the RegistryFactoryResource RPs for notification
*
*
* @param ID
* resource ID
* @param type
* the resource type
* @param operationType
* the type of Operation performed on the Profile
* @param updateTime
* The last operation Time
* @throws Exception
* Exception
*/
private synchronized void updateCounterInfo(String ID,
ResourceType resType, OperationType opType, Calendar updateTime, String profile)
throws Exception {
RegistryProperty property = new RegistryProperty();
property.setUniqueID(ID);
property.setProfile(profile);
property.setOperationType(opType.name());
property.setChangeTime(updateTime);
// select the type of the resource to update
for (Method method : this.getResource().getClass().getDeclaredMethods()) {
if (method.getName().contains(resType.name())
&& method.getName().contains("set")) {
method.invoke(this.getResource(), property);
break;
}
} }
this.getResource().store();
}
/** /**
* return the resource * Gets the profile resource
* *
* @return NotifierResource resource * @param id the Resource ID
* @throws RemoteException * @return the resource or <tt>null</tt> if the resource has not been created yet
* */
*/ protected synchronized ProfileResource getProfileResource(String id) {
private RegistryFactoryResource getResource() throws RemoteException { try {
return (ProfileResource) ProfileContext.getContext().getWSHome()
.find(ProfileContext.getContext().makeKey(id));
} catch (Exception e) {
logger.debug("A profile with the given id " + id+ " has not been created yet");
}
return null;
}
/**
* Checks whether a stateful resource with the given identifier exists
*
* @param id the resource's identifier
* @return <tt>true</tt> if the resource exists, <tt>false</tt> otherwise
*/
protected synchronized boolean isResourceCreated(String id) {
if (getProfileResource(id) != null)
return true;
else
return false;
}
/**
* Updates the RegistryFactoryResource RPs for notification
*
* @param ID resource ID
* @param type the resource type
* @param operationType the type of Operation performed on the Profile
* @param updateTime the last operation Time
* @throws Exception if the update fails
*/
private synchronized void updateCounterInfo(String ID,
ResourceType resType, OperationType opType, Calendar updateTime,
String profile) throws Exception {
RegistryProperty property = new RegistryProperty();
property.setUniqueID(ID);
property.setProfile(profile);
property.setOperationType(opType.name());
property.setChangeTime(updateTime);
// select the type of the resource to update
for (Method method : this.getResource().getClass().getDeclaredMethods()) {
if (method.getName().contains(resType.name())
&& method.getName().contains("set")) {
method.invoke(this.getResource(), property);
break;
}
}
this.getResource().store();
}
/**
* Gets the factory stateful resource
*
* @return the resource
* @throws RemoteException if the stateful resource of the factory cannot be found in the home
*
*/
private RegistryFactoryResource getResource() throws RemoteException {
Object resource = null; Object resource = null;
try { try {
resource = FactoryContext.getContext().getWSHome().find( resource = FactoryContext.getContext().getWSHome().find(
FactoryContext.getContext().makeKey(NOTIFICATOR_RESOURCE_KEY)); FactoryContext.getContext().makeKey(NOTIFICATOR_RESOURCE_KEY));
} catch (Exception e) { } catch (Exception e) {
logger.error(" Unable to access resource", e); logger.error(" Unable to access resource", e);
} }
RegistryFactoryResource factoryResource = (RegistryFactoryResource) resource; RegistryFactoryResource factoryResource = (RegistryFactoryResource) resource;
return factoryResource; return factoryResource;
} }
/** /**
* {@inheritDoc} * Decides whether the given resource is a living resource.
*/ *
@Override * Living resources are those resources that are not automatically destroyed because they are updated regularly.
protected GCUBEServiceContext getServiceContext() { *
return ServiceContext.getContext(); * @param resource the resource to check
} * @return <tt>true</tt> if the given resource is a living resource, <tt>false</tt> otherwise
*/
private boolean isLivingResource(GCUBEResource resource) {
return (livingResourceTypes.contains(resource.getType()))? true : false;
}
/**
* {@inheritDoc}
*/
@Override
protected GCUBEServiceContext getServiceContext() {
return ServiceContext.getContext();
}
} }