2009-08-17 19:32:15 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
package org.gcube.informationsystem.collector.impl.xmlstorage.exist;
|
|
|
|
|
|
|
|
import java.lang.InterruptedException;
|
|
|
|
import java.util.GregorianCalendar;
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.TimeZone;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2009-08-25 03:33:19 +02:00
|
|
|
import org.gcube.common.core.utils.logging.GCUBELog;
|
2009-08-17 19:32:15 +02:00
|
|
|
import org.gcube.informationsystem.collector.impl.xmlstorage.exist.State;
|
|
|
|
import org.gcube.informationsystem.collector.impl.xmlstorage.exist.Sweeper;
|
2010-10-25 05:15:43 +02:00
|
|
|
import org.gcube.informationsystem.collector.impl.resources.GCUBEInstanceStateResource;
|
|
|
|
import org.gcube.informationsystem.collector.impl.resources.GCUBEXMLResource;
|
2009-08-17 19:32:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class provides some cleanup procedures to use to maintain a consistent
|
2010-10-25 05:15:43 +02:00
|
|
|
* content in the XML storage. One of them is a thread, activated at RI startup
|
2009-08-17 19:32:15 +02:00
|
|
|
* time, that periodically drops the out to date resources from the storage
|
|
|
|
*
|
|
|
|
* @author Manuele Simi (ISTI-CNR)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class Sweeper implements Runnable {
|
|
|
|
|
|
|
|
private static long DELAY = 180000; // default value
|
|
|
|
|
|
|
|
private static long resourceExpirationTime = 1800000; // default value
|
|
|
|
|
2009-09-07 23:18:30 +02:00
|
|
|
private static GCUBELog logger = new GCUBELog(Sweeper.class);
|
2009-08-17 19:32:15 +02:00
|
|
|
|
2010-10-25 05:15:43 +02:00
|
|
|
//private static XMLStorage storage = null;
|
2009-08-17 19:32:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes a new Sweeper
|
|
|
|
*
|
|
|
|
* @param delay the sweeper delay
|
|
|
|
* @param resourceExpirationTime the time after that a resource is classified as exipired
|
|
|
|
* @throws Exception if the eXist connection fails
|
|
|
|
*/
|
|
|
|
public Sweeper(long delay, long resourceExpirationTime) throws Exception {
|
|
|
|
Sweeper.DELAY = delay;
|
|
|
|
Sweeper.resourceExpirationTime = resourceExpirationTime;
|
|
|
|
logger.info("Starting sweeper thread with an interval of " + Sweeper.DELAY + " ms");
|
2009-09-23 17:26:01 +02:00
|
|
|
|
2009-08-17 19:32:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public void run() {
|
2009-09-23 17:26:01 +02:00
|
|
|
if (Sweeper.DELAY == -1) return;
|
2009-08-17 19:32:15 +02:00
|
|
|
try {
|
|
|
|
while (!Thread.interrupted()) {
|
|
|
|
Thread.sleep(Sweeper.DELAY);
|
|
|
|
logger.info("Starting IC sweeper...");
|
|
|
|
this.cleanDeletedResourcesList();
|
|
|
|
this.cleanExpiredResources();
|
|
|
|
}
|
|
|
|
} catch (InterruptedException ie) {
|
2009-08-27 04:18:28 +02:00
|
|
|
logger.error("Unable to sleep (yawn)", ie);
|
2009-08-17 19:32:15 +02:00
|
|
|
// thread was interrupted
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes from the backend storage the expired resources
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public void cleanExpiredResources() {
|
|
|
|
|
|
|
|
Calendar now = new GregorianCalendar();
|
|
|
|
now.setTimeZone(TimeZone.getTimeZone("GMT"));
|
|
|
|
try {
|
2010-10-25 05:15:43 +02:00
|
|
|
GCUBEInstanceStateResource fakeresource = new GCUBEInstanceStateResource();
|
|
|
|
//get all IDs for Instance States' collection
|
|
|
|
String[] ids = State.getDataManager().listAllCollectionResourceIDs(fakeresource.getCollectionName());
|
2009-08-17 19:32:15 +02:00
|
|
|
for (String id : ids) {
|
|
|
|
try {
|
2010-10-25 05:15:43 +02:00
|
|
|
GCUBEInstanceStateResource tempresource = new GCUBEInstanceStateResource();
|
|
|
|
tempresource.setResourceName(id);
|
|
|
|
GCUBEXMLResource xmlresource = new GCUBEXMLResource(tempresource);
|
|
|
|
State.getDataManager().retrieveResourceContent(new GCUBEXMLResource(tempresource));
|
|
|
|
if (now.getTimeInMillis() - xmlresource.getLastUpdateTimeinMills() > Sweeper.resourceExpirationTime)
|
2009-08-17 19:32:15 +02:00
|
|
|
// removes the resources from the database
|
2010-10-25 05:15:43 +02:00
|
|
|
State.getDataManager().deleteResource(xmlresource);
|
2009-08-17 19:32:15 +02:00
|
|
|
// break;
|
|
|
|
} catch (Exception e) {
|
2009-08-25 03:33:19 +02:00
|
|
|
logger.debug("IC sweeper - the resource " + id + " is no longer available in the storage");
|
2009-08-17 19:32:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception e2) {
|
2009-08-25 03:33:19 +02:00
|
|
|
logger.warn("IC sweeper - an exception was rised when trying to cleanup the storage ", e2);
|
2009-08-17 19:32:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes all the properties documents related to the given Running
|
|
|
|
* Instance ID
|
|
|
|
*
|
|
|
|
* @param id
|
|
|
|
* the ID of the Running Instance whose properties documents have
|
|
|
|
* to be removed
|
|
|
|
*/
|
|
|
|
public static void cleanResourceForRI(String id) {
|
|
|
|
// TO DO
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes the Properties collection from the storage
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static void cleanRPs() {
|
2010-10-25 05:15:43 +02:00
|
|
|
// cleanup the RPs collection
|
|
|
|
State.getDataManager().deleteAllResourcesFromCollection(new GCUBEInstanceStateResource().getCollectionName());
|
2009-08-17 19:32:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes the expired resources from the list of deleted resources as
|
|
|
|
* notified by the AF
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
public void cleanDeletedResourcesList() {
|
|
|
|
|
|
|
|
Calendar now = new GregorianCalendar();
|
|
|
|
now.setTimeZone(TimeZone.getTimeZone("GMT"));
|
2010-10-25 05:15:43 +02:00
|
|
|
List<GCUBEXMLResource> toRemove = new ArrayList<GCUBEXMLResource>();
|
|
|
|
for (GCUBEXMLResource res : State.deletedResources) {
|
2009-08-17 19:32:15 +02:00
|
|
|
try {
|
|
|
|
if (now.getTimeInMillis() - res.getLastUpdateTimeinMills() > Sweeper.resourceExpirationTime) {
|
|
|
|
toRemove.add(res);
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
2009-08-25 03:33:19 +02:00
|
|
|
logger.error("Failed to clean up the resources", e);
|
2009-08-17 19:32:15 +02:00
|
|
|
}
|
|
|
|
}// end loop on deletedResources
|
|
|
|
synchronized (State.deletedResources) {
|
2010-10-25 05:15:43 +02:00
|
|
|
for (GCUBEXMLResource res : toRemove) {
|
2009-08-17 19:32:15 +02:00
|
|
|
State.deletedResources.remove(res);
|
|
|
|
}
|
|
|
|
}// end synch block
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|