refactor to FWS finished

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/admin/resource-sweeper@81763 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Massimiliano Assante 2013-09-19 16:20:34 +00:00
parent 469aa964b2
commit 65c81d7fa9
12 changed files with 187 additions and 129 deletions

View File

@ -17,5 +17,11 @@
<attribute name="org.eclipse.jst.component.nondependency" value=""/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="war/WEB-INF/classes"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@ -1,4 +1,4 @@
#Fri Dec 28 00:27:06 CET 2012
eclipse.preferences.version=1
entryPointModules=
filesCopiedToWebInfLib=
gwtCompileSettings=PGd3dC1jb21waWxlLXNldHRpbmdzPjxsb2ctbGV2ZWw+SU5GTzwvbG9nLWxldmVsPjxvdXRwdXQtc3R5bGU+T0JGVVNDQVRFRDwvb3V0cHV0LXN0eWxlPjxleHRyYS1hcmdzPjwhW0NEQVRBW11dPjwvZXh0cmEtYXJncz48dm0tYXJncz48IVtDREFUQVstWG14NTEybV1dPjwvdm0tYXJncz48ZW50cnktcG9pbnQtbW9kdWxlPm9yZy5nY3ViZS5wb3J0bGV0cy5hZG1pbi5yZXNvdXJjZXN3ZWVwZXIuUmVzb3VyY2Vfc3dlZXBlcjwvZW50cnktcG9pbnQtbW9kdWxlPjwvZ3d0LWNvbXBpbGUtc2V0dGluZ3M+

View File

@ -0,0 +1,2 @@
disabled=06target
eclipse.preferences.version=1

14
pom.xml
View File

@ -10,12 +10,12 @@
<groupId>org.gcube.portlets.admin</groupId>
<artifactId>resource-sweeper-widget</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
<name>gCube Resource Sweeper Widget</name>
<properties>
<!-- Convenience property to set the GWT version -->
<gwtVersion>2.4.0</gwtVersion>
<gwtVersion>2.5.1</gwtVersion>
<distroDirectory>distro</distroDirectory>
<!-- GWT needs at least java 1.6 -->
<maven.compiler.source>1.6</maven.compiler.source>
@ -33,6 +33,12 @@
<!-- "provided" so that we don't deploy -->
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt-servlet</artifactId>
<version>${gwtVersion}</version>
<scope>provided</scope>
</dependency>
<!-- This dependency is needed to use GWT UI BInder without old Xerces
version of gCore complaining -->
<dependency>
@ -51,7 +57,6 @@
<groupId>org.gcube.portlets.admin</groupId>
<artifactId>rmp-common-library</artifactId>
<version>[2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT)</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
@ -153,7 +158,7 @@
<artifactId>gwt-maven-plugin</artifactId>
<!-- TODO: Update version to 2.5.0 once gwt-maven-plugin 2.5.0 final
is released (post-GWT 2.5.0) -->
<version>2.4.0</version>
<version>${gwtVersion}</version>
<dependencies>
<dependency>
@ -167,6 +172,7 @@
<version>${gwtVersion}</version>
</dependency>
</dependencies>
<!-- JS is only needed in the package phase, this speeds up testing -->
<executions>
<execution>

View File

@ -8,12 +8,11 @@ import com.google.gwt.core.client.EntryPoint;
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class Resource_sweeper implements EntryPoint {
/**
* This is the entry point method.
*/
public void onModuleLoad() {
//### Just for testing leave commented
//new SweeperDialog("/gcube/devNext");
//new SweeperDialog("/gcube/devsec");
}
}

View File

@ -14,5 +14,5 @@ import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
@RemoteServiceRelativePath("sweeper")
public interface SweeperService extends RemoteService {
List<String> getSweepElems(final String scope, final SweeperActions action);
void applySweep(final String scope, final List<ModelData> elems);
Boolean applySweep(final String scope, final List<ModelData> elems);
}

View File

@ -16,6 +16,6 @@ public interface SweeperServiceAsync {
AsyncCallback<List<String>> callback);
void applySweep(String scope, List<ModelData> elems,
AsyncCallback<Void> callback);
AsyncCallback<Boolean> callback);
}

View File

@ -59,15 +59,12 @@ import com.google.gwt.user.client.ui.AbstractImagePrototype;
*/
public class SweeperDialog {
/**
* Create a remote service proxy to talk to the server-side Sweeper service.
*/
private final SweeperServiceAsync sweeperService = GWT.create(SweeperService.class);
private ContentPanel treeContainer = null;
private ContentPanel gridContainer = null;
private Dialog dialog = null;
public SweeperDialog(final String currentScope) {
this.initDialog(currentScope);
@ -80,7 +77,7 @@ public class SweeperDialog {
/// 1) GHN
AtomicTreeNode ghn = new AtomicTreeNode(
ResourceTypeDecorator.GHN.name(),
ResourceTypeDecorator.GHN.getLabel(),
ResourceTypeDecorator.GHN.getFWSName(),
ResourceTypeDecorator.GHN.getIcon());
AtomicTreeNode ghn_expired = new AtomicTreeNode(SweeperActions.GET_GHN_MOVE_TO_UNREACHABLE.name(),
@ -102,7 +99,7 @@ public class SweeperDialog {
/// 2) RUNNING INSTANCE
AtomicTreeNode ri = new AtomicTreeNode(
ResourceTypeDecorator.RunningInstance.name(),
ResourceTypeDecorator.RunningInstance.getLabel(),
ResourceTypeDecorator.RunningInstance.getFWSName(),
ResourceTypeDecorator.RunningInstance.getIcon());
AtomicTreeNode ri_orphan = new AtomicTreeNode(SweeperActions.GET_RI_DELETE.name(),
SweeperActions.GET_RI_DELETE.getLabel(), ResourceTypeDecorator.Sweeper_RI_Orphan.getIcon());
@ -136,67 +133,65 @@ public class SweeperDialog {
* the code to execute once the resources to cleanup are retrieved.
*/
new AsyncCallback<List<String>>() {
public void onSuccess(final List<String> result) {
dialog.unmask();
Grid<ModelData> tmpgrid = null;
// Cleanup the grid
gridContainer.removeAll();
if (selectedModel.getNode().equals(SweeperActions.GET_RI_DELETE.name())) {
// The grid is created for orphanRI model
tmpgrid = ResourceGridFactory.createGrid(ResourceTypeDecorator.Sweeper_RI.name(), result, null);
} else {
// The grid is created for GHN model
tmpgrid = ResourceGridFactory.createGrid(ResourceTypeDecorator.Sweeper_GHN.name(), result, null);
public void onSuccess(final List<String> result) {
dialog.unmask();
Grid<ModelData> tmpgrid = null;
// Cleanup the grid
gridContainer.removeAll();
if (selectedModel.getNode().equals(SweeperActions.GET_RI_DELETE.name())) {
// The grid is created for orphanRI model
tmpgrid = ResourceGridFactory.createGrid(ResourceTypeDecorator.Sweeper_RI.name(), result, null);
} else {
// The grid is created for GHN model
tmpgrid = ResourceGridFactory.createGrid(ResourceTypeDecorator.Sweeper_GHN.name(), result, null);
}
if (tmpgrid == null) {
Commands.showPopup("Sweeper grid creation", "No elements found for the chosen category");
return;
}
final Grid<ModelData> grid = tmpgrid;
gridContainer.add(grid);
gridContainer.layout(true);
Menu gridMnu = new Menu();
gridMnu.add(new MenuItem("Apply Cleanup") {
@Override
protected void onClick(final ComponentEvent be) {
super.onClick(be);
final List<ModelData> selectedElems = grid.getSelectionModel().getSelection();
dialog.mask("Applying the required operation, please wait", "loading-indicator");
for (ModelData selectedModel : selectedElems) {
ConsoleMessageBroker.info(this, ((Object) selectedModel.get("ID")).toString() + " :: " +((Object) selectedModel.get("Actions")).toString());
}
if (tmpgrid == null) {
Commands.showPopup("Sweeper grid creation", "No elements found for the chosen category");
return;
}
final Grid<ModelData> grid = tmpgrid;
gridContainer.add(grid);
gridContainer.layout(true);
Menu gridMnu = new Menu();
gridMnu.add(new MenuItem("Apply Cleanup") {
@Override
protected void onClick(final ComponentEvent be) {
super.onClick(be);
final List<ModelData> selectedElems = grid.getSelectionModel().getSelection();
dialog.mask("Applying the required operation, please wait", "loading-indicator");
sweeperService.applySweep(
currentScope, selectedElems, new AsyncCallback<Void>() {
public void onSuccess(final Void result) {
dialog.unmask();
Commands.showPopup("Apply sweeper", "Success. Refresh operation suggested.");
for (ModelData elem : selectedElems) {
grid.getStore().remove(elem);
}
}
public void onFailure(final Throwable caught) {
dialog.unmask();
Commands.showPopup("Apply sweeper", "Failure");
}
});
for (ModelData selectedModel : selectedElems) {
ConsoleMessageBroker.info(this,
((Object) selectedModel.get("ID")).toString() + " :: " +
((Object) selectedModel.get("Actions")).toString());
sweeperService.applySweep(currentScope, selectedElems, new AsyncCallback<Boolean>() {
public void onSuccess(Boolean result) {
dialog.unmask();
if (result) {
Commands.showPopup("Sweeper Result", "Success. Refresh operation suggested.");
for (ModelData elem : selectedElems)
grid.getStore().remove(elem);
}
else
Commands.showPopup("Sweeper Result", "Error. Error on server, please check server logs.");
}
public void onFailure(final Throwable caught) {
dialog.unmask();
Commands.showPopup("Sweeper Result", "Failure");
}
});
grid.setContextMenu(gridMnu);
}
public void onFailure(final Throwable caught) {
Commands.showPopup("Sweeper", "Failure");
dialog.unmask();
}
});
grid.setContextMenu(gridMnu);
}
public void onFailure(final Throwable caught) {
Commands.showPopup("Sweeper", "Failure");
dialog.unmask();
}
});
}
};
};

View File

@ -0,0 +1,17 @@
package org.gcube.portlets.admin.resourcesweeper.server;
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 + "]";
}
}

View File

@ -16,26 +16,36 @@
package org.gcube.portlets.admin.resourcesweeper.server;
import static org.gcube.resources.discovery.icclient.ICFactory.client;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import org.gcube.common.core.contexts.GHNContext;
import org.gcube.common.core.contexts.GHNContext.Status;
import org.gcube.common.core.informationsystem.client.ISClient;
import org.gcube.common.core.informationsystem.client.QueryParameter;
import org.gcube.common.core.informationsystem.client.XMLResult;
import org.gcube.common.core.informationsystem.client.queries.GCUBEGenericQuery;
import org.gcube.common.core.resources.GCUBEHostingNode;
import org.gcube.common.core.scope.GCUBEScope;
import org.gcube.common.core.utils.logging.GCUBEClientLog;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.gcube.common.resources.gcore.HostingNode;
import org.gcube.common.resources.gcore.Resource;
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.portlets.admin.resourcesweeper.shared.SweeperActions;
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.AbstractResourceManager;
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;
import com.google.gwt.core.shared.GWT;
/**
* @author Massimiliano Assante (ISTI-CNR)
@ -44,26 +54,37 @@ import com.extjs.gxt.ui.client.data.ModelData;
public class Sweeper {
//TODO: Make it configurable from a property file
public static String LIVE_GHN_MAX_MINUTES = "40";
static GCUBEClientLog _log = new GCUBEClientLog(Sweeper.class);
protected ArrayList<String> applyQuery(GCUBEScope queryScope, QueryLocation queryPath, QueryParameter... params) throws Exception {
ISClient client = GHNContext.getImplementation(ISClient.class);
GCUBEGenericQuery isQuery = null;
isQuery = client.getQuery(GCUBEGenericQuery.class);
isQuery.setExpression(QueryLoader.getQuery(queryPath));
private static final Logger _log = LoggerFactory.getLogger(Sweeper.class);
protected ArrayList<String> 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<String> client = client();
if (params != null && params.length > 0) {
isQuery.addParameters(params);
for (int i = 0; i < params.length; i++) {
isQuery.addParameter(params[i].name, params[i].value);
}
}
List<XMLResult> results = client.execute(isQuery, queryScope);
isQuery.addParameter("RESOURCE", QueryLoader.getQuery(returnPath));
List<String> results = client.submit(isQuery);
ArrayList<String> retval = new ArrayList<String>();
for (XMLResult elem : results) {
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 (elem.evaluate("//ID").get(0) != null && elem.evaluate("//ID").get(0).trim().length() > 0) {
retval.add(elem.toString());
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");
}
@ -71,41 +92,42 @@ public class Sweeper {
_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
* @return the expired ghn list
*/
public ArrayList<String> getExpiredGHNs(final GCUBEScope queryScope) {
public ArrayList<String> getExpiredGHNs(final ScopeBean queryScope) {
try {
return applyQuery(
queryScope,
QueryLocation.SWEEPER_EXPIRED_GHN,
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<String> getDeadGHNs(GCUBEScope queryScope) {
public ArrayList<String> getDeadGHNs(ScopeBean queryScope) {
try {
return applyQuery(
queryScope,
QueryLocation.SWEEPER_DEAD_GHN);
QueryLocation.SWEEPER_DEAD_GHN, QueryLocation.RETURN_SWEEPER_DEAD_GHN);
} catch (Exception e) {
_log.error(e.getMessage());
return null;
}
}
public ArrayList<String> getOrphanRI(GCUBEScope queryScope) {
public ArrayList<String> getOrphanRI(ScopeBean queryScope) {
try {
return applyQuery(
queryScope,
QueryLocation.SWEEPER_ORPHAN_RI);
QueryLocation.SWEEPER_ORPHAN_RI, QueryLocation.RETURN_SWEEPER_ORPHAN_RI);
} catch (Exception e) {
_log.error(e.getMessage());
return null;
@ -113,9 +135,13 @@ public class Sweeper {
}
@SuppressWarnings("deprecation")
public void applySweep(GCUBEScope queryScope, List<ModelData> elems) {
public Boolean applySweep(ScopeBean queryScope, List<ModelData> 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();
@ -123,13 +149,14 @@ public class Sweeper {
switch(action) {
case APPLY_GHN_DELETE:
new GHNManager(resID).forceDelete(queryScope);
GHNManager manager = new GHNManager(resID);
manager.forceDelete(queryScope);
break;
case APPLY_GHN_MOVE_TO_UNREACHABLE:
GHNManager ghnManager = new GHNManager(resID);
GCUBEHostingNode res = (GCUBEHostingNode) ghnManager.getGCUBEResource(queryScope);
res.getNodeDescription().setStatus(Status.UNREACHABLE);
ghnManager.getISPublisher().updateGCUBEResource(res, queryScope, ghnManager.getSecurityManager());
HostingNode res = (HostingNode)ghnManager.getResource(queryScope);
res.profile().description().status("unreachable");
ghnManager.getRegistryPublisher().update(res);
break;
case APPLY_RI_DELETE:
RunningInstanceManager riManager = new RunningInstanceManager(resID);
@ -140,7 +167,10 @@ public class Sweeper {
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
ScopeProvider.instance.set(currScope);
return true;
}
}

View File

@ -22,10 +22,12 @@ import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.gcube.common.core.utils.logging.GCUBEClientLog;
import org.gcube.common.scope.impl.ScopeBean;
import org.gcube.portlets.admin.resourcesweeper.client.async.SweeperService;
import org.gcube.portlets.admin.resourcesweeper.shared.SweeperActions;
import org.gcube.resourcemanagement.support.server.managers.scope.ScopeManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
@ -39,8 +41,8 @@ import com.google.gwt.user.server.rpc.RemoteServiceServlet;
*/
@SuppressWarnings("serial")
public class SweeperServiceImpl extends RemoteServiceServlet implements SweeperService {
static GCUBEClientLog _log = new GCUBEClientLog(SweeperServiceImpl.class);
private static final Logger _log = LoggerFactory.getLogger(SweeperServiceImpl.class);
/**
*
*/
@ -50,13 +52,13 @@ public class SweeperServiceImpl extends RemoteServiceServlet implements SweeperS
try {
switch (action) {
case GET_GHN_MOVE_TO_UNREACHABLE: {
List<String> ghnList = sweeper.getExpiredGHNs(ScopeManager.getScope(scope));
List<String> ghnList = sweeper.getExpiredGHNs(new ScopeBean(scope));
return filterExpiredGHNs(ghnList);
}
case GET_GHN_DELETE:
return sweeper.getDeadGHNs(ScopeManager.getScope(scope));
return sweeper.getDeadGHNs(new ScopeBean(scope));
case GET_RI_DELETE:
return sweeper.getOrphanRI(ScopeManager.getScope(scope));
return sweeper.getOrphanRI(new ScopeBean(scope));
default:
return null;
}
@ -66,29 +68,32 @@ public class SweeperServiceImpl extends RemoteServiceServlet implements SweeperS
return null;
}
}
/**
*
*/
@Override
public void applySweep(String scope, List<ModelData> elems) {
public Boolean applySweep(String scope, List<ModelData> selectedElems) {
Sweeper sweeper = new Sweeper();
System.out.println("Applying sweep");
for (ModelData selectedModel : selectedElems) {
_log.trace(((Object) selectedModel.get("ID")).toString() + " :: " +((Object) selectedModel.get("Actions")).toString());
}
try {
sweeper.applySweep(ScopeManager.getScope(scope), elems);
return sweeper.applySweep(ScopeManager.getScope(scope), selectedElems);
} catch (Exception e) {
_log.error(e.getMessage());
e.printStackTrace();
return false;
}
}
/**
* self explaining
* @param allGHNs
* @return
* @return the filtered ghn
* @throws Exception
*/
private ArrayList<String> filterExpiredGHNs(List<String> allGHNs) throws Exception {
ArrayList<String> expiredGHNsToReturn = new ArrayList<String>();
ArrayList<String> expiredGHNs = new ArrayList<String>();
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
@ -98,7 +103,7 @@ public class SweeperServiceImpl extends RemoteServiceServlet implements SweeperS
for (String ghn : allGHNs) {
saxParser.parse(new InputSource(new StringReader(ghn)), handler);
}
for (String ghn : allGHNs) {
for (String expiredGHNid : expiredGHNs)
if (ghn.contains(expiredGHNid.split(",")[0])) {
@ -108,12 +113,12 @@ public class SweeperServiceImpl extends RemoteServiceServlet implements SweeperS
break;
}
}
return expiredGHNsToReturn;
}
private DefaultHandler getParserHandler(final ArrayList<String> expiredGHNs) throws Exception {
DefaultHandler handler = new DefaultHandler() {
int liveMaxGHNMinutes = Integer.parseInt(Sweeper.LIVE_GHN_MAX_MINUTES);
boolean isID = false;
boolean isLastUpdate = false;
@ -139,12 +144,12 @@ public class SweeperServiceImpl extends RemoteServiceServlet implements SweeperS
if (isLastUpdate) {
String lastUpTime = new String(ch, start, length);
lastUpTimeDate = DatatypeConverter.parseDateTime(lastUpTime);
Calendar now = Calendar.getInstance();
now.setTime(new Date());
long diffInMinutes = (now.getTimeInMillis() - lastUpTimeDate.getTimeInMillis()) / (1000 * 60);
if (diffInMinutes > liveMaxGHNMinutes) {
expiredGHNs.add(tempID+","+diffInMinutes);
}
@ -159,7 +164,7 @@ public class SweeperServiceImpl extends RemoteServiceServlet implements SweeperS
*
* @param xml
* @param xslt
* @return
* @return the html string
* @throws Exception
*/
public static String XML2HTML(final String xml, final String xslt)

View File

@ -18,13 +18,11 @@ package org.gcube.portlets.admin.resourcesweeper.shared;
import java.io.Serializable;
import com.google.gwt.user.client.rpc.IsSerializable;
/**
* @author Daniele Strollo (ISTI-CNR)
*
*/
public enum SweeperActions implements IsSerializable, Serializable {
public enum SweeperActions implements Serializable {
// Declarations for getting resources to cleanup
GET_GHN_MOVE_TO_UNREACHABLE(
"Expired GHNs",