is-registry/src/org/gcube/informationsystem/registry/impl/state/ProfileResource.java

278 lines
7.2 KiB
Java

package org.gcube.informationsystem.registry.impl.state;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.gcube.common.core.informationsystem.notifier.ISNotifier;
import org.gcube.common.core.informationsystem.publisher.ISPublisher;
import org.gcube.common.core.informationsystem.publisher.ISPublisherException;
import org.gcube.common.core.contexts.GHNContext;
import org.gcube.common.core.resources.GCUBEHostingNode;
import org.gcube.common.core.resources.GCUBEResource;
import org.gcube.common.core.resources.GCUBERunningInstance;
import org.gcube.common.core.resources.GCUBEService;
import org.gcube.common.core.state.GCUBEWSResource;
import org.gcube.common.core.utils.logging.GCUBELog;
import org.gcube.informationsystem.registry.impl.RegistryFactory;
import org.gcube.informationsystem.registry.impl.contexts.ProfileContext;
import org.gcube.informationsystem.registry.impl.contexts.ServiceContext;
import org.globus.wsrf.ResourceException;
import org.globus.wsrf.impl.SimpleTopic;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
/**
* @author Andrea Manzi
*
*/
public class ProfileResource extends GCUBEWSResource {
private static GCUBELog logger = new GCUBELog(ProfileResource.class.getName());
private ISPublisher publisher = null;
private ISNotifier notifier= null;
ArrayList<SimpleTopic> listQname = new ArrayList<SimpleTopic>();
protected static final String NotificationProfileRP="NotificationProfile";
protected GCUBEResource gCubeResource;
protected static final String ProfileRP="Profile";
@Override
protected String[] getTopicNames(){
return new String[] {NotificationProfileRP};
}
@Override
protected String[] getPropertyNames(){
return new String[] {ProfileRP};
}
/**
* Constructor
* @throws Exception Exception
*/
public ProfileResource() throws Exception{};
/**
* initialize the resource
* @param params Object
*/
@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);
this.setGCubeResource(resource);
if (hasToLive(resource)) this.setTerminationTime(null);
if (hasToLive(resource)) {
//registration of Topic To IS-Notifier
listQname.add(new SimpleTopic(this.generateQName()));
synchronized(RegistryFactory.notificationMap) {
if (!(RegistryFactory.notificationMap.contains(this.getID()))) {
try {
notifier = GHNContext.getImplementation(ISNotifier.class);
notifier.registerISNotification(this.getEPR(), listQname, ServiceContext.getContext());
} catch (Exception e){
e.printStackTrace();
throw new ResourceException(e);
}
RegistryFactory.notificationMap.add(this.getID());
}
}
}
}
/**
* set notification Profile
*
* @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
*
* @return Document 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);
//this.getPersistenceDelegate().store(this);
}
/**
* 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 GCUBEReosurce resource
* @return true/false
*/
private boolean hasToLive(GCUBEResource resource) {
if (resource.getType().compareTo(GCUBERunningInstance.TYPE)==0 ||
resource.getType().compareTo(GCUBEHostingNode.TYPE)==0 ||
resource.getType().compareTo(GCUBEService.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 resource GCUBEReosurce
*/
public GCUBEResource getGCubeResource() {
return this.gCubeResource;
}
/**
* sets the resource
* @param resource GCUBEReosurce
*/
public void setGCubeResource(GCUBEResource resource) {
this.gCubeResource = resource;
}
@Override
public void remove() throws ResourceException{
super.remove();
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.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());
} catch (ISPublisherException e) {
e.printStackTrace();
}
}
/**
* updates the resource
*
* @param resource
* @throws Exception
*/
@SuppressWarnings("unchecked")
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.getPersistenceDelegate().store(this);
}
}