diff --git a/.classpath b/.classpath
index 405cedd..74e5673 100644
--- a/.classpath
+++ b/.classpath
@@ -23,5 +23,6 @@
+
diff --git a/.settings/com.google.gwt.eclipse.core.prefs b/.settings/com.google.gwt.eclipse.core.prefs
index ce23177..9898e9b 100644
--- a/.settings/com.google.gwt.eclipse.core.prefs
+++ b/.settings/com.google.gwt.eclipse.core.prefs
@@ -1,4 +1,4 @@
eclipse.preferences.version=1
entryPointModules=
-filesCopiedToWebInfLib=
+filesCopiedToWebInfLib=gwt-servlet.jar
gwtCompileSettings=PGd3dC1jb21waWxlLXNldHRpbmdzPjxsb2ctbGV2ZWw+SU5GTzwvbG9nLWxldmVsPjxvdXRwdXQtc3R5bGU+T0JGVVNDQVRFRDwvb3V0cHV0LXN0eWxlPjxleHRyYS1hcmdzPjwhW0NEQVRBW11dPjwvZXh0cmEtYXJncz48dm0tYXJncz48IVtDREFUQVstWG14NTEybV1dPjwvdm0tYXJncz48ZW50cnktcG9pbnQtbW9kdWxlPm9yZy5nY3ViZS5yZXNvdXJjZW1hbmFnZW1lbnQuc3VwcG9ydC5SZXNvdXJjZV9zdXBwb3J0PC9lbnRyeS1wb2ludC1tb2R1bGU+PC9nd3QtY29tcGlsZS1zZXR0aW5ncz4\=
diff --git a/pom.xml b/pom.xml
index 3ef0244..5bf3573 100644
--- a/pom.xml
+++ b/pom.xml
@@ -11,7 +11,7 @@
org.gcube.portlets.admin
rmp-common-library
- 2.4.0-SNAPSHOT
+ 2.4.1-SNAPSHOT
Resource Management Library
gCube Resource Management Library is a common library containing shared code for Information System Operations
diff --git a/src/main/java/org/gcube/resourcemanagement/support/server/sweeper/QueryParameter.java b/src/main/java/org/gcube/resourcemanagement/support/server/sweeper/QueryParameter.java
new file mode 100644
index 0000000..d0e8496
--- /dev/null
+++ b/src/main/java/org/gcube/resourcemanagement/support/server/sweeper/QueryParameter.java
@@ -0,0 +1,17 @@
+package org.gcube.resourcemanagement.support.server.sweeper;
+
+public class QueryParameter {
+ protected String name;
+ protected String value;
+
+ public QueryParameter(String name, String value) {
+ super();
+ this.name = name;
+ this.value = value;
+ }
+
+ @Override
+ public String toString() {
+ return "QueryParameter [name=" + name + ", value=" + value + "]";
+ }
+}
diff --git a/src/main/java/org/gcube/resourcemanagement/support/server/sweeper/Sweeper.java b/src/main/java/org/gcube/resourcemanagement/support/server/sweeper/Sweeper.java
new file mode 100644
index 0000000..c5755e8
--- /dev/null
+++ b/src/main/java/org/gcube/resourcemanagement/support/server/sweeper/Sweeper.java
@@ -0,0 +1,173 @@
+/****************************************************************************
+ * 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: Sweeper.java
+ ****************************************************************************
+ * @author Daniele Strollo
+ ***************************************************************************/
+
+package org.gcube.resourcemanagement.support.server.sweeper;
+
+import static org.gcube.resources.discovery.icclient.ICFactory.client;
+
+import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+
+import org.gcube.common.resources.gcore.HostingNode;
+import org.gcube.common.resources.gcore.utils.XPathHelper;
+import org.gcube.common.scope.api.ScopeProvider;
+import org.gcube.common.scope.impl.ScopeBean;
+import org.gcube.resourcemanagement.support.server.gcube.queries.QueryLoader;
+import org.gcube.resourcemanagement.support.server.gcube.queries.QueryLocation;
+import org.gcube.resourcemanagement.support.server.managers.resources.GHNManager;
+import org.gcube.resourcemanagement.support.server.managers.resources.RunningInstanceManager;
+import org.gcube.resources.discovery.client.api.DiscoveryClient;
+import org.gcube.resources.discovery.client.queries.impl.QueryTemplate;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.w3c.dom.Node;
+import org.xml.sax.InputSource;
+
+import com.extjs.gxt.ui.client.data.ModelData;
+
+/**
+ * @author Massimiliano Assante (ISTI-CNR)
+ *
+ */
+public class Sweeper {
+ //TODO: Make it configurable from a property file
+ public static String LIVE_GHN_MAX_MINUTES = "40";
+
+ private static final Logger _log = LoggerFactory.getLogger(Sweeper.class);
+
+
+ protected ArrayList applyQuery(ScopeBean queryScope, QueryLocation queryPath, QueryLocation returnPath, QueryParameter... params) throws Exception {
+ //set the scope
+ String currScope = ScopeProvider.instance.get();
+ ScopeProvider.instance.set(queryScope.toString());
+
+ QueryTemplate isQuery = new QueryTemplate(QueryLoader.getQuery(queryPath));
+ DiscoveryClient client = client();
+
+ if (params != null && params.length > 0) {
+ for (int i = 0; i < params.length; i++) {
+ isQuery.addParameter(params[i].name, params[i].value);
+ }
+ }
+ isQuery.addParameter("RESOURCE", QueryLoader.getQuery(returnPath));
+
+ List results = client.submit(isQuery);
+ ArrayList retval = new ArrayList();
+
+ for (String elem : results) {
+ DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ Node node = docBuilder.parse(new InputSource(new StringReader(elem))).getDocumentElement();
+ XPathHelper helper = new XPathHelper(node);
+ // Removes the resources with no ID or empty
+ try {
+ if (helper.evaluate("//ID").get(0) != null && helper.evaluate("//ID").get(0).trim().length() > 0) {
+ retval.add(elem);
+ System.out.println(elem);
+ } else {
+ _log.debug("*** Found an invalid element with no ID");
+ }
+ } catch (Exception e) {
+ _log.debug("[getResourcesByType] found a resource with empty ID");
+ }
+ }
+ ScopeProvider.instance.set(currScope);
+ return retval;
+ }
+ /**
+ * this method used to read the MAXWAIT param from the resourcemanagement.properties files, currently it reads it from the static var defined on top of this class
+ * TODO: Make it configurable again from a property file
+ * @param queryScope
+ * @return the expired ghn list
+ */
+ public ArrayList getExpiredGHNs(final ScopeBean queryScope) {
+ try {
+ return applyQuery(
+ queryScope,
+ QueryLocation.SWEEPER_EXPIRED_GHN, QueryLocation.RETURN_SWEEPER_EXPIRED_GHN,
+ new QueryParameter("MAXWAIT", LIVE_GHN_MAX_MINUTES));
+ } catch (Exception e) {
+ _log.error(e.getMessage());
+ return null;
+ }
+ }
+ public ArrayList getDeadGHNs(ScopeBean queryScope) {
+ try {
+ return applyQuery(
+ queryScope,
+ QueryLocation.SWEEPER_DEAD_GHN, QueryLocation.RETURN_SWEEPER_DEAD_GHN);
+ } catch (Exception e) {
+ _log.error(e.getMessage());
+ return null;
+ }
+ }
+
+ public ArrayList getOrphanRI(ScopeBean queryScope) {
+ try {
+ return applyQuery(
+ queryScope,
+ QueryLocation.SWEEPER_ORPHAN_RI, QueryLocation.RETURN_SWEEPER_ORPHAN_RI);
+ } catch (Exception e) {
+ _log.error(e.getMessage());
+ return null;
+ }
+ }
+
+ @SuppressWarnings("deprecation")
+ public Boolean applySweep(ScopeBean queryScope, List elems) {
+ //set the scope
+ String currScope = ScopeProvider.instance.get();
+ ScopeProvider.instance.set(queryScope.toString());
+ for (ModelData entry : elems) {
+ try {
+ System.out.println("Action->"+((Object) entry.get("Actions")).toString());
+ SweeperActions action = SweeperActions.valueOf(((Object) entry.get("Actions")).toString());
+ String resID = ((Object) entry.get("ID")).toString();
+
+ _log.info("Cleaning up " + resID + " " + action);
+
+ switch(action) {
+ case APPLY_GHN_DELETE:
+ GHNManager manager = new GHNManager(resID);
+ manager.forceDelete(queryScope);
+ break;
+ case APPLY_GHN_MOVE_TO_UNREACHABLE:
+ GHNManager ghnManager = new GHNManager(resID);
+ HostingNode res = (HostingNode) ghnManager.getResource(queryScope);
+ _log.trace("*** Setting HostingNode " + resID + " status to unreachable");
+ res.profile().description().status("unreachable");
+ HostingNode hn = ghnManager.getRegistryPublisher().update(res);
+ _log.trace("*** getRegistryPublisher returned " + hn.toString());
+ break;
+ case APPLY_RI_DELETE:
+ RunningInstanceManager riManager = new RunningInstanceManager(resID);
+ _log.trace("*** The running instance " + resID + " will be deleted");
+ riManager.forceDelete(queryScope);
+ break;
+ default:
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ return false;
+ }
+ }
+ ScopeProvider.instance.set(currScope);
+ return true;
+ }
+}
diff --git a/src/main/java/org/gcube/resourcemanagement/support/server/sweeper/SweeperActions.java b/src/main/java/org/gcube/resourcemanagement/support/server/sweeper/SweeperActions.java
new file mode 100644
index 0000000..775eafa
--- /dev/null
+++ b/src/main/java/org/gcube/resourcemanagement/support/server/sweeper/SweeperActions.java
@@ -0,0 +1,72 @@
+/****************************************************************************
+ * 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: Actions.java
+ ****************************************************************************
+ * @author Daniele Strollo
+ ***************************************************************************/
+
+package org.gcube.resourcemanagement.support.server.sweeper;
+
+import java.io.Serializable;
+
+/**
+ * @author Daniele Strollo (ISTI-CNR)
+ *
+ */
+public enum SweeperActions implements Serializable {
+ // Declarations for getting resources to cleanup
+ GET_GHN_MOVE_TO_UNREACHABLE(
+ "Expired GHNs",
+ "GHNs no more updated for a long time",
+ "Changes the status of GHNs to unreachable"),
+ GET_GHN_DELETE("Dead GHNs",
+ "GHNs having status down or unreachable",
+ "Removes the GHNs from the IS"),
+ GET_RI_DELETE("Orphan RIs",
+ "RIs related to no more available GHNs",
+ "Removes the RIs from the IS"),
+
+ // Be sure the names are reported in xq files.
+ // Declarations for applying resources cleanup
+ APPLY_GHN_MOVE_TO_UNREACHABLE("Change GHN Status"),
+ APPLY_GHN_DELETE("Remove Dead GHN"),
+ APPLY_RI_DELETE("Remove Orphan RI");
+
+ private String opDescription = null;
+ private String label = null;
+ private String tooltip = null;
+
+ private SweeperActions(final String label) {
+ this.label = label;
+ }
+
+ private SweeperActions(final String label, final String opDescription) {
+ this(label);
+ this.opDescription = opDescription;
+ }
+
+ private SweeperActions(final String label, final String tooltip, final String opDescription) {
+ this(label, opDescription);
+ this.tooltip = tooltip;
+ }
+
+ public String getTooltip() {
+ return this.tooltip;
+ }
+
+ public String getLabel() {
+ return label;
+ }
+ public String getOperationDescription() {
+ return opDescription;
+ }
+}