is-collector/src/org/gcube/informationsystem/collector/impl/porttypes/XMLCollectionAccess.java

253 lines
8.5 KiB
Java
Executable File

package org.gcube.informationsystem.collector.impl.porttypes;
import javax.xml.namespace.QName;
import org.oasis.wsrf.faults.BaseFaultType;
import org.gcube.common.core.contexts.GCUBEServiceContext;
import org.gcube.common.core.porttypes.GCUBEPortType;
import org.gcube.common.core.utils.logging.GCUBELog;
import org.gcube.informationsystem.collector.impl.contexts.ICServiceContext;
import org.gcube.informationsystem.collector.impl.xmlstorage.exist.State;
import org.gcube.informationsystem.collector.impl.xmlstorage.exist.Sweeper;
import org.gcube.informationsystem.collector.impl.xmlstorage.exist.XQuery;
import org.gcube.informationsystem.collector.stubs.DeleteProfileParams;
import org.globus.wsrf.utils.FaultHelper;
import org.xmldb.api.base.ResourceSet;
import org.xmldb.api.modules.XMLResource;
/**
* State service implementation class
*
* @author Manuele Simi (ISTI-CNR)
*
*/
public class XMLCollectionAccess extends GCUBEPortType {
/**
* WS Properties namespace
*/
public static final QName RP_SET = new QName(
"http://gcube-system.org/namespaces/informationsystem/collector/XMLCollectionAccess",
"ICRP");
private final GCUBELog logger = new GCUBELog(XMLCollectionAccess.class);
/**
* Executes the given XQuery on the embedded eXist DB instance
*
* @param xquery
* - a valid XQuery. It must be compliant with the syntax (with
* the exception of XML Schema-related features) as specified in
* W3C's recommendations outlined in the June 2006 candidate
* recommendation
* @return the result set wrapped in a <Resultset> tag. Each record is
* wrapped with a <Recordt>
* @throws BaseFaultType
* if the XQuery is not valid or the connection to eXist is
* broken
*/
public String executeXQuery(String xquery) throws BaseFaultType {
StringBuilder response = new StringBuilder();
try {
logger.debug("executing XQuery: " + xquery);
XQuery q = new XQuery(xquery);
ResourceSet result = State.query_manager.executeXQuery(q);
logger.debug("number of returned documents: " + result.getSize());
response.append("<Resultset>\n");
for (int i = 0; i < (int) result.getSize(); i++) {
XMLResource xmlres = (XMLResource) result.getResource((long) i);
// logger.debug("retrieved resource - " + xmlres.getContent());
response.append("<Record>\n" + xmlres.getContent()+ "\n</Record>\n");
}
response.append("</Resultset>");
} catch (Exception e) {
BaseFaultType fault = new BaseFaultType();
FaultHelper faultHelper = new FaultHelper(fault);
faultHelper.addFaultCause(e);
faultHelper.addDescription("IC service: Exception when executing the requested XQuery");
throw fault;
}
return response.toString();
}
/**
* Removes a profile from the Storage given its DILIGENT Resource ID
*
* @param params
* a stub class including private java.lang.String
* DILIGENTResourceID; the id of the profile to remove private
* java.lang.String profileType; the type of the profile to
* remove (e.g RunningInstance, DHN, Service, etc.);
* @return true if the resource is successfully deleted
* @throws BaseFaultType
* if the type parameter is not valid or an error occurs when
* deleting the profile
*/
public boolean deleteProfile(DeleteProfileParams params)
throws BaseFaultType {
boolean response = false;
String id = params.getID();
String type = params.getProfileType();
if ((id == "") || (id == null)) {
logger.warn("invalid id");
BaseFaultType fault = new BaseFaultType();
FaultHelper faultHelper = new FaultHelper(fault);
faultHelper.addDescription("invalid id");
throw fault;
}
if ((type == "") || (type == null)) {
logger.warn("invalid profile type");
BaseFaultType fault = new BaseFaultType();
FaultHelper faultHelper = new FaultHelper(fault);
faultHelper.addDescription("invalid profile type");
throw fault;
}
try {
logger.debug("deleting profile " + id + " from collection " + type);
State.storage_manager.retrieveAndDeleteProfileFromID(id, type);
if (type.equalsIgnoreCase("RunningInstance"))
Sweeper.cleanResourceForRI(id);
} catch (Exception e) {
logger.error("unable to remove resource: " + id, e);
BaseFaultType fault = new BaseFaultType();
FaultHelper faultHelper = new FaultHelper(fault);
faultHelper.addFaultCause(e);
faultHelper
.addDescription("IC service: Exception when deleting the requested resource");
throw fault;
}
return response;
}
/**
* Removes a resource from the Storage given its ID
*
* @param id
* - the id of the resoure to remove
* @return true if the resource is successfully deleted
* @throws BaseFaultType
* if the id parameter is null or an error occurs when deleting
* the profile
*/
public boolean deleteResource(String id) throws BaseFaultType {
boolean response = false;
if ((id == "") || (id == null)) {
logger.warn("invalid id");
BaseFaultType fault = new BaseFaultType();
FaultHelper faultHelper = new FaultHelper(fault);
faultHelper.addDescription("invalid id");
throw fault;
}
try {
logger.debug("deleting resource: " + id);
State.storage_manager.retrieveAndDeleteResourceFromID(id);
} catch (Exception e) {
logger.error("unable to remove resource: " + id, e);
BaseFaultType fault = new BaseFaultType();
FaultHelper faultHelper = new FaultHelper(fault);
faultHelper.addFaultCause(e);
faultHelper
.addDescription("IC service: Exception when deleting the requested resource");
throw fault;
}
return response;
}
/**
* Deletes the entire set of registered RPs from the storage
*
* @throws BaseFaultType
* if an error occurs when deleting the resources
*/
public void deleteAllRPs() throws BaseFaultType {
try {
logger.info("DeleteAllRPs operation invoked");
Sweeper.cleanRPs();
logger.info("All RPs have been deleted from the storage");
} catch (Exception e) {
logger.error("unable to clean RPs collction: ", e);
BaseFaultType fault = new BaseFaultType();
FaultHelper faultHelper = new FaultHelper(fault);
faultHelper.addFaultCause(e);
faultHelper.addDescription("IC service: Exception when deleting the requested resource");
throw fault;
}
}
/**
* Disposes the State service
*
* @throws BaseFaultType
* if the shutdown fails
*/
public void dispose() throws BaseFaultType {
try {
logger.info("Dispose operation invoked");
logger.info("trying to shutdown the storage instances...");
try {
State.storage_manager.shutdown();
State.query_manager.shutdown();
} catch (NullPointerException se) {/* nothng to do */
}
State.storage_manager = null;
State.query_manager = null;
// request the interruption of the sweeper thread
State.sweeperT.interrupt();
logger.info("done");
} catch (Exception e) {
logger.error("unable to pose the IC with success ", e);
BaseFaultType fault = new BaseFaultType();
FaultHelper faultHelper = new FaultHelper(fault);
faultHelper.addFaultCause(e);
faultHelper
.addDescription("-IC service: Unable to pose the service");
throw fault;
}
}
/**
* Initialized the State service
*
* @throws BaseFaultType
* if the initialization fails
*/
public void initialize() throws BaseFaultType {
try {
logger.info("Initialize operation invoked");
State.initialize();
logger.info("done");
} catch (Exception e) {
logger.error("Unable to initialize the IC state", e);
BaseFaultType fault = new BaseFaultType();
FaultHelper faultHelper = new FaultHelper(fault);
faultHelper.addFaultCause(e);
faultHelper.addDescription("-IC service: Unable to initialize the service");
throw fault;
}
}
/** {@inheritDoc} */
@Override
protected GCUBEServiceContext getServiceContext() {
return ICServiceContext.getContext();
}
}