From bd6c50f0628b512c772d102047af0d6e6febb15c Mon Sep 17 00:00:00 2001 From: Manuele Simi Date: Tue, 2 Nov 2010 20:23:58 +0000 Subject: [PATCH] Adding CreateSubcollection and RemoveSubcollection implementation (plus related testers), removing old invalid testers from the test-suite git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/information-system/gCubeIS/Collector@30455 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../porttypes/wsdaix/XMLCollectionAccess.java | 94 +++++++++++++++++-- .../impl/xmlstorage/exist/DataManager.java | 2 +- .../impl/xmlstorage/exist/State.java | 2 +- .../impl/xmlstorage/exist/Sweeper.java | 6 +- .../impl/xmlstorage/exist/XMLStorage.java | 34 +++++-- .../testsuite/wsdaix/AddDocumentsTester.java | 3 +- .../wsdaix/CreateSubcollectionTester.java | 44 +++++++++ .../wsdaix/RemoveSubcollectionTester.java | 36 +++++++ test-suite/getProfile.sh | 1 - test-suite/getResource.sh | 1 - 10 files changed, 199 insertions(+), 24 deletions(-) create mode 100644 src/org/gcube/informationsystem/collector/stubs/testsuite/wsdaix/CreateSubcollectionTester.java create mode 100644 src/org/gcube/informationsystem/collector/stubs/testsuite/wsdaix/RemoveSubcollectionTester.java delete mode 100755 test-suite/getProfile.sh delete mode 100755 test-suite/getResource.sh diff --git a/src/org/gcube/informationsystem/collector/impl/porttypes/wsdaix/XMLCollectionAccess.java b/src/org/gcube/informationsystem/collector/impl/porttypes/wsdaix/XMLCollectionAccess.java index 23645d4..ee701f9 100644 --- a/src/org/gcube/informationsystem/collector/impl/porttypes/wsdaix/XMLCollectionAccess.java +++ b/src/org/gcube/informationsystem/collector/impl/porttypes/wsdaix/XMLCollectionAccess.java @@ -4,6 +4,7 @@ import java.rmi.RemoteException; import org.apache.axis.message.MessageElement; import org.apache.axis.types.URI; import org.w3c.dom.Document; +import org.xmldb.api.base.XMLDBException; import org.gcube.common.core.contexts.GCUBEServiceContext; import org.gcube.common.core.porttypes.GCUBEPortType; import org.gcube.common.core.utils.logging.GCUBELog; @@ -87,7 +88,7 @@ public class XMLCollectionAccess extends GCUBEPortType { InvalidCollectionNameFaultType { int size = getDocumentsRequest.getGetDocumentRequestWrapper().length; - String targetCollection = this.getTargetCollection(getDocumentsRequest.getCollectionName()); + String targetCollection = this.URItoCollection(getDocumentsRequest.getCollectionName()); GetDocumentResponseWrapper[] responseWrapper = new GetDocumentResponseWrapper[size]; for(int i=0;i //2 - @@ -209,7 +210,7 @@ public class XMLCollectionAccess extends GCUBEPortType { xmlResource.setEntryKey(metadataReader.getEntryKey()); xmlResource.setSourceKey(metadataReader.getKey()); boolean exist = false; - if ( State.getDataManager().checkResource(xmlResource) ) { + if ( State.getDataManager().resourceExists(xmlResource) ) { exist = true; } //store/update the new resource @@ -254,7 +255,7 @@ public class XMLCollectionAccess extends GCUBEPortType { RemoveDocumentsResponse response = new RemoveDocumentsResponse(); RemoveDocumentRequestWrapper[] docs = removeDocumentsRequest.getRemoveDocumentRequestWrapper(); RemoveDocumentResponseWrapper[] responseWrappers = new RemoveDocumentResponseWrapper[docs.length]; - String targetCollection = this.getTargetCollection(removeDocumentsRequest.getCollectionName()); + String targetCollection = this.URItoCollection(removeDocumentsRequest.getCollectionName()); //response wrapper values //v1 = Success //v2 = DocumentNotRemoved-NotAuthorized @@ -268,7 +269,7 @@ public class XMLCollectionAccess extends GCUBEPortType { BaseDAIXResource resource = new BaseDAIXResource(resourceName); resource.setCollectionName(targetCollection); GCUBEXMLResource xmlResource = new GCUBEXMLResource(resource); - if ( ! State.getDataManager().checkResource(xmlResource) ) { + if ( ! State.getDataManager().resourceExists(xmlResource) ) { responseWrappers[i].setResponse(RemoveDocumentResponseWrapperResponse.value3); continue; } @@ -306,8 +307,44 @@ public class XMLCollectionAccess extends GCUBEPortType { public CreateSubcollectionResponse createSubcollection(CreateSubcollectionRequest createSubcollectionRequest) throws RemoteException, CollectionAlreadyExistsFaultType, ServiceBusyFaultType, InvalidResourceNameFaultType, InvalidCollectionNameFaultType, NotAuthorizedFaultType, DataResourceUnavailableFaultType { - - throw new NotAuthorizedFaultType(); + + String collectionPath = null; + if ( createSubcollectionRequest.getCollectionName() != null ) { + collectionPath = this.URItoCollection(createSubcollectionRequest.getCollectionName()); + if (collectionPath != null) { + if (! State.getDataManager().collectionExists(collectionPath)) { + logger.warn("Invalid collection name"); + throw new InvalidCollectionNameFaultType(); + } + } + } else { + logger.warn("Collection "+ collectionPath + " does not exist, assuming ROOT collection"); + } + + if (createSubcollectionRequest.getSubcollectionName() == null) { + logger.warn("Invalid subcollection name"); + throw new InvalidCollectionNameFaultType(); + } + + String subCollectionName = this.URItoCollection(createSubcollectionRequest.getSubcollectionName()); + if (collectionPath != null) + subCollectionName = collectionPath +"/" + subCollectionName; + if (State.getDataManager().collectionExists(subCollectionName)) { + logger.warn("Collection "+ subCollectionName + " already exists"); + throw new CollectionAlreadyExistsFaultType(); + } else { + try { + State.getDataManager().createCollection(subCollectionName); + } catch (XMLDBException e) { + logger.error("Unable to create subcollection", e); + throw new ServiceBusyFaultType(); + } catch (XMLStorageNotAvailableException e) { + logger.error("Unable to create subcollection", e); + throw new DataResourceUnavailableFaultType(); + } + } + + return new CreateSubcollectionResponse(); } @@ -326,7 +363,41 @@ public class XMLCollectionAccess extends GCUBEPortType { */ public RemoveSubcollectionResponse removeSubcollection(RemoveSubcollectionRequest removeSubcollectionRequest) throws RemoteException, ServiceBusyFaultType, InvalidResourceNameFaultType, InvalidCollectionNameFaultType, NotAuthorizedFaultType, DataResourceUnavailableFaultType { - throw new NotAuthorizedFaultType(); + + if (removeSubcollectionRequest.getSubcollectionName() == null) + throw new InvalidCollectionNameFaultType(); + + String collectionPath = null; + if ( removeSubcollectionRequest.getCollectionName() != null ) { + collectionPath = this.URItoCollection(removeSubcollectionRequest.getCollectionName()); + if (collectionPath != null) { + if (! State.getDataManager().collectionExists(collectionPath)) { + logger.warn("Invalid collection name"); + throw new InvalidCollectionNameFaultType(); + } + } + } else { + logger.warn("Collection "+ collectionPath + " does not exist, assuming ROOT collection"); + } + + String subCollectionName = this.URItoCollection(removeSubcollectionRequest.getSubcollectionName()); + if (collectionPath != null) + subCollectionName = collectionPath +"/" + subCollectionName; + if (! State.getDataManager().collectionExists(subCollectionName)) { + logger.warn("Collection "+ subCollectionName + " does not exist"); + throw new InvalidCollectionNameFaultType(); + } else { + try { + State.getDataManager().deleteCollection(subCollectionName); + logger.info("Collection " + subCollectionName + " successfully removed"); + } catch (XMLStorageNotAvailableException e) { + logger.error("Unable to create subcollection", e); + throw new DataResourceUnavailableFaultType(); + } + } + + + return new RemoveSubcollectionResponse(); } @@ -404,8 +475,11 @@ public class XMLCollectionAccess extends GCUBEPortType { throw new NotAuthorizedFaultType(); } - private String getTargetCollection(URI collection) { - return collection.getHost() + "/" + collection.getPath(); + private String URItoCollection(URI collection) { + if ((collection.getPath() == null) || (collection.getPath().length() == 0) ) + return collection.getHost(); + else + return collection.getHost() + "/" + collection.getPath(); } /** {@inheritDoc} */ diff --git a/src/org/gcube/informationsystem/collector/impl/xmlstorage/exist/DataManager.java b/src/org/gcube/informationsystem/collector/impl/xmlstorage/exist/DataManager.java index 2ba59d6..8d1957e 100644 --- a/src/org/gcube/informationsystem/collector/impl/xmlstorage/exist/DataManager.java +++ b/src/org/gcube/informationsystem/collector/impl/xmlstorage/exist/DataManager.java @@ -76,7 +76,7 @@ public class DataManager extends XMLStorage { if (Boolean.valueOf((String) ICServiceContext.getContext().getProperty("deleteRPsOnStartup", true))) { // cleanup the RPs collection logger.info("deleting all RPs..."); - this.deleteAllResourcesFromCollection(new GCUBEInstanceStateResource().getCollectionName()); + this.deleteCollection(new GCUBEInstanceStateResource().getCollectionName()); } logger.info("Restore completed"); } catch (Exception e1) { diff --git a/src/org/gcube/informationsystem/collector/impl/xmlstorage/exist/State.java b/src/org/gcube/informationsystem/collector/impl/xmlstorage/exist/State.java index 01ac71e..b649fe3 100755 --- a/src/org/gcube/informationsystem/collector/impl/xmlstorage/exist/State.java +++ b/src/org/gcube/informationsystem/collector/impl/xmlstorage/exist/State.java @@ -59,7 +59,7 @@ public class State { if (Boolean.valueOf((String) ICServiceContext.getContext().getProperty("deleteRPsOnStartup", true))) { // cleanup the RPs collection logger.info("Deleting all RPs..."); - State.dataManager.deleteAllResourcesFromCollection(new GCUBEInstanceStateResource().getCollectionName()); + State.dataManager.deleteCollection(new GCUBEInstanceStateResource().getCollectionName()); } else { logger.info("All RPs previously stored are kept in the storage"); } diff --git a/src/org/gcube/informationsystem/collector/impl/xmlstorage/exist/Sweeper.java b/src/org/gcube/informationsystem/collector/impl/xmlstorage/exist/Sweeper.java index 0b85421..b1e99c9 100755 --- a/src/org/gcube/informationsystem/collector/impl/xmlstorage/exist/Sweeper.java +++ b/src/org/gcube/informationsystem/collector/impl/xmlstorage/exist/Sweeper.java @@ -13,6 +13,7 @@ import java.util.List; import org.gcube.common.core.utils.logging.GCUBELog; import org.gcube.informationsystem.collector.impl.xmlstorage.exist.State; import org.gcube.informationsystem.collector.impl.xmlstorage.exist.Sweeper; +import org.gcube.informationsystem.collector.impl.xmlstorage.exist.XMLStorage.XMLStorageNotAvailableException; import org.gcube.informationsystem.collector.impl.resources.GCUBEInstanceStateResource; import org.gcube.informationsystem.collector.impl.resources.GCUBEXMLResource; @@ -117,11 +118,12 @@ public class Sweeper implements Runnable { /** * Deletes the Properties collection from the storage + * @throws XMLStorageNotAvailableException * */ - public static void cleanRPs() { + public static void cleanRPs() throws XMLStorageNotAvailableException { // cleanup the RPs collection - State.getDataManager().deleteAllResourcesFromCollection(new GCUBEInstanceStateResource().getCollectionName()); + State.getDataManager().deleteCollection(new GCUBEInstanceStateResource().getCollectionName()); } /** diff --git a/src/org/gcube/informationsystem/collector/impl/xmlstorage/exist/XMLStorage.java b/src/org/gcube/informationsystem/collector/impl/xmlstorage/exist/XMLStorage.java index ca3dce8..daaeec8 100755 --- a/src/org/gcube/informationsystem/collector/impl/xmlstorage/exist/XMLStorage.java +++ b/src/org/gcube/informationsystem/collector/impl/xmlstorage/exist/XMLStorage.java @@ -17,9 +17,6 @@ import org.gcube.informationsystem.collector.impl.xmlstorage.exist.XMLStorage; import org.gcube.informationsystem.collector.impl.resources.GCUBEXMLResource; import org.gcube.informationsystem.collector.impl.resources.GCUBEXMLResource.MalformedXMLResourceException; -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.IOException; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; @@ -161,7 +158,6 @@ public class XMLStorage { for (String subcollection : subcollections) { Collection child = parent.getChildCollection(subcollection); logger.info("Creating subcollection " + subcollection); - System.out.println("Creating subcollection " + subcollection); if (child == null) { child = this.createCollection(parent, subcollection); if (child == null) @@ -451,7 +447,7 @@ public class XMLStorage { * @throws MalformedXMLResourceException if the input resource is not valid * @throws XMLStorageNotAvailableException */ - public boolean checkResource(GCUBEXMLResource resource) throws MalformedXMLResourceException, XMLStorageNotAvailableException { + public boolean resourceExists(GCUBEXMLResource resource) throws MalformedXMLResourceException, XMLStorageNotAvailableException { XMLResource res = null; Collection currentCollection = this.loadCollection(resource.getCollectionName()); try { @@ -611,6 +607,28 @@ public class XMLStorage { this.resetCollection(currentCollection); } } + + /** + * Checks whether a collection exists or not + * @param collectionName the name of the collection to check + * @return true or false, depending if the collection exists or not + */ + public boolean collectionExists(String collectionName) { + String[] subcollections = collectionName.split("/"); + Collection parent = this.rootCollection; + for (String subcollection : subcollections) { + try { + Collection child = parent.getChildCollection(subcollection); + if (child == null) { + return false; + } + parent = child; + } catch (XMLDBException e) { + return false; + } + } + return true; + } /** * Creates a new collection @@ -635,9 +653,10 @@ public class XMLStorage { } /** - * Delete the collection named ROOT_COLLECTION_NAME from the storage + * Deletes the collection from the storage + * @param collectionName the name of the collection to delete */ - public void deleteAllResourcesFromCollection(String collectionName) { + public void deleteCollection(String collectionName) throws XMLStorageNotAvailableException { this.lock(); try { @@ -647,6 +666,7 @@ public class XMLStorage { logger.info("Collection deleted"); } catch (XMLDBException edb) { logger.warn("Unable to delete the collection " + collectionName + ": " + edb.toString()); + throw new XMLStorageNotAvailableException("Unable to delete the collection " + collectionName); } finally { this.unlock(); } diff --git a/src/org/gcube/informationsystem/collector/stubs/testsuite/wsdaix/AddDocumentsTester.java b/src/org/gcube/informationsystem/collector/stubs/testsuite/wsdaix/AddDocumentsTester.java index 0d942a4..60aea50 100644 --- a/src/org/gcube/informationsystem/collector/stubs/testsuite/wsdaix/AddDocumentsTester.java +++ b/src/org/gcube/informationsystem/collector/stubs/testsuite/wsdaix/AddDocumentsTester.java @@ -49,6 +49,7 @@ public class AddDocumentsTester { * 3 - document name * 4 - filename * 5 - type (Properties/Profile/Daix) + * 6 - collection name */ public static void main(String[] args) { @@ -57,7 +58,7 @@ public class AddDocumentsTester { try { AddDocumentsResponse r = addDocuments(portTypeURI,GCUBEScope.getScope(args[2]), new org.apache.axis.types.URI ("gcube://testResourceName"), - new org.apache.axis.types.URI("gcube://collection"), args[5], + new org.apache.axis.types.URI(args[6]), args[5], new String[] { args[3] }, new Document[] {TestDocuments.loadDocument(args[4])} ); logger.info("Number of response wrappers "+ r.getAddDocumentResponseWrapper().length ); diff --git a/src/org/gcube/informationsystem/collector/stubs/testsuite/wsdaix/CreateSubcollectionTester.java b/src/org/gcube/informationsystem/collector/stubs/testsuite/wsdaix/CreateSubcollectionTester.java new file mode 100644 index 0000000..bb11b4b --- /dev/null +++ b/src/org/gcube/informationsystem/collector/stubs/testsuite/wsdaix/CreateSubcollectionTester.java @@ -0,0 +1,44 @@ +package org.gcube.informationsystem.collector.stubs.testsuite.wsdaix; + +import java.net.URL; + +import org.apache.axis.types.URI; +import org.gcube.common.core.contexts.GCUBERemotePortTypeContext; +import org.gcube.common.core.scope.GCUBEScope; +import org.gcube.common.core.utils.logging.GCUBEClientLog; +import org.gcube.informationsystem.collector.stubs.wsdaix.CreateSubcollectionRequest; +import org.gcube.informationsystem.collector.stubs.wsdaix.XMLCollectionAccessPT; +import org.gcube.informationsystem.collector.stubs.wsdaix.service.WsdaixServiceAddressingLocator; + +public class CreateSubcollectionTester { + + private static GCUBEClientLog logger = new GCUBEClientLog(CreateSubcollectionTester.class); + + /** + * @param args + * 0 - host + * 1 - port + * 2 - scope + * 3 - resource name + * 4 - collection name + * 5 - subcollection name + */ + public static void main(String[] args) { + + final String portTypeURI = "http://" + args[0] + ":" + args[1] + "/wsrf/services/gcube/informationsystem/collector/wsdaix/XMLCollectionAccess"; + try { + CreateSubcollectionRequest request = new CreateSubcollectionRequest(); + request.setDataResourceAbstractName(new URI(args[3])); + request.setCollectionName(new URI(args[4])); + request.setSubcollectionName(new URI(args[5])); + XMLCollectionAccessPT stubs = new WsdaixServiceAddressingLocator().getXMLCollectionAccessPTPort(new URL(portTypeURI)); + stubs = GCUBERemotePortTypeContext.getProxy(stubs, GCUBEScope.getScope(args[2])); + stubs.createSubcollection(request); + logger.info("Subcollection successfully created"); + } catch (Exception e) { + logger.error("Failed to create subcollection " + args[5], e); + } + + } + +} diff --git a/src/org/gcube/informationsystem/collector/stubs/testsuite/wsdaix/RemoveSubcollectionTester.java b/src/org/gcube/informationsystem/collector/stubs/testsuite/wsdaix/RemoveSubcollectionTester.java new file mode 100644 index 0000000..00bff77 --- /dev/null +++ b/src/org/gcube/informationsystem/collector/stubs/testsuite/wsdaix/RemoveSubcollectionTester.java @@ -0,0 +1,36 @@ +package org.gcube.informationsystem.collector.stubs.testsuite.wsdaix; + +import java.net.URL; + +import org.apache.axis.types.URI; +import org.gcube.common.core.contexts.GCUBERemotePortTypeContext; +import org.gcube.common.core.scope.GCUBEScope; +import org.gcube.common.core.utils.logging.GCUBEClientLog; +import org.gcube.informationsystem.collector.stubs.wsdaix.RemoveSubcollectionRequest; +import org.gcube.informationsystem.collector.stubs.wsdaix.XMLCollectionAccessPT; +import org.gcube.informationsystem.collector.stubs.wsdaix.service.WsdaixServiceAddressingLocator; + +public class RemoveSubcollectionTester { + + private static GCUBEClientLog logger = new GCUBEClientLog(RemoveSubcollectionTester.class); + + /** + * @param args + */ + public static void main(String[] args) { + final String portTypeURI = "http://" + args[0] + ":" + args[1] + "/wsrf/services/gcube/informationsystem/collector/wsdaix/XMLCollectionAccess"; + try { + RemoveSubcollectionRequest request = new RemoveSubcollectionRequest(); + request.setDataResourceAbstractName(new URI(args[3])); + request.setCollectionName(new URI(args[4])); + request.setSubcollectionName(new URI(args[5])); + XMLCollectionAccessPT stubs = new WsdaixServiceAddressingLocator().getXMLCollectionAccessPTPort(new URL(portTypeURI)); + stubs = GCUBERemotePortTypeContext.getProxy(stubs, GCUBEScope.getScope(args[2])); + stubs.removeSubcollection(request); + logger.info("Subcollection successfully removed"); + } catch (Exception e) { + logger.error("Failed to create subcollection " + args[5], e); + } + } + +} diff --git a/test-suite/getProfile.sh b/test-suite/getProfile.sh deleted file mode 100755 index effeed4..0000000 --- a/test-suite/getProfile.sh +++ /dev/null @@ -1 +0,0 @@ -java -cp .:./lib/org.gcube.informationsystem.collector.testsuite.jar:$CLASSPATH org/gcube/informationsystem/collector/stubs/testsuite/xmlcollectionaccess/GetProfileTester $1 $2 $3 $4 $5 diff --git a/test-suite/getResource.sh b/test-suite/getResource.sh deleted file mode 100755 index 771010c..0000000 --- a/test-suite/getResource.sh +++ /dev/null @@ -1 +0,0 @@ -java -cp .:./lib/org.gcube.informationsystem.collector.testsuite.jar:$CLASSPATH org/gcube/informationsystem/collector/stubs/testsuite/xmlcollectionaccess/GetResourceTester $1 $2 $3 $4 \ No newline at end of file