From dbe11e0cdb107d218a92e5d2974cec2c74a0b2fa Mon Sep 17 00:00:00 2001 From: Manuele Simi Date: Thu, 10 Nov 2011 16:55:48 +0000 Subject: [PATCH] IS-Registry test-suite modified for supporting the recursive registration of entire folders of resources git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/information-system/gCubeIS/Registry@48372 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../testsuite/RegistryRegistrationTest.java | 106 +++++++----------- .../registry/testsuite/Resource.java | 78 +++++++++++++ 2 files changed, 117 insertions(+), 67 deletions(-) create mode 100644 test/org/gcube/informationsystem/registry/testsuite/Resource.java diff --git a/test/org/gcube/informationsystem/registry/testsuite/RegistryRegistrationTest.java b/test/org/gcube/informationsystem/registry/testsuite/RegistryRegistrationTest.java index 7a841b6..9824130 100644 --- a/test/org/gcube/informationsystem/registry/testsuite/RegistryRegistrationTest.java +++ b/test/org/gcube/informationsystem/registry/testsuite/RegistryRegistrationTest.java @@ -1,20 +1,13 @@ package org.gcube.informationsystem.registry.testsuite; -import java.io.FileReader; -import java.io.StringWriter; +import java.io.File; + import org.apache.axis.message.addressing.Address; import org.apache.axis.message.addressing.EndpointReferenceType; import org.gcube.common.core.contexts.GCUBERemotePortTypeContext; -import org.gcube.common.core.contexts.GHNContext; -import org.gcube.common.core.resources.GCUBEGenericResource; -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.scope.GCUBEScope; import org.gcube.common.core.security.GCUBESecurityManagerImpl; -import org.gcube.informationsystem.registry.stubs.resourceregistration.CreateMessage; import org.gcube.informationsystem.registry.stubs.resourceregistration.ResourceRegistrationPortType; import org.gcube.informationsystem.registry.stubs.resourceregistration.service.ResourceRegistrationServiceAddressingLocator; @@ -48,91 +41,70 @@ public class RegistryRegistrationTest { for (String param : args) System.out.println("param "+ param); - // get the scope and the factory URI - GCUBEScope scope = GCUBEScope.getScope(args[3]); - + // get the scope and the REGITRATION URI + GCUBEScope scope = GCUBEScope.getScope(args[2]); + + //create the porttype EndpointReferenceType factoryEPR = new EndpointReferenceType(); - try { - factoryEPR.setAddress(new Address(args[0])); - } catch (Exception e) { - e.printStackTrace(); - Runtime.getRuntime().exit(1); - } - ResourceRegistrationServiceAddressingLocator locator = new ResourceRegistrationServiceAddressingLocator(); GCUBESecurityManagerImpl managerSec = new GCUBESecurityManagerImpl() { public boolean isSecurityEnabled() { return false; } }; - - // load the resource - GCUBEResource resource = getResource(args[2], new FileReader(args[1])); + ResourceRegistrationPortType registration = null; try { - ResourceRegistrationPortType registration = locator.getResourceRegistrationPortTypePort(factoryEPR); + factoryEPR.setAddress(new Address(args[0])); + registration = locator.getResourceRegistrationPortTypePort(factoryEPR); registration = locator.getResourceRegistrationPortTypePort(factoryEPR); - CreateMessage message = new CreateMessage(); registration = GCUBERemotePortTypeContext.getProxy(registration, scope, managerSec); - StringWriter writer = new StringWriter(); - resource.store(writer); - message.setProfile(writer.toString()); - message.setType(resource.getType()); - System.out.println(registration.create(message)); } catch (Exception e) { e.printStackTrace(); Runtime.getRuntime().exit(1); } + File param = new File(args[1]); + if (param.isDirectory()) + registerDirectory(registration,args[3],param); + else { + try { + Resource resource = new Resource(param, args[3]); + resource.register(registration); + } catch (Exception e) { + System.err.print("Unable to register " + param.getAbsolutePath()); + e.printStackTrace(); + } + } } /** - * Loads the specific GCUBEResource class - * - * @param type - * the resource type - * @param file - * the file representation of the profile - * @return the resource class - * @throws Exception - * if the loading fails + * Registers all the resources in a directory, recursively if needed + * @param registration + * @param resourceType + * @param dirToProcess */ - static GCUBEResource getResource(String type, FileReader file) - throws Exception { - if (type.compareTo("Service") == 0) { - GCUBEService service = GHNContext - .getImplementation(GCUBEService.class); - service.load(file); - return (GCUBEResource) service; - } else if (type.compareTo("RunningInstance") == 0) { - GCUBERunningInstance instance = GHNContext - .getImplementation(GCUBERunningInstance.class); - instance.load(file); - return (GCUBEResource) instance; - } else if (type.compareTo("GHN") == 0) { - GCUBEHostingNode node = GHNContext - .getImplementation(GCUBEHostingNode.class); - node.load(file); - return (GCUBEResource) node; - } else if (type.compareTo("GenericResource") == 0) { - GCUBEGenericResource generic = GHNContext - .getImplementation(GCUBEGenericResource.class); - generic.load(file); - return (GCUBEResource) generic; + static void registerDirectory(ResourceRegistrationPortType registration, String resourceType, File dirToProcess) { + for (File f : dirToProcess.listFiles()) { + if (f.isDirectory()) + registerDirectory(registration,resourceType,f); + else + try { + Resource resource = new Resource(f, resourceType); + resource.register(registration); + } catch (Exception e) { + System.err.print("Unable to register " + f.getAbsolutePath()); + e.printStackTrace(); + } } - - throw new Exception(type + " is an invalid resource type"); - } /** * Prints tester usage syntax */ static void printUsage() { - System.out - .println("RegistryRegistrationTest "); - System.out - .println("allowed types are: RunningInstance/Service/GHN/GenericResource"); + System.out.println("RegistryRegistrationTest "); + System.out.println("allowed types are: RunningInstance/Service/GHN/GenericResource"); System.exit(1); } } diff --git a/test/org/gcube/informationsystem/registry/testsuite/Resource.java b/test/org/gcube/informationsystem/registry/testsuite/Resource.java new file mode 100644 index 0000000..49aed1d --- /dev/null +++ b/test/org/gcube/informationsystem/registry/testsuite/Resource.java @@ -0,0 +1,78 @@ +package org.gcube.informationsystem.registry.testsuite; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.StringWriter; + +import org.gcube.common.core.contexts.GHNContext; +import org.gcube.common.core.resources.GCUBEGenericResource; +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.GCUBERuntimeResource; +import org.gcube.common.core.resources.GCUBEService; +import org.gcube.informationsystem.registry.stubs.resourceregistration.CreateMessage; +import org.gcube.informationsystem.registry.stubs.resourceregistration.ResourceRegistrationPortType; + +public class Resource { + + private GCUBEResource resource; + private File source; + protected Resource (File resource, String type) throws FileNotFoundException,Exception { + this.source = resource; + this.resource = this.load(type, new FileReader(resource)); + } + + + /** + * Registers the resource in the given portType + * @param registration + * @param resourceType + * @param fileToRegister + * @throws Exception + */ + protected void register(ResourceRegistrationPortType registration) throws Exception { + CreateMessage message = new CreateMessage(); + StringWriter writer = new StringWriter(); + resource.store(writer); + message.setProfile(writer.toString()); + message.setType(resource.getType()); + registration.create(message); + System.out.println("Resource "+ this.source.getAbsolutePath() +" successuflly registered: " + this.resource.getID()); + } + /** + * Loads the specific GCUBEResource class + * + * @param type the resource type + * @param file the file representation of the profile + * @return the resource class + * @throws Exception + * if the loading fails + */ + private GCUBEResource load(String type, FileReader file) throws Exception { + if (type.compareTo("Service") == 0) { + GCUBEService service = GHNContext.getImplementation(GCUBEService.class); + service.load(file); + return (GCUBEResource) service; + } else if (type.compareTo("RunningInstance") == 0) { + GCUBERunningInstance instance = GHNContext.getImplementation(GCUBERunningInstance.class); + instance.load(file); + return (GCUBEResource) instance; + } else if (type.compareTo("GHN") == 0) { + GCUBEHostingNode node = GHNContext.getImplementation(GCUBEHostingNode.class); + node.load(file); + return (GCUBEResource) node; + } else if (type.compareTo("GenericResource") == 0) { + GCUBEGenericResource generic = GHNContext.getImplementation(GCUBEGenericResource.class); + generic.load(file); + return (GCUBEResource) generic; + } else if (type.compareTo("RuntimeResource") == 0) { + GCUBERuntimeResource rt = GHNContext.getImplementation(GCUBERuntimeResource.class); + rt.load(file); + return (GCUBEResource) rt; + } + + throw new Exception(type + " is an invalid resource type"); + } +}