is-collector/src/org/gcube/informationsystem/collector/impl/xmlstorage/exist/Sweeper.java

158 lines
4.4 KiB
Java
Executable File

/**
*
*/
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;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.gcube.informationsystem.collector.impl.persistence.PersistentResource;
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.XMLStorageManager;
/**
* This class provides some cleanup procedures to use to maintain a consistent
* content in the XML storage One of them is a thread, activated at RI startup
* 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
private static Log logger = LogFactory.getLog(Sweeper.class.getName());
private static XMLStorageManager storage = null;
/**
* 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");
Sweeper.storage = new XMLStorageManager();
Sweeper.storage.initialize();
}
/**
* {@inheritDoc}
*/
public void run() {
try {
while (!Thread.interrupted()) {
Thread.sleep(Sweeper.DELAY);
logger.info("Starting IC sweeper...");
this.cleanDeletedResourcesList();
this.cleanExpiredResources();
}
} catch (InterruptedException ie) {
// thread was interrupted
storage.shutdown();
}
}
/**
* Deletes from the backend storage the expired resources
*
*/
public void cleanExpiredResources() {
Calendar now = new GregorianCalendar();
now.setTimeZone(TimeZone.getTimeZone("GMT"));
try {
String[] ids = Sweeper.storage.listAllPropertiesIDs();
for (String id : ids) {
try {
PersistentResource res = Sweeper.storage
.retrievePropertyResourceFromID(id);
if (now.getTimeInMillis() - res.getLastUpdateTimeinMills() > Sweeper.resourceExpirationTime)
// removes the resources from the database
State.storage_manager
.retrieveAndDeleteResourceFromID(id);
// break;
} catch (Exception e) {
logger.debug(State.logPrefix
+ "DIS-IC sweeper - the resource " + id
+ " is no longer available in the storage");
}
}
} catch (Exception e2) {
logger
.warn(
State.logPrefix
+ "DIS-IC sweeper - an exception was rised when trying to cleanup the storage ",
e2);
}
}
/**
* 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() {
// cleanup the RPs collection
State.storage_manager.deleteAllProperties();
}
/**
* 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"));
List<PersistentResource> toRemove = new ArrayList<PersistentResource>();
for (PersistentResource res : State.deletedResources) {
try {
if (now.getTimeInMillis() - res.getLastUpdateTimeinMills() > Sweeper.resourceExpirationTime) {
toRemove.add(res);
}
} catch (Exception e) {
logger.error(State.logPrefix, e);
}
}// end loop on deletedResources
synchronized (State.deletedResources) {
for (PersistentResource res : toRemove) {
State.deletedResources.remove(res);
}
}// end synch block
}
}