2009-08-17 19:32:15 +02:00
|
|
|
package org.gcube.informationsystem.collector.impl.xmlstorage.exist;
|
|
|
|
|
|
|
|
import org.xmldb.api.base.XMLDBException;
|
|
|
|
import org.xmldb.api.base.Collection;
|
|
|
|
import org.xmldb.api.base.Database;
|
|
|
|
|
|
|
|
import org.xmldb.api.DatabaseManager;
|
|
|
|
|
|
|
|
import org.xmldb.api.modules.XMLResource;
|
|
|
|
import org.xmldb.api.modules.CollectionManagementService;
|
|
|
|
|
|
|
|
import org.exist.xmldb.DatabaseInstanceManager;
|
|
|
|
|
|
|
|
import org.exist.storage.DBBroker;
|
|
|
|
|
|
|
|
import org.gcube.common.core.utils.logging.GCUBELog;
|
2009-09-23 17:26:01 +02:00
|
|
|
import org.gcube.informationsystem.collector.impl.contexts.ICServiceContext;
|
2009-08-17 19:32:15 +02:00
|
|
|
import org.gcube.informationsystem.collector.impl.persistence.AggregatorPersistentResource;
|
|
|
|
import org.gcube.informationsystem.collector.impl.persistence.PersistentResource;
|
2009-08-27 04:18:28 +02:00
|
|
|
import org.gcube.informationsystem.collector.impl.persistence.PersistentResource.MalformedResourceException;
|
2009-08-17 19:32:15 +02:00
|
|
|
import org.gcube.informationsystem.collector.impl.persistence.PersistentResource.RESOURCETYPE;
|
|
|
|
import org.gcube.informationsystem.collector.impl.xmlstorage.exist.XMLStorageManager;
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.FileReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import java.util.concurrent.locks.Lock;
|
|
|
|
import java.util.concurrent.locks.ReentrantLock;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A thread safe manager to interact with the XML Storage repository.
|
|
|
|
*
|
|
|
|
* @author Manuele Simi (ISTI-CNR)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class XMLStorageManager {
|
|
|
|
|
|
|
|
protected static String URI = "xmldb:exist://";
|
2009-09-07 23:18:30 +02:00
|
|
|
|
|
|
|
protected static String USER = "admin";
|
|
|
|
|
|
|
|
protected static String PWD = "";
|
2009-08-17 19:32:15 +02:00
|
|
|
|
|
|
|
protected static String driver = "org.exist.xmldb.DatabaseImpl";
|
|
|
|
|
|
|
|
private static GCUBELog logger = new GCUBELog(XMLStorageManager.class);
|
|
|
|
|
|
|
|
private Database database;
|
|
|
|
|
|
|
|
// private Collection currentCollection;
|
|
|
|
|
2009-08-27 19:10:46 +02:00
|
|
|
protected Collection rootCollection;
|
2009-08-17 19:32:15 +02:00
|
|
|
|
|
|
|
private Collection profilesRootCollection;
|
|
|
|
|
2009-09-23 17:26:01 +02:00
|
|
|
protected Lock lock;
|
|
|
|
|
|
|
|
// lock for writing operations
|
2009-08-17 19:32:15 +02:00
|
|
|
private Lock writeLock;
|
2009-09-23 17:26:01 +02:00
|
|
|
|
|
|
|
// lock for reading operations
|
|
|
|
protected Lock readLock;
|
|
|
|
|
2009-08-17 19:32:15 +02:00
|
|
|
// flag to warn when the DB is locked
|
|
|
|
private boolean locked = false;
|
|
|
|
|
|
|
|
private static final String PROPERTIES_COLLECTION_NAME = "Properties";
|
|
|
|
|
|
|
|
protected static String PROFILES_COLLECTION_NAME = "Profiles";
|
|
|
|
|
2009-08-27 04:18:28 +02:00
|
|
|
enum STATUS {INITIALISED, CLOSED, SHUTDOWN};
|
|
|
|
|
2009-08-27 19:10:46 +02:00
|
|
|
private STATUS status = STATUS.CLOSED;
|
|
|
|
|
2009-09-23 17:26:01 +02:00
|
|
|
protected static long maxOperationsPerConnection = 1000;
|
|
|
|
|
|
|
|
protected long operationsCounter = 0;
|
|
|
|
|
2009-08-27 04:18:28 +02:00
|
|
|
|
2009-08-17 19:32:15 +02:00
|
|
|
/**
|
|
|
|
* Creates a new manager
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public XMLStorageManager() {
|
|
|
|
logger.debug("Creating a new XMLStorageManager");
|
2009-09-23 17:26:01 +02:00
|
|
|
lock = new ReentrantLock();
|
|
|
|
writeLock = lock;
|
|
|
|
readLock = lock;
|
2009-08-17 19:32:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the local XML Storage repository
|
2009-09-23 17:26:01 +02:00
|
|
|
* @param lock
|
2009-08-17 19:32:15 +02:00
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
* if the connection to eXist or its initialization fail
|
|
|
|
*/
|
2009-09-23 17:26:01 +02:00
|
|
|
public void initialize(boolean ... lock) throws XMLStorageNotAvailableException {
|
2009-08-27 04:18:28 +02:00
|
|
|
|
2009-08-27 19:10:46 +02:00
|
|
|
if (this.getStatus() == STATUS.INITIALISED) {
|
2009-08-27 04:18:28 +02:00
|
|
|
logger.warn("XMLStorage already initialized");
|
|
|
|
return;
|
2009-09-23 17:26:01 +02:00
|
|
|
}
|
|
|
|
// lock the instance
|
|
|
|
if (lock != null && lock.length > 0 && lock[0]) {
|
|
|
|
this.lock();
|
2009-08-27 04:18:28 +02:00
|
|
|
}
|
2009-08-17 19:32:15 +02:00
|
|
|
try {
|
2009-08-27 04:18:28 +02:00
|
|
|
logger.info("Initializing XMLStorage...");
|
2009-08-17 19:32:15 +02:00
|
|
|
// this.printEnv();
|
2009-09-23 17:26:01 +02:00
|
|
|
maxOperationsPerConnection = Long.valueOf((String) ICServiceContext.getContext().getProperty("maxOperationsPerConnection", true));
|
2009-08-17 19:32:15 +02:00
|
|
|
|
|
|
|
// register/create the DB instance
|
|
|
|
Class<?> cl = Class.forName(driver);
|
|
|
|
this.database = (Database) cl.newInstance();
|
|
|
|
database.setProperty("create-database", "true");
|
2009-08-25 03:33:19 +02:00
|
|
|
DatabaseManager.registerDatabase(this.database);
|
2009-08-17 19:32:15 +02:00
|
|
|
|
|
|
|
// try to load the collections for props and profiles
|
2009-09-07 23:18:30 +02:00
|
|
|
logger.info("Initializing the root collection");
|
|
|
|
this.rootCollection = DatabaseManager.getCollection(URI + DBBroker.ROOT_COLLECTION, USER, PWD);
|
2009-08-17 19:32:15 +02:00
|
|
|
if (this.rootCollection == null) {
|
|
|
|
logger.error("invalid root collection!");
|
2009-08-27 04:18:28 +02:00
|
|
|
throw new XMLStorageNotAvailableException("unable to load root collection");
|
2009-08-17 19:32:15 +02:00
|
|
|
}
|
2009-09-07 23:18:30 +02:00
|
|
|
logger.info("Initializing the Profiles collection");
|
2009-08-17 19:32:15 +02:00
|
|
|
this.profilesRootCollection = this.rootCollection.getChildCollection(XMLStorageManager.PROFILES_COLLECTION_NAME);
|
|
|
|
if (this.profilesRootCollection == null) {
|
|
|
|
logger.debug("Creating Profiles collection");
|
|
|
|
this.profilesRootCollection = this.createCollection(this.rootCollection, XMLStorageManager.PROFILES_COLLECTION_NAME);
|
|
|
|
logger.debug("Profiles collection created");
|
|
|
|
}
|
|
|
|
if (this.profilesRootCollection == null) {
|
2009-08-27 04:18:28 +02:00
|
|
|
throw new XMLStorageNotAvailableException("Unable to load/create Profiles collection");
|
2009-08-17 19:32:15 +02:00
|
|
|
}
|
|
|
|
this.rootCollection.setProperty("pretty", "true");
|
|
|
|
this.rootCollection.setProperty("encoding", "UTF-8");
|
|
|
|
this.profilesRootCollection.setProperty("pretty", "true");
|
|
|
|
this.profilesRootCollection.setProperty("encoding", "UTF-8");
|
2009-08-27 19:10:46 +02:00
|
|
|
this.setStatus(STATUS.INITIALISED);
|
2009-09-23 17:26:01 +02:00
|
|
|
|
2009-09-07 23:18:30 +02:00
|
|
|
logger.info("XMLStorage initialized with success");
|
2009-08-17 19:32:15 +02:00
|
|
|
} catch (XMLDBException edb) {
|
|
|
|
logger.error("unable to initialize XML storage ", edb);
|
2009-08-27 04:18:28 +02:00
|
|
|
throw new XMLStorageNotAvailableException("unable to initialize XML storage");
|
2009-08-17 19:32:15 +02:00
|
|
|
} catch (Exception e) {
|
2009-08-27 04:18:28 +02:00
|
|
|
logger.error("unable to initialize XML storage ", e);
|
|
|
|
throw new XMLStorageNotAvailableException("unable to initialize XML storage");
|
2009-08-17 19:32:15 +02:00
|
|
|
} catch (java.lang.NoClassDefFoundError ncdfe) {
|
2009-08-27 04:18:28 +02:00
|
|
|
logger.error("unable to initialize XML storage", ncdfe);
|
|
|
|
throw new XMLStorageNotAvailableException("unable to initialize XML storage");
|
2009-08-17 19:32:15 +02:00
|
|
|
} finally {
|
2009-09-23 17:26:01 +02:00
|
|
|
if (lock != null && lock.length > 0 && lock[0]) {
|
|
|
|
this.unlock();
|
|
|
|
}
|
2009-08-17 19:32:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2009-09-23 17:26:01 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Blocks all the writing operations
|
|
|
|
*/
|
|
|
|
protected void lock() {
|
|
|
|
writeLock.lock();
|
|
|
|
this.locked = true;
|
|
|
|
logger.trace("WRITE LOCK acquired");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unblocks all the writing operations
|
|
|
|
*/
|
|
|
|
protected void unlock() {
|
|
|
|
writeLock.unlock();
|
|
|
|
this.locked = false;
|
|
|
|
logger.trace("WRITE LOCK released");
|
|
|
|
}
|
|
|
|
|
2009-08-17 19:32:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shutdowns the XML Storage repository
|
|
|
|
*
|
|
|
|
* @return true if the operation succeed
|
|
|
|
*/
|
2009-09-23 17:26:01 +02:00
|
|
|
public boolean shutdown(boolean lock) {
|
|
|
|
if (lock)
|
|
|
|
this.lock();
|
|
|
|
|
|
|
|
logger.info("XML storage is shutting down");
|
2009-08-17 19:32:15 +02:00
|
|
|
try {
|
|
|
|
DatabaseInstanceManager manager = (DatabaseInstanceManager) rootCollection.getService("DatabaseInstanceManager", "1.0");
|
|
|
|
manager.shutdown();
|
2009-08-27 19:10:46 +02:00
|
|
|
this.setStatus(STATUS.SHUTDOWN);
|
2009-09-23 17:26:01 +02:00
|
|
|
logger.info("...XML storage is down");
|
|
|
|
if (lock)
|
|
|
|
this.lock();
|
2009-08-17 19:32:15 +02:00
|
|
|
} catch (XMLDBException edb) {
|
2009-09-23 17:26:01 +02:00
|
|
|
logger.fatal("Unable to shutdown XML storage", edb);
|
|
|
|
}
|
2009-08-17 19:32:15 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads a collection. If it does not exist, the collection is created.
|
|
|
|
*
|
|
|
|
* @param parentCollection
|
|
|
|
* the parent collection of the collection to load
|
|
|
|
* @param collectionName
|
|
|
|
* name of the collection to load
|
|
|
|
* @return the collection
|
|
|
|
*/
|
|
|
|
private Collection loadCollection(Collection parentCollection, String collectionName) {
|
|
|
|
// set the current collection
|
|
|
|
Collection currentCollection = null;
|
|
|
|
try {
|
|
|
|
currentCollection = parentCollection.getChildCollection(collectionName);
|
|
|
|
if (currentCollection == null) {
|
|
|
|
// the collection does not exist, it is created
|
|
|
|
logger.info("Creating a new collection " + collectionName + "...");
|
|
|
|
currentCollection = this.createCollection(parentCollection, collectionName);
|
|
|
|
}
|
|
|
|
currentCollection.setProperty("pretty", "true");
|
|
|
|
currentCollection.setProperty("encoding", "UTF-8");
|
|
|
|
} catch (XMLDBException edb) {
|
|
|
|
logger.error("failed to create collection " + collectionName + "!");
|
|
|
|
logger.error("" + edb.getCause());
|
|
|
|
|
2009-08-26 03:25:38 +02:00
|
|
|
} catch (java.lang.NullPointerException e) {
|
|
|
|
logger.fatal("the XMLStorage is GONE!! a Restore is needed");
|
|
|
|
|
2009-08-17 19:32:15 +02:00
|
|
|
}
|
|
|
|
return currentCollection;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Discards the current collection
|
|
|
|
*
|
|
|
|
*/
|
2009-08-27 19:10:46 +02:00
|
|
|
protected void resetCollection(Collection currentCollection) {
|
2009-08-17 19:32:15 +02:00
|
|
|
try {
|
|
|
|
currentCollection.close();
|
|
|
|
} catch (XMLDBException edb) {
|
|
|
|
// Catch any issues with closing the exception.
|
|
|
|
logger.error("unable to close collection " + edb.getMessage());
|
|
|
|
}
|
|
|
|
currentCollection = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the collection containing the WS-ResourceProperties documents. It must be used when
|
|
|
|
* quering/storing/updating WS-ResourceProperties documents
|
|
|
|
*
|
|
|
|
* @return the Collection
|
|
|
|
*/
|
|
|
|
public Collection loadPropertiesCollection() {
|
|
|
|
logger.debug("Loading collection Properties... ");
|
|
|
|
return this.loadCollection(this.rootCollection, XMLStorageManager.PROPERTIES_COLLECTION_NAME);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads from the children of the Profile Collection, the collection identified by the given
|
|
|
|
* name. It must be used when quering/storing/updating a particular kind of profile
|
|
|
|
*
|
|
|
|
* @param collectionName
|
|
|
|
* the child collection of the Profile collection to load
|
|
|
|
* @return the Collection
|
|
|
|
*/
|
|
|
|
public Collection loadProfileCollection(String collectionName) {
|
|
|
|
logger.debug("Loading collection " + collectionName + "... ");
|
|
|
|
return this.loadCollection(this.profilesRootCollection, collectionName);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the parent collection of all collections containing resources profiles. It must be used
|
|
|
|
* when quering all the profiles at the same time
|
|
|
|
*
|
|
|
|
* @return the Collection
|
|
|
|
*/
|
|
|
|
public Collection loadAllProfilesCollection() {
|
|
|
|
logger.debug("Loading all profiles collection... ");
|
|
|
|
return this.loadCollection(this.rootCollection, XMLStorageManager.PROFILES_COLLECTION_NAME);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the root collection. It must be used when quering all the information maintained by the
|
|
|
|
* DB instance at the same time
|
|
|
|
*
|
|
|
|
* @return the Collection
|
|
|
|
*/
|
|
|
|
public Collection loadAllCollections() {
|
|
|
|
|
|
|
|
Collection currentCollection = null;
|
|
|
|
logger.debug("Loading all collections... ");
|
|
|
|
// return this.loadCollection(this.rootCollection,
|
|
|
|
// XMLStorageManager.PROFILES_COLLECTION_NAME);
|
|
|
|
try {
|
2009-09-07 23:18:30 +02:00
|
|
|
currentCollection = DatabaseManager.getCollection(URI + DBBroker.ROOT_COLLECTION, USER, PWD);
|
2009-08-17 19:32:15 +02:00
|
|
|
} catch (XMLDBException edb) {
|
|
|
|
logger.error("Failed to load all collections!");
|
|
|
|
logger.error("", edb);
|
|
|
|
}
|
|
|
|
return currentCollection;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores a AggregatorPersistentResource in the current collection. If the resource already
|
|
|
|
* exists in the storage, it is updated.
|
|
|
|
*
|
|
|
|
* @param resource
|
|
|
|
* the resource to store
|
|
|
|
* @throws Exception
|
|
|
|
* if the storing fails
|
|
|
|
*/
|
2009-08-27 04:18:28 +02:00
|
|
|
public void storeResource(PersistentResource resource) throws XMLStorageNotAvailableException, MalformedResourceException {
|
2009-08-17 19:32:15 +02:00
|
|
|
|
2009-09-27 22:08:18 +02:00
|
|
|
this.lock();
|
|
|
|
if (status != STATUS.INITIALISED) {
|
|
|
|
this.unlock();
|
|
|
|
throw new XMLStorageNotAvailableException("XMLStorage not initialized");
|
|
|
|
}
|
|
|
|
|
2009-08-17 19:32:15 +02:00
|
|
|
Collection currentCollection = null;
|
|
|
|
if (resource.getType() == RESOURCETYPE.Profile) {
|
|
|
|
// the entry contains a gCube resource profile
|
|
|
|
currentCollection = this.loadProfileCollection(resource.getProfileType());
|
|
|
|
} else {
|
|
|
|
// the entry contains generic properties
|
|
|
|
currentCollection = this.loadPropertiesCollection();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentCollection == null) {
|
|
|
|
logger.error("Unable to open the Collection");
|
2009-09-27 22:08:18 +02:00
|
|
|
this.unlock();
|
2009-08-27 04:18:28 +02:00
|
|
|
throw new XMLStorageNotAvailableException("Unable to open the Collection");
|
2009-08-17 19:32:15 +02:00
|
|
|
}
|
2009-09-27 22:08:18 +02:00
|
|
|
|
2009-08-17 19:32:15 +02:00
|
|
|
try {
|
|
|
|
XMLResource document = (XMLResource) currentCollection.createResource(resource.getID(), "XMLResource");
|
|
|
|
document.setContent(resource.toString());
|
|
|
|
logger.debug("Storing/updating resource " + document.getId() + " in collection " + currentCollection.getName() + "...");
|
|
|
|
currentCollection.storeResource(document);
|
|
|
|
logger.debug("...done");
|
|
|
|
} catch (XMLDBException edb) {
|
|
|
|
logger.error("Failed to store resource " + resource.getID());
|
|
|
|
logger.error("" + edb.errorCode + " " + edb.getMessage(), edb);
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error("" + e.getMessage(), e);
|
|
|
|
} finally {
|
|
|
|
this.resetCollection(currentCollection);
|
2009-09-23 17:26:01 +02:00
|
|
|
operationsCounter++;
|
|
|
|
this.unlock();
|
2009-08-17 19:32:15 +02:00
|
|
|
}
|
2009-09-23 17:26:01 +02:00
|
|
|
this.checkConnection();
|
2009-08-17 19:32:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return true if the connection to eXist is locked
|
|
|
|
*/
|
|
|
|
public boolean isLocked() {
|
|
|
|
return this.locked;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves a resource from the storage given its ID
|
|
|
|
*
|
|
|
|
* @param resourceID
|
|
|
|
* @return
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public AggregatorPersistentResource retrieveResourceFromID(String resourceID) throws Exception {
|
|
|
|
XMLResource res = null;
|
|
|
|
Collection currentCollection = this.loadAllCollections();
|
|
|
|
try {
|
|
|
|
res = (XMLResource) currentCollection.getResource(resourceID);
|
|
|
|
if (res == null)
|
|
|
|
logger.warn("Resource " + resourceID + " not found!");
|
|
|
|
} catch (XMLDBException edb) {
|
|
|
|
logger.error("Failed to retrieve document " + resourceID);
|
|
|
|
logger.error("" + edb.errorCode + " " + edb.getMessage(), edb);
|
|
|
|
throw new Exception();
|
|
|
|
}
|
|
|
|
return new AggregatorPersistentResource(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves a resource from the storage given its ID
|
|
|
|
*
|
|
|
|
* @param resourceID
|
|
|
|
* @return
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public AggregatorPersistentResource retrievePropertyResourceFromID(String resourceID) throws Exception {
|
|
|
|
XMLResource res = null;
|
|
|
|
Collection currentCollection = this.loadPropertiesCollection();
|
|
|
|
try {
|
|
|
|
res = (XMLResource) currentCollection.getResource(resourceID);
|
|
|
|
if (res == null)
|
|
|
|
logger.warn("Resource " + resourceID + " not found!");
|
|
|
|
} catch (XMLDBException edb) {
|
|
|
|
logger.error("Failed to retrieve document " + resourceID);
|
|
|
|
logger.error("" + edb.errorCode + " " + edb.getMessage(), edb);
|
|
|
|
throw new Exception();
|
|
|
|
}
|
|
|
|
return new AggregatorPersistentResource(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param xpathquery
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public AggregatorPersistentResource executeXPathQuery(String xpathquery) {
|
|
|
|
|
|
|
|
AggregatorPersistentResource res = null;
|
|
|
|
// ArrayList<AggregatorPersistentResource> results = new
|
|
|
|
// ArrayList<AggregatorPersistentResource>();
|
|
|
|
/*
|
|
|
|
* try { // get query-service XPathQueryServiceImpl service = (XPathQueryServiceImpl)
|
|
|
|
* currentCollection.getService("XPathQueryService", "1.0"); // set pretty-printing on
|
|
|
|
* service.setProperty(OutputKeys.INDENT, "yes"); service.setProperty(OutputKeys.ENCODING,
|
|
|
|
* "UTF-8"); ResourceSet set = service.query(xpathquery); logger.debug("number of returned
|
|
|
|
* documents: " + set.getSize()); ResourceIterator i = set.getIterator();
|
|
|
|
* while(i.hasMoreResources()) { res = new AggregatorPersistentResource((XMLResource)
|
|
|
|
* i.nextResource()); System.out.println("DILIGENT resource " + i + " " + res.toString()); }
|
|
|
|
*
|
|
|
|
* for (int i = 0; i < (int) set.getSize(); i++) { res = new
|
|
|
|
* AggregatorPersistentResource((XMLResource) set.getResource((long) i));
|
|
|
|
* System.out.println("DILIGENT resource " + i + " " + res.toString()); } } catch
|
|
|
|
* (XMLDBException edb) { logger.error("failed to execute Xpath query " + xpathquery);
|
|
|
|
* edb.printStackTrace(); } catch (Exception e) { logger.error("exception " + xpathquery);
|
|
|
|
* logger.error(e.getStackTrace()); }
|
|
|
|
*/
|
|
|
|
return res;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a WS-ResourceProperties resource identified by the given ID
|
|
|
|
*/
|
|
|
|
synchronized public void retrieveAndDeleteResourceFromID(String resourceID) throws Exception {
|
|
|
|
|
|
|
|
if (resourceID == null) {
|
|
|
|
logger.warn("Invalid resource ID");
|
|
|
|
return;
|
|
|
|
}
|
2009-09-29 00:29:06 +02:00
|
|
|
this.lock();
|
2009-08-17 19:32:15 +02:00
|
|
|
Collection propCollection = this.loadPropertiesCollection();
|
|
|
|
|
|
|
|
if (propCollection == null) {
|
|
|
|
logger.error("Unable to load collection Properties!");
|
|
|
|
throw new Exception();
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
logger.info("Trying to remove resource " + resourceID + " from collection " + propCollection.getName());
|
2009-09-29 00:29:06 +02:00
|
|
|
deleteResource(resourceID, propCollection, false);
|
2009-08-17 19:32:15 +02:00
|
|
|
} catch (XMLDBException edb) {
|
|
|
|
logger.error("Failed to remove the resource from the storage! ");
|
|
|
|
logger.error("", edb);
|
|
|
|
throw new Exception();
|
|
|
|
} finally {
|
|
|
|
this.resetCollection(propCollection);
|
2009-09-29 00:29:06 +02:00
|
|
|
this.unlock();
|
2009-08-17 19:32:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a Profile resource identified by the given ID
|
|
|
|
*/
|
|
|
|
synchronized public void retrieveAndDeleteProfileFromID(String profileID, String profileType) throws Exception {
|
|
|
|
|
|
|
|
if (profileID == null) {
|
|
|
|
logger.warn("Invalid profile ID");
|
|
|
|
return;
|
|
|
|
}
|
2009-09-29 00:29:06 +02:00
|
|
|
this.lock();
|
2009-08-17 19:32:15 +02:00
|
|
|
Collection profileCollection = this.loadProfileCollection(profileType);
|
|
|
|
if (profileCollection == null) {
|
2009-09-29 00:29:06 +02:00
|
|
|
logger.error("Unable to load collection Profile!");
|
|
|
|
this.unlock();
|
2009-08-17 19:32:15 +02:00
|
|
|
throw new Exception("unable to load collection Profile");
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
logger.info("Trying to remove profile '" + profileID + "' from collection " + profileCollection.getName());
|
2009-09-29 00:29:06 +02:00
|
|
|
deleteResource(profileID, profileCollection, false);
|
|
|
|
} catch (Exception edb) {
|
2009-08-17 19:32:15 +02:00
|
|
|
logger.error("Failed to remove the profile from the storage! ");
|
2009-09-29 00:29:06 +02:00
|
|
|
throw new Exception(edb);
|
|
|
|
} finally {
|
2009-08-17 19:32:15 +02:00
|
|
|
this.resetCollection(profileCollection);
|
2009-09-29 00:29:06 +02:00
|
|
|
this.unlock();
|
2009-08-17 19:32:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes the resource with the given ID from the local storage
|
|
|
|
*
|
|
|
|
* @param resourceID
|
|
|
|
* - the ID of the resource
|
|
|
|
* @param col
|
|
|
|
* - the collection from which the resource has to be removed
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2009-09-29 00:29:06 +02:00
|
|
|
private void deleteResource(String resourceID, Collection col, boolean ... lock) throws Exception {
|
2009-08-17 19:32:15 +02:00
|
|
|
|
|
|
|
XMLResource res = null;
|
|
|
|
// lock the instance
|
2009-09-29 00:29:06 +02:00
|
|
|
if (lock != null && lock.length > 0 && lock[0])
|
|
|
|
this.lock();
|
2009-08-17 19:32:15 +02:00
|
|
|
try {
|
|
|
|
res = (XMLResource) col.getResource(resourceID);
|
|
|
|
if (res == null)
|
|
|
|
logger.warn("Resource " + resourceID + " not found!");
|
|
|
|
else {
|
|
|
|
col.removeResource(res);
|
|
|
|
logger.info("Resource successfully removed");
|
|
|
|
}
|
|
|
|
} catch (XMLDBException edb) {
|
|
|
|
logger.error("Failed to retrieve resource " + resourceID);
|
2009-09-29 00:29:06 +02:00
|
|
|
throw new Exception(edb);
|
2009-08-17 19:32:15 +02:00
|
|
|
} finally {
|
|
|
|
this.resetCollection(col);
|
2009-09-29 00:29:06 +02:00
|
|
|
if (lock != null && lock.length > 0 && lock[0])
|
|
|
|
this.unlock();
|
2009-08-17 19:32:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prints all the IDs of the Resources stored in the DB instance
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public void printResourcesIDs() {
|
|
|
|
|
|
|
|
String[] ress;
|
|
|
|
Collection currentCollection = null;
|
|
|
|
currentCollection = this.loadAllCollections();
|
|
|
|
try {
|
|
|
|
if (currentCollection == null) {
|
|
|
|
ress = this.rootCollection.listResources();
|
|
|
|
} else {
|
|
|
|
ress = currentCollection.listResources();
|
|
|
|
}
|
|
|
|
for (int i = 0; i < ress.length; i++) {
|
|
|
|
logger.debug("Resource ID:" + ress[i]);
|
|
|
|
}
|
|
|
|
} catch (XMLDBException edb) {
|
|
|
|
logger.error("Failed to read resource IDs ", edb);
|
|
|
|
} finally {
|
|
|
|
this.resetCollection(currentCollection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new collection
|
|
|
|
*
|
|
|
|
* @param collectionName
|
|
|
|
* @return the create Collection object
|
|
|
|
*/
|
|
|
|
private Collection createCollection(Collection parentCollection, String collectionName) {
|
|
|
|
|
|
|
|
Collection col = null;
|
2009-09-23 17:26:01 +02:00
|
|
|
this.lock();
|
2009-08-17 19:32:15 +02:00
|
|
|
try {
|
|
|
|
CollectionManagementService mgtService = (CollectionManagementService) parentCollection.getService("CollectionManagementService", "1.0");
|
|
|
|
col = mgtService.createCollection(collectionName);
|
|
|
|
} catch (XMLDBException edb) {
|
|
|
|
logger.error("Failed to create collection " + collectionName, edb);
|
|
|
|
} finally {
|
2009-09-23 17:26:01 +02:00
|
|
|
this.unlock();
|
2009-08-17 19:32:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return col;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete the collection named PROPERTIES_COLLECTION_NAME from the storage
|
|
|
|
*/
|
|
|
|
public void deleteAllProperties() {
|
|
|
|
|
2009-09-23 17:26:01 +02:00
|
|
|
this.lock();
|
2009-08-17 19:32:15 +02:00
|
|
|
try {
|
|
|
|
logger.info("Trying to delete the collection " + XMLStorageManager.PROPERTIES_COLLECTION_NAME + "...");
|
|
|
|
CollectionManagementService mgtService = (CollectionManagementService) rootCollection.getService("CollectionManagementService", "1.0");
|
|
|
|
mgtService.removeCollection(XMLStorageManager.PROPERTIES_COLLECTION_NAME);
|
|
|
|
logger.info("Collection deleted");
|
|
|
|
} catch (XMLDBException edb) {
|
|
|
|
logger.warn("Unable to delete the collection " + XMLStorageManager.PROPERTIES_COLLECTION_NAME + ": " + edb.toString());
|
|
|
|
} finally {
|
2009-09-23 17:26:01 +02:00
|
|
|
this.unlock();
|
2009-08-17 19:32:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public String[] listAllPropertiesIDs() {
|
|
|
|
return listAllColletionIDs(this.loadPropertiesCollection());
|
|
|
|
}
|
|
|
|
|
2009-08-27 19:10:46 +02:00
|
|
|
/**
|
|
|
|
* @return the status
|
|
|
|
*/
|
|
|
|
public STATUS getStatus() {
|
|
|
|
logger.trace("Status is " + status);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param status the status to set
|
|
|
|
*/
|
|
|
|
public void setStatus(STATUS status) {
|
|
|
|
logger.trace("New status is " + status);
|
|
|
|
this.status = status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-17 19:32:15 +02:00
|
|
|
private String[] listAllColletionIDs(Collection collection) {
|
|
|
|
String[] ids = null;
|
|
|
|
String collectionName = "";
|
|
|
|
try {
|
|
|
|
collectionName = collection.getName();
|
|
|
|
logger.debug("Retrieving all resource IDs from collection " + collectionName);
|
|
|
|
ids = new String[collection.getResourceCount()];
|
|
|
|
ids = collection.listResources();
|
|
|
|
logger.debug("Retrieved " + ids.length + " elements");
|
|
|
|
} catch (XMLDBException edb) {
|
|
|
|
logger.warn("Unable to retrieve ids from collection " + collectionName + " " + edb.toString());
|
|
|
|
}
|
|
|
|
return ids;
|
|
|
|
}
|
2009-09-23 17:26:01 +02:00
|
|
|
|
|
|
|
protected void checkConnection() {
|
|
|
|
if (this.operationsCounter > XMLStorageManager.maxOperationsPerConnection) {
|
|
|
|
logger.info("It's time to reset the connection...");
|
|
|
|
this.lock();
|
|
|
|
this.shutdown(false);
|
|
|
|
//give a breath to the Transaction Manager
|
|
|
|
try {Thread.sleep(5000);} catch (InterruptedException e) {}
|
|
|
|
try {
|
|
|
|
this.initialize(false);
|
|
|
|
} catch (XMLStorageNotAvailableException e) {
|
|
|
|
logger.fatal("Unable to initialize XML storage", e);
|
|
|
|
}
|
|
|
|
this.operationsCounter = 0;
|
|
|
|
this.unlock();
|
|
|
|
logger.info("Connection reset");
|
|
|
|
}
|
|
|
|
}
|
2009-08-17 19:32:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads a file content in a String
|
|
|
|
*
|
|
|
|
* @param file
|
|
|
|
* path and name of the file to read
|
|
|
|
* @return the file content
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
|
|
|
protected static String readFile(String file) throws IOException {
|
|
|
|
|
|
|
|
BufferedReader f = new BufferedReader(new FileReader(file));
|
|
|
|
String line;
|
|
|
|
StringBuffer content = new StringBuffer();
|
|
|
|
while ((line = f.readLine()) != null)
|
|
|
|
content.append(line);
|
|
|
|
f.close();
|
|
|
|
return content.toString();
|
|
|
|
}
|
|
|
|
|
2009-08-27 04:18:28 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* XMLStorage not initialized exception
|
|
|
|
*
|
|
|
|
* @author Manuele Simi (ISTI-CNR)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static class XMLStorageNotAvailableException extends Exception {
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
public XMLStorageNotAvailableException(String message) { super(message);}
|
|
|
|
|
|
|
|
}
|
2009-08-17 19:32:15 +02:00
|
|
|
}
|