This commit is contained in:
Manuele Simi 2009-10-13 23:03:22 +00:00
parent 55350ea099
commit 5a50b1ba0a
7 changed files with 390 additions and 518 deletions

View File

@ -7,10 +7,7 @@ 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.Calendar; import java.util.Calendar;
import java.util.Collections;
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;
@ -19,7 +16,6 @@ import org.gcube.common.core.porttypes.GCUBEPortType;
import org.gcube.common.core.resources.GCUBEHostingNode; import org.gcube.common.core.resources.GCUBEHostingNode;
import org.gcube.common.core.resources.GCUBEResource; import org.gcube.common.core.resources.GCUBEResource;
import org.gcube.common.core.scope.GCUBEScope; import org.gcube.common.core.scope.GCUBEScope;
import org.gcube.common.core.state.GCUBEWSResourceKey;
import org.gcube.common.core.utils.events.GCUBEEvent; import org.gcube.common.core.utils.events.GCUBEEvent;
import org.gcube.common.core.utils.logging.GCUBELog; import org.gcube.common.core.utils.logging.GCUBELog;
import org.gcube.informationsystem.registry.impl.contexts.FactoryContext; import org.gcube.informationsystem.registry.impl.contexts.FactoryContext;
@ -37,21 +33,16 @@ import org.gcube.informationsystem.registry.stubs.ProfileAlreadyRegisteredFault;
import org.gcube.informationsystem.registry.stubs.RegistryProperty; import org.gcube.informationsystem.registry.stubs.RegistryProperty;
import org.gcube.informationsystem.registry.stubs.RemoveResourceMessage; import org.gcube.informationsystem.registry.stubs.RemoveResourceMessage;
import org.gcube.informationsystem.registry.stubs.RemoveResourceResponse; import org.gcube.informationsystem.registry.stubs.RemoveResourceResponse;
import org.gcube.informationsystem.registry.stubs.RemoveScopeInProfileMessage;
import org.gcube.informationsystem.registry.stubs.ResourceNotAcceptedFault; import org.gcube.informationsystem.registry.stubs.ResourceNotAcceptedFault;
import org.gcube.informationsystem.registry.stubs.SchemaValidationFault; import org.gcube.informationsystem.registry.stubs.SchemaValidationFault;
import org.gcube.informationsystem.registry.stubs.StartRegistration;
import org.gcube.informationsystem.registry.stubs.StartRegistrationResponse;
import org.gcube.informationsystem.registry.stubs.UpdateResourceMessage; import org.gcube.informationsystem.registry.stubs.UpdateResourceMessage;
import org.gcube.informationsystem.registry.stubs.UpdateResourceResponse; import org.gcube.informationsystem.registry.stubs.UpdateResourceResponse;
import org.gcube.informationsystem.registry.stubs.UpdateScopeInProfileMessage;
import org.gcube.informationsystem.registry.stubs.UpdateStateMessage;
import static org.gcube.informationsystem.registry.impl.core.RegistryConfiguration.ResourceType; import static org.gcube.informationsystem.registry.impl.core.RegistryConfiguration.ResourceType;
/** /**
* Implementation of the <em>Registry Factory</em> portType * Implementation of the <em>Registry Factory</em> portType
* *
* @author Andrea Manzi, Manuele Simi (ISTI-CNR) * @author Lucio Lelii, Manuele Simi (ISTI-CNR)
* *
*/ */
public class RegistryFactory extends GCUBEPortType { public class RegistryFactory extends GCUBEPortType {
@ -70,15 +61,8 @@ public class RegistryFactory extends GCUBEPortType {
/** Object logger */ /** Object logger */
protected final GCUBELog logger = new GCUBELog(RegistryFactory.class); protected final GCUBELog logger = new GCUBELog(RegistryFactory.class);
enum OperationType { enum OperationType {create, update, destroy};
create, update, destroy
};
/**
* Map of registration to Notification
*/
public static final List<GCUBEWSResourceKey> notificationMap = Collections
.synchronizedList(new ArrayList<GCUBEWSResourceKey>());
/** the key used to label the Factory Resource */ /** the key used to label the Factory Resource */
public static final String NOTIFICATOR_RESOURCE_KEY = "RegistryResource"; public static final String NOTIFICATOR_RESOURCE_KEY = "RegistryResource";
@ -88,36 +72,10 @@ public class RegistryFactory extends GCUBEPortType {
*/ */
@Override @Override
protected void onInitialisation() throws Exception { protected void onInitialisation() throws Exception {
temporaryResourceLifetimeInMs = (Long) ServiceContext.getContext() temporaryResourceLifetimeInMs = (Long) ServiceContext.getContext().getProperty(LIFETIME_JNDI_NAME);
.getProperty(LIFETIME_JNDI_NAME); logger.info("Temporary resources lifetime = " + temporaryResourceLifetimeInMs);
logger.info("Temporary resources lifetime = "
+ temporaryResourceLifetimeInMs);
}
/**
* Use to Load from Disk persistence profiles and registers it into the
* IS-IC ( in case the IS-IC is empty)
*
*
* @param start
* same as void
* @return void
* @throws RemoteException
* Exception
*/
public StartRegistrationResponse startRegistration(StartRegistration start)
throws GCUBEFault {
// start registration to broker and new profiles registration
logger.debug("Starting profile registration from disk");
try {
// loadProfiles();
} catch (Exception e) {
logger.error("Error Loading profiles");
throw new GCUBEFault(e.getMessage());
}
return new StartRegistrationResponse();
} }
/** /**
* *
@ -133,108 +91,108 @@ public class RegistryFactory extends GCUBEPortType {
public String createResource(CreateResourceMessage mess) throws SchemaValidationFault, RemoteException, public String createResource(CreateResourceMessage mess) throws SchemaValidationFault, RemoteException,
ProfileAlreadyRegisteredFault, ResourceNotAcceptedFault { 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 {
resource = ResourceType.valueOf(mess.getType()).getResourceClass();
resource.load(new BufferedReader(new InputStreamReader(new ByteArrayInputStream(profile.getBytes("UTF-8")), "UTF-8")));
// the parse Profile class allows to extract from profiles
// information about type/SCOPE/UniqueID
// in order to distinguish among different Resource Type
// Adding scopes to Profile
String[] scopes = mess.getScopes();
if (scopes != null) {
for (String scope : scopes) {
logger.debug(resource.getID()
+ " Adding Scopes to Profile " + scope);
resource.addScope(GCUBEScope.getScope(scope));
} }
} try {
// check ID resource = ResourceType.valueOf(mess.getType()).getResourceClass();
if (resource.getID() == null || resource.getID().compareTo("") == 0) {
resource.setID(uuidgen.nextUUID()); resource.load(new BufferedReader(new InputStreamReader(new ByteArrayInputStream(profile.getBytes("UTF-8")), "UTF-8")));
}
// the parse Profile class allows to extract from profiles
} catch (Exception ex) { // information about type/SCOPE/UniqueID
logger.error("Error trying to load profile", ex); // in order to distinguish among different Resource Type
throw new SchemaValidationFault(); // Adding scopes to Profile
} String[] scopes = mess.getScopes();
// apply resource filter if (scopes != null) {
try { for (String scope : scopes) {
if (!FilterManager.getExecutor(resource.getType()).accept(resource)) { logger.debug(resource.getID()
logger.warn("Resource " + resource.getID() + " NOT accepted "); + " Adding Scopes to Profile " + scope);
throw new ResourceNotAcceptedFault(); resource.addScope(GCUBEScope.getScope(scope));
} }
logger.trace("Resource " + resource.getID() + " accepted ");
} catch (InvalidFilterException e) { }
logger.warn("Invalid filter selected, the resource " // check ID
+ resource.getID() + " CANNOT be filtered "); if (resource.getID() == null || resource.getID().compareTo("") == 0) {
} resource.setID(uuidgen.nextUUID());
}
// check if the Resource already exists
if (isResourceCreated(resource.getID())) { } catch (Exception ex) {
// update the existing resource logger.error("Error trying to load profile", ex);
logger.warn("A Resource with ID " + resource.getID() throw new SchemaValidationFault();
+ " is already registered"); }
UpdateResourceMessage upmess = new UpdateResourceMessage(); // apply resource filter
upmess.setUniqueID(resource.getID()); try {
upmess.setXmlProfile(mess.getProfile()); if (!FilterManager.getExecutor(resource.getType()).accept(resource)) {
upmess.setType(mess.getType()); logger.warn("Resource " + resource.getID() + " NOT accepted ");
this.updateResource(upmess); throw new ResourceNotAcceptedFault();
// throw new ProfileAlreadyRegisteredFault(); }
} else { logger.trace("Resource " + resource.getID() + " accepted ");
// try to create resource } catch (InvalidFilterException e) {
try { logger.warn("Invalid filter selected, the resource "
logger.debug(resource.getID() + " Creating the stateful resource for " + resource.getID()); + resource.getID() + " CANNOT be filtered ");
}
ProfileResource presource = (ProfileResource) ProfileContext.getContext().getWSHome().create(ProfileContext.getContext()
.makeKey(resource.getID()), resource); // check if the Resource already exists
presource.store(); if (isResourceCreated(resource.getID())) {
// update the existing resource
// Deleting the WSResource created if != from GHN logger.warn("A Resource with ID " + resource.getID()
if (resource.getType().compareTo(GCUBEHostingNode.TYPE) != 0) + " is already registered");
this.scheduleForLazyDeletion(presource); UpdateResourceMessage upmess = new UpdateResourceMessage();
upmess.setUniqueID(resource.getID());
} catch (Exception ex) { upmess.setXmlProfile(mess.getProfile());
String msg = "Error creating Resource " + resource.getID(); upmess.setType(mess.getType());
logger.error(msg, ex); this.updateResource(upmess);
throw new RemoteException(msg); // throw new ProfileAlreadyRegisteredFault();
} } else {
try { // try to create resource
updateCounterInfo(resource.getID(), ResourceType.valueOf(mess.getType()), OperationType.create, Calendar.getInstance()); try {
GCUBEEvent<ServiceContext.RegistryTopic, GCUBEResource> event = new GCUBEEvent<ServiceContext.RegistryTopic, GCUBEResource>(); logger.debug(resource.getID() + " Creating the stateful resource for " + resource.getID());
event.setPayload(resource);
ServiceContext.getContext().getTopicProducer().notify( ProfileResource presource = (ProfileResource) ProfileContext.getContext().getWSHome().create(ProfileContext.getContext()
ServiceContext.RegistryTopic.CREATE, event); .makeKey(resource.getID()), resource);
} catch (Exception e) { presource.store();
logger.warn("Error updating Counting info for resource with ID " + resource.getID(), e);
} // Deleting the WSResource created if != from GHN
} if (resource.getType().compareTo(GCUBEHostingNode.TYPE) != 0)
this.scheduleForLazyDeletion(presource);
StringWriter writer = new StringWriter();
try { } catch (Exception ex) {
logger.debug("Persisting the stateful resource for " String msg = "Error creating Resource " + resource.getID();
+ resource.getID()); logger.error(msg, ex);
resource.store(writer); throw new RemoteException(msg);
} catch (Exception e) { }
logger.error("Persistence failed for " + resource.getID(), e); try {
} updateCounterInfo(resource.getID(), ResourceType.valueOf(mess.getType()), OperationType.create, Calendar.getInstance());
GCUBEEvent<ServiceContext.RegistryTopic, GCUBEResource> event = new GCUBEEvent<ServiceContext.RegistryTopic, GCUBEResource>();
logger.info("Profile " + resource.getID() + " created "); event.setPayload(resource);
return writer.toString(); 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();
try {
logger.debug("Persisting the stateful resource for "
+ resource.getID());
resource.store(writer);
} catch (Exception e) {
logger.error("Persistence failed for " + resource.getID(), e);
}
logger.info("Profile " + resource.getID() + " created ");
return writer.toString();
} }
/** /**
@ -275,72 +233,66 @@ public class RegistryFactory extends GCUBEPortType {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public UpdateResourceResponse updateResource(UpdateResourceMessage mess) public UpdateResourceResponse updateResource(UpdateResourceMessage mess)
throws RemoteException, SchemaValidationFault, throws RemoteException, SchemaValidationFault, ResourceNotAcceptedFault, GCUBEFault {
ResourceNotAcceptedFault, GCUBEFault {
logger.info("UpdateResource operation invoked");
// logSecurityInfo("updateResource"); String ID = mess.getUniqueID();
logger.info("UpdateResource operation invoked"); String xmlProfile = mess.getXmlProfile();
String ID = mess.getUniqueID(); GCUBEResource resource = null;
String xmlProfile = mess.getXmlProfile(); CreateResourceMessage input = null;
GCUBEResource resource = null; // validating input parameters
CreateResourceMessage input = null; if (ID == null || ID.compareTo("") == 0) {
// validating input parameters logger.debug(" ID missing ");
if (ID == null || ID.compareTo("") == 0) { throw new RemoteException("Error, ID missing");
logger.debug(" ID missing ");
throw new RemoteException("Error, ID missing");
}
if (xmlProfile == null || xmlProfile.compareTo("") == 0) {
logger.debug("Profile missing");
throw new RemoteException("Error, profile missing");
}
// check if the profile exist
if (!isResourceCreated(ID)) {
input = new CreateResourceMessage();
input.setProfile(xmlProfile);
input.setType(mess.getType());
this.createResource(input);
return new UpdateResourceResponse();
}
try {
resource = ResourceType.valueOf(mess.getType()).getResourceClass();
resource.load(new StringReader(xmlProfile));
// apply resource filter
try {
if (!FilterManager.getExecutor(resource.getType()).accept(resource)) {
logger.warn("Resource " + resource.getID() + " NOT accepted ");
throw new ResourceNotAcceptedFault();
} }
logger.trace("Resource " + resource.getID() + " accepted "); if (xmlProfile == null || xmlProfile.compareTo("") == 0) {
} catch (InvalidFilterException e) { logger.debug("Profile missing");
logger.warn("Invalid filter selected, the resource " + resource.getID() + " CANNOT be filtered "); throw new RemoteException("Error, profile missing");
} }
// check if the profile exist
ProfileResource presource = getProfileResource(ID); if (!isResourceCreated(ID)) {
presource.updateResource(resource); input = new CreateResourceMessage();
input.setProfile(xmlProfile);
// Deleting the WSResource created input.setType(mess.getType());
if (resource.getType().compareTo(GCUBEHostingNode.TYPE) != 0) this.createResource(input);
this.scheduleForLazyDeletion(presource); return new UpdateResourceResponse();
}
} catch (Exception e) { try {
logger.error("Error updating profile for ID " + e); resource = ResourceType.valueOf(mess.getType()).getResourceClass();
throw new RemoteException(e.toString()); resource.load(new StringReader(xmlProfile));
} // apply resource filter
try {
try { if (!FilterManager.getExecutor(resource.getType()).accept(resource)) {
updateCounterInfo(ID, ResourceType.valueOf(mess.getType()), logger.warn("Resource " + resource.getID() + " NOT accepted ");
OperationType.update, Calendar.getInstance()); throw new ResourceNotAcceptedFault();
GCUBEEvent<ServiceContext.RegistryTopic, GCUBEResource> event = new GCUBEEvent<ServiceContext.RegistryTopic, GCUBEResource>(); }
event.setPayload(resource); logger.trace("Resource " + resource.getID() + " accepted ");
ServiceContext.getContext().getTopicProducer().notify( } catch (InvalidFilterException e) {
ServiceContext.RegistryTopic.UPDATE, event); logger.warn("Invalid filter selected, the resource " + resource.getID() + " CANNOT be filtered ");
} catch (Exception e) { }
logger.warn("Error updating counting info for resource with ID "
+ resource.getID(), e); ProfileResource presource = getProfileResource(ID);
} presource.updateResource(resource);
logger.info("Profile " + mess.getUniqueID() + " updated");
return new UpdateResourceResponse(); // Deleting the WSResource created
if (resource.getType().compareTo(GCUBEHostingNode.TYPE) != 0)
this.scheduleForLazyDeletion(presource);
} catch (Exception e) {
logger.error("Error updating profile for ID " + e);
throw new RemoteException(e.toString());
}
try {
this.updateCounterInfo(ID, ResourceType.valueOf(mess.getType()), OperationType.update, Calendar.getInstance());
GCUBEEvent<ServiceContext.RegistryTopic, GCUBEResource> event = new GCUBEEvent<ServiceContext.RegistryTopic, GCUBEResource>();
event.setPayload(resource);
ServiceContext.getContext().getTopicProducer().notify(ServiceContext.RegistryTopic.UPDATE, event);
} catch (Exception e) {
logger.warn("Error while updating the counting info for resource with ID " + resource.getID(), e);
}
logger.info("Profile " + mess.getUniqueID() + " updated");
return new UpdateResourceResponse();
} }
/** /**
@ -481,63 +433,27 @@ public class RegistryFactory extends GCUBEPortType {
* *
* @return NotifierResource resource * @return NotifierResource resource
* @throws RemoteException * @throws RemoteException
* Exception *
*/ */
private RegistryFactoryResource getResource() throws RemoteException { 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( FactoryContext.getContext().makeKey(NOTIFICATOR_RESOURCE_KEY));
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}
* @param message
* UpdateScopeInProfileMessage
* @return String
* @throws GCUBEFault
* -
*/ */
public String updateScopeInProfile(UpdateScopeInProfileMessage message)
throws GCUBEFault {
return null;
}
/**
*
* @param message
* RemoveScopeInProfileMessage
* @return String
* @throws GCUBEFault
* -
*/
public String removeScopeInProfile(RemoveScopeInProfileMessage message)
throws GCUBEFault {
return null;
}
/**
*
* @param message
* UpdateStateMessage
* @return
* @throws GCUBEFault
* -
*/
public String updateState(UpdateStateMessage message) throws GCUBEFault {
return null;
}
@Override @Override
protected GCUBEServiceContext getServiceContext() { protected GCUBEServiceContext getServiceContext() {
return ServiceContext.getContext(); return ServiceContext.getContext();
} }
} }

View File

@ -16,7 +16,7 @@ import org.gcube.informationsystem.registry.impl.contexts.ServiceContext;
*/ */
public class EliminatePoolingThread extends Thread { public class EliminatePoolingThread extends Thread {
private final long sleepTime = (Long) ServiceContext.getContext().getProperty("temporaryResourceSweeperIntervalInMs");; private final long sleepTime = (Long) ServiceContext.getContext().getProperty("temporaryResourceSweeperIntervalInMs");
private List<Pair> stack = Collections.synchronizedList(new LinkedList<Pair>()); private List<Pair> stack = Collections.synchronizedList(new LinkedList<Pair>());
@ -24,49 +24,57 @@ public class EliminatePoolingThread extends Thread {
public void run() { public void run() {
boolean noErrors = true; while (true) {
while (noErrors) {
try { try {
Thread.sleep(sleepTime); Thread.sleep(sleepTime);
} catch (InterruptedException e) {} } catch (InterruptedException e) {}
LinkedList<Pair> tmpStack = new LinkedList<Pair>(); try {
try {
long timestamp = System.currentTimeMillis();
synchronized (stack) { synchronized (stack) {
int numRes = stack.size(); int numRes = stack.size();
while (stack.size() > 0) { LinkedList<Pair> undeletedResources = this.checkResources();
Pair c = stack.remove(stack.size() - 1); logger.debug("cannot destroy " + undeletedResources.size() + " resources, retrying later");
logger.trace("checking resource for deletion " + c.resource.getID()); logger.debug("destroyed " + (numRes - undeletedResources.size()) + " resources ");
logger.trace("timenstamp now: " + timestamp + ", resource lifetime: " + c.lifetime); stack.addAll(undeletedResources);
if (timestamp >= c.lifetime) {
try {
logger.debug("temporary resource " + c.resource.getID() + " is going to be deleted");
ProfileContext.getContext().getWSHome().remove(c.resource.getID());
} catch (Exception e) {
logger.error(e);
tmpStack.offer(c);// re-insert the resource in the list
}
} else {
tmpStack.offer(c); // re-insert the resource in the list
}
}
logger.debug("cannot destroy " + tmpStack.size() + " resources retrying in 30 seconds");
logger.debug("destroyed " + (numRes - tmpStack.size()) + " resources ");
stack.addAll(tmpStack);
} // end synchronized block } // end synchronized block
} catch (Exception e) { } catch (Exception e) {
logger.error("Cannot continue with thread Excecution " + e); logger.error("Cannot continue with thread Excecution " + e);
noErrors = false; break;
} }
} }
} }
public synchronized List<Pair> getStack() { public synchronized List<Pair> getStack() {
return this.stack; return this.stack;
} }
/**
* Checks and deletes expired temporary resources
* @return the list of still living resources
*/
private LinkedList<Pair> checkResources() {
LinkedList<Pair> tmpStack = new LinkedList<Pair>();
long timestamp = System.currentTimeMillis();
while (stack.size() > 0) {
Pair c = stack.remove(stack.size() - 1);
logger.trace("checking resource for deletion " + c.resource.getID());
logger.trace("timenstamp now: " + timestamp + ", resource lifetime: " + c.lifetime);
if (timestamp >= c.lifetime) {
try {
logger.debug("temporary resource " + c.resource.getID() + " is going to be deleted");
ProfileContext.getContext().getWSHome().remove(c.resource.getID());
} catch (Exception e) {
logger.error(e);
tmpStack.offer(c);// re-insert the resource in the list
}
} else {
tmpStack.offer(c); // re-insert the resource in the list
}
}
return tmpStack;
}
} }

View File

@ -0,0 +1,84 @@
package org.gcube.informationsystem.registry.impl.resources;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.gcube.common.core.resources.GCUBEHostingNode;
import org.gcube.common.core.resources.GCUBEResource;
/**
* A to-be-registered {@link GCUBEResource} passed to the ISRegistry
*
* @author Manuele Simi (ISTI-CNR)
*
*/
public class GCUBERegisteredResource {
protected GCUBEResource resource;
Document dom = null;
public GCUBERegisteredResource (GCUBEResource resource) throws Exception {
this.resource = resource;
this.parse();
}
public String getID() {
return this.resource.getID();
}
/**
* States if the resource is a temporary resource, i.e. must be destroyed after its registration
* @return
*/
public boolean isTemporary() {
if (resource.getType().compareTo(GCUBEHostingNode.TYPE) == 0)
return false;
else
return true;
}
private void parse() throws Exception {
StringWriter writer = new StringWriter();
try {
resource.store(writer);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
StringReader reader = new StringReader(writer.toString().substring(writer.toString().indexOf("?>") + 2, writer.toString().length()));
InputSource source = new InputSource(reader);
this.dom = builder.parse(source);
} catch (Exception e1) {
throw new Exception("Unable to parse the resource");
}
}
/**
* @return the XML Document representation of the resouce
*/
public Document getAsDOM() {
return this.dom;
}
/**
* @return the source resource
*/
public GCUBEResource getSource() {
return this.resource;
}
}

View File

@ -1,253 +1,117 @@
package org.gcube.informationsystem.registry.impl.state; package org.gcube.informationsystem.registry.impl.state;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.gcube.common.core.resources.GCUBEHostingNode;
import org.gcube.common.core.resources.GCUBEResource; import org.gcube.common.core.resources.GCUBEResource;
import org.gcube.common.core.state.GCUBEWSResource; import org.gcube.common.core.state.GCUBEWSResource;
import org.gcube.common.core.utils.logging.GCUBELog; import org.gcube.common.core.utils.logging.GCUBELog;
import org.gcube.informationsystem.registry.impl.resources.GCUBERegisteredResource;
import org.globus.wsrf.ResourceException; import org.globus.wsrf.ResourceException;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.xml.sax.InputSource;
/** /**
* Stateful resource wrapping a {@link GCUBEResource} * Profile Stateful resource
* *
* @author Andrea Manzi, Lucio Lelii, Manuele Simi (ISTI-CNR) * @author Andrea Manzi, Lucio Lelii, Manuele Simi (ISTI-CNR)
* *
*/ */
public class ProfileResource extends GCUBEWSResource { public class ProfileResource extends GCUBEWSResource {
private static GCUBELog logger = new GCUBELog(ProfileResource.class private static GCUBELog logger = new GCUBELog(ProfileResource.class.getName());
.getName());
protected GCUBEResource gCubeResource; protected GCUBEResource gCubeResource;
protected static final String ProfileRP = "Profile"; protected static final String ProfileRP = "Profile";
@Override /**
protected String[] getPropertyNames() { * Constructor
return new String[] { ProfileRP }; *
} */
public ProfileResource() {};
/**
* Constructor /**
* * {@inheritDoc}
* @throws Exception */
* Exception @Override
*/ protected String[] getPropertyNames() {
public ProfileResource() throws Exception { return new String[] { ProfileRP };
};
/**
* initialize the resource
*
* @param params
* Object
* @throws ResourceException
* if resource is missing
*/
@Override
public void initialise(Object... params) throws ResourceException {
GCUBEResource resource = (GCUBEResource) params[0];
logger.debug("initializing resource " + resource.getID());
Document dom = null;
StringWriter writer = new StringWriter();
try {
resource.store(writer);
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
StringReader reader = new StringReader(writer.toString().substring(
writer.toString().indexOf("?>") + 2,
writer.toString().length()));
InputSource source = new InputSource(reader);
dom = builder.parse(source);
} catch (Exception e1) {
throw new ResourceException(e1);
} }
this.setProfile(dom); /**
// this.setNotificationProfile(dom); * Initializes the resource
this.setGCubeResource(resource);
if (hasToLive(resource))
this.setTerminationTime(null);
/*
* if (hasToLive(resource)) { //registration of Topic To IS-Notifier
* *
* listQname.add(new SimpleTopic(this.generateQName())); * @param params
* synchronized(RegistryFactory.notificationMap) { if * Object
* (!(RegistryFactory.notificationMap.contains(this.getID()))) { try { * @throws ResourceException
* notifier = GHNContext.getImplementation(ISNotifier.class); * if resource is missing
* notifier.registerISNotification(this.getEPR(), listQname,
* ServiceContext.getContext());
*
* } catch (Exception e){ e.printStackTrace(); throw new
* ResourceException(e); }
*
* RegistryFactory.notificationMap.add(this.getID()); } } }
*/ */
} @Override
public void initialise(Object... params) throws ResourceException {
GCUBERegisteredResource resource;
try {
resource = new GCUBERegisteredResource((GCUBEResource) params[0]);
} catch (Exception e) {
throw new ResourceException("Invalid GCUBEResource");
}
logger.debug("initializing resource " + resource.getID());
this.setProfile(resource.getAsDOM());
// this.setNotificationProfile(dom);
this.setGCubeResource(resource.getSource());
/** if (resource.isTemporary())
* set notification Profile this.setTerminationTime(null);
* }
* @param profile
* Document
*/
/*
* public void setNotificationProfile(Document profile) {
* this.getResourcePropertySet().get(NotificationProfileRP).clear();
* this.getResourcePropertySet().get(NotificationProfileRP).add(profile);
* //this.getPersistenceDelegate().store(this); }
*/
/** /**
* get notification Profile * Sets Profile
* *
* @return Document profile * @param profile the profile
*/
/*
* public Document getNotificationProfile() { return
* (Document)this.getResourcePropertySet
* ().get(NotificationProfileRP).get(0); }
*/
/**
* set Profile
*
* @param profile
* Document
*/
public void setProfile(Document profile) {
this.getResourcePropertySet().get(ProfileRP).clear();
this.getResourcePropertySet().get(ProfileRP).add(profile);
}
/**
* get Profile
*
* @return Document profile
*/
public Document getProfile() {
return (Document) this.getResourcePropertySet().get(ProfileRP).get(0);
}
/**
* Checks if the profile has to live or not
*
* @param resource the resource
* @return true/false
*/
private boolean hasToLive(GCUBEResource resource) {
if (resource.getType().compareTo(GCUBEHostingNode.TYPE) == 0)
return true;
else
return false;
}
/**
* Gets the Resource QName
*
* @return the Resource QName
*/
// private QName generateQName() {
// return new QName(ProfileContext.getContext().getNamespace(),
// NotificationProfileRP+this.getID().getValue());
// }
/**
* Returns the Resource
*
* @return the resource
*/
public GCUBEResource getGCubeResource() {
return this.gCubeResource;
}
/**
* Sets the resource
*
* @param resource the resource
*/
public void setGCubeResource(GCUBEResource resource) {
this.gCubeResource = resource;
}
// /** {@inheritDoc}*/
// @Override
// public void onRemove() throws ResourceException{
// super.onRemove();
// logger.info("Resource " + this.getID()+ " is going to be removed.");
//
// try {
// publisher = GHNContext.getImplementation(ISPublisher.class);
// } catch (Exception e) {
// e.printStackTrace();
// }
//
// /*
// if (RegistryFactory.notificationMap.contains(this.getID()))
// {
// try {
// notifier = GHNContext.getImplementation(ISNotifier.class);
// notifier.unregisterISNotification(this.getEPR(),listQname,ServiceContext.getContext());
// } catch (ISPublisherException e) {
// e.printStackTrace();
// } catch (Exception e) {
// e.printStackTrace();
// }
//
// synchronized(RegistryFactory.notificationMap) {
// RegistryFactory.notificationMap.remove(this.getID());
// }
// }*/
//
// try {
// publisher.removeWSResource(this,ServiceContext.getContext().getScope());
// //System.out.println(" "+this.getResourcePropertySet().getScope().get(0));
// } catch (ISPublisherException e) {
// e.printStackTrace();
// }
//
// }
/**
* Updates the resource
*
* @param resource the resource to update
* @throws Exception if something goes wrong
*/
public void updateResource(GCUBEResource resource) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
StringWriter writer = new StringWriter();
resource.store(writer);
StringReader reader = new StringReader(writer.toString());
InputSource source = new InputSource(reader);
Document dom = builder.parse(source);
this.setProfile(dom);
this.setGCubeResource(resource);
// updates the notification profile too
/*
* if (this.hasToLive(resource)) this.setNotificationProfile(dom);
*/ */
this.store(); public void setProfile(Document profile) {
} this.getResourcePropertySet().get(ProfileRP).clear();
this.getResourcePropertySet().get(ProfileRP).add(profile);
}
/**
* Gets Profile
*
* @return the profile
*/
public Document getProfile() {
return (Document) this.getResourcePropertySet().get(ProfileRP).get(0);
}
/**
* Returns the source {@link GCUBEResource}
*
* @return the resource
*/
public GCUBEResource getGCubeResource() {
return this.gCubeResource;
}
/**
* Sets the source {@link GCUBEResource}
*
* @param resource the resource
*/
public void setGCubeResource(GCUBEResource resource) {
this.gCubeResource = resource;
}
/**
* Updates the resource
*
* @param resource
* the resource to update
* @throws Exception
* if something goes wrong
*/
public void updateResource(GCUBEResource resource) throws Exception {
GCUBERegisteredResource regResource = new GCUBERegisteredResource(resource);
this.setProfile(regResource.getAsDOM());
this.setGCubeResource(resource);
this.store();
}
} }