From 7ca1e0dfc4a3eb63047186f5ff355d6ed6396f85 Mon Sep 17 00:00:00 2001 From: Manuele Simi Date: Fri, 29 Oct 2010 02:25:51 +0000 Subject: [PATCH] Fixing some backward compatibility problems with profiles registered via AF git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/information-system/gCubeIS/Collector@30238 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../state/AggregatorRegisteredResource.java | 28 +++++++--- .../collector/impl/utils/EntryParser.java | 16 ++++-- .../collector/impl/utils/Identifier.java | 34 +++++++++++ .../collector/impl/utils/MsgParser.java | 56 ++++++++++--------- 4 files changed, 95 insertions(+), 39 deletions(-) diff --git a/src/org/gcube/informationsystem/collector/impl/state/AggregatorRegisteredResource.java b/src/org/gcube/informationsystem/collector/impl/state/AggregatorRegisteredResource.java index be7cabd..589c18d 100755 --- a/src/org/gcube/informationsystem/collector/impl/state/AggregatorRegisteredResource.java +++ b/src/org/gcube/informationsystem/collector/impl/state/AggregatorRegisteredResource.java @@ -8,7 +8,9 @@ import org.gcube.common.core.utils.logging.GCUBELog; import org.gcube.informationsystem.collector.impl.state.AggregatorRegisteredResource; import org.gcube.informationsystem.collector.impl.utils.*; import org.gcube.informationsystem.collector.impl.xmlstorage.exist.State; +import org.gcube.informationsystem.collector.impl.resources.BaseDAIXResource; import org.gcube.informationsystem.collector.impl.resources.GCUBEInstanceStateResource; +import org.gcube.informationsystem.collector.impl.resources.GCUBEProfileResource; import org.gcube.informationsystem.collector.impl.resources.GCUBEXMLResource; import org.globus.mds.aggregator.impl.AggregatorServiceGroupResource; import org.globus.mds.aggregator.impl.AggregatorServiceGroupEntryResource; @@ -177,14 +179,24 @@ public class AggregatorRegisteredResource extends AggregatorServiceGroupResource logger.debug("Aggregator Sink " + entryparser.getSink()); // Build the new resource to store - logger.debug("Storing the new delivered resource"); - - //TODO: check if it's a profile or an RP, by relying on the publisher (registry or not) - - GCUBEInstanceStateResource instanceState = new GCUBEInstanceStateResource(); - instanceState.setResourceName(Identifier.buildInstanceStateID(entryparser)); - instanceState.setContent(aentry.getEntryAsString()); - GCUBEXMLResource res = new GCUBEXMLResource(instanceState); + logger.debug("Storing the new delivered resource"); + BaseDAIXResource resource; + if (entryparser.getType().compareToIgnoreCase(GCUBEProfileResource.TYPE) == 0) { + //TODO: this will be removed after all the gHNs have the ISPublisher 2.0 installed + // and all the profiles arrive via the XMLCollectionAccess PT + resource = new GCUBEProfileResource(); + String profile = aentry.getProfile(); + //logger.debug("Received profile: " + profile); + resource.setResourceName(Identifier.buildProfileID(profile)); + resource.setContent(profile); + } else { + resource = new GCUBEInstanceStateResource(); + resource.setResourceName(Identifier.buildInstanceStateID(entryparser)); + resource.setContent(aentry.getEntryAsString()); + } + + + GCUBEXMLResource res = new GCUBEXMLResource(resource); res.setEntryKey(sinkparser.getEntryKey()); res.setGroupKey(sinkparser.getGroupKey()); res.setTerminationTime(entry.getTerminationTime()); diff --git a/src/org/gcube/informationsystem/collector/impl/utils/EntryParser.java b/src/org/gcube/informationsystem/collector/impl/utils/EntryParser.java index 799f548..b54dcad 100644 --- a/src/org/gcube/informationsystem/collector/impl/utils/EntryParser.java +++ b/src/org/gcube/informationsystem/collector/impl/utils/EntryParser.java @@ -15,6 +15,12 @@ import org.globus.mds.aggregator.impl.AggregatorServiceGroupEntryResource; public class EntryParser { private AggregatorServiceGroupEntryResource entry = null; + + private static final String registryNS = "gcube/informationsystem/registry/Registry"; + + public enum RESOURCETYPE { + Profile, Properties + } public EntryParser(AggregatorServiceGroupEntryResource entry) { this.entry = entry; @@ -83,17 +89,17 @@ public class EntryParser { /** * - * @return the {@link RESOURCETYPE} + * @return the type of the resource (Profile or Properties) */ - /* public RESOURCETYPE getType() { + public String getType() { EndpointReferenceType memberEpr = entry.getMemberEPR(); if (memberEpr.getAddress().toString().endsWith(registryNS)) { - return RESOURCETYPE.Profile; + return "Profile"; } else { - return RESOURCETYPE.Properties; + return "Properties"; } } -*/ + public void getRPSet() { // get RP set from entry // ResourcePropertySet rpSet = entry.getResourcePropertySet(); diff --git a/src/org/gcube/informationsystem/collector/impl/utils/Identifier.java b/src/org/gcube/informationsystem/collector/impl/utils/Identifier.java index 512441c..87a2284 100644 --- a/src/org/gcube/informationsystem/collector/impl/utils/Identifier.java +++ b/src/org/gcube/informationsystem/collector/impl/utils/Identifier.java @@ -1,5 +1,15 @@ package org.gcube.informationsystem.collector.impl.utils; +import java.io.StringReader; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathFactory; + +import org.w3c.dom.Document; +import org.xml.sax.InputSource; + /** * * Build instance state identifiers @@ -27,4 +37,28 @@ public class Identifier { public static String buildInstanceStateID(String source, String id) { return source.replace("http://", "").replace(":", "").replace("/", "-") + "-" + id.replace("http://", "").replace(":", "").replace("/", "-"); } + + /** + * Builds a profile ID starting from the message (a string serialization of the profile) + * @param message the message + * @return the ID of the profile + * @throws Exception if the profile is not well-formed + */ + public static String buildProfileID(String message) throws Exception { + Document internalDOM; + try { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + StringReader reader = new StringReader(message); + InputSource source = new InputSource(reader); + internalDOM = builder.parse(source); + XPath path = XPathFactory.newInstance().newXPath(); + // uses the GCUBEResource ID as local resource ID + return path.evaluate("/Resource/ID", internalDOM); + } catch (Exception e) { + throw new Exception("Unable to extract the ID from the resource " + e.getMessage()); + } finally { + internalDOM = null; + } + } } diff --git a/src/org/gcube/informationsystem/collector/impl/utils/MsgParser.java b/src/org/gcube/informationsystem/collector/impl/utils/MsgParser.java index 5c1ee40..b891345 100755 --- a/src/org/gcube/informationsystem/collector/impl/utils/MsgParser.java +++ b/src/org/gcube/informationsystem/collector/impl/utils/MsgParser.java @@ -15,7 +15,10 @@ import javax.xml.parsers.*; import javax.xml.xpath.*; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import java.io.StringWriter; @@ -84,33 +87,17 @@ public class MsgParser { public String getEntryType() { return this.type; } - + /** - * Returns the entry type if the entry is a profile + * Returns the Scope Name included in the entry * * @throws Exception * - * @return the entry type + * @return the Scope name */ - public String getResourceType() throws Exception { + public String getScopeName() throws Exception { - if (this.type.equalsIgnoreCase("Profile")) { - logger.warn("The entry does not have a ResourceType"); - throw new Exception("Invalid resource (type = Properties)"); - } - return this.getGCUBEProperty("DILIGENTResourceType"); - } - - /** - * Returns the VO Name included in the entry - * - * @throws Exception - * - * @return the VO name - */ - public String getVOName() throws Exception { - - return this.getGCUBEProperty("VO"); + return this.getGCUBEProperty("Scope"); } /** @@ -168,11 +155,31 @@ public class MsgParser { * * @return the DHN ID */ - public String getDHNID() throws Exception { + public String getGHNID() throws Exception { return this.getGCUBEProperty("GHN"); } + /** + * + * @return the profile, it any + * @throws Exception + */ + public String getProfile() throws Exception { + try { + TransformerFactory transFactory = TransformerFactory.newInstance(); + Transformer transformer = transFactory.newTransformer(); + StringWriter buffer = new StringWriter(); + transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); + transformer.transform(new DOMSource(this.internalDOM.getElementsByTagName("Resource").item(0)), new StreamResult(buffer)); + return buffer.toString(); + } catch (Exception e) { + throw new Exception("Unable to deserialise content data"); + } + + } + + /** * Reads a GCUBE Property * @@ -182,17 +189,14 @@ public class MsgParser { */ private String getGCUBEProperty(String propName) throws Exception { - String value = null; - try { - value = path.evaluate("//" + rootElement + return path.evaluate("//" + rootElement + "/child::*[local-name() = '" + propName + "']", internalDOM); } catch (XPathExpressionException xpee) { logger.error("", xpee); throw new Exception("XPath evaluation error"); } - return value; } /**