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

217 lines
6.0 KiB
Java
Raw Normal View History

package org.gcube.informationsystem.registry.impl.core;
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 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.globus.wsrf.ResourceException;
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;
ArrayList<QName> listQname = new ArrayList<QName>();
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{};
@Override
public void initialise(Object... params) throws ResourceException {
GCUBEResource resource = (GCUBEResource) params[0];
Document dom = null;
StringWriter writer =new StringWriter();
try {
resource.store(writer);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
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(this.generateQName());
synchronized(RegistryFactory.notificationMap) {
if (!(RegistryFactory.notificationMap.contains(this.getID()))) {
try {
publisher = GHNContext.getImplementation(ISPublisher.class);
publisher.registerToISNotification(this.getEPR(), listQname, GHNContext.getContext().getDefaultScope(), ServiceContext.getContext());
} catch (Exception e){
e.printStackTrace();
throw new ResourceException(e);
}
RegistryFactory.notificationMap.add(this.getID());
}
}
}
}
public void setNotificationProfile(Document profile) {
this.getResourcePropertySet().get(NotificationProfileRP).clear();
this.getResourcePropertySet().get(NotificationProfileRP).add(profile);
//this.getPersistenceDelegate().store(this);
}
public Document getNotificationProfile() {
return (Document)this.getResourcePropertySet().get(NotificationProfileRP).get(0);
}
public void setProfile(Document profile) {
this.getResourcePropertySet().get(ProfileRP).clear();
this.getResourcePropertySet().get(ProfileRP).add(profile);
//this.getPersistenceDelegate().store(this);
}
public Document getProfile() {
return (Document)this.getResourcePropertySet().get(ProfileRP).get(0);
}
/**
* Checks if the profile has to live or not
* 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 QName.valueOf(NotificationProfileRP.toString()+"_"+this.getID());
}
public GCUBEResource getGCubeResource() {
return this.gCubeResource;
}
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 {
publisher.unregisterFromISNotification(this.getEPR(),listQname,GHNContext.getContext().getDefaultScope(),ServiceContext.getContext());
} catch (ISPublisherException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
synchronized(RegistryFactory.notificationMap) {
RegistryFactory.notificationMap.remove(this.getID());
}
}
try {
publisher.removeWSResource(this,GHNContext.getContext().getDefaultScope());
} catch (ISPublisherException e) {
e.printStackTrace();
}
}
protected void removeProfile() throws ResourceException {
this.getPersistenceDelegate().remove(this);
}
public void updateResource(GCUBEResource resource) throws Exception{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
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);
}
}