Initial import.

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portlets/admin/rmp-common-library@64966 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Massimiliano Assante 2012-12-19 12:00:57 +00:00
parent 0aa5406efe
commit 74f72f488d
48 changed files with 4817 additions and 0 deletions

8
.checkstyle Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<fileset-config file-format-version="1.2.0" simple-config="true" sync-formatter="false">
<fileset name="all" enabled="true" check-config-name="CustomChecks" local="false">
<file-match-pattern match-pattern="." include-pattern="true"/>
</fileset>
<filter name="UnOpenedFiles" enabled="true"/>
</fileset-config>

7
.classpath Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry including="**/*.java" kind="src" output="target/classes" path="src/main/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>

29
.project Normal file
View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ResourceManagementSupport</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>net.sf.eclipsecs.core.CheckstyleBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>net.sf.eclipsecs.core.CheckstyleNature</nature>
</natures>
</projectDescription>

View File

@ -0,0 +1,4 @@
#Wed Dec 19 12:49:25 CET 2012
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding/<project>=UTF-8

View File

@ -0,0 +1,13 @@
#Wed Dec 19 12:49:25 CET 2012
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.6

View File

@ -0,0 +1,5 @@
#Wed Dec 19 12:41:58 CET 2012
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1

1
distro/INSTALL Normal file
View File

@ -0,0 +1 @@
Used as a Web service in the gCube Framework

8
distro/LICENSE Normal file
View File

@ -0,0 +1,8 @@
gCube System - License
------------------------------------------------------------
The gCube/gCore software is licensed as Free Open Source software conveying to
the EUPL (http://ec.europa.eu/idabc/eupl).
The software and documentation is provided by its authors/distributors "as is"
and no expressed or implied warranty is given for its use, quality or fitness
for a particular case.

2
distro/MAINTAINERS Normal file
View File

@ -0,0 +1,2 @@
Daniele Strollo (daniele.strollo@isti.cnr.it), CNR Pisa,
Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo"

45
distro/README Normal file
View File

@ -0,0 +1,45 @@
The gCube System
------------------------------------------------------------
This work is partially funded by the European Commission in the
context of the D4Science project (www.d4science.eu), under the 1st
call of FP7 IST priority.
Authors
-------
* Daniele Strollo (daniele.strollo@isti.cnr.it), CNR Pisa,
Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo"
Version and Release Date
------------------------
version 1.0.0 (20-05-2010)
Description
--------------------
Consists of a gcube service able to properly manage the scheduling and
the deployment planning of services in a gcube infrastructure.
Download information
--------------------
Source code is available from SVN:
http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/vre-management/ResourceBroker/org.gcube.vremanagement.resourcebroker/
Binaries can be downloaded from:
http://software.d4science.research-infrastructures.eu/
Documentation
-------------
VREManager documentation is available on-line from the Projects Documentation Wiki:
https://gcube.wiki.gcube-system.org/gcube/index.php/Resource_Broker
Licensing
---------
This software is licensed under the terms you may find in the file named "LICENSE" in this directory.

10
distro/changelog.xml Normal file
View File

@ -0,0 +1,10 @@
<ReleaseNotes
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="xsd/changelog.xsd">
<Changeset component="org.gcube.resourcemanagement.support.1-0-0" date="2010-10-15">
<Change>First release</Change>
</Changeset>
<Changeset component="org.gcube.resourcemanagement.support.1-1-0" date="2010-10-24">
<Change>For 1.2.0 of resource management</Change>
</Changeset>
</ReleaseNotes>

48
distro/descriptor.xml Normal file
View File

@ -0,0 +1,48 @@
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>servicearchive</id>
<formats>
<format>tar.gz</format>
</formats>
<baseDirectory>/</baseDirectory>
<fileSets>
<fileSet>
<directory>${distroDirectory}</directory>
<outputDirectory>/</outputDirectory>
<useDefaultExcludes>true</useDefaultExcludes>
<includes>
<include>README</include>
<include>LICENSE</include>
<include>INSTALL</include>
<include>MAINTAINERS</include>
<include>changelog.xml</include>
</includes>
<fileMode>755</fileMode>
<filtered>true</filtered>
</fileSet>
<fileSet>
<directory>target/apidocs</directory>
<outputDirectory>/${artifactId}/doc/api</outputDirectory>
<useDefaultExcludes>true</useDefaultExcludes>
<fileMode>755</fileMode>
</fileSet>
</fileSets>
<files>
<file>
<source>${distroDirectory}/profile.xml</source>
<outputDirectory>./</outputDirectory>
<filtered>true</filtered>
</file>
<file>
<source>target/${build.finalName}.jar</source>
<outputDirectory>/${artifactId}</outputDirectory>
</file>
<file>
<source>${distroDirectory}/svnpath.txt</source>
<outputDirectory>/${artifactId}</outputDirectory>
<filtered>true</filtered>
</file>
</files>
</assembly>

25
distro/profile.xml Normal file
View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<Resource xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ID></ID>
<Type>Library</Type>
<Profile>
<Description>Resource Management Portlet Common IS Operations Support Library</Description>
<Class>PortletsAdmin</Class>
<Name>${artifactId}</Name>
<Version>1.0.0</Version>
<Packages>
<Software>
<Name>${artifactId}</Name>
<Version>${version}</Version>
<MavenCoordinates>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
</MavenCoordinates>
<Files>
<File>${build.finalName}.jar</File>
</Files>
</Software>
</Packages>
</Profile>
</Resource>

1
distro/svnpath.txt Normal file
View File

@ -0,0 +1 @@
${scm.url}

4
etc/build.properties Normal file
View File

@ -0,0 +1,4 @@
name = ResourceManagerSupport
package = org.gcube.resourcemanagement.support
package.dir = org/gcube/resourcemanagement/support
lib.dir = ResourceManagementPortlet/war/WEB-INF/lib

22
etc/profile.xml Normal file
View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<Resource xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../xsd/service.xsd">
<ID/>
<Type>Library</Type>
<Profile>
<Description>ResourceManagement Support Library</Description>
<Class>portlet</Class>
<Name>ResourceManagementSupport</Name>
<Version>1.0.0</Version>
<Packages>
<Software>
<Description>ResourceManagement Support Library</Description>
<Name>ResourceManagement-support</Name>
<Version>1.0.1</Version>
<Files>
<File>org.gcube.resourcemanagement.support.jar</File>
</Files>
</Software>
</Packages>
</Profile>
</Resource>

152
pom.xml Normal file
View File

@ -0,0 +1,152 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>maven-parent</artifactId>
<groupId>org.gcube.tools</groupId>
<version>1.0.0</version>
<relativePath />
</parent>
<groupId>org.gcube.portlets.admin</groupId>
<artifactId>rmp-common-library</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>Resource Management Portlet Common IS Operations Support Library</name>
<properties>
<distroDirectory>distro</distroDirectory>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-profile</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>target</outputDirectory>
<resources>
<resource>
<directory>${distroDirectory}</directory>
<filtering>true</filtering>
<includes>
<include>profile.xml</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2</version>
<configuration>
<descriptors>
<descriptor>${distroDirectory}/descriptor.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>servicearchive</id>
<phase>install</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>gcf</artifactId>
<version>[1.4.0,1.5.0]</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gcube.applicationsupportlayer</groupId>
<artifactId>aslcore</artifactId>
<version>3.2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.gcube.resourcemanagement</groupId>
<artifactId>softwarerepository-stubs</artifactId>
<version>1.2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.gcube.resourcemanagement</groupId>
<artifactId>resource-manager-stubs</artifactId>
<version>2.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.gcube.resourcemanagement</groupId>
<artifactId>ghnmanager-stubs</artifactId>
<version>1.5.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,48 @@
/****************************************************************************
* This software is part of the gCube Project.
* Site: http://www.gcube-system.org/
****************************************************************************
* The gCube/gCore software is licensed as Free Open Source software
* conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
* The software and documentation is provided by its authors/distributors
* "as is" and no expressed or
* implied warranty is given for its use, quality or fitness for a
* particular case.
****************************************************************************
* Filename: AbstractResourceException.java
****************************************************************************
* @author <a href="mailto:daniele.strollo@isti.cnr.it">Daniele Strollo</a>
***************************************************************************/
package org.gcube.resourcemanagement.support.exceptions;
/**
* Represents the basic type of exception thrown by functionalities exposed
* in this library.
*
* The reason is to provide an access point for serialization issues
* (e.g. for GWT based portlets that require additional serialization
* annotations).
*
* @author Daniele Strollo (ISTI-CNR)
*/
public class AbstractResourceException extends Exception {
private static final long serialVersionUID = 4851998460190622583L;
public AbstractResourceException() {
super();
}
public AbstractResourceException(final String message, final Throwable cause) {
super(message, cause);
}
public AbstractResourceException(final String message) {
super(message);
}
public AbstractResourceException(final Throwable cause) {
super(cause);
}
}

View File

@ -0,0 +1,43 @@
/****************************************************************************
* This software is part of the gCube Project.
* Site: http://www.gcube-system.org/
****************************************************************************
* The gCube/gCore software is licensed as Free Open Source software
* conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
* The software and documentation is provided by its authors/distributors
* "as is" and no expressed or
* implied warranty is given for its use, quality or fitness for a
* particular case.
****************************************************************************
* Filename: ResourceAccessException.java
****************************************************************************
* @author <a href="mailto:daniele.strollo@isti.cnr.it">Daniele Strollo</a>
***************************************************************************/
package org.gcube.resourcemanagement.support.exceptions;
/**
* Thrown when is required an operation the user is not allowed to execute.
*
* @author Daniele Strollo (ISTI-CNR)
*/
public class ResourceAccessException extends AbstractResourceException {
private static final long serialVersionUID = -4491094127600507185L;
public ResourceAccessException() {
super();
}
public ResourceAccessException(final String message, final Throwable cause) {
super(message, cause);
}
public ResourceAccessException(final String message) {
super(message);
}
public ResourceAccessException(final Throwable cause) {
super(cause);
}
}

View File

@ -0,0 +1,55 @@
/****************************************************************************
* This software is part of the gCube Project.
* Site: http://www.gcube-system.org/
****************************************************************************
* The gCube/gCore software is licensed as Free Open Source software
* conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
* The software and documentation is provided by its authors/distributors
* "as is" and no expressed or
* implied warranty is given for its use, quality or fitness for a
* particular case.
****************************************************************************
* Filename: ResourceOperationException.java
****************************************************************************
* @author <a href="mailto:daniele.strollo@isti.cnr.it">Daniele Strollo</a>
***************************************************************************/
package org.gcube.resourcemanagement.support.exceptions;
/**
* If an operation of the library internally fails.
* @author Daniele Strollo (ISTI-CNR)
*/
public class ResourceOperationException extends AbstractResourceException {
private static final long serialVersionUID = -8748539948441128210L;
/**
*
*/
public ResourceOperationException() {
super();
}
/**
* @param message
*/
public ResourceOperationException(final String message) {
super(message);
}
/**
* @param cause
*/
public ResourceOperationException(final Throwable cause) {
super(cause);
}
/**
* @param message
* @param cause
*/
public ResourceOperationException(final String message, final Throwable cause) {
super(message, cause);
}
}

View File

@ -0,0 +1,42 @@
/****************************************************************************
* This software is part of the gCube Project.
* Site: http://www.gcube-system.org/
****************************************************************************
* The gCube/gCore software is licensed as Free Open Source software
* conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
* The software and documentation is provided by its authors/distributors
* "as is" and no expressed or
* implied warranty is given for its use, quality or fitness for a
* particular case.
****************************************************************************
* Filename: ResourceParameterException.java
****************************************************************************
* @author <a href="mailto:daniele.strollo@isti.cnr.it">Daniele Strollo</a>
***************************************************************************/
package org.gcube.resourcemanagement.support.exceptions;
/**
* Wrong parameters provided by the user.
* @author Daniele Strollo (ISTI-CNR)
*/
public class ResourceParameterException extends AbstractResourceException {
private static final long serialVersionUID = -1117521050663690780L;
public ResourceParameterException() {
super();
}
public ResourceParameterException(final String message, final Throwable cause) {
super(message, cause);
}
public ResourceParameterException(final String message) {
super(message);
}
public ResourceParameterException(final Throwable cause) {
super(cause);
}
}

View File

@ -0,0 +1,56 @@
/****************************************************************************
* This software is part of the gCube Project.
* Site: http://www.gcube-system.org/
****************************************************************************
* The gCube/gCore software is licensed as Free Open Source software
* conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
* The software and documentation is provided by its authors/distributors
* "as is" and no expressed or
* implied warranty is given for its use, quality or fitness for a
* particular case.
****************************************************************************
* Filename: ReportBuilder.java
****************************************************************************
* @author <a href="mailto:daniele.strollo@isti.cnr.it">Daniele Strollo</a>
***************************************************************************/
package org.gcube.resourcemanagement.support.managers.report;
import java.util.List;
import java.util.Vector;
/**
* @author Daniele Strollo (ISTI-CNR)
*
*/
public class ReportBuilder {
private List<ReportEntry> entries = null;
public ReportBuilder() {
entries = new Vector<ReportEntry>();
}
public final void addEntry(final ReportEntry entry) {
this.entries.add(entry);
}
public final int size() {
return this.entries.size();
}
public final String getXML() {
StringBuilder builder = new StringBuilder();
builder.append("<Report>\n");
for (ReportEntry entry : this.entries) {
builder.append(entry.toXML());
}
builder.append("</Report>\n");
return builder.toString();
}
@Override
public final String toString() {
return this.getXML();
}
}

View File

@ -0,0 +1,100 @@
/****************************************************************************
* This software is part of the gCube Project.
* Site: http://www.gcube-system.org/
****************************************************************************
* The gCube/gCore software is licensed as Free Open Source software
* conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
* The software and documentation is provided by its authors/distributors
* "as is" and no expressed or
* implied warranty is given for its use, quality or fitness for a
* particular case.
****************************************************************************
* Filename: ReportEntry.java
****************************************************************************
* @author <a href="mailto:daniele.strollo@isti.cnr.it">Daniele Strollo</a>
***************************************************************************/
package org.gcube.resourcemanagement.support.managers.report;
import org.gcube.resourcemanagement.support.exceptions.AbstractResourceException;
import org.gcube.resourcemanagement.support.managers.resources.AbstractResourceManager;
/**
* @author Daniele Strollo (ISTI-CNR)
*
*/
public class ReportEntry {
private ReportOperation operation = null;
private AbstractResourceManager resource = null;
private boolean success = false;
private String message = null;
public ReportEntry(
final ReportOperation operation,
final AbstractResourceManager resource
) throws AbstractResourceException {
this(operation, resource, null, false);
}
public ReportEntry(
final ReportOperation operation,
final AbstractResourceManager resource,
final boolean success
) throws AbstractResourceException {
this(operation, resource, null, success);
}
public ReportEntry(
final ReportOperation operation,
final AbstractResourceManager resource,
final String message,
final boolean success
) throws AbstractResourceException {
this.operation = operation;
this.resource = resource;
this.success = success;
this.message = message;
}
public final ReportOperation getOperation() {
return operation;
}
public final AbstractResourceManager getResource() {
return resource;
}
public final boolean isSuccess() {
return success;
}
public final void setSuccess(final boolean success) {
this.success = success;
}
public final void setMessage(final String message) {
this.message = message;
}
public final void setMessage(final Throwable ex) {
this.message = ex.getMessage();
}
@Override
public final String toString() {
return this.toXML();
}
public final String toXML() {
return
"\t<Operation type=\"" + this.getOperation().getLabel() + "\">\n" +
"\t\t<Status>" + (this.isSuccess() ? "SUCCESS" : "FAILURE") + "</Status>\n" +
"\t\t<Resource>\n" +
"\t\t\t<ID>" + this.getResource().getID() + "</ID>\n" +
// If the resource has a name
(this.getResource().getName() != null ?
"\t\t\t<Name>" + this.getResource().getName() + "</Name>\n" : "") +
"\t\t\t<Type>" + this.getResource().getType() + "</Type>\n" +
// If a message is present
(this.message != null ?
"\t\t\t<Message>\n\t\t\t\t" + this.message.trim() + "\n\t\t\t</Message>\n" : "") +
"\t\t</Resource>\n" +
"\t</Operation>\n";
}
}

View File

@ -0,0 +1,33 @@
/****************************************************************************
* This software is part of the gCube Project.
* Site: http://www.gcube-system.org/
****************************************************************************
* The gCube/gCore software is licensed as Free Open Source software
* conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
* The software and documentation is provided by its authors/distributors
* "as is" and no expressed or
* implied warranty is given for its use, quality or fitness for a
* particular case.
****************************************************************************
* Filename: ReportOperation.java
****************************************************************************
* @author <a href="mailto:daniele.strollo@isti.cnr.it">Daniele Strollo</a>
***************************************************************************/
package org.gcube.resourcemanagement.support.managers.report;
/**
* @author Daniele Strollo (ISTI-CNR)
*
*/
public enum ReportOperation {
AddToScope("Add To Scope");
private String label = null;
private ReportOperation(final String label) {
this.label = label;
}
public final String getLabel() {
return this.label;
}
}

View File

@ -0,0 +1,798 @@
/****************************************************************************
* This software is part of the gCube Project.
* Site: http://www.gcube-system.org/
****************************************************************************
* The gCube/gCore software is licensed as Free Open Source software
* conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
* The software and documentation is provided by its authors/distributors
* "as is" and no expressed or
* implied warranty is given for its use, quality or fitness for a
* particular case.
****************************************************************************
* Filename: ResourceManager.java
****************************************************************************
* @author <a href="mailto:daniele.strollo@isti.cnr.it">Daniele Strollo</a>
***************************************************************************/
package org.gcube.resourcemanagement.support.managers.resources;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Vector;
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.informationsystem.client.AtomicCondition;
import org.gcube.common.core.informationsystem.client.ISClient;
import org.gcube.common.core.informationsystem.client.XMLResult;
import org.gcube.common.core.informationsystem.client.queries.GCUBEGenericQuery;
import org.gcube.common.core.informationsystem.client.queries.GCUBERIQuery;
import org.gcube.common.core.informationsystem.publisher.ISPublisher;
import org.gcube.common.core.resources.GCUBEResource;
import org.gcube.common.core.resources.GCUBERunningInstance;
import org.gcube.common.core.scope.GCUBEScope;
import org.gcube.common.core.scope.GCUBEScope.Type;
import org.gcube.common.core.security.GCUBESecurityManagerImpl;
import org.gcube.resourcemanagement.support.exceptions.AbstractResourceException;
import org.gcube.resourcemanagement.support.exceptions.ResourceAccessException;
import org.gcube.resourcemanagement.support.exceptions.ResourceOperationException;
import org.gcube.resourcemanagement.support.exceptions.ResourceParameterException;
import org.gcube.resourcemanagement.support.managers.report.ReportBuilder;
import org.gcube.resourcemanagement.support.managers.report.ReportEntry;
import org.gcube.resourcemanagement.support.managers.report.ReportOperation;
import org.gcube.resourcemanagement.support.managers.scope.ScopeManager;
import org.gcube.resourcemanagement.support.types.AllowedResourceTypes;
import org.gcube.resourcemanagement.support.utils.Assertion;
import org.gcube.resourcemanagement.support.utils.ServerConsole;
import org.gcube.vremanagement.resourcemanager.stubs.binder.AddResourcesParameters;
import org.gcube.vremanagement.resourcemanager.stubs.binder.RemoveResourcesParameters;
import org.gcube.vremanagement.resourcemanager.stubs.binder.ResourceBinderPortType;
import org.gcube.vremanagement.resourcemanager.stubs.binder.ResourceItem;
import org.gcube.vremanagement.resourcemanager.stubs.binder.ResourceList;
import org.gcube.vremanagement.resourcemanager.stubs.binder.service.ResourceBinderServiceAddressingLocator;
import org.gcube.vremanagement.resourcemanager.stubs.reporting.ReportingPortType;
import org.gcube.vremanagement.resourcemanager.stubs.reporting.service.ReportingServiceAddressingLocator;
/**
* The minimal interface all the resource managers must implement.
* Here is implemented the greatest part of the operations exposed in the
* <a href="https://gcube.wiki.gcube-system.org/gcube/index.php/Programmatic_Administration_Interface">
* official wiki</a>.
* @author Daniele Strollo (ISTI-CNR)
*/
public abstract class AbstractResourceManager {
private String id = null;
private String name = null;
private AllowedResourceTypes type = null;
private String subType = null;
private ISClient client = null;
private GCUBESecurityManagerImpl managerSec = null;
private ISPublisher publisher = null;
private static final String LOG_PREFIX = "[AbstractResMgr]";
/**
* @deprecated discouraged use. With no ID some operations cannot be accessed. For internal use only.
*/
public AbstractResourceManager(final AllowedResourceTypes type)
throws ResourceParameterException, ResourceAccessException {
Assertion<ResourceParameterException> checker = new Assertion<ResourceParameterException>();
checker.validate(type != null, new ResourceParameterException("Invalid Parameter type"));
this.type = type;
/**
* Initially the security management is disabled.
*/
this.managerSec = new GCUBESecurityManagerImpl() {
public boolean isSecurityEnabled() {
return false;
}
};
try {
client = GHNContext.getImplementation(ISClient.class);
} catch (Exception e) {
throw new ResourceAccessException("Cannot instantiate the ISClient");
}
try {
this.publisher = GHNContext.getImplementation(ISPublisher.class);
} catch (Exception e) {
ServerConsole.error(LOG_PREFIX, e);
}
}
/**
* Instantiate the handler of a resource given its identifier and its type.
* Notice that this constructor is implicitly invoked when instantiating the
* concrete resource manager (e.g. new GHNManager(id)).
* @param id the identifier of the resource the manage
* @param type the type (GHN, RI, ...).
* @throws ResourceParameterException
* @throws ResourceAccessException
*/
public AbstractResourceManager(
final String id,
final AllowedResourceTypes type)
throws ResourceParameterException, ResourceAccessException {
this(type);
Assertion<ResourceParameterException> checker = new Assertion<ResourceParameterException>();
checker.validate(id != null && id.trim().length() > 0, new ResourceParameterException("Invalid Parameter id"));
this.id = id.trim();
}
public AbstractResourceManager(
final String id,
final String name,
final AllowedResourceTypes type)
throws ResourceParameterException, ResourceAccessException {
this(id, type);
Assertion<ResourceParameterException> checker = new Assertion<ResourceParameterException>();
checker.validate(name != null && name.trim().length() > 0, new ResourceParameterException("Invalid Parameter name"));
this.name = name;
}
public AbstractResourceManager(
final String id,
final String name,
final AllowedResourceTypes type,
final String subtype)
throws ResourceParameterException, ResourceAccessException {
this(id, name, type);
if (subtype != null) {
this.subType = subtype.trim();
}
}
/**
* The singleton ISClient instance is preferred.
* All resource managers can internally access this instance
* to make queries to the IS.
* @return
*/
protected final ISClient getISClient() {
return this.client;
}
/**
* The singleton ISPublisher instance is preferred.
* All resource managers can internally access this instance
* to interact with ISPublisher to handle resources.
* @return
*/
public final ISPublisher getISPublisher() {
return publisher;
}
public final void setSecurityManager(final GCUBESecurityManagerImpl securityManager) {
this.managerSec = securityManager;
}
/**
* The security manager is initially instantiated with empty permissions.
* The {@link AbstractResourceManager#setSecurityManager} can be used to
* change it.
* @return
*/
public final GCUBESecurityManagerImpl getSecurityManager() {
return this.managerSec;
}
/**
* All resources must be identifiable through an unique ID.
* <br/>
* <b>This field is mandatory</b>
* @return
*/
public final String getID() {
return this.id;
}
public final void setID(final String id) {
this.id = id;
}
/**
* All resources must have a valid name.
* </br>
* <b>This field is mandatory</b>
* @return
*/
public final String getName() {
return this.name;
}
/**
* All resources have a type.
* </br>
* <b>This field is mandatory</b>
* @return
*/
public final AllowedResourceTypes getType() {
return this.type;
}
/**
* Resources can have a subtype (e.g. for GHNs is the domain).
* </br>
* <b>This field is optional</b>
* @return
*/
public final String getSubType() {
return this.subType;
}
/**
* Internally used by {@link AbstractResourceManager#getResourceManager(GCUBEScope)}.
* @param scope
* @return a raw list of resource manager descriptors.
* @throws Exception
*/
private List<GCUBERunningInstance> getResourceManagerFromScope(final GCUBEScope scope)
throws Exception {
GCUBERIQuery query = this.client.getQuery(GCUBERIQuery.class);
query.addAtomicConditions(new AtomicCondition("//Profile/ServiceClass", "VREManagement"));
query.addAtomicConditions(new AtomicCondition("//Profile/ServiceName", "ResourceManager"));
List<GCUBERunningInstance> result = client.execute(query, scope);
List<GCUBERunningInstance> toReturn = new ArrayList<GCUBERunningInstance>();
for (GCUBERunningInstance ri : result) {
if (ri.getScopes().containsValue(scope)) {
toReturn.add(ri);
}
}
return toReturn;
}
/**
* The the list of resource managers able to handle the resource in a given scope.
* @param scope the scope in which to operate
* @return all the available managers
* @throws ResourceAccessException if no manager can be instantiated
* @throws ResourceParameterException if the parameters are invalid
*/
public final List<ResourceBinderPortType> getResourceManagers(final GCUBEScope scope) throws ResourceAccessException, ResourceParameterException {
Assertion<ResourceParameterException> checker = new Assertion<ResourceParameterException>();
checker.validate(scope != null, new ResourceParameterException("Invalid scope"));
List<GCUBERunningInstance> resourceManagerList = null;
try {
resourceManagerList = this.getResourceManagerFromScope(scope);
} catch (Exception e) {
e.printStackTrace();
throw new ResourceAccessException("Cannot retrieve the managers for resource: " + this.getID());
}
List<ResourceBinderPortType> retval = new Vector<ResourceBinderPortType>();
if (resourceManagerList.isEmpty()) {
throw new ResourceAccessException("Unable to find ResourceManagers for resource " + this.getType() + " in scope: " + scope.toString());
}
EndpointReferenceType endpoint = null;
ResourceBinderPortType pt = null;
for (GCUBERunningInstance resourceManager : resourceManagerList) {
try {
endpoint = resourceManager.getAccessPoint().getEndpoint("gcube/vremanagement/resourcemanager/binder");
pt = GCUBERemotePortTypeContext.getProxy(
new ResourceBinderServiceAddressingLocator()
.getResourceBinderPortTypePort(endpoint),
scope,
this.managerSec);
if (pt != null) {
retval.add(pt);
}
} catch (Throwable e) {
// trying on next entry
ServerConsole.error(LOG_PREFIX, e);
}
}
if (retval != null && retval.size() > 0) {
// Return a random manager from the available ones
return retval;
}
// no managers found
throw new ResourceAccessException("Unable to find ResourceManagers for resource " + this.getID() + "in scope: " + scope.toString());
}
/**
* The the list of resource report managers able to handle the resource in a given scope.
* @param scope the scope in which to operate
* @return all the available managers
* @throws ResourceAccessException if no manager can be instantiated
* @throws ResourceParameterException if the parameters are invalid
*/
public final List<ReportingPortType> getResourceReportManagers(final GCUBEScope scope) throws ResourceAccessException, ResourceParameterException {
Assertion<ResourceParameterException> checker = new Assertion<ResourceParameterException>();
checker.validate(scope != null, new ResourceParameterException("Invalid scope"));
List<GCUBERunningInstance> resourceManagerList = null;
try {
resourceManagerList = this.getResourceManagerFromScope(scope);
} catch (Exception e) {
e.printStackTrace();
throw new ResourceAccessException("Cannot retrieve the managers for resource: " + this.getID());
}
List<ReportingPortType> retval = new Vector<ReportingPortType>();
if (resourceManagerList.isEmpty()) {
throw new ResourceAccessException("Unable to find ResourceManagers for resource " + this.getType() + " in scope: " + scope.toString());
}
EndpointReferenceType endpoint = null;
ReportingPortType pt = null;
for (GCUBERunningInstance resourceManager : resourceManagerList) {
try {
endpoint = resourceManager.getAccessPoint().getEndpoint("gcube/vremanagement/resourcemanager/reporting");
pt = GCUBERemotePortTypeContext.getProxy(
new ReportingServiceAddressingLocator().getReportingPortTypePort(endpoint),
scope,
this.managerSec);
if (pt != null) {
retval.add(pt);
}
} catch (Throwable e) {
// trying on next entry
ServerConsole.error(LOG_PREFIX, e);
}
}
if (retval != null && retval.size() > 0) {
// Return a random report manager from the available ones
return retval;
}
// no managers found
throw new ResourceAccessException("Unable to find ReportResourceManagers for resource " + this.getID() + "in scope: " + scope.toString());
}
/**
* The resource manager needed to handle the resource in a given scope.
* @param scope the scope in which to operate
* @return a random chosen manager from the avaiable ones
* @throws ResourceAccessException if no manager can be instantiated
* @throws ResourceParameterException if the parameters are invalid
*/
public final ResourceBinderPortType getResourceManager(final GCUBEScope scope)
throws AbstractResourceException {
ServerConsole.info(LOG_PREFIX, "Getting Resource Manager in scope [" + scope.toString() + "]");
List<ResourceBinderPortType> retval = this.getResourceManagers(scope);
if (retval != null && retval.size() > 0) {
Random generator = new Random();
// Return a random manager from the available ones
ResourceBinderPortType manager = retval.get(generator.nextInt(retval.size()));
return manager;
}
// no managers found
throw new ResourceAccessException("Unable to find ResourceManagers for resource " + this.getType() + " in scope: " + scope.toString());
}
/**
* The report resource manager needed to handle the resource in a given scope.
* @param scope the scope in which to operate
* @return a random chosen manager from the avaiable ones
* @throws ResourceAccessException if no manager can be instantiated
* @throws ResourceParameterException if the parameters are invalid
*/
public final ReportingPortType getReportResourceManager(final GCUBEScope scope)
throws AbstractResourceException {
ServerConsole.info(LOG_PREFIX, "Getting Resource Manager in scope [" + scope.toString() + "]");
List<ReportingPortType> retval = this.getResourceReportManagers(scope);
if (retval != null && retval.size() > 0) {
Random generator = new Random();
// Return a random manager from the available ones
ReportingPortType manager = retval.get(generator.nextInt(retval.size()));
return manager;
}
// no managers found
throw new ResourceAccessException("Unable to find ResourceManagers for resource " + this.getType() + " in scope: " + scope.toString());
}
/**
* Once the conditions for binding a resource to a target scope are satisfied
* (see the
* <a href="https://gcube.wiki.gcube-system.org/gcube/index.php/Programmatic_Administration_Interface">
* official wiki</a>
* for scope binding rules) this method is internally called.
* @param targetScope
* @return
* @throws ResourceOperationException
*/
private String bindToScope(final GCUBEScope targetScope) throws AbstractResourceException {
Assertion<AbstractResourceException> checker = new Assertion<AbstractResourceException>();
checker.validate(targetScope != null, new ResourceParameterException("Invalid parameter targetScope. null not allowed."));
checker.validate(this.getID() != null, new ResourceOperationException("Invalid resource ID. null not allowed."));
ServerConsole.trace(
LOG_PREFIX,
"[BIND-SCOPE-ENTER] Adding " + this.getType() + " " + this.getID() + " to scope [" +
targetScope.toString() + "]");
AddResourcesParameters addParam = new AddResourcesParameters();
ResourceItem toAdd = new ResourceItem();
toAdd.setID(this.getID());
toAdd.setType(this.getType().name());
ResourceList r = new ResourceList();
r.setResource(new ResourceItem[]{toAdd});
addParam.setResources(r);
addParam.setTargetScope(targetScope.toString());
ResourceBinderPortType manager = this.getResourceManager(targetScope);
try {
String reportID = manager.addResources(addParam);
ServerConsole.trace(
LOG_PREFIX,
"[BIND-SCOPE-EXIT] Applyed Adding " + this.getType() + " " + this.getID() + " to scope [" +
targetScope.toString() + "]... reportID: " + reportID);
ReportingPortType pt = this.getReportResourceManager(targetScope);
return pt.getReport(reportID);
} catch (Exception e) {
e.printStackTrace();
ServerConsole.trace(
LOG_PREFIX,
"[BIND-SCOPE-EXIT] [FAILURE]");
throw new ResourceOperationException("During resource::addToScope: " + e.getMessage());
}
}
/**
* Add a scope to a gHN and the related Service Map is already available on the gHN.
* @param nestingPublication true for resources different from gHN and RI.
* @return the reportID generated
*/
public final String addToExistingScope(
final GCUBEScope sourceScope, final GCUBEScope targetScope)
throws AbstractResourceException {
Assertion<AbstractResourceException> checker = new Assertion<AbstractResourceException>();
checker.validate(sourceScope != null, new ResourceParameterException("Invalid parameter sourceScope. null not allowed."));
checker.validate(targetScope != null, new ResourceParameterException("Invalid parameter targetScope. null not allowed."));
checker.validate(this.getID() != null, new ResourceOperationException("Invalid resource ID. null not allowed."));
ReportBuilder report = new ReportBuilder();
ServerConsole.trace(
LOG_PREFIX,
"[ADD-ToExistingScope] Adding from scope [" +
sourceScope.toString() +
"] to existing scope [" +
targetScope.toString() +
"] " + this.getType() + " " + this.getID());
// If not RI or GHN and the scopes are sibling and VO copyFromToVO
if (!(this.getType() == AllowedResourceTypes.GHN) &&
!(this.getType() == AllowedResourceTypes.RunningInstance) &&
sourceScope.getType() == Type.VO && targetScope.getType() == Type.VO) {
return copyFromToVO(sourceScope, targetScope);
}
// Add a gCube Resource to
// (i) a VRE scope from the parent VO or
// (ii) a VO scope from the infrastructure scope
if (!targetScope.isEnclosedIn(sourceScope)) {
throw new ResourceOperationException(
"You are not allowed to apply to this scope. Target scope is not enclosed in the source one.");
}
report.addEntry(new ReportEntry(ReportOperation.AddToScope, this,
"Added " + this.getType() + " " + this.getID() + " to parent scope " +
targetScope.toString() + " the remote report ID is: " +
this.bindToScope(targetScope), true));
return report.getXML();
}
/**
* Similar to the {@link AbstractResourceManager#addToExistingScope} method but involves
* two scopes of type VO.
* Notice that this operation in reserved for resources different from gHN and RI.
* See
* <a href="https://gcube.wiki.gcube-system.org/gcube/index.php/Programmatic_Administration_Interface#Add_a_gCube_Resource_from_a_VO_to_another_VO">
* here</a> for further details.
* @param sourceScope
* @param targetScope
* @return
* @throws AbstractResourceException
*/
public final String copyFromToVO(final GCUBEScope sourceScope, final GCUBEScope targetScope)
throws AbstractResourceException {
Assertion<AbstractResourceException> checker = new Assertion<AbstractResourceException>();
checker.validate(
sourceScope != null && sourceScope.getType() == Type.VO,
new ResourceParameterException("The sourceScope is invalid or not of type VO."));
checker.validate(
targetScope != null && targetScope.getType() == Type.VO,
new ResourceParameterException("The targetScope is invalid or not of type VO."));
checker.validate(
sourceScope.getEnclosingScope() == targetScope.getEnclosingScope(),
new ResourceParameterException("The sourceScope and targetScope must be children of the same root VO."));
checker.validate(this.getType() != AllowedResourceTypes.GHN && this.getType() != AllowedResourceTypes.RunningInstance,
new ResourceAccessException("Operation not allowed for RI and GHNs."));
checker.validate(this.getID() != null,
new ResourceAccessException("Operation not allowed on resources with no ID."));
// Phase 1. retrieve the resource to copy
GCUBEResource resStub = this.getGCUBEResource(sourceScope);
// Phase 2. Before to register the resource, the scope must be
// bound to the local GCUBEResource
this.bindToScope(targetScope);
// Phase 3. Register to the new VO through the ISPublisher
try {
return this.getISPublisher().registerGCUBEResource(resStub, targetScope, this.getSecurityManager());
} catch (Exception e) {
throw new ResourceAccessException(e.getMessage());
}
}
/**
* Given the XML profile representation of a gcube resource, its GCUBEResource is built.
* Since it depends on the type of the resource, each concrete implementation of resource
* managers must implement it.
* @param xmlRepresentation
* @return
* @throws AbstractResourceException
*/
protected abstract GCUBEResource buildGCUBEResource(final String xmlRepresentation) throws AbstractResourceException;
/**
* From a resource retrieves its GCUBEResource depending on the scope in which it is
* published.
* @param scope
* @return
* @throws AbstractResourceException
*/
public final GCUBEResource getGCUBEResource(final GCUBEScope scope) throws AbstractResourceException {
return this.buildGCUBEResource(this.getXMLDescription(scope));
}
/**
* Returns the XML profile of a resource (given its ID that is encapsulated inside the resource
* manager).
* @param scope
* @return
* @throws AbstractResourceException
*/
protected final String getXMLDescription(final GCUBEScope scope) throws AbstractResourceException {
Assertion<AbstractResourceException> checker = new Assertion<AbstractResourceException>();
checker.validate(this.getID() != null, new ResourceAccessException("Cannot execute on resources with no ID."));
// Phase 1. retrieve the resource to copy
GCUBEGenericQuery query = null;
try {
query = this.getISClient().getQuery(GCUBEGenericQuery.class);
query.setExpression(
"for $resource in collection('/db/Profiles/" + this.getType().name() + "')//Resource " +
"where ( $resource/ID/string() eq '" +
this.getID() +
"') " +
"return $resource"
);
} catch (Exception e) {
throw new ResourceAccessException(e);
}
XMLResult resDescription = null;
try {
resDescription = this.getISClient().execute(query, scope).get(0);
return resDescription.toString();
} catch (Exception e) {
throw new ResourceAccessException("Cannot retrieve the IS profile for resource: " + this.getID() +
" in scope: " + scope.toString());
}
}
/**
* The first phase of remove form scope.
* This is common to all the resources (RI and GHNs).
* @param scope
* @return
* @throws AbstractResourceException
*/
private String basicRemoveFromScope(final GCUBEScope scope)
throws AbstractResourceException {
Assertion<AbstractResourceException> checker = new Assertion<AbstractResourceException>();
checker.validate(scope != null, new ResourceParameterException("Invalid parameter scope. null not allowed."));
checker.validate(this.getID() != null, new ResourceOperationException("Invalid ID. null not allowed."));
ServerConsole.trace(LOG_PREFIX, "[REMOVE-FROM-SCOPE] Removing from scope [" + scope.toString() + "] " + this.getType() + " " + this.getID());
String retval = null;
RemoveResourcesParameters params = new RemoveResourcesParameters();
ResourceItem toRemove = new ResourceItem();
toRemove.setID(this.getID());
toRemove.setType(this.getType().name());
ResourceList resourcesToRemove = new ResourceList();
resourcesToRemove.setResource(new ResourceItem[]{toRemove});
params.setResources(resourcesToRemove);
params.setTargetScope(scope.toString());
ServerConsole.trace(LOG_PREFIX, "[REMOVE-FROM-SCOPE] Sending the Remove Resource request....");
try {
ResourceBinderPortType manager = this.getResourceManager(scope);
retval = manager.removeResources(params);
} catch (Exception e) {
throw new ResourceOperationException("During removeFrom scope of "
+ this.getType()
+ " " + this.getID() + ": " + e.getMessage());
}
return retval;
}
/**
* Removes the current resource from the scope.
* @param nestingRemoval true for resources different from gHN and RI
*/
public final String removeFromScope(final GCUBEScope scope)
throws AbstractResourceException {
Assertion<AbstractResourceException> checker = new Assertion<AbstractResourceException>();
checker.validate(scope != null, new ResourceParameterException("Invalid parameter scope. null not allowed."));
checker.validate(this.getID() != null, new ResourceOperationException("Invalid ID. null not allowed."));
String retval = this.basicRemoveFromScope(scope);
// -- PHASE 2 - optional
// Builds the stub without the removed scope
GCUBEResource resStub = this.getGCUBEResource(scope);
// Refreshes the local profile
resStub.removeScope(scope);
/*
* These steps are for resources different from GHN and RI
*/
if (!this.getType().equals(AllowedResourceTypes.GHN.name()) &&
!this.getType().equals(AllowedResourceTypes.RunningInstance.name())) {
// Phase 1
// if the resource joins only the current scope, after the removal it has also to be deleted
try {
List<GCUBEScope> boundedScopes = this.validateScopes(resStub.getScopes().values().toArray(new GCUBEScope[]{}));
if (boundedScopes == null) {
ServerConsole.warn(LOG_PREFIX, "[REMOVE-FROM-SCOPE] The resource " + this.getType() + " has no bound scopes");
return retval;
}
if (boundedScopes.size() == 0) {
ServerConsole.trace(LOG_PREFIX, "[REMOVE-FROM-SCOPE] [DELETE-BRANCH] deleting resource since no more scopes remained");
this.getISPublisher().removeGCUBEResource(
this.getID(), this.getType().name(), scope, this.getSecurityManager());
return retval;
} else {
// requires the update of the resources in all other scopes
for (GCUBEScope _scope : boundedScopes) {
if (!scope.equals(_scope)) {
try {
ServerConsole.trace(LOG_PREFIX, "[REMOVE-FROM-SCOPE] [UPDATE-BRANCH] Updating profile in scope [" + _scope.toString() + "]");
this.getISPublisher().updateGCUBEResource(resStub, _scope, getSecurityManager());
} catch (Exception e) {
ServerConsole.error(LOG_PREFIX, e);
}
}
}
}
} catch (Exception e) {
throw new ResourceOperationException(e);
}
}
return retval;
}
/**
* Adds to scopes the required maps (if needed).
* Internally used to ensure that the scopes at Infrastructure or VO level
* have correctly setup the maps.
* @param scopes
* @return
*/
protected List<GCUBEScope> validateScopes(final GCUBEScope[] scopes) {
List<GCUBEScope> retval = new Vector<GCUBEScope>();
for (GCUBEScope scope : scopes) {
try {
retval.add(ScopeManager.getScope(scope.toString()));
} catch (Exception e) {
ServerConsole.error(LOG_PREFIX, e);
}
}
return retval;
}
/**
* @deprecated you must be sure before requiring this operation... take care
* @param scope where the resource is bound
* @throws AbstractResourceException
*/
public final void forceDelete(final GCUBEScope scope) throws AbstractResourceException {
ServerConsole.trace(LOG_PREFIX, "[DELETE] [DELETE-BRANCH] deleting resource since no more scopes remained");
try {
this.getISPublisher().removeGCUBEResource(
this.getID(), this.getType().name(), scope, this.getSecurityManager());
} catch (Exception e) {
ServerConsole.error(LOG_PREFIX, e);
}
}
/**
* Removes a resource.
* According to the
* <a href="https://gcube.wiki.gcube-system.org/gcube/index.php/Programmatic_Administration_Interface">
* official wiki</a> the resource is deleted only if is not bound in other scopes. Otherwise this
* operation simply corresponds to the in deep remove from scope.
* @param scope
* @throws ResourceOperationException
*/
public final void delete(final GCUBEScope scope) throws AbstractResourceException {
Assertion<AbstractResourceException> checker = new Assertion<AbstractResourceException>();
checker.validate(scope != null, new ResourceParameterException("Invalid parameter scope. null not allowed."));
checker.validate(this.getID() != null, new ResourceOperationException("Invalid ID. null not allowed."));
System.out.println("DELETING TYPE: "+ this.getType());
GCUBEResource resStub = this.getGCUBEResource(scope);
List<GCUBEScope> boundedScopes = this.validateScopes(resStub.getScopes().values().toArray(new GCUBEScope[]{}));
ServerConsole.trace(LOG_PREFIX, "[DELETE] " + this.getType() + " " + this.getID() + " in scope [" + scope + "]");
ServerConsole.trace(LOG_PREFIX, "[DELETE] " + this.getType() + " " + this.getID() + " is bound to (" + boundedScopes.size() + ") scopes");
/*
* Removefromscope accetta un boolean piu la resource gia ottenuta.
* boolean per decidere se saltare fase due di update che la ripeto piu volte
* rispetto a qui che lo faccio una volta sola.
*/
for (GCUBEScope _scope : boundedScopes) {
// Removing from the children scopes
if (_scope.isEnclosedIn(scope) || scope.equals(_scope)) {
this.basicRemoveFromScope(_scope);
resStub.removeScope(_scope);
}
}
// Phase 2
// after the removal, the resource must be also updated in the other scopes it belongs to,
// otherwise it will wrongly report there a scope where actually it is not registered anymore.
// In order to do so, the just removed scope must be removed from the local
// GCUBEResource object by invoking the method GCUBEResource.removeScope(<scope to remove>)
// and then the resource must be updated in all the remaining scopes.
if (resStub.getScopes().isEmpty()) {
ServerConsole.trace(LOG_PREFIX, "[DELETE] [DELETE-BRANCH] deleting resource since no more scopes remained");
try {
this.getISPublisher().removeGCUBEResource(
this.getID(), this.getType().name(), scope, this.getSecurityManager());
} catch (Exception e) {
}
} else {
boundedScopes = this.validateScopes(resStub.getScopes().values().toArray(new GCUBEScope[]{}));
for (GCUBEScope _scope : boundedScopes) {
try {
ServerConsole.trace(LOG_PREFIX, "[DELETE] [UPDATE-BRANCH]" + this.getType() + " " + this.getID() + " in scope [" + _scope + "]");
this.getISPublisher().updateGCUBEResource(resStub, _scope, getSecurityManager());
} catch (Exception e) {
ServerConsole.error(LOG_PREFIX, e);
}
}
}
}
}

View File

@ -0,0 +1,84 @@
/****************************************************************************
* This software is part of the gCube Project.
* Site: http://www.gcube-system.org/
****************************************************************************
* The gCube/gCore software is licensed as Free Open Source software
* conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
* The software and documentation is provided by its authors/distributors
* "as is" and no expressed or
* implied warranty is given for its use, quality or fitness for a
* particular case.
****************************************************************************
* Filename: CollectionManager.java
****************************************************************************
* @author <a href="mailto:daniele.strollo@isti.cnr.it">Daniele Strollo</a>
***************************************************************************/
package org.gcube.resourcemanagement.support.managers.resources;
import java.io.StringReader;
import org.gcube.common.core.contexts.GHNContext;
import org.gcube.common.core.resources.GCUBECollection;
import org.gcube.common.core.resources.GCUBEResource;
import org.gcube.resourcemanagement.support.exceptions.AbstractResourceException;
import org.gcube.resourcemanagement.support.exceptions.ResourceAccessException;
import org.gcube.resourcemanagement.support.exceptions.ResourceParameterException;
import org.gcube.resourcemanagement.support.types.AllowedResourceTypes;
/**
* @author Daniele Strollo (ISTI-CNR)
*
*/
public class CollectionManager extends AbstractResourceManager {
/**
* @deprecated discouraged use. With no ID some operations cannot be accessed.
*/
public CollectionManager()
throws ResourceParameterException, ResourceAccessException {
super(AllowedResourceTypes.Collection);
}
public CollectionManager(final String id)
throws ResourceParameterException, ResourceAccessException {
super(id, AllowedResourceTypes.Collection);
}
/**
* @param id
* @param name
* @throws ResourceParameterException
* @throws ResourceAccessException
*/
public CollectionManager(final String id, final String name)
throws ResourceParameterException, ResourceAccessException {
super(id, name, AllowedResourceTypes.Collection);
}
/**
* @param id
* @param name
* @param subtype
* @throws ResourceParameterException
* @throws ResourceAccessException
*/
public CollectionManager(final String id, final String name, final String subtype)
throws ResourceParameterException, ResourceAccessException {
super(id, name, AllowedResourceTypes.Collection, subtype);
}
/**
* {@inheritDoc}
*/
@Override
protected final GCUBEResource buildGCUBEResource(final String xmlRepresentation)
throws AbstractResourceException {
try {
GCUBECollection impl = GHNContext.getImplementation(GCUBECollection.class);
impl.load(new StringReader(xmlRepresentation));
return impl;
} catch (Exception e) {
throw new ResourceAccessException("Cannot load the stub for resource " + this.getType(), e);
}
}
}

View File

@ -0,0 +1,210 @@
/****************************************************************************
* This software is part of the gCube Project.
* Site: http://www.gcube-system.org/
****************************************************************************
* The gCube/gCore software is licensed as Free Open Source software
* conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
* The software and documentation is provided by its authors/distributors
* "as is" and no expressed or
* implied warranty is given for its use, quality or fitness for a
* particular case.
****************************************************************************
* Filename: GHNManager.java
****************************************************************************
* @author <a href="mailto:daniele.strollo@isti.cnr.it">Daniele Strollo</a>
***************************************************************************/
package org.gcube.resourcemanagement.support.managers.resources;
import java.io.StringReader;
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.GCUBEHostingNode;
import org.gcube.common.core.resources.GCUBEResource;
import org.gcube.common.core.scope.GCUBEScope;
import org.gcube.common.vremanagement.ghnmanager.stubs.AddScopeInputParams;
import org.gcube.common.vremanagement.ghnmanager.stubs.GHNManagerPortType;
import org.gcube.common.vremanagement.ghnmanager.stubs.ShutdownOptions;
import org.gcube.common.vremanagement.ghnmanager.stubs.service.GHNManagerServiceAddressingLocator;
import org.gcube.resourcemanagement.support.exceptions.AbstractResourceException;
import org.gcube.resourcemanagement.support.exceptions.ResourceAccessException;
import org.gcube.resourcemanagement.support.exceptions.ResourceOperationException;
import org.gcube.resourcemanagement.support.exceptions.ResourceParameterException;
import org.gcube.resourcemanagement.support.types.AllowedResourceTypes;
import org.gcube.resourcemanagement.support.utils.Assertion;
import org.gcube.resourcemanagement.support.utils.ServerConsole;
/**
* @author Daniele Strollo (ISTI-CNR)
*
*/
public class GHNManager extends AbstractResourceManager {
private static final String LOG_PREFIX = "[GHN-MGR]";
/**
* @deprecated discouraged use. With no ID some operations cannot be accessed.
*/
public GHNManager()
throws ResourceParameterException, ResourceAccessException {
super(AllowedResourceTypes.GHN);
}
/**
* @param id the identifier of wrapper resource.
* @throws ResourceParameterException
* @throws ResourceAccessException
*/
public GHNManager(final String id)
throws ResourceParameterException, ResourceAccessException {
super(id, AllowedResourceTypes.GHN);
}
/**
* @param id
* @param name
* @throws ResourceParameterException
* @throws ResourceAccessException
*/
public GHNManager(final String id, final String name)
throws ResourceParameterException, ResourceAccessException {
super(id, name, AllowedResourceTypes.GHN);
}
/**
* @param id
* @param name
* @param subtype
* @throws ResourceParameterException
* @throws ResourceAccessException
*/
public GHNManager(final String id, final String name, final String subtype)
throws ResourceParameterException, ResourceAccessException {
super(id, name, AllowedResourceTypes.GHN, subtype);
}
/**
* Add a scope to a gHN and the related Service Map that is not available on the gHN.
* <p>
* <b>Required information:</b>
* <br/>
* The <i>ID</i> of the resource must be specified and valid.</p>
* @param scope
* @param scopeMap
* @return the generated reportID
* @throws ResourceParameterException
* @throws ResourceOperationException
*/
public final String addToNewScope(
final GCUBEScope sourceScope,
final GCUBEScope targetScope,
final String scopeMap)
throws AbstractResourceException {
Assertion<AbstractResourceException> checker = new Assertion<AbstractResourceException>();
checker.validate(sourceScope != null, new ResourceParameterException("Parameter sourceScope null not allowed."));
checker.validate(targetScope != null, new ResourceParameterException("Parameter targetScope null not allowed."));
checker.validate(scopeMap != null && scopeMap.trim().length() > 0, new ResourceParameterException("Invalid scopeMap parameter."));
checker.validate(this.getID() != null, new ResourceOperationException("This operation cannot be applied to resources with no ID."));
if (!targetScope.isEnclosedIn(sourceScope)) {
throw new ResourceOperationException(
"You are not allowed to apply to this scope. Target scope is not enclosed in the source one.");
}
ServerConsole.trace(
LOG_PREFIX,
"Adding from scope " +
sourceScope.toString() +
"Adding to existing scope " +
targetScope.toString() +
" " + this.getType() + " " + this.getID());
AddScopeInputParams params = new AddScopeInputParams();
params.setScope(sourceScope.toString());
params.setMap(scopeMap.trim()); //eventually, set here the new Service Map
try {
this.getGHNManager(sourceScope).addScope(params);
} catch (Exception e) {
throw new ResourceOperationException(
"Failed to add the new scope to the gHN " + this.getID() + ": " + e.getMessage());
}
return this.addToExistingScope(sourceScope, targetScope);
}
/**
* <p>
* <b>Required information:</b>
* <br/>
* The <i>name</i> of the resource must be specified and valid. It is used to retrieve the GHN manager URL.</p>
* @param scope the scope in which the manager is bound.
* @return
* @throws AbstractResourceException
*/
public final GHNManagerPortType getGHNManager(final GCUBEScope scope)
throws AbstractResourceException {
Assertion<AbstractResourceException> checker = new Assertion<AbstractResourceException>();
checker.validate(scope != null, new ResourceParameterException("Invalid scope"));
checker.validate(this.getName() != null, new ResourceOperationException("This operation cannot be applied to resources with no name."));
EndpointReferenceType endpoint = new EndpointReferenceType();
try {
endpoint.setAddress(new Address("http://" + this.getName() + "/wsrf/services/gcube/common/vremanagement/GHNManager"));
GHNManagerServiceAddressingLocator locator = new GHNManagerServiceAddressingLocator();
GHNManagerPortType pt = locator.getGHNManagerPortTypePort(endpoint);
pt = GCUBERemotePortTypeContext.getProxy(pt, scope, this.getSecurityManager());
return pt;
} catch (Exception e) {
throw new ResourceAccessException(e.getMessage());
}
}
/**
* Implements all the three possible shutdown policies according to
* restart and clean parameters.
* <p>
* <b>Required information:</b>
* <br/>
* The <i>ID</i> of the resource must be specified and valid.</p>
* @param scope
* @param restart
* @param clean
* @throws ResourceOperationException
* @throws ResourceAccessException
* @throws ResourceParameterException
*/
public final void shutDown(final GCUBEScope scope, final boolean restart, final boolean clean)
throws AbstractResourceException {
Assertion<ResourceAccessException> checker = new Assertion<ResourceAccessException>();
checker.validate(this.getID() != null, new ResourceAccessException("This operation cannot be applied to resources with no ID."));
GHNManagerPortType ghnManager = this.getGHNManager(scope);
ServerConsole.trace(LOG_PREFIX, "Shutting down " + scope.toString() + " " + this.getType() + " " + this.getID());
ShutdownOptions options = new ShutdownOptions();
options.setRestart(restart);
options.setClean(clean);
try {
ghnManager.shutdown(options);
} catch (Exception e) {
throw new ResourceOperationException("Cannot shutdown ghn: " + this.getID());
}
}
@Override
protected final GCUBEResource buildGCUBEResource(final String xmlRepresentation)
throws AbstractResourceException {
try {
GCUBEHostingNode impl = GHNContext.getImplementation(GCUBEHostingNode.class);
impl.load(new StringReader(xmlRepresentation));
return impl;
} catch (Exception e) {
throw new ResourceAccessException("Cannot load the stub for resource " + this.getType(), e);
}
}
}

View File

@ -0,0 +1,203 @@
/****************************************************************************
* This software is part of the gCube Project.
* Site: http://www.gcube-system.org/
****************************************************************************
* The gCube/gCore software is licensed as Free Open Source software
* conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
* The software and documentation is provided by its authors/distributors
* "as is" and no expressed or
* implied warranty is given for its use, quality or fitness for a
* particular case.
****************************************************************************
* Filename: GenericResourceManager.java
****************************************************************************
* @author <a href="mailto:daniele.strollo@isti.cnr.it">Daniele Strollo</a>
***************************************************************************/
package org.gcube.resourcemanagement.support.managers.resources;
import java.io.StringReader;
import org.gcube.common.core.contexts.GHNContext;
import org.gcube.common.core.informationsystem.publisher.ISPublisher;
import org.gcube.common.core.resources.GCUBEGenericResource;
import org.gcube.common.core.resources.GCUBEResource;
import org.gcube.common.core.scope.GCUBEScope;
import org.gcube.resourcemanagement.support.exceptions.AbstractResourceException;
import org.gcube.resourcemanagement.support.exceptions.ResourceAccessException;
import org.gcube.resourcemanagement.support.exceptions.ResourceOperationException;
import org.gcube.resourcemanagement.support.exceptions.ResourceParameterException;
import org.gcube.resourcemanagement.support.managers.scope.ScopeManager;
import org.gcube.resourcemanagement.support.types.AllowedResourceTypes;
import org.gcube.resourcemanagement.support.utils.Assertion;
import org.gcube.resourcemanagement.support.utils.ServerConsole;
import org.w3c.dom.Document;
/**
* @author Daniele Strollo (ISTI-CNR)
*
*/
public class GenericResourceManager extends AbstractResourceManager {
// Used internally to require static functionalities (e.g. deploy).
private static GenericResourceManager singleton = null;
private static final String LOG_PREFIX = "[GenericResource-MGR]";
static {
if (GenericResourceManager.singleton == null) {
try {
GenericResourceManager.singleton = new GenericResourceManager("dummyservice", "dummyservice");
} catch (Exception e) {
ServerConsole.error(LOG_PREFIX, e);
}
}
}
/**
* @deprecated discouraged use. With no ID some operations cannot be accessed.
*/
public GenericResourceManager()
throws ResourceParameterException,
ResourceAccessException {
super(AllowedResourceTypes.GenericResource);
}
public GenericResourceManager(final String id)
throws ResourceParameterException,
ResourceAccessException {
super(id, AllowedResourceTypes.GenericResource);
}
/**
* @param id
* @param name
* @param type
* @throws ResourceParameterException
* @throws ResourceAccessException
*/
public GenericResourceManager(final String id, final String name)
throws ResourceParameterException,
ResourceAccessException {
super(id, name, AllowedResourceTypes.GenericResource);
}
/**
* @param id
* @param name
* @param type
* @param subtype
* @throws ResourceParameterException
* @throws ResourceAccessException
*/
public GenericResourceManager(final String id, final String name, final String subtype)
throws ResourceParameterException, ResourceAccessException {
super(id, name, AllowedResourceTypes.GenericResource, subtype);
}
/**
* Updates the resource.
* @param name (Mandatory) the name to assign to the resource
* @param description (optional) if null it will not be changed
* @param body (optional) if null it will not be changed
* @param subType (optional) if null it will not be changed
* @param scope (optional) if null it will not be changed
* @throws AbstractResourceException
*/
public final void update(
final String name,
final String description,
final String body,
final String subType,
final GCUBEScope scope)
throws AbstractResourceException {
Assertion<AbstractResourceException> checker = new Assertion<AbstractResourceException>();
checker.validate(name != null && name.trim().length() != 0, new ResourceParameterException("Invalid field name. Null or empty value not allowed"));
ServerConsole.trace(LOG_PREFIX, "[RES-UPDATE] updating resource " + this.getType() + " " + this.getID());
GCUBEGenericResource res = (GCUBEGenericResource) this.getGCUBEResource(scope);
res.setName(name.trim());
if (description != null) {
res.setDescription(description.trim());
}
if (body != null) {
res.setBody(body.trim());
}
if (subType != null) {
res.setSecondaryType(subType.trim());
}
try {
this.getISPublisher().updateGCUBEResource(res, scope, this.getSecurityManager());
/* FIXME old release
* List<GCUBEScope> boundedScopes = this.validateScopes(res.getScopes().values().toArray(new GCUBEScope[]{}));
for (GCUBEScope _scope : boundedScopes) {
ServerConsole.trace(LOG_PREFIX, "[RES-UPDATE] ISPublisher updating resource " + this.getType() + " " + this.getID() + " in scope: [" + _scope + "]");
this.getISPublisher().updateGCUBEResource(res, _scope, this.getSecurityManager());
}
*/
} catch (Exception e) {
throw new ResourceOperationException(e.getMessage());
}
}
/**
* Creates a Generic Resource and returns the ID given by the
* resource manager at creation phase.
* @return the id assigned to the newly created resource
*/
public static final synchronized String create(
final String ID,
final GCUBEScope scope,
final String name,
final String description,
final String body,
final String subType)
throws Exception {
Assertion<AbstractResourceException> checker = new Assertion<AbstractResourceException>();
checker.validate(name != null && name.trim().length() != 0, new ResourceParameterException("Invalid field name. Null or empty value not allowed"));
checker.validate(subType != null && subType.trim().length() != 0, new ResourceParameterException("Invalid field subType. Null or empty value not allowed"));
GCUBEGenericResource resource = GHNContext.getImplementation(GCUBEGenericResource.class);
if (ID != null) {
resource.setID(ID);
}
resource.setName(name.trim());
if (description != null) {
resource.setDescription(description.trim());
}
if (body != null) {
resource.setBody(body.trim());
}
resource.setSecondaryType(subType.trim());
resource.addScope(scope);
GenericResourceManager gm = new GenericResourceManager();
ISPublisher publisher = gm.getISPublisher();
String retval = publisher.registerGCUBEResource(resource, scope, gm.getSecurityManager());
if (retval == null || retval.length() == 0) {
throw new Exception("The GenericResource has not been created");
}
Document doc = ScopeManager.getDocumentGivenXML(retval);
String id = doc.getElementsByTagName("ID").item(0).getFirstChild().getNodeValue();
ServerConsole.info(LOG_PREFIX, "Resource Created with ID: " + id);
return id;
}
@Override
protected final GCUBEResource buildGCUBEResource(
final String xmlRepresentation) throws AbstractResourceException {
try {
GCUBEGenericResource impl = GHNContext.getImplementation(GCUBEGenericResource.class);
impl.load(new StringReader(xmlRepresentation));
return impl;
} catch (Exception e) {
throw new ResourceAccessException("Cannot load the stub for resource " + this.getType(), e);
}
}
}

View File

@ -0,0 +1,340 @@
/****************************************************************************
* This software is part of the gCube Project.
* Site: http://www.gcube-system.org/
****************************************************************************
* The gCube/gCore software is licensed as Free Open Source software
* conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
* The software and documentation is provided by its authors/distributors
* "as is" and no expressed or
* implied warranty is given for its use, quality or fitness for a
* particular case.
****************************************************************************
* Filename: ManagementUtils.java
****************************************************************************
* @author <a href="mailto:daniele.strollo@isti.cnr.it">Daniele Strollo</a>
***************************************************************************/
package org.gcube.resourcemanagement.support.managers.resources;
import java.io.File;
import java.rmi.RemoteException;
import java.util.List;
import java.util.Vector;
import org.gcube.common.core.faults.GCUBEFault;
import org.gcube.common.core.informationsystem.client.AtomicCondition;
import org.gcube.common.core.informationsystem.client.queries.GCUBEServiceQuery;
import org.gcube.common.core.resources.GCUBEResource;
import org.gcube.common.core.resources.GCUBEService;
import org.gcube.common.core.scope.GCUBEScope;
import org.gcube.common.core.scope.GCUBEScope.Type;
import org.gcube.resourcemanagement.support.exceptions.AbstractResourceException;
import org.gcube.resourcemanagement.support.exceptions.ResourceAccessException;
import org.gcube.resourcemanagement.support.exceptions.ResourceOperationException;
import org.gcube.resourcemanagement.support.exceptions.ResourceParameterException;
import org.gcube.resourcemanagement.support.managers.scope.ScopeManager;
import org.gcube.resourcemanagement.support.types.AllowedResourceTypes;
import org.gcube.resourcemanagement.support.utils.Assertion;
import org.gcube.resourcemanagement.support.utils.ServerConsole;
import org.gcube.vremanagement.resourcemanager.stubs.binder.AddResourcesParameters;
import org.gcube.vremanagement.resourcemanager.stubs.binder.PackageItem;
import org.gcube.vremanagement.resourcemanager.stubs.binder.ResourceBinderPortType;
import org.gcube.vremanagement.resourcemanager.stubs.binder.ResourceItem;
import org.gcube.vremanagement.resourcemanager.stubs.binder.ResourceList;
import org.gcube.vremanagement.resourcemanager.stubs.binder.SoftwareList;
/**
* A support class containing operations to manage multiple resources.
* Here are provided the functionalities to delete/addToScope/deploy
* groups of homogeneous resources.
* @author Daniele Strollo (ISTI-CNR)
*/
public class ManagementUtils {
private static final String LOG_PREFIX = "[MANAGEMENT-UTILS]";
/**
* Applies the add to scope to multiple resources having the same type.
* @param type
* @param resources
* @param sourceScope
* @param targetScope
* @return the generated report ID
*/
public static final synchronized String addToExistingScope(
final AllowedResourceTypes type,
final String[] resourceIDs,
final GCUBEScope sourceScope,
final GCUBEScope targetScope)
throws Exception {
ServerConsole.trace(
LOG_PREFIX,
"[ADD-ToExistingScope] Adding from scope [" +
sourceScope.toString() +
"] to existing scope [" +
targetScope.toString() +
"] resources having type " + type.name());
// 1 - If not RI or GHN and the scopes are sibling and VO copyFromToVO
if (!(type == AllowedResourceTypes.GHN) &&
!(type == AllowedResourceTypes.RunningInstance) &&
sourceScope.getType() == Type.VO && targetScope.getType() == Type.VO) {
// Phase 1. retrieve the resource to copy
//GCUBEResource resStub = this.getGCUBEResource(sourceScope);
// Phase 2. Before to register the resource, the scope must be
// bound to the local GCUBEResource
String retval = bindToScope(type, resourceIDs, targetScope);
// Phase 3. Register to the new VO through the ISPublisher
// applies a copy of old descriptors bound in other scopes.
try {
for (String id : resourceIDs) {
AbstractResourceManager res = ResourceFactory.createResourceManager(type, id);
GCUBEResource resStub = res.getGCUBEResource(sourceScope);
res.getISPublisher().registerGCUBEResource(resStub, targetScope, res.getSecurityManager());
}
} catch (Exception e) {
throw new ResourceAccessException(e.getMessage());
}
return retval;
}
// Add a gCube Resource to
// (i) a VRE scope from the parent VO or
// (ii) a VO scope from the infrastructure scope
if (!targetScope.isEnclosedIn(sourceScope)) {
throw new ResourceOperationException(
"You are not allowed to apply to this scope. Target scope is not enclosed in the source one.");
}
// 2 - Applies the normal scope binding
return bindToScope(type, resourceIDs, targetScope);
}
/**
* Applies the add to scope to multiple resources having the same type.
* @param type
* @param resources
* @param sourceScope
* @param targetScope
* @return the generated report ID
*/
public static final synchronized String removeFromExistingScope(final AllowedResourceTypes type, final String[] resourceIDs,
final GCUBEScope sourceScope, final GCUBEScope targetScope) throws Exception {
ServerConsole.trace(
LOG_PREFIX,
"[REMOVE-FromExistingScope] Removing scope [" +
sourceScope.toString() +
"] to existing scope [" +
targetScope.toString() +
"] resources having type " + type.name());
// cannot remove a Scope if its the same
if (targetScope.toString().compareTo(sourceScope.toString()) == 0) {
return "You are not allowed to remove this scope. Current and Target scope are the same.";
}
AbstractResourceManager resource = ResourceFactory.createResourceManager(type);
for (String id : resourceIDs) {
try {
resource.setID(id);
resource.delete(targetScope);
} catch (AbstractResourceException e) {
ServerConsole.error(LOG_PREFIX, e);
}
}
return "ACK";
}
/**
*
* @param type
* @param resourceIDs
* @param targetScope
* @return the ID of generated report
* @throws AbstractResourceException
*/
private static synchronized String bindToScope(
final AllowedResourceTypes type,
final String[] resourceIDs,
final GCUBEScope targetScope)
throws AbstractResourceException {
AddResourcesParameters addParam = new AddResourcesParameters();
ResourceBinderPortType manager = ResourceFactory.createResourceManager(type).getResourceManager(targetScope);
List<ResourceItem> resToBind = new Vector<ResourceItem>();
for (String id : resourceIDs) {
ResourceItem toAdd = new ResourceItem();
toAdd.setID(id);
toAdd.setType(type.name());
resToBind.add(toAdd);
}
ResourceList r = new ResourceList();
r.setResource(resToBind.toArray(new ResourceItem[]{}));
addParam.setResources(r);
addParam.setTargetScope(targetScope.toString());
try {
String reportID = manager.addResources(addParam);
ServerConsole.trace(
LOG_PREFIX,
"[BIND-SCOPE-EXIT] Applyed Adding of resources " + type.name() + " to scope [" +
targetScope.toString() + "]... reportID: " + reportID);
return reportID;
} catch (Exception e) {
ServerConsole.trace(
LOG_PREFIX,
"[BIND-SCOPE-EXIT] [FAILURE]");
throw new ResourceOperationException("During resource::addToScope: " + e.getMessage());
}
}
public static synchronized void delete(
final AllowedResourceTypes type,
final String[] resourceIDs,
final GCUBEScope scope)
throws AbstractResourceException {
AbstractResourceManager resource = ResourceFactory.createResourceManager(type);
for (String id : resourceIDs) {
try {
resource.setID(id);
resource.delete(scope);
} catch (AbstractResourceException e) {
ServerConsole.error(LOG_PREFIX, e);
}
}
}
/**
* Makes the deployment of software on a list of ghns.
* @param ghnsID
* @param servicesID
* @return the generated report ID
* @throws Exception
*/
public static final synchronized String deploy(
final GCUBEScope scope,
final String[] ghnsID,
final String[] servicesID)
throws Exception {
Assertion<Exception> checker = new Assertion<Exception>();
checker.validate(ghnsID != null && ghnsID.length != 0, new ResourceParameterException("Invalid ghnsID parameter. It cannot be null or empty."));
checker.validate(servicesID != null && servicesID.length != 0, new ResourceParameterException("Invalid servicesID parameter. It cannot be null or empty."));
checker.validate(scope != null, new Exception("Cannot retrieve the scope."));
AbstractResourceManager resource = ResourceFactory.createResourceManager(AllowedResourceTypes.Service);
ResourceBinderPortType manager = ResourceFactory.createResourceManager(AllowedResourceTypes.Service).getResourceManager(scope);
System.out.println("\n\n**** These are the service ids to deploy on SCOPE " + scope);
for (String sid : servicesID) {
System.out.println(sid);
}
System.out.println("\n\n**** These are the gHNs ids to deploy on SCOPE " + scope);
for (String ghn : ghnsID) {
System.out.println(ghn);
}
List<PackageItem> serviceProfiles = new Vector<PackageItem>();
// Retrieves the profiles of services
final GCUBEServiceQuery query = resource.getISClient().getQuery(GCUBEServiceQuery.class);
prepareServices: for (String serviceID : servicesID) {
System.out.println("\n\n**** Query the IsClient to get the profile");
query.clearConditions();
query.addAtomicConditions(new AtomicCondition("/ID", serviceID));
System.out.println("**** Query : " + query.getExpression());
List<GCUBEService> results = resource.getISClient().execute(query, scope);
System.out.println("**** results received : " + results.size());
GCUBEService ret = null;
if (results != null && results.size() > 0) {
ret = results.get(0);
} else {
continue prepareServices;
}
if (ret == null ||
ret.getServiceClass() == null ||
ret.getServiceName() == null ||
ret.getVersion() == null) {
ServerConsole.error(LOG_PREFIX, "found an invalid service profile");
continue;
}
PackageItem toAdd = new PackageItem();
toAdd.setServiceClass(ret.getServiceClass());
toAdd.setServiceName(ret.getServiceName());
toAdd.setServiceVersion(ret.getVersion());
if (ret.getPackages().size() == 1) {
toAdd.setPackageName(ret.getPackages().get(0).getName());
toAdd.setPackageVersion(ret.getPackages().get(0).getVersion());
} else {
for (org.gcube.common.core.resources.service.Package p : ret.getPackages()) {
if (p.getClass().isAssignableFrom(org.gcube.common.core.resources.service.MainPackage.class)) {
toAdd.setPackageName(p.getName());
toAdd.setPackageVersion(p.getVersion());
break;
}
}
}
serviceProfiles.add(toAdd);
}
SoftwareList serviceList = new SoftwareList();
serviceList.setSuggestedTargetGHNNames(ghnsID);
serviceList.setSoftware(serviceProfiles.toArray(new PackageItem[0]));
AddResourcesParameters addResourcesParameters = new AddResourcesParameters();
addResourcesParameters.setSoftware(serviceList);
addResourcesParameters.setTargetScope(scope.toString());
System.out.println("\n\n**** These is the ServiceList i pass to ResourceManagerPortType: ");
for (int i = 0; i < serviceList.getSoftware().length; i++) {
System.out.println(serviceList.getSoftware()[i]);
}
String id = "";
try {
id = manager.addResources(addResourcesParameters);
ServerConsole.debug(LOG_PREFIX, "Report ID = " + id);
} catch (GCUBEFault e) {
ServerConsole.error(LOG_PREFIX, "during deployment.", e);
throw e;
} catch (RemoteException e) {
ServerConsole.error(LOG_PREFIX, "during deployment.", e);
throw e;
}
System.out.println("Returning.... no exceptions");
return id;
}
public static final void main(final String[] args) {
List<String> ids = new Vector<String>();
ids.add(null);
ids.add("id2");
ids.add(null);
ScopeManager.setScopeConfigFile("test-suite" + File.separator + "scopes" + File.separator + "scopedata.xml");
try {
ManagementUtils.delete(AllowedResourceTypes.GenericResource,
new String[]{
"3f7384a0-d51c-11df-80cc-ece35605c26c",
"975419b0-d2e1-11df-b0ed-f8e6e669b8ad",
null,
"test"
},
ScopeManager.getScope("/gcube/devsec/devVRE"));
} catch (Exception e) {
ServerConsole.error(LOG_PREFIX, e);
}
}
}

View File

@ -0,0 +1,71 @@
/****************************************************************************
* This software is part of the gCube Project.
* Site: http://www.gcube-system.org/
****************************************************************************
* The gCube/gCore software is licensed as Free Open Source software
* conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
* The software and documentation is provided by its authors/distributors
* "as is" and no expressed or
* implied warranty is given for its use, quality or fitness for a
* particular case.
****************************************************************************
* Filename: ResourceFactory.java
****************************************************************************
* @author <a href="mailto:daniele.strollo@isti.cnr.it">Daniele Strollo</a>
***************************************************************************/
package org.gcube.resourcemanagement.support.managers.resources;
import org.gcube.resourcemanagement.support.exceptions.ResourceOperationException;
import org.gcube.resourcemanagement.support.types.AllowedResourceTypes;
import org.gcube.resourcemanagement.support.utils.ServerConsole;
/**
* For lazy developers here given the facilities to instantiate the proper
* resource manager of a resource given its identifier and type.
* @author Daniele Strollo (ISTI-CNR)
*/
public class ResourceFactory {
private static final String LOG_PREFIX = "[RES-MGR-FACTORY]";
public static final AbstractResourceManager createResourceManager(final AllowedResourceTypes type)
throws ResourceOperationException {
return createResourceManager(type, null);
}
/**
*
* @param type
* @param id if null the default constructor (empty params) will be used
* @return
* @throws ResourceOperationException
*/
public static final AbstractResourceManager createResourceManager(final AllowedResourceTypes type, final String id)
throws ResourceOperationException {
ServerConsole.info(LOG_PREFIX, "ResourceFactory building the " + type.name() + "Manager");
String classToLoad =
// It is supposed that the other classes are in this package
ResourceFactory.class.getPackage().getName() + "."
+ type.name() + "Manager";
try {
if (id == null) {
return (AbstractResourceManager) Class.forName(classToLoad).newInstance();
}
return (AbstractResourceManager) Class.forName(classToLoad).getConstructor(String.class).newInstance(id);
} catch (Exception e) {
throw new ResourceOperationException(e);
}
}
public static final void main(final String[] args) throws Exception {
for (AllowedResourceTypes res : AllowedResourceTypes.values()) {
try {
ServerConsole.info(LOG_PREFIX,
"Loaded " + res.name() + " with ID: " +
ResourceFactory.createResourceManager(res, "HelloID").getID());
} catch (Exception e) {
ServerConsole.error(LOG_PREFIX, "[ERR] Failed to load: " + res.name());
}
}
}
}

View File

@ -0,0 +1,215 @@
/****************************************************************************
* This software is part of the gCube Project.
* Site: http://www.gcube-system.org/
****************************************************************************
* The gCube/gCore software is licensed as Free Open Source software
* conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
* The software and documentation is provided by its authors/distributors
* "as is" and no expressed or
* implied warranty is given for its use, quality or fitness for a
* particular case.
****************************************************************************
* Filename: RunningInstanceManager.java
****************************************************************************
* @author <a href="mailto:daniele.strollo@isti.cnr.it">Daniele Strollo</a>
***************************************************************************/
package org.gcube.resourcemanagement.support.managers.resources;
import java.io.StringReader;
import java.util.List;
import java.util.Vector;
import org.gcube.common.core.contexts.GHNContext;
import org.gcube.common.core.informationsystem.client.AtomicCondition;
import org.gcube.common.core.informationsystem.client.queries.GCUBEServiceQuery;
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.resourcemanagement.support.exceptions.AbstractResourceException;
import org.gcube.resourcemanagement.support.exceptions.ResourceAccessException;
import org.gcube.resourcemanagement.support.exceptions.ResourceOperationException;
import org.gcube.resourcemanagement.support.exceptions.ResourceParameterException;
import org.gcube.resourcemanagement.support.types.AllowedResourceTypes;
import org.gcube.resourcemanagement.support.utils.Assertion;
import org.gcube.resourcemanagement.support.utils.ServerConsole;
import org.gcube.vremanagement.resourcemanager.stubs.binder.AddResourcesParameters;
import org.gcube.vremanagement.resourcemanager.stubs.binder.PackageItem;
import org.gcube.vremanagement.resourcemanager.stubs.binder.RemoveResourcesParameters;
import org.gcube.vremanagement.resourcemanager.stubs.binder.ResourceBinderPortType;
import org.gcube.vremanagement.resourcemanager.stubs.binder.ResourceItem;
import org.gcube.vremanagement.resourcemanager.stubs.binder.ResourceList;
import org.gcube.vremanagement.resourcemanager.stubs.binder.SoftwareList;
import org.gcube.vremanagement.resourcemanager.stubs.reporting.ReportingPortType;
/**
* @author Daniele Strollo (ISTI-CNR)
*
*/
public class RunningInstanceManager extends AbstractResourceManager {
// Used internally to require static functionalities (e.g. deploy).
private static final String LOG_PREFIX = "[RI-MGR]";
/**
* @deprecated discouraged use. With no ID some operations cannot be accessed.
*/
public RunningInstanceManager()
throws ResourceParameterException, ResourceAccessException {
super(AllowedResourceTypes.RunningInstance);
}
public RunningInstanceManager(final String id)
throws ResourceParameterException, ResourceAccessException {
super(id, AllowedResourceTypes.RunningInstance);
}
public RunningInstanceManager(final String id, final String name)
throws ResourceParameterException, ResourceAccessException {
super(id, name, AllowedResourceTypes.RunningInstance);
}
public RunningInstanceManager(final String id, final String name, final String subType)
throws ResourceParameterException, ResourceAccessException {
super(id, name, AllowedResourceTypes.RunningInstance, subType);
}
public final String deploy(
final GCUBEScope scope, final String[] ghnsID, final String[] servicesID)
throws ResourceParameterException, ResourceOperationException {
Assertion<ResourceParameterException> checker = new Assertion<ResourceParameterException>();
checker.validate(servicesID != null && servicesID.length != 0,
new ResourceParameterException("Invalid servicesID parameter. It cannot be null or empty."));
checker.validate(scope != null,
new ResourceParameterException("Cannot retrieve the scope."));
List<PackageItem> serviceProfiles = new Vector<PackageItem>();
try {
// Retrieves the profiles of services
final GCUBEServiceQuery query = this.getISClient().getQuery(GCUBEServiceQuery.class);
prepareServices: for (String serviceID : servicesID) {
query.addAtomicConditions(new AtomicCondition("/ID", serviceID));
List<GCUBEService> results = this.getISClient().execute(query, scope);
GCUBEService ret = null;
if (results != null && results.size() > 0) {
ret = results.get(0);
} else {
continue prepareServices;
}
if (ret == null ||
ret.getServiceClass() == null ||
ret.getServiceName() == null ||
ret.getVersion() == null) {
ServerConsole.error(LOG_PREFIX, "found an invalid service profile");
continue;
}
PackageItem toAdd = new PackageItem();
toAdd.setServiceClass(ret.getServiceClass());
toAdd.setServiceName(ret.getServiceName());
toAdd.setServiceVersion(ret.getVersion());
if (ret.getPackages().size() == 1) {
toAdd.setPackageName(ret.getPackages().get(0).getName());
toAdd.setPackageVersion(ret.getPackages().get(0).getVersion());
} else {
for (org.gcube.common.core.resources.service.Package p : ret.getPackages()) {
if (p.getClass().isAssignableFrom(org.gcube.common.core.resources.service.MainPackage.class)) {
toAdd.setPackageName(p.getName());
toAdd.setPackageVersion(p.getVersion());
break;
}
}
}
serviceProfiles.add(toAdd);
}
SoftwareList serviceList = new SoftwareList();
// The GHNs are optional, suggested gHNs to use.
if (ghnsID != null && ghnsID.length > 0) {
serviceList.setSuggestedTargetGHNNames(ghnsID);
}
serviceList.setSoftware(serviceProfiles.toArray(new PackageItem[0]));
AddResourcesParameters addResourcesParameters = new AddResourcesParameters();
addResourcesParameters.setSoftware(serviceList);
addResourcesParameters.setTargetScope(scope.toString());
String reportID = this.getResourceManager(scope).addResources(addResourcesParameters);
ServerConsole.debug(LOG_PREFIX, "Report ID = " + reportID);
return reportID;
} catch (Exception e) {
ServerConsole.error(LOG_PREFIX, "Error during deployment.", e);
throw new ResourceOperationException("Software deployment failure: " + e.getMessage());
}
}
public final String undeploy(
final GCUBEScope scope)
throws AbstractResourceException {
Assertion<AbstractResourceException> checker = new Assertion<AbstractResourceException>();
checker.validate(scope != null,
new ResourceParameterException("Cannot retrieve the scope."));
checker.validate(this.getID() != null,
new ResourceOperationException("Invalid ID."));
try {
ResourceBinderPortType rm = this.getResourceManager(scope);
//prepare the parameters
RemoveResourcesParameters params = new RemoveResourcesParameters();
ResourceItem[] resourcelist = new ResourceItem[1];
resourcelist[0] = new ResourceItem();
resourcelist[0].setID(this.getID());
resourcelist[0].setType(this.getType().name());
ResourceList r = new ResourceList();
r.setResource(resourcelist);
params.setResources(r);
params.setTargetScope(scope.toString());
//sending the request
ServerConsole.info(LOG_PREFIX, "Sending the Remove Resource request....");
String reportID = rm.removeResources(params);
ServerConsole.info(LOG_PREFIX, "Returned report ID: " + reportID);
return reportID;
} catch (Exception e) {
ServerConsole.error(LOG_PREFIX, "Error during deployment.", e);
throw new ResourceOperationException("Software deployment failure: " + e.getMessage());
}
}
public final String checkDeployStatus(final GCUBEScope scope, final String deployID)
throws AbstractResourceException {
Assertion<ResourceParameterException> checker = new Assertion<ResourceParameterException>();
checker.validate(scope != null,
new ResourceParameterException("Invalid scope passed"));
checker.validate(deployID != null && deployID.trim().length() > 0,
new ResourceParameterException("Invalid reportID passed"));
ReportingPortType vreManagerPortType = this.getReportResourceManager(scope);
try {
return vreManagerPortType.getReport(deployID);
} catch (Exception e) {
ServerConsole.error(LOG_PREFIX, e);
throw new ResourceOperationException("Cannot retrieve the report: " + deployID + " " + e.getMessage());
}
}
@Override
protected final GCUBEResource buildGCUBEResource(final String xmlRepresentation)
throws AbstractResourceException {
try {
GCUBERunningInstance impl = GHNContext.getImplementation(GCUBERunningInstance.class);
impl.load(new StringReader(xmlRepresentation));
return impl;
} catch (Exception e) {
throw new ResourceAccessException("Cannot load the stub for resource " + this.getType(), e);
}
}
}

View File

@ -0,0 +1,109 @@
/****************************************************************************
* This software is part of the gCube Project.
* Site: http://www.gcube-system.org/
****************************************************************************
* The gCube/gCore software is licensed as Free Open Source software
* conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
* The software and documentation is provided by its authors/distributors
* "as is" and no expressed or
* implied warranty is given for its use, quality or fitness for a
* particular case.
****************************************************************************
* Filename: RuntimeResourceManager.java
****************************************************************************
* @author <a href="mailto:assante@isti.cnr.it">Massimiliano Assante</a>
***************************************************************************/
package org.gcube.resourcemanagement.support.managers.resources;
import java.io.StringReader;
import org.gcube.common.core.contexts.GHNContext;
import org.gcube.common.core.resources.GCUBEResource;
import org.gcube.common.core.resources.GCUBERuntimeResource;
import org.gcube.common.core.scope.GCUBEScope;
import org.gcube.resourcemanagement.support.exceptions.AbstractResourceException;
import org.gcube.resourcemanagement.support.exceptions.ResourceAccessException;
import org.gcube.resourcemanagement.support.exceptions.ResourceOperationException;
import org.gcube.resourcemanagement.support.exceptions.ResourceParameterException;
import org.gcube.resourcemanagement.support.types.AllowedResourceTypes;
import org.gcube.resourcemanagement.support.utils.Assertion;
import org.gcube.resourcemanagement.support.utils.ServerConsole;
import org.gcube.vremanagement.resourcemanager.stubs.binder.ResourceBinderPortType;
import org.gcube.vremanagement.resourcemanager.stubs.reporting.ReportingPortType;
/**
* @author Massimiliano Assante (ISTI-CNR)
*
*/
public class RuntimeResourceManager extends AbstractResourceManager {
// Used internally to RuntimeResourceManager static functionalities (e.g. deploy).
private static RuntimeResourceManager singleton = null;
private static final String LOG_PREFIX = "[RR-MGR]";
static {
if (RuntimeResourceManager.singleton == null) {
try {
RuntimeResourceManager.singleton = new RuntimeResourceManager("dummyservice", "dummyservice");
} catch (Exception e) {
ServerConsole.error(LOG_PREFIX, e);
}
}
}
/**
* @deprecated discouraged use. With no ID some operations cannot be accessed.
*/
public RuntimeResourceManager()
throws ResourceParameterException, ResourceAccessException {
super(AllowedResourceTypes.RuntimeResource);
}
public RuntimeResourceManager(final String id)
throws ResourceParameterException, ResourceAccessException {
super(id, AllowedResourceTypes.RuntimeResource);
}
public RuntimeResourceManager(final String id, final String name)
throws ResourceParameterException, ResourceAccessException {
super(id, name, AllowedResourceTypes.RuntimeResource);
}
public RuntimeResourceManager(final String id, final String name, final String subType)
throws ResourceParameterException, ResourceAccessException {
super(id, name, AllowedResourceTypes.RuntimeResource, subType);
}
public final String checkDeployStatus(final GCUBEScope scope, final String deployID)
throws AbstractResourceException {
Assertion<ResourceParameterException> checker = new Assertion<ResourceParameterException>();
checker.validate(scope != null,
new ResourceParameterException("Invalid scope passed"));
checker.validate(deployID != null && deployID.trim().length() > 0,
new ResourceParameterException("Invalid reportID passed"));
ReportingPortType vreManagerPortType = this.getReportResourceManager(scope);
try {
return vreManagerPortType.getReport(deployID);
} catch (Exception e) {
ServerConsole.error(LOG_PREFIX, e);
throw new ResourceOperationException("Cannot retrieve the report: " + deployID + " " + e.getMessage());
}
}
@Override
protected final GCUBEResource buildGCUBEResource(final String xmlRepresentation)
throws AbstractResourceException {
try {
GCUBERuntimeResource impl = GHNContext.getImplementation(GCUBERuntimeResource.class);
impl.load(new StringReader(xmlRepresentation));
return impl;
} catch (Exception e) {
throw new ResourceAccessException("Cannot load the stub for resource " + this.getType(), e);
}
}
}

View File

@ -0,0 +1,83 @@
/****************************************************************************
* This software is part of the gCube Project.
* Site: http://www.gcube-system.org/
****************************************************************************
* The gCube/gCore software is licensed as Free Open Source software
* conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
* The software and documentation is provided by its authors/distributors
* "as is" and no expressed or
* implied warranty is given for its use, quality or fitness for a
* particular case.
****************************************************************************
* Filename: ServiceManager.java
****************************************************************************
* @author <a href="mailto:daniele.strollo@isti.cnr.it">Daniele Strollo</a>
***************************************************************************/
package org.gcube.resourcemanagement.support.managers.resources;
import java.io.StringReader;
import org.gcube.common.core.contexts.GHNContext;
import org.gcube.common.core.resources.GCUBEResource;
import org.gcube.common.core.resources.GCUBEService;
import org.gcube.resourcemanagement.support.exceptions.AbstractResourceException;
import org.gcube.resourcemanagement.support.exceptions.ResourceAccessException;
import org.gcube.resourcemanagement.support.exceptions.ResourceParameterException;
import org.gcube.resourcemanagement.support.types.AllowedResourceTypes;
/**
* @author Daniele Strollo (ISTI-CNR)
*
*/
public class ServiceManager extends AbstractResourceManager {
/**
* @deprecated discouraged use. With no ID some operations cannot be accessed.
*/
public ServiceManager() throws ResourceParameterException, ResourceAccessException {
super(AllowedResourceTypes.Service);
}
public ServiceManager(final String id)
throws ResourceParameterException, ResourceAccessException {
super(id, AllowedResourceTypes.Service);
}
/**
* @param id
* @param name
* @param type
* @throws ResourceParameterException
* @throws ResourceAccessException
*/
public ServiceManager(final String id, final String name)
throws ResourceParameterException, ResourceAccessException {
super(id, name, AllowedResourceTypes.Service);
}
/**
* @param id
* @param name
* @param type
* @param subtype
* @throws ResourceParameterException
* @throws ResourceAccessException
*/
public ServiceManager(final String id, final String name, final String subtype)
throws ResourceParameterException,
ResourceAccessException {
super(id, name, AllowedResourceTypes.Service, subtype);
}
@Override
protected final GCUBEResource buildGCUBEResource(final String xmlRepresentation)
throws AbstractResourceException {
try {
GCUBEService impl = GHNContext.getImplementation(GCUBEService.class);
impl.load(new StringReader(xmlRepresentation));
return impl;
} catch (Exception e) {
throw new ResourceAccessException("Cannot load the stub for resource " + this.getType(), e);
}
}
}

View File

@ -0,0 +1,86 @@
/****************************************************************************
* This software is part of the gCube System.
* Site: http://www.gcube-system.org/
****************************************************************************
* The gCube/gCore software is licensed as Free Open Source software
* conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
* The software and documentation is provided by its authors/distributors
* "as is" and no expressed or
* implied warranty is given for its use, quality or fitness for a
* particular case.
****************************************************************************
* Filename: MetadataCollectionManager.java
****************************************************************************
* @author <a href="mailto:daniele.strollo@isti.cnr.it">Daniele Strollo</a>
***************************************************************************/
package org.gcube.resourcemanagement.support.managers.resources;
import java.io.StringReader;
import org.gcube.common.core.contexts.GHNContext;
import org.gcube.common.core.resources.GCUBEMCollection;
import org.gcube.common.core.resources.GCUBEResource;
import org.gcube.resourcemanagement.support.exceptions.AbstractResourceException;
import org.gcube.resourcemanagement.support.exceptions.ResourceAccessException;
import org.gcube.resourcemanagement.support.exceptions.ResourceParameterException;
import org.gcube.resourcemanagement.support.types.AllowedResourceTypes;
/**
* @author Daniele Strollo (ISTI-CNR)
*
*/
public class ViewManager extends AbstractResourceManager {
/**
* @deprecated discouraged use. With no ID some operations cannot be accessed.
*/
public ViewManager()
throws ResourceParameterException,
ResourceAccessException {
super(AllowedResourceTypes.VIEW);
}
public ViewManager(final String id)
throws ResourceParameterException,
ResourceAccessException {
super(id, AllowedResourceTypes.VIEW);
}
/**
* @param id
* @param name
* @param type
* @throws ResourceParameterException
* @throws ResourceAccessException
*/
public ViewManager(final String id, final String name)
throws ResourceParameterException,
ResourceAccessException {
super(id, name, AllowedResourceTypes.VIEW);
}
/**
* @param id
* @param name
* @param type
* @param subtype
* @throws ResourceParameterException
* @throws ResourceAccessException
*/
public ViewManager(final String id, final String name, final String subtype)
throws ResourceParameterException, ResourceAccessException {
super(id, name, AllowedResourceTypes.VIEW, subtype);
}
@Override
protected final GCUBEResource buildGCUBEResource(final String xmlRepresentation)
throws AbstractResourceException {
try {
GCUBEMCollection impl = GHNContext.getImplementation(GCUBEMCollection.class);
impl.load(new StringReader(xmlRepresentation));
return impl;
} catch (Exception e) {
throw new ResourceAccessException("Cannot load the stub for resource " + this.getType(), e);
}
}
}

View File

@ -0,0 +1,236 @@
/****************************************************************************
* This software is part of the gCube Project.
* Site: http://www.gcube-system.org/
****************************************************************************
* The gCube/gCore software is licensed as Free Open Source software
* conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
* The software and documentation is provided by its authors/distributors
* "as is" and no expressed or
* implied warranty is given for its use, quality or fitness for a
* particular case.
****************************************************************************
* Filename: ScopeManager.java
****************************************************************************
* @author <a href="mailto:daniele.strollo@isti.cnr.it">Daniele Strollo</a>
***************************************************************************/
package org.gcube.resourcemanagement.support.managers.scope;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.gcube.common.core.contexts.GHNContext;
import org.gcube.common.core.informationsystem.ISException;
import org.gcube.common.core.informationsystem.client.AtomicCondition;
import org.gcube.common.core.informationsystem.client.ISClient;
import org.gcube.common.core.informationsystem.client.queries.GCUBEGenericResourceQuery;
import org.gcube.common.core.resources.GCUBEGenericResource;
import org.gcube.common.core.scope.GCUBEScope;
import org.gcube.common.core.scope.ServiceMap;
import org.gcube.common.core.scope.VO;
import org.gcube.common.core.scope.VRE;
import org.gcube.resourcemanagement.support.utils.ServerConsole;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
* Scopes and their maps are persisted in this structure.
*
* @author Massimiliano Assante (ISTI-CNR)
* @author Daniele Strollo
*
*/
public class ScopeManager {
private static final Map<String, GCUBEScope> SCOPES = new LinkedHashMap<String, GCUBEScope>();
private static String confFile = null;
private static final String LOG_PREFIX = "[SCOPE-MGR]";
public static GCUBEScope getScope(final String scope) throws Exception {
synchronized (SCOPES) {
if (getAvailableScopes().containsKey(scope)) {
return getAvailableScopes().get(scope);
}
}
ServerConsole.warn(LOG_PREFIX, "Using DEFAULT scope manager");
return GCUBEScope.getScope(scope);
}
public static void setScopeConfigFile(final String file) {
confFile = file;
}
/**
* Refreshes the list of scopes and associated maps.
*/
public static Map<String, GCUBEScope> getAvailableScopes()
throws Exception {
if (SCOPES.size() == 0) {
update();
}
return SCOPES;
}
public static void clear() {
SCOPES.clear();
}
public static void update()
throws Exception {
if (confFile == null) {
throw new NullPointerException("the scope file has not been defined");
}
String scopeXML = fileToString(confFile);
ServerConsole.info(LOG_PREFIX, "Starting retrieving scopes..");
Document scopeDocument = getDocumentGivenXML(scopeXML);
NodeList voElements = scopeDocument.getElementsByTagName("vo");
for (int i = 0; i < voElements.getLength(); i++) {
NodeList voDetails = voElements.item(i).getChildNodes();
String voString = voDetails.item(5).getFirstChild().getNodeValue();
String fileName = voDetails.item(3).getFirstChild().getNodeValue();
// String voName = voDetails.item(1).getFirstChild().getNodeValue();
GCUBEScope vo = GCUBEScope.getScope(voString);
try {
vo.setServiceMap(loadServiceMap((VO) vo, fileName));
SCOPES.put(vo.toString(), vo);
ServerConsole.info(LOG_PREFIX, " Scopes in VO " + vo.toString());
try {
for (VRE vre : getVREFromVO((VO) vo)) {
// This operation overrides the vo map
vre.getEnclosingScope().setServiceMap(vo.getServiceMap());
SCOPES.put(vre.toString(), vre);
}
} catch (ISException e) {
ServerConsole.error(LOG_PREFIX, "Exception raised while loading VREs for VO : " + vo, e);
}
}
catch (FileNotFoundException e) {
ServerConsole.warn(LOG_PREFIX, "skipping... map not found for VO : " + vo, e);
}
}
ServerConsole.info(LOG_PREFIX, "*** found scopes : " + SCOPES.keySet());
}
protected static List<VRE> getVREFromVO(final VO vo)
throws Exception {
ServerConsole.info(LOG_PREFIX, "*******************\n\n********************\nStarting Retrieving VREs for VO : " + vo);
List<VRE> toReturn = new ArrayList<VRE>();
ISClient client = GHNContext.getImplementation(ISClient.class);
// FIXME query to get VREs
try {
GCUBEGenericResourceQuery query = client.getQuery(GCUBEGenericResourceQuery.class);
query.addAtomicConditions(new AtomicCondition("/Profile/SecondaryType", "VRE"));
//query.addGenericCondition("not($result/Scopes/Scope/string() eq '" + vo.toString() + "')");
System.out.println( "************** \n\n\n *****************\nready to query : " + query.getExpression());
for (GCUBEGenericResource resource : client.execute(query, vo)) {
ServerConsole.info(LOG_PREFIX, "Found: " + resource.getName());
for (String vreName : resource.getScopes().keySet()) {
GCUBEScope vre = resource.getScopes().get(vreName);
if (vre.getType().equals(GCUBEScope.Type.VRE)) {
toReturn.add((VRE) vre);
}
}
}
} catch (Exception e) {
ServerConsole.error(LOG_PREFIX, e);
}
return toReturn;
}
/**
* Given a scope, if it is a VO or an infrastructure, the corresponding
* map file is retrieved from the globus location and the contained xml
* representation is returned.
* @param searchvo
* @return
* @throws Exception
*/
public static String getMapXML(final GCUBEScope searchvo) throws Exception {
if (confFile == null) {
throw new NullPointerException("the scope file has not been defined");
}
String scopeXML = fileToString(confFile);
ServerConsole.info(LOG_PREFIX, "Starting retrieving scopes..");
Document scopeDocument = getDocumentGivenXML(scopeXML);
NodeList voElements = scopeDocument.getElementsByTagName("vo");
for (int i = 0; i < voElements.getLength(); i++) {
NodeList voDetails = voElements.item(i).getChildNodes();
String voString = voDetails.item(5).getFirstChild().getNodeValue();
String fileName = voDetails.item(3).getFirstChild().getNodeValue();
// String voName = voDetails.item(1).getFirstChild().getNodeValue();
GCUBEScope vo = GCUBEScope.getScope(voString);
if (vo.equals(searchvo)) {
return fileToString(System.getenv("GLOBUS_LOCATION") + File.separator + "config" + File.separator + fileName);
}
}
ServerConsole.error(LOG_PREFIX, "*** maps for " + searchvo + " not found");
return null;
}
private static ServiceMap loadServiceMap(final VO vo, final String fileName) throws Exception {
ServiceMap map = new ServiceMap();
String filePath = System.getenv("GLOBUS_LOCATION") + File.separator + "config" + File.separator + fileName;
ServerConsole.info(LOG_PREFIX, "--- Loading " + vo.getName() + " from: " + filePath);
map.load(new FileReader(filePath));
return map;
}
public static String fileToString(final String path) throws IOException {
BufferedReader filebuf = null;
String nextStr = null;
StringBuilder ret = new StringBuilder();
filebuf = new BufferedReader(new FileReader(path));
nextStr = filebuf.readLine(); // legge una riga dal file
while (nextStr != null) {
ret.append(nextStr);
nextStr = filebuf.readLine(); // legge la prossima riga
}
filebuf.close(); // chiude il file
return ret.toString();
}
public static Document getDocumentGivenXML(final String result) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
DocumentBuilder db;
Document document = null;
try {
db = dbf.newDocumentBuilder();
document = db.parse(new ByteArrayInputStream(result.getBytes()));
} catch (ParserConfigurationException e1) {
e1.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return document;
}
}

View File

@ -0,0 +1,187 @@
/****************************************************************************
* This software is part of the gCube Project.
* Site: http://www.gcube-system.org/
****************************************************************************
* The gCube/gCore software is licensed as Free Open Source software
* conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
* The software and documentation is provided by its authors/distributors
* "as is" and no expressed or
* implied warranty is given for its use, quality or fitness for a
* particular case.
****************************************************************************
* Filename: SWRepositoryManager.java
****************************************************************************
* @author <a href="mailto:daniele.strollo@isti.cnr.it">Daniele Strollo</a>
***************************************************************************/
package org.gcube.resourcemanagement.support.managers.services;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Vector;
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.informationsystem.client.AtomicCondition;
import org.gcube.common.core.informationsystem.client.ISClient;
import org.gcube.common.core.informationsystem.client.queries.GCUBERIQuery;
import org.gcube.common.core.resources.GCUBERunningInstance;
import org.gcube.common.core.scope.GCUBEScope;
import org.gcube.common.core.security.GCUBESecurityManagerImpl;
import org.gcube.resourcemanagement.support.exceptions.ResourceAccessException;
import org.gcube.resourcemanagement.support.exceptions.ResourceParameterException;
import org.gcube.resourcemanagement.support.utils.Assertion;
import org.gcube.resourcemanagement.support.utils.ServerConsole;
import org.gcube.vremanagement.softwarerepository.stubs.SoftwareRepositoryPortType;
import org.gcube.vremanagement.softwarerepository.stubs.service.SoftwareRepositoryServiceAddressingLocator;
/**
* A support library to handle and retrieve remote software
* repository manager.
* <br/>
* Useful to handle the proxy for repository manager.
* <br/>Usage:
* <pre>
* <b>SWRepositoryManager</b> repMgr = new SWRepositoryManager();
* // if needed sets the time out for socket connection
* // -- repMgr.setCallTimeOut(240000);
* SoftwareRepositoryPortType swrep = repMgr.getSoftwareRepository(scope);
* // Applies a deploy
* swrep.<i>store(...)</i>;
* </pre>
* @author Daniele Strollo (ISTI-CNR)
*/
public class SWRepositoryManager {
private GCUBESecurityManagerImpl managerSec = null;
private ISClient client = null;
private int callTimeOut = 240000;
private static final String LOG_PREFIX = "[SW-UPDT-MGR]";
public SWRepositoryManager() throws ResourceAccessException {
/**
* Initially the security management is disabled.
*/
this.managerSec = new GCUBESecurityManagerImpl() {
public boolean isSecurityEnabled() {
return false;
}
};
try {
client = GHNContext.getImplementation(ISClient.class);
} catch (Exception e) {
throw new ResourceAccessException("Cannot instantiate the ISClient");
}
}
/**
* Internally used by {@link AbstractResourceManager#getResourceManager(GCUBEScope)}.
* @param scope
* @return a raw list of resource manager descriptors.
* @throws Exception
*/
private List<GCUBERunningInstance> getSoftwareRepositoryFromScope(final GCUBEScope scope)
throws Exception {
GCUBERIQuery query = this.client.getQuery(GCUBERIQuery.class);
query.addAtomicConditions(new AtomicCondition("//Profile/ServiceClass", "VREManagement"));
query.addAtomicConditions(new AtomicCondition("//Profile/ServiceName", "SoftwareRepository"));
List<GCUBERunningInstance> result = client.execute(query, scope);
List<GCUBERunningInstance> toReturn = new ArrayList<GCUBERunningInstance>();
for (GCUBERunningInstance ri : result) {
if (ri.getScopes().containsValue(scope)) {
toReturn.add(ri);
}
}
return toReturn;
}
/**
* The the list of resource managers able to handle the resource in a given scope.
* @param scope the scope in which to operate
* @return all the available managers
* @throws ResourceAccessException if no manager can be instantiated
* @throws ResourceParameterException if the parameters are invalid
*/
private List<SoftwareRepositoryPortType> getSoftwareRepositories(final GCUBEScope scope)
throws ResourceAccessException, ResourceParameterException {
Assertion<ResourceParameterException> checker = new Assertion<ResourceParameterException>();
checker.validate(scope != null, new ResourceParameterException("Invalid scope"));
List<GCUBERunningInstance> resourceManagerList = null;
try {
resourceManagerList = this.getSoftwareRepositoryFromScope(scope);
} catch (Exception e) {
throw new ResourceAccessException("Cannot retrieve the software repository in scope: " + scope.toString());
}
List<SoftwareRepositoryPortType> retval = new Vector<SoftwareRepositoryPortType>();
if (resourceManagerList.isEmpty()) {
throw new ResourceAccessException("Unable to find the software repository in scope: " + scope.toString());
}
EndpointReferenceType endpoint = null;
SoftwareRepositoryPortType pt = null;
for (GCUBERunningInstance resourceManager : resourceManagerList) {
try {
endpoint = resourceManager.getAccessPoint().getEndpoint("gcube/vremanagement/softwarerepository/SoftwareRepository");
pt = GCUBERemotePortTypeContext.getProxy(
new SoftwareRepositoryServiceAddressingLocator()
.getSoftwareRepositoryPortTypePort(endpoint),
scope,
this.callTimeOut,
this.managerSec);
if (pt != null) {
retval.add(pt);
}
} catch (Throwable e) {
// trying on next entry
ServerConsole.error(LOG_PREFIX, e);
}
}
if (retval != null && retval.size() > 0) {
// Return a random manager from the available ones
return retval;
}
// no managers found
throw new ResourceAccessException("Unable to find the software repository in scope: " + scope.toString());
}
/**
* Retrieves form the current scope the list of registered software managers
* and returns one of them (random choice).
* @param scope the scope in which search the sw repository
* @return one randomly chosen repository manager (if many available).
* @throws ResourceAccessException if no sw manager found
* @throws ResourceParameterException if wrong parameters passed
*/
public final SoftwareRepositoryPortType getSoftwareRepository(final GCUBEScope scope)
throws ResourceAccessException, ResourceParameterException {
List<SoftwareRepositoryPortType> retval = this.getSoftwareRepositories(scope);
if (retval != null && retval.size() > 0) {
Random generator = new Random();
// Return a random software repository manager from the available ones
return retval.get(generator.nextInt(retval.size()));
}
// no managers found
throw new ResourceAccessException("Unable to find SoftwareRepository in scope: " + scope.toString());
}
/**
* Used to set the time out for socket connection with the software repository
* to update.
* @param callTimeOut
*/
public final void setCallTimeOut(final int callTimeOut) {
this.callTimeOut = callTimeOut;
}
public final int getCallTimeOut() {
return callTimeOut;
}
}

View File

@ -0,0 +1,571 @@
/****************************************************************************
* This software is part of the gCube Project.
* Site: http://www.gcube-system.org/
****************************************************************************
* The gCube/gCore software is licensed as Free Open Source software
* conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
* The software and documentation is provided by its authors/distributors
* "as is" and no expressed or
* implied warranty is given for its use, quality or fitness for a
* particular case.
****************************************************************************
* Filename: SWRepositoryUpgrader.java
****************************************************************************
* @author <a href="mailto:daniele.strollo@isti.cnr.it">Daniele Strollo</a>
***************************************************************************/
package org.gcube.resourcemanagement.support.managers.services;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.gcube.common.core.types.StringArray;
import org.gcube.resourcemanagement.support.exceptions.ResourceAccessException;
import org.gcube.resourcemanagement.support.exceptions.ResourceParameterException;
import org.gcube.resourcemanagement.support.managers.scope.ScopeManager;
import org.gcube.resourcemanagement.support.utils.Assertion;
import org.gcube.resourcemanagement.support.utils.ServerConsole;
import org.gcube.vremanagement.softwarerepository.stubs.ListServicePackagesMessage;
import org.gcube.vremanagement.softwarerepository.stubs.SoftwareRepositoryPortType;
import org.gcube.vremanagement.softwarerepository.stubs.Store;
import org.gcube.vremanagement.softwarerepository.stubs.StoreItem;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
class UpgradeStatus implements Serializable {
private static final long serialVersionUID = 2884364307629521252L;
private double currentPackage = 0;
private double totalPackages = 0;
/**
* @deprecated for serialization only
*/
public UpgradeStatus() {
super();
}
public UpgradeStatus(final double currentPackage, final double totalPackages) {
this.currentPackage = currentPackage;
this.totalPackages = totalPackages;
}
public double getCurrentPackage() {
return this.currentPackage;
}
public double getTotalPackages() {
return this.totalPackages;
}
}
/**
* <p>
* Responsible to interact with the repository manager to require
* an update of the infrastructure.
* </p>
* <p>To facilitate the monitoring of main steps involved the
* {@link UpgradeListener} class has been introduced.
* </p>
* <p>
* <b>Notice</b> that one single update per time can be required.
* If a pending update is running the next request will be blocked
* and an exception will be raised.
* </p>
* <p>
* Three files will be created at update request (the folder in which they
* will be created depends on the parameter <i>reportsDir</i>).
* <ul>
* <li><b>swupdt-rept-&lt;timestamp&gt;.txt</b> containing
* the final report of the sw repository update procedure</li>
* <li><b>swupdt-dist-&lt;timestamp&gt;.txt</b> that stores the list of packages to update</li>
* <li><b>swreport.lock</b> represents the lock file and has a double purpose
* <br/>
* <i>i) avoids multiple requests of update</i>
* <br/>
* <i>ii) contains information about the progress status</i>.
* </li>
* </ul>
* </p>
* <b>Usage:</b>
* <pre>
* // [optional]
* // to ensure at least a repository manager is registered in the scope
* new SWRepositoryManager().getSoftwareRepository(scope);
* ServerConsole.info(LOG_PREFIX, "Software repository [FOUND]");
*
* new <b>SWRepositoryUpgrader</b>(
* // The URL containing the list of packages to update
* "http://acme.org/builds/build.txt",
* // The directory to store report files
* "temp",
* // The scope
* "/gcube/devsec")
* // requires the upgrade
* .<b>doUpgrade()</b>;
* </pre>
* @author Daniele Strollo (ISTI-CNR)
*
*/
public class SWRepositoryUpgrader implements Serializable {
private static final long serialVersionUID = 733460314188332825L;
private static final String LOG_PREFIX = "[SW-UPGR]";
private String todeployTxtURL = null;
private String reportFileName = null;
private String distributionFileName = null;
private String scope = null;
private String reportsDir = "temp";
private String lockFile = null;
private List<UpgradeListener> listeners = new Vector<UpgradeListener>();
/**
* @deprecated for internal use only. Required for serialization.
*/
private SWRepositoryUpgrader() {
super();
}
/**
* Builds a new instance of repository update manager.
*
* @param todeployTxtURL the URL to use to retrieve the list of software to update
* @param reportsDir where the reports must be stored
* @throws Exception
*/
public SWRepositoryUpgrader(
final String todeployTxtURL,
final String reportsDir,
final String scope) throws Exception {
this();
// Checks parameters
Assertion<ResourceParameterException> checker = new Assertion<ResourceParameterException>();
checker.validate(todeployTxtURL != null && todeployTxtURL.trim().length() > 0,
new ResourceParameterException("Invalid parameter todeployTxtURL"));
checker.validate(reportsDir != null && reportsDir.trim().length() > 0,
new ResourceParameterException("Invalid parameter reportsDir"));
checker.validate(scope != null && scope.trim().length() > 0 && ScopeManager.getScope(scope) != null,
new ResourceParameterException("Invalid parameter scope"));
this.reportsDir = reportsDir;
// Create the folder if it does not exist
boolean success = (new File(this.reportsDir)).mkdirs();
if (success) {
ServerConsole.trace(LOG_PREFIX, "Directory " + this.reportsDir + " [CREATED]");
}
this.todeployTxtURL = todeployTxtURL;
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyMMdd-hhmm");
Date date = new Date();
String curDate = dateFormatter.format(date);
this.reportFileName = this.reportsDir + File.separator + "swupdt-rept-" + curDate + ".txt";
this.distributionFileName = this.reportsDir + File.separator + "swupdt-dist-" + curDate + ".txt";
this.lockFile = this.reportsDir + File.separator + "swreport.lock";
ServerConsole.trace(LOG_PREFIX, "Setting report file: " + this.reportFileName);
ServerConsole.trace(LOG_PREFIX, "Setting distribution file: " + this.distributionFileName);
this.scope = scope;
}
public final String getScope() {
return this.scope;
}
/**
* Upgrades a software repository.
* @param softwareRepositoryURL the url to contact the service repository
* @param todeployTxtURL the txt file containing software descriptions
* @param reportFileName the filename where report must be stored
* @param distributionFileName the filename where distribution must be stored
* @param scope
*/
public final void doUpgrade() throws Exception {
try {
this.reserve();
} catch (Exception e) {
this.release();
throw new Exception("The required report file already exists. It cannot be replaced.");
}
File reportFile = new File(reportFileName);
if (reportFile.exists()) {
this.release();
throw new Exception("The required report file already exists. It cannot be replaced.");
}
File distributionFile = new File(distributionFileName);
if (distributionFile.exists()) {
this.release();
throw new Exception("The required distribution file already exists. It cannot be replaced.");
}
SoftwareRepositoryPortType swManager = null;
try {
swManager = new SWRepositoryManager().getSoftwareRepository(ScopeManager.getScope(this.scope));
} catch (Exception e) {
this.release();
throw new Exception("The swManager cannot be instantiated. " + e.getMessage());
}
ArrayList<String> listIds = new ArrayList<String>();
List<ListServicePackagesMessage> listServicePackagesMessages = new ArrayList<ListServicePackagesMessage>();
List<StoreItem> storeItemsList = null;
try {
storeItemsList = parseTxtFile(this.todeployTxtURL);
} catch (Exception e) {
this.release();
throw new Exception("The passed update URL is not valid. " + e.getMessage());
}
try {
stringToFile("<Services>\n", reportFile);
} catch (IOException e) {
ServerConsole.error(LOG_PREFIX, "doUpgrade", e);
}
for (int i = 0; i < storeItemsList.size(); i++) {
StoreItem[] storeItems = new StoreItem[1];
storeItems[0] = storeItemsList.get(i);
Store store = new Store();
store.setStoreMessage(storeItems);
try {
String ret = swManager.store(store);
ret = ret.replace("<Services>\n", "");
ret = ret.replace("</Services>\n", "");
listIds.add(parseXMLStoreSoftwareArchive(ret, listServicePackagesMessages));
stringToFile(ret, reportFile);
// -- PROGRESS UPDATE PROCEDURE
// updates the status so that the client can retrieve
// the current update progress (0..1).
UpgradeStatus status = new UpgradeStatus(i + 1, storeItemsList.size());
this.storeStatus(status);
double progress = 1.0d * (status.getCurrentPackage() / status.getTotalPackages());
// Informs the listeners of update progress
for (UpgradeListener listener : this.listeners) {
listener.onProgress(progress);
}
ServerConsole.trace(LOG_PREFIX, "*** UPDATED [" + (i + 1) + "/" + storeItemsList.size() + "]");
// -- ENDOF PROGRESS UPDATE PROCEDURE
} catch (Exception e) {
ServerConsole.error(LOG_PREFIX, e);
}
}
try {
stringToFile("</Services>\n", reportFile);
} catch (IOException e) {
e.printStackTrace();
}
StringBuilder sb = new StringBuilder();
sb.append("<Packages>\n");
for (int k = 0; k < listIds.size(); k++) {
try {
StringArray stringArray = swManager.listUniquesServicePackages(listServicePackagesMessages.get(k));
if (stringArray != null) {
String[] aux = stringArray.getItems();
for (int i = 0; i < aux.length; i++) {
sb.append(aux[i]);
}
} else {
ServerConsole.info(LOG_PREFIX, "listServicePackages return null");
continue;
}
} catch (Exception e) {
ServerConsole.error(LOG_PREFIX, "Error getting package list Service with Class=" +
listServicePackagesMessages.get(k).getServiceClass() +
" Name=" + listServicePackagesMessages.get(k).getServiceName() +
" Version=" + listServicePackagesMessages.get(k).getServiceVersion() + "\n\n");
// Informs the listeners of update exception
for (UpgradeListener listener : this.listeners) {
listener.onError(
// provides the package raising update failure
"Error getting package list Service with Class=" +
listServicePackagesMessages.get(k).getServiceClass() +
" Name=" + listServicePackagesMessages.get(k).getServiceName() +
" Version=" + listServicePackagesMessages.get(k).getServiceVersion(),
e);
}
// Continues to next package
continue;
}
}
sb.append("</Packages>");
try {
stringToFile(sb.toString(), distributionFile);
} catch (IOException e) {
ServerConsole.info(LOG_PREFIX, "Unable to write on output file");
ServerConsole.error(LOG_PREFIX, e);
}
this.release();
ServerConsole.debug(LOG_PREFIX, "Infrastructure Update [DONE]");
// Informs the release has been done
for (UpgradeListener listener : this.listeners) {
listener.onFinish(this.todeployTxtURL, this.reportFileName, this.distributionFileName);
}
}
/**
* Each time a single update can be required.
* For a such reason the repository upgrader must be reserved and
* released at the end of the process.
*/
private void reserve() throws Exception {
if (isReserved()) {
throw new ResourceAccessException("The Infrastracture update cannot be executed. It is already locked by another process. Try later.");
}
// Informs the release has been done
for (UpgradeListener listener : this.listeners) {
listener.onReserve();
}
stringToFile("Reserved by: " + this.reportFileName, new File(this.lockFile));
this.storeStatus(new UpgradeStatus(0, 1));
}
private void storeStatus(final UpgradeStatus status) {
ServerConsole.info(LOG_PREFIX, "Storing status");
try {
File file = new File(this.lockFile);
ObjectOutputStream mine = new ObjectOutputStream(new FileOutputStream(file));
mine.reset();
mine.writeObject(status);
mine.flush();
mine.close();
} catch (Exception e) {
ServerConsole.error(LOG_PREFIX, e);
}
}
/**
* Gives the completed upgrade percentage (0...1).
* @return
*/
public final double getProgressStatus() {
ServerConsole.info(LOG_PREFIX, "Getting status");
if (!this.isReserved()) {
return 1.0;
}
try {
File file = new File(this.lockFile);
ObjectInputStream mine = new ObjectInputStream(new FileInputStream(file));
UpgradeStatus status = (UpgradeStatus) mine.readObject();
mine.close();
double retval = 1.0d * (status.getCurrentPackage() / status.getTotalPackages());
ServerConsole.info(LOG_PREFIX, "Current Status: " + retval);
return retval;
} catch (Exception e) {
return 1;
}
}
/**
* Checks if the software update is locked by another process.
* @return
*/
private boolean isReserved() {
return new File(this.lockFile).exists();
}
/**
* Releases the reservation (removes the lock file).
*/
private void release() {
// Informs the release has been done
for (UpgradeListener listener : this.listeners) {
listener.onRelease();
}
ServerConsole.debug(LOG_PREFIX, "Releasing lock for " + this.reportFileName);
new File(this.lockFile).delete();
}
private List<StoreItem> parseTxtFile(final String txtURL) throws Exception {
List<StoreItem> ret = new ArrayList<StoreItem>();
ServerConsole.info(LOG_PREFIX, "Opening URL connection to " + txtURL);
final URL url = new URL(txtURL);
final HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
ServerConsole.info(LOG_PREFIX, "Getting data from given URL " + txtURL);
InputStream is = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
ServerConsole.info(LOG_PREFIX, "Connection return code: " + httpURLConnection.getResponseCode());
ServerConsole.info(LOG_PREFIX, "Bytes available: " + is.available() + "\n\n\n");
String nextStr = bufferedReader.readLine(); // read a row
while (nextStr != null) {
String row = nextStr.toLowerCase();
if (!row.contains("-servicearchive-")) {
nextStr = bufferedReader.readLine(); // read the next row before continuing
continue;
}
String serviceClass = "Class";
ServerConsole.info(LOG_PREFIX, "Service Class = " + serviceClass);
String serviceName = "Name";
ServerConsole.info(LOG_PREFIX, "Service Name = " + serviceName);
//String version = nextStr.split("-servicearchive-")[1];
String version = "1.0.0";
// version = version.split("-")[0];
ServerConsole.info(LOG_PREFIX, "Version = " + version);
String softwareArchiveURL = nextStr;
ServerConsole.info(LOG_PREFIX, "URL = " + nextStr);
String description = "ETICS Client programmed Description";
ServerConsole.info(LOG_PREFIX, "Description = " + description + "\n\n\n");
String[] scopes = new String[] {scope};
StoreItem storeItem = new StoreItem();
storeItem.setServiceClass(serviceClass);
storeItem.setServiceName(serviceName);
storeItem.setServiceVersion(version);
storeItem.setURL(softwareArchiveURL);
storeItem.setDescription(description);
storeItem.setScopes(scopes);
ret.add(storeItem);
nextStr = bufferedReader.readLine(); // read the next row
}
bufferedReader.close();
httpURLConnection.disconnect();
return ret;
}
/**
*
* @param xml
* @return
*/
private String parseXMLStoreSoftwareArchive(
final String xml,
final List<ListServicePackagesMessage> listServicePackagesMessages) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
Document document = null;
try {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xml.getBytes());
document = db.parse(byteArrayInputStream);
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Element root = document.getDocumentElement();
NodeList nl = root.getChildNodes();
String id = "";
ListServicePackagesMessage listServicePackagesMessage = new ListServicePackagesMessage();
for (int i = 0; i < nl.getLength(); i++) {
if (!(nl.item(i) instanceof Element)) {
continue;
}
Element el = (Element) nl.item(i);
if (el == null) {
continue;
} else if (el.getTagName().compareTo("ServiceClass") == 0) {
listServicePackagesMessage.setServiceClass(el.getTextContent());
} else if (el.getTagName().compareTo("ID") == 0) {
id = el.getTextContent();
} else if (el.getTagName().compareTo("ServiceName") == 0) {
listServicePackagesMessage.setServiceName(el.getTextContent());
} else if (el.getTagName().compareTo("ServiceVersion") == 0) {
listServicePackagesMessage.setServiceVersion(el.getTextContent());
}
}
listServicePackagesMessages.add(listServicePackagesMessage);
ServerConsole.info(LOG_PREFIX, "required store for SA ID: " + id);
return id;
}
/**
* @param str string
* @param targetFile Target File
* @throws IOException if fails
*/
private void stringToFile(
final String str,
final File targetFile) throws IOException {
try {
FileWriter fw = new FileWriter(targetFile, true);
fw.append(str);
fw.flush();
fw.close();
} catch (IOException e) {
throw e;
}
}
public final void addListener(final UpgradeListener listener) {
if (listener == null) {
return;
}
listener.setUpgrader(this);
this.listeners.add(listener);
}
}

View File

@ -0,0 +1,102 @@
/****************************************************************************
* This software is part of the gCube Project.
* Site: http://www.gcube-system.org/
****************************************************************************
* The gCube/gCore software is licensed as Free Open Source software
* conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
* The software and documentation is provided by its authors/distributors
* "as is" and no expressed or
* implied warranty is given for its use, quality or fitness for a
* particular case.
****************************************************************************
* Filename: UpgradeListener.java
****************************************************************************
* @author <a href="mailto:daniele.strollo@isti.cnr.it">Daniele Strollo</a>
***************************************************************************/
package org.gcube.resourcemanagement.support.managers.services;
import org.gcube.resourcemanagement.support.utils.ServerConsole;
/**
* @author Daniele Strollo (ISTI-CNR)
*
*/
/**
* To an upgrade process can be attached an handler to perform
* additional operations at the happening of events.
* @author Daniele Strollo (ISTI-CNR)
*
*/
public abstract class UpgradeListener {
protected static final String LOG_PREFIX = "[SW-UPGR-LISTENER]";
private SWRepositoryUpgrader upgrader = null;
public UpgradeListener() {
this.onInit();
}
/**
* Method invoked at construction of listener.
*/
public abstract void onInit();
/**
* Automatically invoked when the listener is attached to an upgrader.
*
* @param upgrader
*/
public final void setUpgrader(final SWRepositoryUpgrader upgrader) {
this.upgrader = upgrader;
}
public final SWRepositoryUpgrader getUpgrader() {
return this.upgrader;
}
/**
* Invoked each time the update progresses.
* @param progress
*/
public abstract void onProgress(final double progress);
/**
* Invoked in case of failure during update operation.
* @param failure
*/
public final void onError(final Exception failure) {
ServerConsole.error(LOG_PREFIX, failure);
}
/**
* Invoked in case of failure during update operation.
* @param failure
*/
public final void onError(final String message, final Exception failure) {
ServerConsole.error(LOG_PREFIX, message, failure);
}
/**
* In successful update, the resulting report files will be
* provided in input.
*/
public abstract void onFinish(
final String deployURL,
final String reportFileName,
final String distributionFileName);
/**
* Invoked once the lock has been acquired.
*/
public final void onReserve() {
ServerConsole.info(LOG_PREFIX, "Software repository lock request [DONE]");
}
/**
* Once finished the lock is released and this event raised.
* The release is called both if the process successfully finish
* or an abort is required.
*/
public void onRelease() {
ServerConsole.info(LOG_PREFIX, "Software repository lock release [DONE]");
}
}

View File

@ -0,0 +1,187 @@
/****************************************************************************
* This software is part of the gCube Project.
* Site: http://www.gcube-system.org/
****************************************************************************
* The gCube/gCore software is licensed as Free Open Source software
* conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
* The software and documentation is provided by its authors/distributors
* "as is" and no expressed or
* implied warranty is given for its use, quality or fitness for a
* particular case.
****************************************************************************
* Filename: GenericTest.java
****************************************************************************
* @author <a href="mailto:daniele.strollo@isti.cnr.it">Daniele Strollo</a>
***************************************************************************/
package org.gcube.resourcemanagement.support.tests;
import java.io.File;
import java.util.Map;
import org.gcube.common.core.contexts.GHNContext.Status;
import org.gcube.common.core.resources.GCUBEHostingNode;
import org.gcube.common.core.scope.GCUBEScope;
import org.gcube.common.core.scope.GCUBEScope.Type;
import org.gcube.resourcemanagement.support.managers.resources.GHNManager;
import org.gcube.resourcemanagement.support.managers.resources.GenericResourceManager;
import org.gcube.resourcemanagement.support.managers.resources.ResourceFactory;
import org.gcube.resourcemanagement.support.managers.scope.ScopeManager;
import org.gcube.resourcemanagement.support.types.AllowedResourceTypes;
import org.gcube.resourcemanagement.support.utils.ServerConsole;
/**
* @author Daniele Strollo (ISTI-CNR)
*
*/
public class GenericTest {
private static final String LOG_PREFIX = "[SW-SUPPORT-TEST]";
public static void testScope() {
try {
Map<String, GCUBEScope> scopes = ScopeManager.getAvailableScopes();
System.out.println("\n\n\n******************** TEST SCOPE ***************\n");
for (GCUBEScope scope : scopes.values()) {
if ((scope.getType().compareTo(Type.INFRASTRUCTURE) == 0)
|| (scope.getType().compareTo(Type.VO) == 0)) {
if (scope.getServiceMap() != null) {
ServerConsole.trace(null, "*** Map loaded for " + scope.getType() + " " + scope.toString() + " [OK]");
} else {
ServerConsole.trace(null, "*** Map loaded for " + scope.getType() + " " + scope.toString() + " [ERR]");
}
} else {
try {
if (scope.getEnclosingScope().getServiceMap() != null) {
ServerConsole.trace(null, "*** Map loaded for " + scope.getType() + scope.toString() + " [OK]");
}
} catch (Exception e) {
ServerConsole.trace(null, "*** Map loaded for " + scope.getType() + scope.toString() + " [ERR]");
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("\n******************** TEST SCOPE END ***************\n");
}
}
public static String testCreation() {
System.out.println("\n\n\n******************** TEST CREATION ***************");
String resID = null;
try {
resID = GenericResourceManager.create(
null,
ScopeManager.getScope("/gcube/devsec"),
"GR Test",
"GR Test Description",
"<TestBody>Hello</TestBody>",
"XXX");
ServerConsole.trace(null, "Generic Resource Created with ID: " + resID);
} catch (Exception e) {
ServerConsole.error(LOG_PREFIX, e);
} finally {
System.out.println("\n******************** TEST CREATION END ***************\n");
}
return resID;
}
public static void testScopeCopy(final String resID, final String fromScope, final String toScope) {
System.out.println("\n\n\n******************** TEST SCOPE COPY ***************");
try {
GenericResourceManager res = new GenericResourceManager(resID);
ServerConsole.trace(null,
res.addToExistingScope(ScopeManager.getScope(fromScope), ScopeManager.getScope(toScope))
);
} catch (Exception e) {
ServerConsole.error(LOG_PREFIX, e);
} finally {
System.out.println("\n******************** TEST SCOPE COPY END ***************\n");
}
}
public static void testResourceEdit(final String resID, final GCUBEScope scope) {
System.out.println("\n\n\n******************** TEST RESEDIT COPY ***************");
try {
GenericResourceManager res = new GenericResourceManager(resID);
res.update("New Name", "updated description", null, null, scope);
} catch (Exception e) {
ServerConsole.error(LOG_PREFIX, e);
} finally {
System.out.println("\n******************** TEST RESEDIT END ***************\n");
}
}
public static void testModeGHN(final String resID, final String scope)
throws Exception {
GCUBEScope queryScope = ScopeManager.getScope(scope);
GHNManager ghnManager = new GHNManager(resID);
GCUBEHostingNode res = (GCUBEHostingNode) ghnManager.getGCUBEResource(queryScope);
res.getNodeDescription().setStatus(Status.UNREACHABLE);
ghnManager.getISPublisher().updateGCUBEResource(res, queryScope, ghnManager.getSecurityManager());
}
public static void testGHN() {
System.out.println("\n\n\n******************** TEST GHN ***************");
try {
GHNManager ghn1 = new GHNManager("20ddb210-b779-11df-96c9-a66904b26e27", "pc-strollo");
ghn1.addToExistingScope(ScopeManager.getScope("/gcube"), ScopeManager.getScope("/gcube/devsec"));
} catch (Exception e) {
ServerConsole.error(LOG_PREFIX, e);
} finally {
System.out.println("\n******************** TEST GHN END ***************\n");
}
}
public static void testRemoveFromScope(final String resID, final GCUBEScope scope) {
System.out.println("\n\n\n******************** TEST RESOURCE REMOVEFROMSCOPE ***************");
try {
GenericResourceManager res = new GenericResourceManager(resID);
res.removeFromScope(scope);
} catch (Exception e) {
ServerConsole.error(LOG_PREFIX, e);
} finally {
System.out.println("\n******************** TEST RESOURCE REMOVEFROMSCOPE END ***************\n");
}
}
public static void testDelete(final String resID, final GCUBEScope scope) {
System.out.println("\n\n\n******************** TEST RESOURCE DELETE ***************");
try {
GenericResourceManager res = new GenericResourceManager(resID);
res.delete(scope);
} catch (Exception e) {
ServerConsole.error(LOG_PREFIX, e);
} finally {
System.out.println("\n******************** TEST RESOURCE DELETE END ***************\n");
}
}
@SuppressWarnings("deprecation")
public static void main(final String[] args) throws Exception {
// The scopes must be initialized
ScopeManager.setScopeConfigFile("test-suite" + File.separator + "scopes" + File.separator + "scopedata_admin.xml");
boolean deepTest = false;
// testScope();
// testGHN();
if (deepTest) {
String resID = testCreation();
testScopeCopy(resID, "/gcube/devsec", "/gcube/devsec/devVRE");
System.out.println("\n\nWaiting for resource refresh 60secs.\n\n\n");
Thread.sleep(60000);
//testResourceEdit(resID, ScopeManager.getScope("/gcube/devsec"));
testDelete(resID, ScopeManager.getScope("/gcube/devsec/devVRE"));
// testRemoveFromScope(resID, ScopeManager.getScope("/gcube/devsec"));
} else {
testModeGHN("f5cb0640-f1a7-11df-93d0-fc409084cf46", "/gcube/devsec/devVRE");
}
}
}

View File

@ -0,0 +1,45 @@
/****************************************************************************
* This software is part of the gCube Project.
* Site: http://www.gcube-system.org/
****************************************************************************
* The gCube/gCore software is licensed as Free Open Source software
* conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
* The software and documentation is provided by its authors/distributors
* "as is" and no expressed or
* implied warranty is given for its use, quality or fitness for a
* particular case.
****************************************************************************
* Filename: ScopeTest.java
****************************************************************************
* @author <a href="mailto:daniele.strollo@isti.cnr.it">Daniele Strollo</a>
***************************************************************************/
package org.gcube.resourcemanagement.support.tests;
import java.io.File;
import org.gcube.resourcemanagement.support.managers.scope.ScopeManager;
/**
* Makes tests on ScopeManager.
* @author Daniele Strollo (ISTI-CNR)
*/
public class ScopeTest {
public static final void testLoadScopes() {
ScopeManager.setScopeConfigFile("test-suite" + File.separator + "scopes" + File.separator + "scopedata_admin.xml");
try {
ScopeManager.update();
} catch (Exception e) {
e.printStackTrace();
System.out.println("Scopes retrievial [ERR]");
return;
}
System.out.println("Scopes retrievial [DONE]");
}
public static void main(final String[] args) {
testLoadScopes();
}
}

View File

@ -0,0 +1,31 @@
/****************************************************************************
* This software is part of the gCube Project.
* Site: http://www.gcube-system.org/
****************************************************************************
* The gCube/gCore software is licensed as Free Open Source software
* conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
* The software and documentation is provided by its authors/distributors
* "as is" and no expressed or
* implied warranty is given for its use, quality or fitness for a
* particular case.
****************************************************************************
* Filename: AllowedResourceTypes.java
****************************************************************************
* @author <a href="mailto:daniele.strollo@isti.cnr.it">Daniele Strollo</a>
***************************************************************************/
package org.gcube.resourcemanagement.support.types;
/**
* @author Massimiliano Assante (ISTI-CNR)
*
*/
public enum AllowedResourceTypes {
GHN(),
RunningInstance(),
Service(),
VIEW(),
GenericResource(),
RuntimeResource(),
Collection();
}

View File

@ -0,0 +1,65 @@
/****************************************************************************
* This software is part of the gCube Project.
* Site: http://www.gcube-system.org/
****************************************************************************
* The gCube/gCore software is licensed as Free Open Source software
* conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
* The software and documentation is provided by its authors/distributors
* "as is" and no expressed or
* implied warranty is given for its use, quality or fitness for a
* particular case.
****************************************************************************
* Filename: Assertion.java
****************************************************************************
* @author <a href="mailto:daniele.strollo@isti.cnr.it">Daniele Strollo</a>
***************************************************************************/
package org.gcube.resourcemanagement.support.utils;
import java.io.Serializable;
/**
* General purpose assertion handler.
* Assertion can be generalized to check a boolean expression and
* to raise an exception in correspondence to a failure happening
* during checking.
* <pre>
* <b>Example:</b>
*
* <b>Assertion</b>&lt;<i>TheExceptionType</i>&gt; assertion = new Assertion&lt;ParamException&gt; ();
* assertion.<b>validate</b> (param != null, new <i>TheExceptionType</i>("invalid parameter null"));
*
* <b>or</b>, in a more compact form:
* <i>// The exception to throw in case of failure
* // during the evaluation of the expected condition</i>
* new <b>Assertion</b>&lt;<i>TheExceptionType</i>&gt;().<b>validate</b>(
* i>5, <i>// The expected boolean <b>condition</b></i>
* new <i>TheExceptionType</i>("Parameter must be greater than 5")); <i>//The <b>error</b> message</i>
*
* </pre>
*
* @author Daniele Strollo (ISTI-CNR)
*/
public class Assertion <T extends Throwable> implements Serializable {
private static final long serialVersionUID = -2007903339251667541L;
/**
* Makes an assertion and if the expression evaluation fails, throws an
* exception of type T.
* <pre>
* Example:
* new Assertion&lt;MyException&gt;().validate(whatExpected, new MyException("guard failed"));
* </pre>
* @param assertion the boolean expression to evaluate
* @param exc the exception to throw if the condition does not hold
* @throws T the exception extending {@link java.lang.Throwable}
*/
public final void validate(final boolean assertion, final T exc)
throws T {
// TOCHECK junit.framework.Assert.assertTrue(assertion);
if (!assertion) {
throw exc;
}
}
}

View File

@ -0,0 +1,60 @@
/****************************************************************************
* This software is part of the gCube Project.
* Site: http://www.gcube-system.org/
****************************************************************************
* The gCube/gCore software is licensed as Free Open Source software
* conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
* The software and documentation is provided by its authors/distributors
* "as is" and no expressed or
* implied warranty is given for its use, quality or fitness for a
* particular case.
****************************************************************************
* Filename: ServerConsole.java
****************************************************************************
* @author <a href="mailto:daniele.strollo@isti.cnr.it">Daniele Strollo</a>
***************************************************************************/
package org.gcube.resourcemanagement.support.utils;
import org.gcube.common.core.utils.logging.GCUBEClientLog;
import org.gcube.resourcemanagement.support.managers.resources.AbstractResourceManager;
/**
* @author Daniele Strollo (ISTI-CNR)
*
*/
public class ServerConsole {
private static final GCUBEClientLog LOGGER = new GCUBEClientLog(AbstractResourceManager.class);
private static final String LOG_PREFIX = "*** [RMP] ";
public static void error(final String prefix, final String msg) {
LOGGER.error(LOG_PREFIX + ((prefix != null) ? prefix + " " : "") + msg);
}
public static void error(final String prefix, final Throwable exc) {
LOGGER.error(LOG_PREFIX + ((prefix != null) ? prefix : ""), exc);
}
public static void error(final String prefix, final String msg, final Throwable exc) {
LOGGER.error(LOG_PREFIX + ((prefix != null) ? prefix + " " : "") + msg, exc);
}
public static void warn(final String prefix, final String msg) {
LOGGER.warn(LOG_PREFIX + ((prefix != null) ? prefix + " " : "") + msg);
}
public static void warn(final String prefix, final String msg, final Throwable exc) {
LOGGER.warn(LOG_PREFIX + ((prefix != null) ? prefix + " " : "") + msg, exc);
}
public static void info(final String prefix, final String msg) {
LOGGER.info(LOG_PREFIX + ((prefix != null) ? prefix + " " : "") + msg);
}
public static void trace(final String prefix, final String msg) {
LOGGER.trace(LOG_PREFIX + ((prefix != null) ? prefix + " " : "") + msg);
}
public static void debug(final String prefix, final String msg) {
LOGGER.debug(LOG_PREFIX + ((prefix != null) ? prefix + " " : "") + msg);
}
public static void fatal(final String prefix, final String msg) {
LOGGER.fatal(LOG_PREFIX + ((prefix != null) ? prefix + " " : "") + msg);
}
public static void fatal(final String prefix, final String msg, final Throwable exc) {
LOGGER.fatal(LOG_PREFIX + ((prefix != null) ? prefix + " " : "") + msg, exc);
}
}

View File

@ -0,0 +1,62 @@
/****************************************************************************
* This software is part of the gCube Project.
* Site: http://www.gcube-system.org/
****************************************************************************
* The gCube/gCore software is licensed as Free Open Source software
* conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
* The software and documentation is provided by its authors/distributors
* "as is" and no expressed or
* implied warranty is given for its use, quality or fitness for a
* particular case.
****************************************************************************
* Filename: PersistenceHandler.java
****************************************************************************
* @author <a href="mailto:daniele.strollo@isti.cnr.it">Daniele Strollo</a>
***************************************************************************/
package org.gcube.resourcemanagement.support.utils.persistence;
/**
* @author Daniele Strollo (ISTI-CNR)
*
*/
public interface PersistenceHandler<T> {
/**
* The code to execute once the persistent object is refreshed.
*/
void onRefresh();
/**
* Automatically invoked by the library at instantiation
* of persistent items.
* Used to make initializations before starting the persistence
* manager.
*/
void onLoad();
/**
* When the item need no more to be persisted
* the refresh loop is closed and this method is
* invoked.
*/
void onDestroy();
/**
* Requires the destroy of the persistent resource.
* The persistent file will be deleted and the onDestroy
* event will be raised.
*/
void destroy();
/**
* The implementation to retrieve data from the persistence manager.
* @return
*/
T getData();
/**
* Sets the new data to persist.
* @param data
*/
void setData(final T data);
}

View File

@ -0,0 +1,152 @@
/****************************************************************************
* This software is part of the gCube Project.
* Site: http://www.gcube-system.org/
****************************************************************************
* The gCube/gCore software is licensed as Free Open Source software
* conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
* The software and documentation is provided by its authors/distributors
* "as is" and no expressed or
* implied warranty is given for its use, quality or fitness for a
* particular case.
****************************************************************************
* Filename: PersistentItem.java
****************************************************************************
* @author <a href="mailto:daniele.strollo@isti.cnr.it">Daniele Strollo</a>
***************************************************************************/
package org.gcube.resourcemanagement.support.utils.persistence;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.Serializable;
import java.util.List;
import java.util.Vector;
import org.gcube.resourcemanagement.support.utils.ServerConsole;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.StaxDriver;
/**
* Wrapping for data that can be persisted on the filesystem.
* <pre>
* new <b>PersistentItem</b><i>&lt;DataTypeToPersist&gt;</i>(fileToStore, refreshDelay) {
* <b>public void</b> onLoad() {
* <i>//...</i>
* }
* <i>
* // The other methods to overload
* // ...
* </i>
* }
* </pre>
* @author Daniele Strollo (ISTI-CNR)
*/
public abstract class PersistentItem<T extends Serializable>
implements PersistenceHandler<T> {
protected static final String LOG_PREFIX = "[PERSISTENCE]";
private long refreshDelay = -1;
private boolean processClosed = false;
private String persistenceFileName = null;
private XStream serializer = null;
/**
* @deprecated for internal use only.
*/
public PersistentItem() {
}
public PersistentItem(final String persistenceFileName) {
this(persistenceFileName, -1);
}
public PersistentItem(final String persistenceFileName, final long refreshDelay) {
this.setRefreshDelay(refreshDelay);
this.setPersistenceFileName(persistenceFileName);
this.onLoad();
// loops to retrieve the data to persist
if (this.refreshDelay > 0) {
new Thread() {
public void run() {
while (!processClosed) {
try {
sleep(refreshDelay);
} catch (final InterruptedException e) {
ServerConsole.error(LOG_PREFIX, e);
}
// time elapsed... do refresh
onRefresh();
}
}
} .start();
}
}
public PersistentItem(final T data, final String persistenceFileName, final long refreshDelay) {
this(persistenceFileName, refreshDelay);
this.setData(data);
}
protected final void setPersistenceFileName(final String persistenceFileName) {
this.persistenceFileName = persistenceFileName;
}
protected final void setRefreshDelay(final long refreshDelay) {
this.refreshDelay = refreshDelay;
}
private synchronized XStream getSerializer() {
if (this.serializer == null) {
StaxDriver driver = new StaxDriver();
driver.setRepairingNamespace(false);
this.serializer = new XStream(driver);
this.serializer.addDefaultImplementation(Vector.class, List.class);
}
return this.serializer;
}
public final void destroy() {
this.processClosed = true;
// Thread has finished... invoking destroy
onDestroy();
}
/**
* Returns the corresponding persistent data.
* @return
*/
@SuppressWarnings("unchecked")
public final T getData() {
try {
StringBuilder xml = new StringBuilder();
BufferedReader reader = new BufferedReader(new FileReader(persistenceFileName));
String currLine = null;
while ((currLine = reader.readLine()) != null) {
xml.append(currLine);
}
return (T) this.getSerializer().fromXML(xml.toString());
} catch (Exception e) {
ServerConsole.error(LOG_PREFIX, e);
return null;
}
}
public final void setData(final T data) {
String xmlData = this.getSerializer().toXML(data);
BufferedOutputStream mine = null;
try {
mine = new BufferedOutputStream(new FileOutputStream(persistenceFileName));
mine.write(xmlData.getBytes());
//mine.flush();
mine.close();
} catch (Exception e) {
ServerConsole.error(LOG_PREFIX, e);
return;
}
}
}

View File

@ -0,0 +1,46 @@
package org.gcube.resourcemanagement.support.utils.persistence;
import java.io.File;
import org.gcube.common.core.scope.GCUBEScope;
import org.gcube.resourcemanagement.support.managers.scope.ScopeManager;
import org.gcube.resourcemanagement.support.utils.ServerConsole;
public class TestPersistence {
/**
* @param args
*/
public static void main(final String[] args) {
PersistentItem<GCUBEScope[]> persistentScopes = new PersistentItem<GCUBEScope[]>("data.xml", 10000) {
// Builds the data to persist
private void refreshData() {
try {
GCUBEScope[] toStore =
ScopeManager.getAvailableScopes().values().toArray(new GCUBEScope[]{});
this.setData(toStore);
} catch (Exception e) {
ServerConsole.error(LOG_PREFIX, e);
}
}
public void onLoad() {
ScopeManager.setScopeConfigFile("test-suite" + File.separator + "scopes" + File.separator + "scopedata_admin.xml");
this.refreshData();
}
public void onRefresh() {
this.refreshData();
GCUBEScope[] scopes = this.getData();
System.out.println(scopes);
}
public void onDestroy() {
this.setData(null);
}
};
}
}

View File

@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<infrastructures>
<!--
<infrastructure>
<name>d4science</name>
<vos>
<vo>
<name>d4science</name>
<src>ServiceMap_d4science.research-infrastructures.eu.xml</src>
<scope>/d4science.research-infrastructures.eu</scope>
</vo>
<vo>
<name>d4science/FARM</name>
<src>ServiceMap_FARM.xml</src>
<scope>/d4science.research-infrastructures.eu/FARM</scope>
</vo>
<vo>
<name>d4science/Ecosystem</name>
<src>ServiceMap_Ecosystem.xml</src>
<scope>/d4science.research-infrastructures.eu/Ecosystem</scope>
</vo>
</vos>
</infrastructure>
-->
<infrastructure>
<name>gcube</name>
<vos>
<vo>
<name>gcube</name>
<src>ServiceMap_gcube.xml</src>
<scope>/gcube</scope>
</vo>
<vo>
<name>gcube/devsec</name>
<src>ServiceMap_devsec.xml</src>
<scope>/gcube/devsec</scope>
</vo>
</vos>
</infrastructure>
<!--
<infrastructure>
<name>testing</name>
<vos>
<vo>
<name>testing</name>
<src>ServiceMap_testing.xml</src>
<scope>/testing</scope>
</vo>
<vo>
<name>testing/vo1</name>
<src>ServiceMap_vo1.xml</src>
<scope>/testing/vo1</scope>
</vo>
</vos>
</infrastructure>
-->
</infrastructures>

View File

@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<infrastructures>
<infrastructure>
<name>d4science</name>
<vos>
<vo>
<name>d4science</name>
<src>ServiceMap_d4science.research-infrastructures.eu.xml</src>
<scope>/d4science.research-infrastructures.eu</scope>
</vo>
<vo>
<name>d4science/FARM</name>
<src>ServiceMap_FARM.xml</src>
<scope>/d4science.research-infrastructures.eu/FARM</scope>
</vo>
<vo>
<name>d4science/Ecosystem</name>
<src>ServiceMap_Ecosystem.xml</src>
<scope>/d4science.research-infrastructures.eu/Ecosystem</scope>
</vo>
<vo>
<name>d4science/INSPIRE</name>
<src>ServiceMap_INSPIRE.xml</src>
<scope>/d4science.research-infrastructures.eu/INSPIRE</scope>
</vo>
<vo>
<name>d4science/Arts-Humanities</name>
<src>ServiceMap_Arts-Humanities.xml</src>
<scope>/d4science.research-infrastructures.eu/Arts-Humanities</scope>
</vo>
</vos>
</infrastructure>
<infrastructure>
<name>gcube</name>
<vos>
<vo>
<name>gcube</name>
<src>ServiceMap_gcube.xml</src>
<scope>/gcube</scope>
</vo>
<vo>
<name>gcube/devsec</name>
<src>ServiceMap_devsec.xml</src>
<scope>/gcube/devsec</scope>
</vo>
<vo>
<name>gcube/devNext</name>
<src>ServiceMap_devNext.xml</src>
<scope>/gcube/devNext</scope>
</vo>
</vos>
</infrastructure>
</infrastructures>