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

225 lines
8.3 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.persistence.AggregatorPersistentResource;
import org.gcube.informationsystem.collector.impl.xmlstorage.exist.State;
import org.gcube.informationsystem.collector.impl.xmlstorage.exist.Sweeper;
import org.gcube.informationsystem.collector.stubs.DeleteProfileParams;
import org.gcube.informationsystem.collector.stubs.DocumentNotFoundFaultType;
import org.gcube.informationsystem.collector.stubs.GetProfileCriteria;
import org.gcube.informationsystem.collector.stubs.GetProfileLastModificationTimeMsCriteria;
import org.gcube.informationsystem.collector.stubs.GetResourceCriteria;
import org.gcube.informationsystem.collector.stubs.GetResourceLastModificationTimeMsCriteria;
import org.globus.wsrf.utils.FaultHelper;
/**
* 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);
/**
* 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.getDataManager().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.getDataManager().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;
}
}
/**
* Retrieves the given Resource
* @param criteria the Resource ID
* @return the XML serialization of the Resource
* @throws DocumentNotFoundFaultType if the Resource does not exist
*/
public String getResource(GetResourceCriteria criteria) throws DocumentNotFoundFaultType {
try {
AggregatorPersistentResource aresource = State.getDataManager().retrievePropertyResourceFromID(criteria.getID());
return aresource.getData();
} catch (Exception e) {
logger.warn("Unable to find Resource with ID=" + criteria.getID(), e);
throw new DocumentNotFoundFaultType();
}
}
/**
* Retrieves the given Profile
* @param criteria the Profile ID and Type
* @return the XML serialization of the Profile
* @throws DocumentNotFoundFaultType if the Profile does not exist
*/
public String getProfile(GetProfileCriteria criteria) throws DocumentNotFoundFaultType {
try {
AggregatorPersistentResource aresource = State.getDataManager().retrieveProfile(criteria.getID(), criteria.getProfileType());
return aresource.getData();
} catch (Exception e) {
logger.warn("Unable to find Profile with ID=" + criteria.getID(), e);
throw new DocumentNotFoundFaultType();
}
}
/**
* Gets the last modification time in milliseconds of the given Profile
* @param criteria the Profile ID and Type
* @return the last update time in milliseconds
* @throws DocumentNotFoundFaultType if the Profile does not exist
*/
public long getProfileLastModificationTimeMs(GetProfileLastModificationTimeMsCriteria criteria) throws DocumentNotFoundFaultType {
try {
AggregatorPersistentResource aresource = State.getDataManager().retrieveProfile(criteria.getID(), criteria.getProfileType());
return aresource.getLastUpdateTimeinMills();
} catch (Exception e) {
logger.warn("Unable to find Profile with ID=" + criteria.getID(), e);
throw new DocumentNotFoundFaultType();
}
}
/**
* Gets the last modification time in milliseconds of the given Resource
* @param criteria the Resource ID
* @return the last update time in milliseconds
* @throws DocumentNotFoundFaultType if the Resource does not exist
*/
public long getResourceLastModificationTimeMs(GetResourceLastModificationTimeMsCriteria criteria) throws DocumentNotFoundFaultType {
try {
AggregatorPersistentResource aresource = State.getDataManager().retrievePropertyResourceFromID(criteria.getID());
return aresource.getLastUpdateTimeinMills();
} catch (Exception e) {
logger.warn("Unable to find Resource with ID=" + criteria.getID(), e);
throw new DocumentNotFoundFaultType();
}
}
/** {@inheritDoc} */
@Override
protected GCUBEServiceContext getServiceContext() {
return ICServiceContext.getContext();
}
}