[MOBY-guts] biomoby commit

Eddie Kawas kawas at dev.open-bio.org
Wed Feb 20 16:59:03 UTC 2008


kawas
Wed Feb 20 11:58:51 EST 2008
Update of /home/repository/moby/moby-live/Java/src/main/org/biomoby/client
In directory dev.open-bio.org:/tmp/cvs-serv17658/Java/src/main/org/biomoby/client

Modified Files:
	CentralDigestCachedImpl.java 
Log Message:
modified the caching mechanism to use RDF instead of several calls to the api
moby-live/Java/src/main/org/biomoby/client CentralDigestCachedImpl.java,1.19,1.20
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/CentralDigestCachedImpl.java,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -r1.19 -r1.20
--- /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/CentralDigestCachedImpl.java	2007/05/29 03:51:46	1.19
+++ /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/CentralDigestCachedImpl.java	2008/02/20 16:58:51	1.20
@@ -8,116 +8,182 @@
 
 package org.biomoby.client;
 
-import org.biomoby.shared.CentralAll;
-import org.biomoby.shared.MobyDataType;
-import org.biomoby.shared.MobyNamespace;
-import org.biomoby.shared.MobyException;
-import org.biomoby.shared.MobyService;
-import org.biomoby.shared.MobyServiceType;
-import org.biomoby.shared.NoSuccessException;
-import org.biomoby.shared.Utils;
-
 import java.io.BufferedOutputStream;
 import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.FileReader;
 import java.io.IOException;
+import java.io.InputStreamReader;
 import java.io.PrintWriter;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Map;
-import java.util.HashSet;
-import java.util.HashMap;
+import java.util.Properties;
 import java.util.TreeMap;
 import java.util.Vector;
-import java.util.Arrays;
-import java.util.Comparator;
-import java.util.Properties;
-import java.util.Date;
+
+import org.biomoby.client.rdf.builder.ServiceInstanceRDF;
+import org.biomoby.registry.meta.Registry;
+import org.biomoby.shared.CentralAll;
+import org.biomoby.shared.MobyDataType;
+import org.biomoby.shared.MobyException;
+import org.biomoby.shared.MobyNamespace;
+import org.biomoby.shared.MobyResourceRef;
+import org.biomoby.shared.MobyService;
+import org.biomoby.shared.MobyServiceType;
+import org.biomoby.shared.Utils;
+import org.biomoby.shared.extended.ServiceInstanceParser;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+
+import com.hp.hpl.jena.rdf.model.Model;
+import com.hp.hpl.jena.rdf.model.ModelFactory;
 
 /**
- * An implementation of {@link org.biomoby.shared.CentralAll},
- * allowing to cache locally results of the cumulative methods so it
- * does not need to access Moby registry all the time. The other
- * methods of the Central interface do not use the results of the
- * cached cumulative results (their implementation is just passed to
- * the parent class). <p>
- *
- * The caching is done in the file system, not in memory, so the
- * results are permanent (until someone removes the caching
- * directory, or calls {@link #removeFromCache}). <p>
-
- * This class can be used also without caching - just instantiate it
- * with 'cacheDir' set to null in the constructor. <p>
- *
+ * An implementation of {@link org.biomoby.shared.CentralAll}, allowing to
+ * cache locally results of the cumulative methods so it does not need to access
+ * Moby registry all the time. The other methods of the Central interface do not
+ * use the results of the cached cumulative results (their implementation is
+ * just passed to the parent class).
+ * <p>
+ * 
+ * The caching is done in the file system, not in memory, so the results are
+ * permanent (until someone removes the caching directory, or calls
+ * {@link #removeFromCache}).
+ * <p>
+ * 
+ * This class can be used also without caching - just instantiate it with
+ * 'cacheDir' set to null in the constructor.
+ * <p>
+ * 
  * @author <A HREF="mailto:martin.senger at gmail.com">Martin Senger</A>
- * @version $Id$
+ * @version $Id: CentralDigestCachedImpl.java,v 1.19 2007/05/29 03:51:46 senger
+ *          Exp $
  */
+ at SuppressWarnings("unchecked")
+public class CentralDigestCachedImpl extends CentralDigestImpl implements
+	CentralAll {
 
-public class CentralDigestCachedImpl
-    extends CentralDigestImpl
-    implements CentralAll {
-
-    private static org.apache.commons.logging.Log log =
-       org.apache.commons.logging.LogFactory.getLog (CentralDigestCachedImpl.class);
+    private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
+	    .getLog(CentralDigestCachedImpl.class);
 
     // filename for a list of cached entities
     protected static final String LIST_FILE = "__L__I__S__T__";
 
+    protected static final String RDF_FILE = "__R__D__F__";
+
     /** An ID used in {@link #removeFromCache} indicating data types part. */
-    public static final String CACHE_PART_DATATYPES    = "c1";
+    public static final String CACHE_PART_DATATYPES = "c1";
 
     /** An ID used in {@link #removeFromCache} indicating services part. */
-    public static final String CACHE_PART_SERVICES     = "c2";
+    public static final String CACHE_PART_SERVICES = "c2";
 
     /** An ID used in {@link #removeFromCache} indicating service types part. */
     public static final String CACHE_PART_SERVICETYPES = "c3";
 
     /** An ID used in {@link #removeFromCache} indicating namespaces part. */
-    public static final String CACHE_PART_NAMESPACES   = "c4";
+    public static final String CACHE_PART_NAMESPACES = "c4";
 
     // TBD: this will go to cache interface later, probably
     // names of properties returned by getCacheInfo()
-    public static final String CACHE_PROP_NAME         = "cache-name";
-    public static final String CACHE_PROP_COUNT        = "cache-count";
-    public static final String CACHE_PROP_OLDEST       = "cache-oldest";
-    public static final String CACHE_PROP_YOUNGEST     = "cache-youngest";
-    public static final String CACHE_PROP_SIZE         = "cache-size";
-    public static final String CACHE_PROP_LOCATION     = "cache-loc";
+    public static final String CACHE_PROP_NAME = "cache-name";
+
+    public static final String CACHE_PROP_COUNT = "cache-count";
+
+    public static final String CACHE_PROP_OLDEST = "cache-oldest";
+
+    public static final String CACHE_PROP_YOUNGEST = "cache-youngest";
+
+    public static final String CACHE_PROP_SIZE = "cache-size";
+
+    public static final String CACHE_PROP_LOCATION = "cache-loc";
+
     public static final String CACHE_PROP_REGISTRY_URL = "cache-reg-url";
 
     // cache location
-    private String cacheDir;        // as defined in the constructor
+    private String cacheDir; // as defined in the constructor
+
     protected File dataTypesCache;
+
     protected File servicesCache;
+
     protected File namespacesCache;
+
     protected File serviceTypesCache;
 
     // for optimalization
     private String fileSeparator;
 
-    /*************************************************************************
-     * Create an instance that will access a default Moby registry and
-     * will cache results in the 'cacheDir' directory. <p>
-     *************************************************************************/
-    public CentralDigestCachedImpl (String cacheDir)
-	throws MobyException {
-	this (null, null, cacheDir);
-    }
-
-    /*************************************************************************
-     * Create an instance that will access a Moby registry defined by
-     * its 'endpoint' and 'namespace', and will cache results in the
-     * 'cacheDir' directory. Note that the same 'cacheDir' can be
-     * safely used for more Moby registries. <p>
-     *************************************************************************/
-    public CentralDigestCachedImpl (String endpoint, String namespace, String cacheDir)
-	throws MobyException {
-	super (endpoint, namespace);
+    private String datatypesResourceRef = null;
+
+    private String servicesResourceRef = null;
+
+    private String namespacesResourceRef = null;
+
+    private String serviceTypesResourceRef = null;
+
+    private Registry reg = null;
+
+    private boolean datatypesLoaded = false;
+
+    private boolean servicesLoaded = false;
 
-	fileSeparator = System.getProperty ("file.separator");
+    private boolean namespacesLoaded = false;
+
+    private boolean serviceTypesLoaded = false;
+
+    /***************************************************************************
+     * Create an instance that will access a default Moby registry and will
+     * cache results in the 'cacheDir' directory.
+     * <p>
+     **************************************************************************/
+    public CentralDigestCachedImpl(String cacheDir) throws MobyException {
+	this(null, null, cacheDir);
+    }
+
+    /***************************************************************************
+     * Create an instance that will access a Moby registry defined by its
+     * 'endpoint' and 'namespace', and will cache results in the 'cacheDir'
+     * directory. Note that the same 'cacheDir' can be safely used for more Moby
+     * registries.
+     * <p>
+     **************************************************************************/
+    public CentralDigestCachedImpl(String endpoint, String namespace,
+	    String cacheDir) throws MobyException {
+	super(endpoint, namespace);
+
+	fileSeparator = System.getProperty("file.separator");
 	this.cacheDir = cacheDir;
 	initCache();
+
+	MobyResourceRef[] refs = getResourceRefs();
+	for (MobyResourceRef ref : refs) {
+	    if (ref.getResourceName().equals(DATA_TYPES_RESOURCE_NAME)) {
+		datatypesResourceRef = ref.getResourceLocation().toString();
+	    } else if (ref.getResourceName().equals(
+		    SERVICE_INSTANCES_RESOURCE_NAME)) {
+		servicesResourceRef = ref.getResourceLocation().toString();
+	    } else if (ref.getResourceName().equals(NAMESPACES_RESOURCE_NAME)) {
+		namespacesResourceRef = ref.getResourceLocation().toString();
+	    } else if (ref.getResourceName()
+		    .equals(SERVICE_TYPES_RESOURCE_NAME)) {
+		serviceTypesResourceRef = ref.getResourceLocation().toString();
+	    }
+	}
+	reg = new Registry(getRegistryEndpoint(), getRegistryEndpoint(),
+		getRegistryNamespace());
+
     }
 
     // it makes all necessary directories for cache given in the
@@ -125,121 +191,131 @@
     // separated here because it can be called either from the
     // constructor, or everytime a cache is going to be used but it is
     // not there (somebody removed it)
-    protected void initCache()
-	throws MobyException {
+    protected void initCache() throws MobyException {
 	if (cacheDir != null) {
-	    File cache = createCacheDir (cacheDir, getRegistryEndpoint());
-	    dataTypesCache = createSubCacheDir (cache, "dataTypes");
-	    servicesCache  = createSubCacheDir (cache, "services");
-	    namespacesCache = createSubCacheDir (cache, "namespaces");
-	    serviceTypesCache = createSubCacheDir (cache, "serviceTypes");
+	    File cache = createCacheDir(cacheDir, getRegistryEndpoint());
+	    dataTypesCache = createSubCacheDir(cache, "dataTypes");
+	    servicesCache = createSubCacheDir(cache, "services");
+	    namespacesCache = createSubCacheDir(cache, "namespaces");
+	    serviceTypesCache = createSubCacheDir(cache, "serviceTypes");
 	}
     }
 
-    /**************************************************************************
-     * Return a directory name representing the current cache. This is
-     * the same name as given in constructors. <p>
-     *
+    /***************************************************************************
+     * Return a directory name representing the current cache. This is the same
+     * name as given in constructors.
+     * <p>
+     * 
      * @return current cache directory name
      **************************************************************************/
     public String getCacheDir() {
 	return cacheDir;
     }
 
-    /**************************************************************************
-     * Removes object groups from the cache. If 'id' is null it
-     * removes the whole cache (for that Moby registry this instance
-     * was initiated for). Otherwise 'id' indicates which part of the
-     * cache that will be removed. <p>
-     *
-     * @param id should be either null, or one of the following:
-     * {@link #CACHE_PART_DATATYPES}, {@link #CACHE_PART_SERVICES},
-     * {@link #CACHE_PART_SERVICETYPES}, and {@link
-     * #CACHE_PART_NAMESPACES}.
+    /***************************************************************************
+     * Removes object groups from the cache. If 'id' is null it removes the
+     * whole cache (for that Moby registry this instance was initiated for).
+     * Otherwise 'id' indicates which part of the cache that will be removed.
+     * <p>
+     * 
+     * @param id
+     *                should be either null, or one of the following:
+     *                {@link #CACHE_PART_DATATYPES},
+     *                {@link #CACHE_PART_SERVICES},
+     *                {@link #CACHE_PART_SERVICETYPES}, and {@link
+     *                #CACHE_PART_NAMESPACES}.
      **************************************************************************/
-    public void removeFromCache (String id) {
+    public void removeFromCache(String id) {
 	try {
 	    if (cacheDir != null) {
 		String[] parts = null;
 		if (id == null)
-		    parts = new String[] { "dataTypes", "services", "serviceTypes", "namespaces" };
-		else if (id.equals (CACHE_PART_SERVICES))
+		    parts = new String[] { "dataTypes", "services",
+			    "serviceTypes", "namespaces" };
+		else if (id.equals(CACHE_PART_SERVICES))
 		    parts = new String[] { "services" };
-		else if (id.equals (CACHE_PART_DATATYPES))
+		else if (id.equals(CACHE_PART_DATATYPES))
 		    parts = new String[] { "dataTypes" };
-		else if (id.equals (CACHE_PART_SERVICETYPES))
+		else if (id.equals(CACHE_PART_SERVICETYPES))
 		    parts = new String[] { "serviceTypes" };
-		else if (id.equals (CACHE_PART_NAMESPACES))
+		else if (id.equals(CACHE_PART_NAMESPACES))
 		    parts = new String[] { "namespaces" };
 		if (parts != null) {
-		    removeCacheDir (cacheDir, getRegistryEndpoint(), parts);
+		    removeCacheDir(cacheDir, getRegistryEndpoint(), parts);
 		}
 	    }
 	} catch (MobyException e) {
-	    log.error ("Removing cache failed: " + e.getMessage());
+	    log.error("Removing cache failed: " + e.getMessage());
 	}
     }
 
-    /*************************************************************************
-     * Update the indicated part of the cache. If 'id' is null it
-     * updates the whole cache (for that Moby registry this instance
-     * was initiated for). <p>
-     *
-     * Updates means to fetch a new list of entities, compare it with
-     * existing entities in the cache, fetch the missing ones and
-     * remove the redundant ones. <p>
-     *
-     * @param id should be either null, or one of the following:
-     * {@link #CACHE_PART_DATATYPES}, {@link #CACHE_PART_SERVICES},
-     * {@link #CACHE_PART_SERVICETYPES}, and {@link
-     * #CACHE_PART_NAMESPACES}.
-     *
-     *************************************************************************/
-    public void updateCache (String id)
-	throws MobyException {
+    /***************************************************************************
+     * Update the indicated part of the cache. If 'id' is null it updates the
+     * whole cache (for that Moby registry this instance was initiated for).
+     * <p>
+     * 
+     * Updates means to fetch a new list of entities, compare it with existing
+     * entities in the cache, fetch the missing ones and remove the redundant
+     * ones.
+     * <p>
+     * 
+     * @param id
+     *                should be either null, or one of the following:
+     *                {@link #CACHE_PART_DATATYPES},
+     *                {@link #CACHE_PART_SERVICES},
+     *                {@link #CACHE_PART_SERVICETYPES}, and {@link
+     *                #CACHE_PART_NAMESPACES}.
+     * 
+     **************************************************************************/
+    public void updateCache(String id) throws MobyException {
 	if (cacheDir != null) {
 	    initCache();
-	    if (id == null || id.equals (CACHE_PART_SERVICES)) {
-// 		remove (servicesCache, LIST_FILE);
+	    if (id == null || id.equals(CACHE_PART_SERVICES)) {
+		// remove (servicesCache, LIST_FILE);
 		fillServicesCache();
-	    } else if (id == null || id.equals (CACHE_PART_DATATYPES)) {
-// 		remove (dataTypesCache, LIST_FILE);
+	    } else if (id == null || id.equals(CACHE_PART_DATATYPES)) {
+		// remove (dataTypesCache, LIST_FILE);
 		fillDataTypesCache();
-	    } else if (id == null || id.equals (CACHE_PART_SERVICETYPES)) {
-// 		remove (serviceTypesCache, LIST_FILE);
+	    } else if (id == null || id.equals(CACHE_PART_SERVICETYPES)) {
+		// remove (serviceTypesCache, LIST_FILE);
 		fillServiceTypesCache();
-	    } else if (id == null || id.equals (CACHE_PART_NAMESPACES)) {
+	    } else if (id == null || id.equals(CACHE_PART_NAMESPACES)) {
 		fillNamespacesCache();
 	    }
 	}
     }
 
-
     /**
      * Create a cache directory from 'cacheDirectory' and 'registryId' if it
-     * does not exist yet. Make sure that it is writable. Return a
-     * File representing created directory.
-     *
-     * 'registryId' (which may be null) denotes what registry this
-     * cache is going to be created for. If null, an endpoint of a
-     * default Moby registry is used.
+     * does not exist yet. Make sure that it is writable. Return a File
+     * representing created directory.
+     * 
+     * 'registryId' (which may be null) denotes what registry this cache is
+     * going to be created for. If null, an endpoint of a default Moby registry
+     * is used.
      */
-    protected File createCacheDir (String cacheDirectory, String registryId)
-	throws MobyException {
-	if (registryId == null || registryId.equals (""))
-	    registryId = CentralImpl.DEFAULT_ENDPOINT;
-	File cache = new File (cacheDirectory + fileSeparator + clean (registryId));
+    protected File createCacheDir(String cacheDirectory, String registryId)
+	    throws MobyException {
+	if (registryId == null || registryId.equals(""))
+	    registryId = CentralImpl.getDefaultURL();
+	File cache = new File(cacheDirectory + fileSeparator
+		+ clean(registryId));
 	try {
-	    if (! cache.exists())
-		if (! cache.mkdirs())
-		    throw new MobyException ("Cannot create '" + cache.getAbsolutePath() + "'.");
-	    if (! cache.isDirectory())
-		throw new MobyException ("Cache location '" + cache.getAbsolutePath() + "' exists but it is not a directory.");
-	    if (! cache.canWrite())
-		throw new MobyException ("Cache location '" + cache.getAbsolutePath() + "' is not writable for me.");
+	    if (!cache.exists())
+		if (!cache.mkdirs())
+		    throw new MobyException("Cannot create '"
+			    + cache.getAbsolutePath() + "'.");
+	    if (!cache.isDirectory())
+		throw new MobyException("Cache location '"
+			+ cache.getAbsolutePath()
+			+ "' exists but it is not a directory.");
+	    if (!cache.canWrite())
+		throw new MobyException("Cache location '"
+			+ cache.getAbsolutePath() + "' is not writable for me.");
 	    return cache;
 	} catch (SecurityException e) {
-	    throw new MobyException ("Cannot handle cache location '" + cache.getAbsolutePath() + "'. " + e.toString());
+	    throw new MobyException("Cannot handle cache location '"
+		    + cache.getAbsolutePath() + "'. " + e.toString());
 	}
     }
 
@@ -247,298 +323,393 @@
      * Remove cache and all (but given in 'subCacheDirNames') its
      * subdirectories.
      */
-    protected void removeCacheDir (String cacheDirectory,
-        String registryId,
-        String[] subCacheDirNames)
-	throws MobyException {
-	if (registryId == null || registryId.equals (""))
-	    registryId = CentralImpl.DEFAULT_ENDPOINT;
-	File cache = new File (cacheDirectory + fileSeparator + clean (registryId));
+    protected void removeCacheDir(String cacheDirectory, String registryId,
+	    String[] subCacheDirNames) throws MobyException {
+	if (registryId == null || registryId.equals(""))
+	    registryId = CentralImpl.getDefaultURL();
+	File cache = new File(cacheDirectory + fileSeparator
+		+ clean(registryId));
 	try {
-	    if (! cache.exists()) return;
-	    if (! cache.isDirectory())
-		throw new MobyException ("Cache location '" + cache.getAbsolutePath() + "' exists but it is not a directory.");
-	    if (! cache.canWrite())
-		throw new MobyException ("Cache location '" + cache.getAbsolutePath() + "' is not writable for me.");
+	    if (!cache.exists())
+		return;
+	    if (!cache.isDirectory())
+		throw new MobyException("Cache location '"
+			+ cache.getAbsolutePath()
+			+ "' exists but it is not a directory.");
+	    if (!cache.canWrite())
+		throw new MobyException("Cache location '"
+			+ cache.getAbsolutePath() + "' is not writable for me.");
 	    for (int i = 0; i < subCacheDirNames.length; i++) {
-		File cacheSubDir = new File (cache.getAbsolutePath() + fileSeparator + clean (subCacheDirNames[i]));
+		File cacheSubDir = new File(cache.getAbsolutePath()
+			+ fileSeparator + clean(subCacheDirNames[i]));
 		File[] files = cacheSubDir.listFiles();
 		for (int f = 0; f < files.length; f++) {
 		    if (files[f].isDirectory())
-			throw new MobyException ("Found a directory '" + files[f].getAbsolutePath() + "' where no directory should be");
-		    if (! files[f].delete())
-		    	log.error ("Can't delete file '" + files[f] + "'.");
+			throw new MobyException("Found a directory '"
+				+ files[f].getAbsolutePath()
+				+ "' where no directory should be");
+		    if (!files[f].delete())
+			log.error("Can't delete file '" + files[f] + "'.");
 		}
 		cacheSubDir.delete();
 	    }
 	    cache.delete();
 
 	} catch (SecurityException e) {
-	    throw new MobyException ("Cannot handle cache location '" + cache.getAbsolutePath() + "'. " + e.toString());
+	    throw new MobyException("Cannot handle cache location '"
+		    + cache.getAbsolutePath() + "'. " + e.toString());
 	}
     }
 
     //
-    protected File createSubCacheDir (File mainCache, String subCacheDirName)
-	throws MobyException {
-	File cache = new File (mainCache.getAbsolutePath() + fileSeparator + clean (subCacheDirName));
+    protected File createSubCacheDir(File mainCache, String subCacheDirName)
+	    throws MobyException {
+	File cache = new File(mainCache.getAbsolutePath() + fileSeparator
+		+ clean(subCacheDirName));
 	try {
-	    if (! cache.exists())
-		if (! cache.mkdirs())
-		    throw new MobyException ("Cannot create '" + cache.getAbsolutePath() + "'.");
+	    if (!cache.exists())
+		if (!cache.mkdirs())
+		    throw new MobyException("Cannot create '"
+			    + cache.getAbsolutePath() + "'.");
 	    return cache;
 	} catch (SecurityException e) {
-	    throw new MobyException ("Cannot handle cache location '" + cache.getAbsolutePath() + "'. " + e.toString());
+	    throw new MobyException("Cannot handle cache location '"
+		    + cache.getAbsolutePath() + "'. " + e.toString());
 	}
     }
-    
-    /**************************************************************************
-     * Replace non digit/letter characters in 'toBeCleaned' by their
-     * numeric value. If there are more such numeric values side by
-     * side, put a dot between them. Return the cleaned string.
+
+    /***************************************************************************
+     * Replace non digit/letter characters in 'toBeCleaned' by their numeric
+     * value. If there are more such numeric values side by side, put a dot
+     * between them. Return the cleaned string.
      **************************************************************************/
-    protected static String clean (String toBeCleaned) {
+    protected static String clean(String toBeCleaned) {
 
 	char[] chars = toBeCleaned.toCharArray();
 	int len = chars.length;
 	int i = -1;
 	while (++i < len) {
 	    char c = chars[i];
-	    if (!Character.isLetterOrDigit (c) && c != '_')
+	    if (!Character.isLetterOrDigit(c) && c != '_')
 		break;
 	}
 	if (i < len) {
-	    StringBuffer buf = new StringBuffer (len*2);
-	    for (int j = 0 ; j < i ; j++) {
-		buf.append (chars[j]);
+	    StringBuffer buf = new StringBuffer(len * 2);
+	    for (int j = 0; j < i; j++) {
+		buf.append(chars[j]);
 	    }
 	    boolean lastOneWasDigitalized = false;
 	    while (i < len) {
 		char c = chars[i];
-		if (Character.isLetterOrDigit (c) || c == '_') {
-		    buf.append (c);
+		if (Character.isLetterOrDigit(c) || c == '_') {
+		    buf.append(c);
 		    lastOneWasDigitalized = false;
 		} else {
 		    if (lastOneWasDigitalized)
-			buf.append ('.');
-		    buf.append ((int)c);
+			buf.append('.');
+		    buf.append((int) c);
 		    lastOneWasDigitalized = true;
 		}
 		i++;
 	    }
-	    return new String (buf);
+	    return new String(buf);
 	}
 	return toBeCleaned;
     }
-    
+
     // create a file and put into it data to be cached
-    protected void store (File cache, String name, String data)
-	throws MobyException {
-// 	File outputFile = new File (cache.getAbsolutePath() + fileSeparator + clean (name));
-	File outputFile = new File (cache.getAbsolutePath() + fileSeparator + name);
+    protected void store(File cache, String name, String data)
+	    throws MobyException {
+	// File outputFile = new File (cache.getAbsolutePath() + fileSeparator +
+	// clean (name));
+	File outputFile = new File(cache.getAbsolutePath() + fileSeparator
+		+ name);
 	try {
-	    PrintWriter fileout =
-		new PrintWriter (new BufferedOutputStream (new FileOutputStream (outputFile)));
-	    fileout.write (data);
+	    PrintWriter fileout = new PrintWriter(new BufferedOutputStream(
+		    new FileOutputStream(outputFile)));
+	    fileout.write(data);
 	    fileout.close();
 	} catch (IOException e) {
-	    throw new MobyException ("Cannot write to '" + outputFile.getAbsolutePath() + ". " + e.toString());
+	    throw new MobyException("Cannot write to '"
+		    + outputFile.getAbsolutePath() + ". " + e.toString());
 	}
     }
 
     // remove a file from a cache
-    protected void remove (File cache, String name) {
-	File file = new File (cache, name);
+    protected void remove(File cache, String name) {
+	File file = new File(cache, name);
 	// do not throw here an exception because a missing file
 	// can be a legitimate status (e.g. for LIST_FILE when we
 	// are updating)
 	file.delete();
     }
 
-    /**************************************************************************
+    /***************************************************************************
      * Read a cached file
-     *************************************************************************/
-    protected static String load (File file)
-	throws MobyException {
+     **************************************************************************/
+    protected static String load(File file) throws MobyException {
 	try {
 	    StringBuffer buf = new StringBuffer();
-	    BufferedReader in
-		= new BufferedReader (new FileReader (file));
+	    BufferedReader in = new BufferedReader(new FileReader(file));
 	    char[] buffer = new char[1024];
 	    int charsRead;
 
-	    while ((charsRead = in.read (buffer, 0, 1024)) != -1) {
-		buf.append (buffer, 0, charsRead);
+	    while ((charsRead = in.read(buffer, 0, 1024)) != -1) {
+		buf.append(buffer, 0, charsRead);
 	    }
 
-            return new String (buf);
+	    return new String(buf);
 
-	} catch (Throwable e) {     // be prepare for "out-of-memory" error
-	    throw new MobyException ("Serious error when reading from cache. " + e.toString());
+	} catch (Throwable e) { // be prepare for "out-of-memory" error
+	    throw new MobyException("Serious error when reading from cache. "
+		    + e.toString());
 
 	}
     }
 
-    /**************************************************************************
-     * Is the given cache empty (meaning: cache directory does not
-     * exist, is empty, or contains only files to be ignored)?
-     *************************************************************************/
-    protected boolean isCacheEmpty (File cache)
-	throws MobyException {
-	if (cache == null) return true;
+    /***************************************************************************
+     * Is the given cache empty (meaning: cache directory does not exist, is
+     * empty, or contains only files to be ignored)?
+     **************************************************************************/
+    protected boolean isCacheEmpty(File cache) throws MobyException {
+	if (cache == null)
+	    return true;
 	String[] list = cache.list();
 	if (list == null || list.length == 0)
 	    return true;
 	for (int i = 0; i < list.length; i++) {
-	    if ( ! ignoredForEmptiness (new File (list[i])) )
+	    if (!ignoredForEmptiness(new File(list[i])))
 		return false;
 	}
 	return true;
     }
 
-    /**************************************************************************
-     * Update data types from a moby registry:
-     *   - get a new LIST_FILE (but do not put it into the cache yet)
-     *       if failed do nothing (except reporting it)
-     *   - remove LIST_FILE
-     *   - compare contents of new LIST_FILE with file names in the cache
-     *     and remove them, or fetched missing ones
-     *       if success add there new LIST_FILE
-     *************************************************************************/
-    protected boolean fillDataTypesCache()
-	throws MobyException {
+    /***************************************************************************
+     * Update data types from a moby registry: - get a new LIST_FILE (but do not
+     * put it into the cache yet) if failed do nothing (except reporting it) -
+     * remove LIST_FILE - compare contents of new LIST_FILE with file names in
+     * the cache and remove them, or fetched missing ones if success add there
+     * new LIST_FILE
+     **************************************************************************/
+
+    protected boolean fillDataTypesCache() throws MobyException {
 	try {
-	    fireEvent (DATA_TYPES_START);
+	    fireEvent(DATA_TYPES_START);
+	    // XML from API
 	    String typesAsXML = getDataTypeNamesAsXML();
 
 	    // get a list file with all data type names currently in
 	    // the cache...
-	    Map cachedTypes = new HashMap();
-	    String xmlList = getListFile (dataTypesCache);
+	    Map<String, MobyDataType> cachedTypes = new HashMap<String, MobyDataType>();
+	    // XML from Cache
+	    String xmlList = getListFile(dataTypesCache);
 	    if (xmlList != null)
-		cachedTypes = createDataTypeNamesFromXML (xmlList, false);
+		cachedTypes = createDataTypeNamesFromXML(xmlList, false);
 
 	    // ...and remove it
- 	    remove (dataTypesCache, LIST_FILE);
+	    remove(dataTypesCache, LIST_FILE);
 
 	    // get a list file with all data types from the registry
-	    Map types = createDataTypeNamesFromXML (typesAsXML, false);
-	    fireEvent (DATA_TYPES_COUNT, new Integer (types.size()));
-
-	    // list of current files in this cache
-	    HashSet currentFiles = new HashSet();
-	    File[] list = dataTypesCache.listFiles();
-	    if (list == null)
-		throw new MobyException (MSG_CACHE_NOT_DIR (dataTypesCache));
-	    for (int i = 0; i < list.length; i++) {
-		if (! ignored (list[i]))
-		    currentFiles.add (list[i].getName());
-	    }
-
-	    // iterate over LIST_FILE and fetch missing files
-	    for (Iterator it = types.entrySet().iterator(); it.hasNext(); ) {
-		Map.Entry entry = (Map.Entry)it.next();
-		boolean needToFetch = false;
-		String name = (String)entry.getKey();
-		if ( ! currentFiles.contains (name)) {
-		    // missing file
-		    needToFetch = true;
-		} else {
-		    // check by comparing LSIDs
-		    MobyDataType dt = (MobyDataType)entry.getValue();
-		    String lsid = dt.getLSID();
-		    if (cachedTypes.containsKey (name)) {
-			// should always go here - or we have a broken cache, anyway
-			String cachedLSID =
-			    ( (MobyDataType)cachedTypes.get (name) ).getLSID();
-			if (! lsid.equals (cachedLSID)) {
-			    needToFetch = true;
-			}
-		    } else {
-			needToFetch = true;
+	    // map of <name, dt>
+	    Map<String, MobyDataType> types = createDataTypeNamesFromXML(
+		    typesAsXML, false);
+	    fireEvent(DATA_TYPES_COUNT, new Integer(types.size()));
+
+	    // check if cachedTypes and types have the same information ...
+	    // datatypes, etc
+	    boolean isStale = cachedTypes.size() != types.size();
+	    if (!isStale) {
+		for (String name : types.keySet()) {
+		    // does the cache have the datatype?
+		    if (!cachedTypes.containsKey(name)) {
+			isStale = true;
+			break;
 		    }
-		}
-		if (needToFetch) {
-		    // missing file: fetch it from a registry
-		    fireEvent (DATA_TYPE_LOADING, name);
-		    String xml = getDataTypeAsXML (name);
-		    store (dataTypesCache, name, xml);
-		    fireEvent (DATA_TYPE_LOADED, name);
-		    if (stopDT) {
-			return false;
+		    // are the lsids the same?
+		    MobyDataType md = cachedTypes.remove(name);
+		    if (!md.getLSID().trim().equals(
+			    types.get(name).getLSID().trim())) {
+			isStale = true;
+			break;
 		    }
 		}
-		currentFiles.remove (name);
+		// we iterated over all of the types ... are there any in the
+		// cache that arent in the registry?
+		if (!isStale && cachedTypes.size() > 0) {
+		    isStale = true;
+		}
 	    }
 
-	    // remove files that are not any more needed
-	    for (Iterator it = currentFiles.iterator(); it.hasNext(); )
-		remove (dataTypesCache, (String)it.next());
+	    // if we are stale, fetch new RDF
+	    fireEvent(DATA_TYPE_LOADING, DATA_TYPES_RESOURCE_NAME
+		    + " RDF downloading");
+	    // make sure that the RDF file exists before we we try to read it
+	    // below
+	    try {
+		load(new File(serviceTypesCache, RDF_FILE));
+	    } catch (Exception e) {
+		isStale = true;
+	    }
+	    if (isStale) {
+		// store the RDF_FILE
+		String rdf = getUrlAsString(datatypesResourceRef);
+		store(dataTypesCache, RDF_FILE, rdf.toString());
+	    }
+	    fireEvent(DATA_TYPE_LOADED, DATA_TYPES_RESOURCE_NAME
+		    + " RDF download");
+	    // load the RDF into memory
+	    fireEvent(DATA_TYPE_LOADING, DATA_TYPES_RESOURCE_NAME
+		    + " RDF parsing");
+	    MobyDataType.loadDataTypes(new URL("file:///"
+		    + dataTypesCache.getAbsolutePath() + fileSeparator
+		    + RDF_FILE), reg);
+	    // set the flag that datatypes have been loaded
+	    setDatatypesLoaded(true);
+	    // fire an event to say the datatypes have been loaded
+	    fireEvent(DATA_TYPE_LOADED, DATA_TYPES_RESOURCE_NAME
+		    + " RDF parsing");
 
-	    // finally, put there the new LIST_FILE
- 	    store (dataTypesCache, LIST_FILE, typesAsXML);
+	    // finally, store the the new LIST_FILE
+	    store(dataTypesCache, LIST_FILE, typesAsXML);
 	    return true;
 
 	} catch (Exception e) {
-	    throw new MobyException (formatException (e), e);
+	    throw new MobyException(formatException(e), e);
 	} finally {
-	    fireEvent (stopDT ? DATA_TYPES_CANCELLED : DATA_TYPES_END);
+	    fireEvent(stopDT ? DATA_TYPES_CANCELLED : DATA_TYPES_END);
 	    stopDT = false;
 	}
     }
 
-    /**************************************************************************
-     * Update services from a moby registry:
-     *   - get a new LIST_FILE (but do not put it into the cache yet)
-     *       if failed do nothing (except reporting it)
-     *   - remove LIST_FILE
-     *   - compare contents of new LIST_FILE with file names in the cache
-     *     and remove them, or fetched missing ones;
-     *     in order to compare properly you need to read individual files
-     *     and look if they really contain all services mentioned in the
-     *     LIST_FILE
-     *       if success add there new LIST_FILE
-     *************************************************************************/
-    protected boolean fillServicesCache()
-	throws MobyException {
+    /**
+     * @return a string of text as obtained from the url
+     * @throws MalformedURLException
+     * @throws IOException
+     */
+    private String getUrlAsString(String url) throws MalformedURLException,
+	    IOException {
+	StringBuilder rdf = new StringBuilder();
+	String line = null;
+	URL rdf_url = new URL(url);
+	HttpURLConnection urlConnection = null;
+	urlConnection = (HttpURLConnection) rdf_url.openConnection();
+	urlConnection.setDefaultUseCaches(false);
+	urlConnection.setUseCaches(false);
+	urlConnection.setRequestProperty("User-agent",
+		"jMoby_CentralDigest/1.19");
+	urlConnection.setConnectTimeout(1000 * 60 * 5);
+
+	BufferedReader br = new BufferedReader(new InputStreamReader(
+		urlConnection.getInputStream()));
+	String newline = System.getProperty("line.separator");
+	while ((line = br.readLine()) != null) {
+	    rdf.append(line + newline);
+	}
+	return rdf.toString();
+    }
+
+    /***************************************************************************
+     * Update services from a moby registry: - get a new LIST_FILE (but do not
+     * put it into the cache yet) if failed do nothing (except reporting it) -
+     * remove LIST_FILE - compare contents of new LIST_FILE with file names in
+     * the cache and remove them, or fetched missing ones; in order to compare
+     * properly you need to read individual files and look if they really
+     * contain all services mentioned in the LIST_FILE if success add there new
+     * LIST_FILE
+     **************************************************************************/
+    protected boolean fillServicesCache() throws MobyException {
 	try {
-	    fireEvent (AUTHORITIES_START);
+	    fireEvent(AUTHORITIES_START);
+
+	    // check if RDF file exists ...
+	    if (!rdfExists(servicesCache)) {
+		try {
+		    // TODO add the fireevent calls ...
+		    // download the file, save it, then parse it
+		    store(servicesCache, RDF_FILE,
+			    getUrlAsString(servicesResourceRef));
+		    // extract the services
+		    ServiceInstanceParser sip = new ServiceInstanceParser(
+			    "file:///" + servicesCache.getAbsolutePath()
+				    + fileSeparator + RDF_FILE);
+		    MobyService[] services = sip.getMobyServicesFromRDF();
+		    // sort the services
+		    Map<String, ArrayList<MobyService>> sorted = new HashMap<String, ArrayList<MobyService>>();
+		    for (MobyService s : services) {
+			ArrayList<MobyService> list = (sorted.containsKey(s
+				.getAuthority()) ? sorted.remove(s
+				.getAuthority()) : new ArrayList<MobyService>());
+			list.add(s);
+			sorted.put(s.getAuthority(), list);
+		    }
+		    // free memory
+		    services = null;
+		    // for each authority, create an rdf file
+		    ServiceInstanceRDF sRdf = new ServiceInstanceRDF(reg);
+		    for (String authURI : sorted.keySet()) {
+			String s = sRdf.serializeModel(sRdf.createRDFModel(
+				null, sorted.get(authURI).toArray(
+					new MobyService[] {}), true));
+			store(servicesCache, authURI, s);
+		    }
+		    // free more memory
+		    sorted.clear();
+		    // get a list file and save it
+		    String byAuthorityAsXML = getServiceNamesByAuthorityAsXML();
+
+		    // finally, put there the new LIST_FILE
+		    store(servicesCache, LIST_FILE, byAuthorityAsXML);
+		    // store a list file
+		    return true;
+		} catch (Exception e) {
+		    log.warn("Error with Services RDF", e);
+		}
+	    }
+
+	    // RDF exists ... are there changes?
+
 	    String byAuthorityAsXML = getServiceNamesByAuthorityAsXML();
- 	    remove (servicesCache, LIST_FILE);
-	    Map authorities = createServicesByAuthorityFromXML (byAuthorityAsXML,
-								false);
+	    remove(servicesCache, LIST_FILE);
+	    Map authorities = createServicesByAuthorityFromXML(
+		    byAuthorityAsXML, false);
 	    // list of current files in this cache
-	    HashSet currentFiles = new HashSet();
+	    HashSet<String> currentFiles = new HashSet<String>();
 	    File[] list = servicesCache.listFiles();
 	    if (list == null)
-		throw new MobyException (MSG_CACHE_NOT_DIR (servicesCache));
+		throw new MobyException(MSG_CACHE_NOT_DIR(servicesCache));
 	    for (int i = 0; i < list.length; i++) {
-		if (! ignored (list[i]))
-		    currentFiles.add (list[i].getName());
+		if (!ignored(list[i]))
+		    currentFiles.add(list[i].getName());
 	    }
-
+	    // some flag that determines if we need to re-merge rdf
+	    boolean hasChanged = false;
 	    // iterate over LIST_FILE and fetch missing files
-	    fireEvent (AUTHORITIES_COUNT, new Integer (authorities.size()));
-	    for (Iterator it = authorities.entrySet().iterator(); it.hasNext(); ) {
-		Map.Entry entry = (Map.Entry)it.next();
-		String authority = (String)entry.getKey();
-		if (currentFiles.contains (authority)) {
-		    MobyService[] servs =
-			extractServices (load (new File (servicesCache, authority)));
-		    // compare names in 'servs' (those are services we have in cache)
+	    fireEvent(AUTHORITIES_COUNT, new Integer(authorities.size()));
+	    for (Iterator it = authorities.entrySet().iterator(); it.hasNext();) {
+		Map.Entry entry = (Map.Entry) it.next();
+		String authority = (String) entry.getKey();
+		if (currentFiles.contains(authority)) {
+		    MobyService[] servs = extractServices(load(new File(
+			    servicesCache, authority)));
+		    // compare names in 'servs' (those are services we have in
+		    // cache)
 		    // with names in 'entry' (those are the ones we should have)
 		    boolean theyAreEqual = true;
-		    HashMap currentServices = new HashMap (servs.length);
+		    HashMap currentServices = new HashMap(servs.length);
 		    for (int i = 0; i < servs.length; i++)
-			currentServices.put (servs[i].getName(), servs[i]);
-		    MobyService[] newServices = (MobyService[])entry.getValue();
+			currentServices.put(servs[i].getName(), servs[i]);
+		    MobyService[] newServices = (MobyService[]) entry
+			    .getValue();
 		    for (int i = 0; i < newServices.length; i++) {
 			String currName = newServices[i].getName();
-			if (currentServices.containsKey (currName)) {
-			    // check whether the old and new ones have the same LSID
-			    MobyService current = (MobyService)currentServices.get (currName);
-			    if (newServices[i].getLSID().equals (current.getLSID())) {
-				currentServices.remove (currName);
+			if (currentServices.containsKey(currName)) {
+			    // check whether the old and new ones have the same
+			    // LSID
+			    MobyService current = (MobyService) currentServices
+				    .get(currName);
+			    if (newServices[i].getLSID().equals(
+				    current.getLSID())) {
+				currentServices.remove(currName);
 			    } else {
 				theyAreEqual = false;
 			    }
@@ -549,566 +720,782 @@
 		    }
 		    if (currentServices.size() > 0)
 			theyAreEqual = false;
-		    if (! theyAreEqual)
-			currentFiles.remove (authority);
+		    if (!theyAreEqual)
+			currentFiles.remove(authority);
 		}
 
-		if (! currentFiles.contains (authority)) {
+		if (!currentFiles.contains(authority)) {
 		    // missing file: fetch it from a registry
-		    fireEvent (AUTHORITY_LOADING, authority);
-		    MobyService pattern = new MobyService (MobyService.DUMMY_NAME, authority);
-		    pattern.setCategory ("");
-		    String xml = getServicesAsXML (pattern, null, true, true);
-		    store (servicesCache, authority, xml);
-		    fireEvent (AUTHORITY_LOADED, authority);
+		    fireEvent(AUTHORITY_LOADING, authority);
+		    MobyService pattern = new MobyService(
+			    MobyService.DUMMY_NAME, authority);
+		    pattern.setCategory("");
+		    String xml = getServicesAsXML(pattern, null, true, true);
+		    MobyService[] services = extractServices(xml);
+		    ServiceInstanceRDF sRdf = new ServiceInstanceRDF(reg);
+		    xml = sRdf.serializeModel(sRdf.createRDFModel(null,
+			    services, true));
+		    store(servicesCache, authority, xml);
+		    hasChanged = true;
+		    fireEvent(AUTHORITY_LOADED, authority);
 		    if (stopS) {
 			return false;
 		    }
 		} else {
-		    currentFiles.remove (authority);
+		    currentFiles.remove(authority);
 		}
 	    }
 
 	    // remove files that are not any more needed
-	    for (Iterator it = currentFiles.iterator(); it.hasNext(); )
-		remove (servicesCache, (String)it.next());
+	    for (String it : currentFiles) {
+		log.debug("Removing the authority '" + it+"' from the cache");
+		remove(servicesCache, it);
+		hasChanged = true;
+	    }
 
+	    if (hasChanged) {
+		remergeServiceRDF();
+	    }
 	    // finally, put there the new LIST_FILE
-	    store (servicesCache, LIST_FILE, byAuthorityAsXML);
+	    store(servicesCache, LIST_FILE, byAuthorityAsXML);
 	    return true;
 
 	} catch (Exception e) {
-	    throw new MobyException (formatException (e), e);
+	    throw new MobyException(formatException(e), e);
 	} finally {
-	    fireEvent (stopS ? AUTHORITIES_CANCELLED : AUTHORITIES_END);
+	    fireEvent(stopS ? AUTHORITIES_CANCELLED : AUTHORITIES_END);
 	    stopS = false;
 	}
     }
+    /********************************************************
+     * iterate over file system and re-merge the RDF
+     ********************************************************/
+    private void remergeServiceRDF() throws MobyException {
+	ServiceInstanceRDF siRdf = new ServiceInstanceRDF(reg);
+	ServiceInstanceParser p = new ServiceInstanceParser();
+
+	File[] list = servicesCache.listFiles();
+	if (list == null)
+	    return;
+	Model m = null;
+	for (int i = 0; i < list.length; i++) {
+	    if (!ignored(list[i])) {
+		try {
+		    log.debug("loading: " + list[i].getName());
+		    p.setUrl("file:///" + servicesCache.getAbsolutePath()
+			    + fileSeparator + list[i].getName());
+		    Model model = siRdf.createRDFModel(null, p.getMobyServicesFromRDF(), true);
+		    log.debug("loaded ... " + list[i].getName());
+		    if (m == null && model != null) {
+			m = model;
+			continue;
+		    }
+		    if (model != null)
+			m.add(model);
+		} catch (Exception e) {
+		    log.warn("file: " + list[i].getName()
+			    + " contains invalid RDF ...", e);
+		}
+	    }
+	}
+	if (m != null)
+	    store(servicesCache, RDF_FILE, siRdf.serializeModel(m));
+
+    }
+
+    protected Map createServicesByAuthorityFromXML(String result)
+	    throws MobyException {
 
-    /**************************************************************************
-     * Update service types from a moby registry:
-     *   - get a new LIST_FILE (but do not put it into the cache yet)
-     *       if failed do nothing (except reporting it)
-     *   - remove LIST_FILE
-     *   - compare contents of new LIST_FILE with file names in the cache
-     *     and remove them, or fetched missing ones
-     *       if success add there new LIST_FILE
-     *************************************************************************/
-    protected boolean fillServiceTypesCache()
-	throws MobyException {
+	// parse returned XML
+	Map<String, Map<String, String>> results = new TreeMap<String, Map<String, String>>(
+		getStringComparator());
+	Document document = loadDocument(new ByteArrayInputStream(result
+		.getBytes()));
+	NodeList list = document.getElementsByTagName("serviceName");
+	for (int i = 0; i < list.getLength(); i++) {
+	    Element elem = (Element) list.item(i);
+	    String name = elem.getAttribute("name");
+	    String auth = elem.getAttribute("authURI");
+	    String lsid = elem.getAttribute("lsid");
+	    Map map = (results.containsKey(auth) ? results.get(auth)
+		    : new HashMap<String, String>());
+	    map.put(name, lsid);
+	    results.put(auth, map);
+	}
+
+	return results;
+    }
+
+    protected String[] extractServiceProviders(String xml) {
+	Document document;
 	try {
-	    fireEvent (SERVICE_TYPES_START);
+	    document = loadDocument(new ByteArrayInputStream(xml.getBytes()));
+	} catch (MobyException e) {
+	    return new String[] {};
+	}
+	NodeList list = document.getElementsByTagName("serviceName");
+	String[] results = new String[list.getLength()];
+	for (int i = 0; i < list.getLength(); i++)
+	    results[i] = ((Element) list.item(i)).getAttribute("authURI");
+	return results;
+    }
+
+    /***************************************************************************
+     * Update service types from a moby registry: - get a new LIST_FILE (but do
+     * not put it into the cache yet) if failed do nothing (except reporting it) -
+     * remove LIST_FILE - compare contents of new LIST_FILE with file names in
+     * the cache and remove them, or fetched missing ones if success add there
+     * new LIST_FILE
+     **************************************************************************/
+    protected boolean fillServiceTypesCache() throws MobyException {
+	try {
+	    fireEvent(SERVICE_TYPES_START);
+	    // XML from API
 	    String typesAsXML = getServiceTypesAsXML();
 
 	    // get a list file with all service type names currently
 	    // in the cache...
 	    MobyServiceType[] cachedList = new MobyServiceType[] {};
-	    String xmlList = getListFile (serviceTypesCache);
+	    // XML from cache
+	    String xmlList = getListFile(serviceTypesCache);
 	    if (xmlList != null)
-		cachedList = createServiceTypesFromXML (xmlList);
+		cachedList = createServiceTypesFromXML(xmlList);
 
-	    HashMap cachedTypes = new HashMap();
+	    // map of name -> service type
+	    HashMap<String, MobyServiceType> cachedTypes = new HashMap<String, MobyServiceType>();
 	    for (int i = 0; i < cachedList.length; i++) {
-		cachedTypes.put (cachedList[i].getName(), cachedList[i]);
+		cachedTypes.put(cachedList[i].getName(), cachedList[i]);
 	    }
 
 	    // ...and remove it
- 	    remove (serviceTypesCache, LIST_FILE);
+	    remove(serviceTypesCache, LIST_FILE);
 
 	    // get a list file with all service types from the
 	    // registry
-	    MobyServiceType[] types = createServiceTypesFromXML (typesAsXML);
-	    fireEvent (SERVICE_TYPES_COUNT, new Integer (types.length));
-
-	    // list of current files in this cache
-	    HashSet currentFiles = new HashSet();
-	    File[] list = serviceTypesCache.listFiles();
-	    if (list == null)
-		throw new MobyException (MSG_CACHE_NOT_DIR (serviceTypesCache));
-	    for (int i = 0; i < list.length; i++) {
-		if (! ignored (list[i]))
-		    currentFiles.add (list[i].getName());
-	    }
+	    MobyServiceType[] types = createServiceTypesFromXML(typesAsXML);
+	    fireEvent(SERVICE_TYPES_COUNT, new Integer(types.length));
 
-	    // iterate over LIST_FILE and fetch missing files
-	    for (int i = 0 ; i < types.length; i++) {
-		boolean needToFetch = false;
-		String name = types[i].getName();
-		if ( ! currentFiles.contains (name)) {
-		    // missing file
-		    needToFetch = true;
-		} else {
-		    // check by comparing LSIDs
-		    String lsid = types[i].getLSID();
-		    if (cachedTypes.containsKey (name)) {
-			// should always go here - or we have a broken cache, anyway
-			String cachedLSID =
-			    ( (MobyServiceType)cachedTypes.get (name) ).getLSID();
-			if (! lsid.equals (cachedLSID)) {
-			    needToFetch = true;
-			}
-		    } else {
-			needToFetch = true;
+	    boolean isStale = cachedTypes.size() != types.length;
+	    if (!isStale) {
+		for (MobyServiceType service : types) {
+		    String name = service.getName();
+		    // does the cache have the datatype?
+		    if (!cachedTypes.containsKey(name)) {
+			isStale = true;
+			break;
 		    }
-		}
-		if (needToFetch) {
-		    fireEvent (SERVICE_TYPE_LOADING, name);
-		    String xml = getServiceTypeRelationshipsAsXML (name, false);
-		    store (serviceTypesCache, name, xml);
-		    fireEvent (SERVICE_TYPE_LOADED, name);
-		    if (stopST) {
-			log.warn ("Service types cache not fully updated");
-			return false;
+		    // are the lsids the same?
+		    MobyServiceType md = cachedTypes.remove(name);
+		    if (!md.getLSID().trim().equals(service.getLSID().trim())) {
+			isStale = true;
+			break;
 		    }
 		}
-		currentFiles.remove (name);
+		// we iterated over all of the types ... are there any in the
+		// cache that arent in the registry?
+		if (!isStale && cachedTypes.size() > 0) {
+		    isStale = true;
+		}
 	    }
+	    fireEvent(SERVICE_TYPE_LOADING, SERVICE_TYPES_RESOURCE_NAME
+		    + " RDF downloading");
+	    // make sure that the RDF file exists before we we try to read it
+	    // below
+	    try {
+		load(new File(serviceTypesCache, RDF_FILE));
+	    } catch (Exception e) {
+		isStale = true;
+	    }
+	    // if we are stale, fetch new RDF
+	    if (isStale) {
+		// store the RDF_FILE
+		String rdf = getUrlAsString(serviceTypesResourceRef);
+		store(serviceTypesCache, RDF_FILE, rdf.toString());
+	    }
+	    fireEvent(SERVICE_TYPE_LOADED, SERVICE_TYPES_RESOURCE_NAME
+		    + " RDF downloading");
+
+	    // load the RDF into memory
+	    fireEvent(SERVICE_TYPE_LOADING, SERVICE_TYPES_RESOURCE_NAME
+		    + " RDF parsing");
+	    MobyServiceType.loadServiceTypes(new URL("file:///"
+		    + serviceTypesCache.getAbsolutePath() + fileSeparator
+		    + RDF_FILE), reg);
+	    // set the flag that service types have been loaded
+	    fireEvent(SERVICE_TYPE_LOADED, SERVICE_TYPES_RESOURCE_NAME
+		    + " RDF parsing");
+	    setServiceTypesLoaded(true);
 
-	    // remove files that are not any more needed
-	    for (Iterator it = currentFiles.iterator(); it.hasNext(); )
-		remove (serviceTypesCache, (String)it.next());
-
-	    // finally, put there the new LIST_FILE
-	    store (serviceTypesCache, LIST_FILE, typesAsXML);
+	    // finally, store the new LIST_FILE
+	    store(serviceTypesCache, LIST_FILE, typesAsXML);
 	    return true;
 
 	} catch (Exception e) {
-	    throw new MobyException (formatException (e), e);
+	    throw new MobyException(formatException(e), e);
 	} finally {
-	    fireEvent (stopST ? SERVICE_TYPES_CANCELLED :SERVICE_TYPES_END);
+	    fireEvent(stopST ? SERVICE_TYPES_CANCELLED : SERVICE_TYPES_END);
 	    stopST = false;
 	}
     }
 
-    /**************************************************************************
-     * Update namespaces from a moby registry - this is easier than with
-     * other entities: just get a new LIST_FILE.
-     *************************************************************************/
-    protected boolean fillNamespacesCache()
-	throws MobyException {
+    /***************************************************************************
+     * Update namespaces from a moby registry - this is easier than with other
+     * entities: just get a new LIST_FILE.
+     **************************************************************************/
+    protected boolean fillNamespacesCache() throws MobyException {
 	try {
-	    fireEvent (NAMESPACES_START);
-	    String xml = getNamespacesAsXML();
-	    store (namespacesCache, LIST_FILE, xml);
+	    fireEvent(NAMESPACES_START);
+	    // XML from API
+	    String typesAsXML = getNamespacesAsXML();
+
+	    // get a list file with all namespaces names currently
+	    // in the cache...
+	    MobyNamespace[] cachedList = new MobyNamespace[] {};
+	    // XML from cache
+	    String xmlList = getListFile(namespacesCache);
+	    if (xmlList != null)
+		cachedList = createNamespacesFromXML(xmlList);
+
+	    // map of name -> service type
+	    HashMap<String, MobyNamespace> cachedTypes = new HashMap<String, MobyNamespace>();
+	    for (int i = 0; i < cachedList.length; i++) {
+		cachedTypes.put(cachedList[i].getName(), cachedList[i]);
+	    }
+
+	    // ...and remove it
+	    remove(namespacesCache, LIST_FILE);
+
+	    // get a list file with all namespaces from the
+	    // registry
+	    MobyNamespace[] types = createNamespacesFromXML(typesAsXML);
+	    fireEvent(NAMESPACES_COUNT, new Integer(types.length));
+
+	    boolean isStale = cachedTypes.size() != types.length;
+	    if (!isStale) {
+		for (MobyNamespace namespace : types) {
+		    String name = namespace.getName();
+		    // does the cache have the datatype?
+		    if (!cachedTypes.containsKey(name)) {
+			isStale = true;
+			break;
+		    }
+		    // are the lsids the same?
+		    MobyNamespace md = cachedTypes.remove(name);
+		    if (!md.getLSID().trim().equals(namespace.getLSID().trim())) {
+			isStale = true;
+			break;
+		    }
+		}
+		// we iterated over all of the types ... are there any in the
+		// cache that arent in the registry?
+		if (!isStale && cachedTypes.size() > 0) {
+		    isStale = true;
+		}
+	    }
+	    fireEvent(NAMESPACE_LOADING, NAMESPACES_RESOURCE_NAME
+		    + " RDF downloading");
+	    // make sure that the RDF file exists before we we try to read it
+	    // below
+	    try {
+		load(new File(namespacesCache, RDF_FILE));
+	    } catch (Exception e) {
+		isStale = true;
+	    }
+	    // if we are stale, fetch new RDF
+	    if (isStale) {
+		// store the RDF_FILE
+		String rdf = getUrlAsString(namespacesResourceRef);
+		store(namespacesCache, RDF_FILE, rdf.toString());
+	    }
+	    fireEvent(NAMESPACE_LOADED, NAMESPACES_RESOURCE_NAME
+		    + " RDF downloading");
+
+	    // load the RDF into memory
+	    fireEvent(NAMESPACE_LOADING, NAMESPACES_RESOURCE_NAME
+		    + " RDF parsing");
+	    MobyNamespace.loadNamespaces(new URL("file:///"
+		    + namespacesCache.getAbsolutePath() + fileSeparator
+		    + RDF_FILE), reg);
+	    // set the flag that service types have been loaded
+	    fireEvent(NAMESPACE_LOADED, NAMESPACES_RESOURCE_NAME
+		    + " RDF parsing");
+	    setNamespacesLoaded(true);
+
+	    // finally, store the new LIST_FILE
+	    store(namespacesCache, LIST_FILE, typesAsXML);
 	    return true;
+
 	} catch (Exception e) {
-	    throw new MobyException (formatException (e), e);
+	    throw new MobyException(formatException(e), e);
 	} finally {
-	    fireEvent (NAMESPACES_END);
+	    fireEvent(NAMESPACES_END);
 	}
     }
 
-    /*************************************************************************
-     *
-     *************************************************************************/
-    public Map getDataTypeNames()
-	throws MobyException {
+    /***************************************************************************
+     * 
+     **************************************************************************/
+    public Map getDataTypeNames() throws MobyException {
 	if (dataTypesCache == null)
 	    return super.getDataTypeNames();
 	synchronized (dataTypesCache) {
-	    if (isCacheEmpty (dataTypesCache)) {
+	    if (isCacheEmpty(dataTypesCache)) {
 		initCache();
-		if (! fillDataTypesCache())
+		if (!fillDataTypesCache())
 		    // callback stopped filling
 		    return new TreeMap();
 	    }
 
 	    // get a list file (with all data type names)
-	    String xmlList = getListFile (dataTypesCache);
+	    String xmlList = getListFile(dataTypesCache);
 	    if (xmlList == null) {
 		initCache();
-		if (! fillDataTypesCache())
+		if (!fillDataTypesCache())
 		    // callback stopped filling
 		    return new TreeMap();
 		else {
-		    xmlList = getListFile (dataTypesCache);
+		    xmlList = getListFile(dataTypesCache);
 		    if (xmlList == null)
 			return new TreeMap();
 		}
 	    }
-	    return createDataTypeNamesFromXML (xmlList, true);
+	    return createDataTypeNamesFromXML(xmlList, true);
 	}
     }
 
-    /*************************************************************************
-     *
-     *************************************************************************/
-    public MobyDataType[] getDataTypes()
-	throws MobyException {
+    /***************************************************************************
+     * 
+     **************************************************************************/
+    public MobyDataType[] getDataTypes() throws MobyException {
 	if (dataTypesCache == null)
 	    return super.getDataTypes();
 	synchronized (dataTypesCache) {
 	    Vector v = new Vector();
-	    if (isCacheEmpty (dataTypesCache)) {
+	    if (isCacheEmpty(dataTypesCache)) {
 		initCache();
-		if (! fillDataTypesCache())
+		if (!fillDataTypesCache())
+		    // callback stopped filling
+		    return new MobyDataType[] {};
+	    }
+	    // iterate over datatypes in the LIST_FILE
+	    Map<String, MobyDataType> cachedTypes = new HashMap<String, MobyDataType>();
+	    // XML from Cache
+	    if (getListFile(dataTypesCache) == null || !isDatatypesLoaded()) {
+		if (!fillDataTypesCache())
 		    // callback stopped filling
 		    return new MobyDataType[] {};
 	    }
-	    File[] list = dataTypesCache.listFiles();
-	    if (list == null)
-		throw new MobyException (MSG_CACHE_NOT_DIR (dataTypesCache));
-	    Arrays.sort (list, getFileComparator());
 
-	    for (int i = 0; i < list.length; i++) {
-		try {
-		    if (ignored (list[i])) continue;
-		    v.addElement (createDataTypeFromXML (load (list[i]), "-dummy-"));
-		} catch (NoSuccessException e) {
-			log.error (MSG_CACHE_BAD_FILE (list[i], e));
-			//System.err.println (MSG_CACHE_BAD_FILE (list[i], e));
-		}
+	    String xmlList = getListFile(dataTypesCache);
+	    // get the datatype names from the XML
+	    if (xmlList != null)
+		cachedTypes = createDataTypeNamesFromXML(xmlList, true);
+	    for (String name : cachedTypes.keySet()) {
+		MobyDataType data = MobyDataType.getDataType(name, reg); 
+		if (data != null)
+		    v.addElement(data);
 	    }
-	    MobyDataType[] result = new MobyDataType [v.size()];
-	    v.copyInto (result);
+	    MobyDataType[] result = new MobyDataType[v.size()];
+	    v.copyInto(result);
 	    return result;
 	}
     }
 
-    /*************************************************************************
-     *
-     *************************************************************************/
-    public Map getServiceNamesByAuthority()
-	throws MobyException {
+    /***************************************************************************
+     * 
+     **************************************************************************/
+    public Map getServiceNamesByAuthority() throws MobyException {
 	if (servicesCache == null)
 	    return super.getServiceNamesByAuthority();
 	synchronized (servicesCache) {
-	    if (isCacheEmpty (servicesCache)) {
+	    if (isCacheEmpty(servicesCache)) {
 		initCache();
-		if (! fillServicesCache())
+		if (!fillServicesCache())
 		    // callback stopped filling
 		    return new TreeMap();
 	    }
 
 	    // get a list file (with all service names)
-	    String xmlList = getListFile (servicesCache);
+	    String xmlList = getListFile(servicesCache);
 	    if (xmlList == null) {
 		initCache();
-		if (! fillServicesCache())
+		if (!fillServicesCache())
 		    // callback stopped filling
 		    return new TreeMap();
 		else {
-		    xmlList = getListFile (servicesCache);
+		    xmlList = getListFile(servicesCache);
 		    if (xmlList == null)
 			return new TreeMap();
 		}
 	    }
-	    return createServicesByAuthorityFromXML (xmlList, true);
+	    return createServicesByAuthorityFromXML(xmlList, true);
 	}
     }
 
-    /*************************************************************************
-     *
-     *************************************************************************/
-    public MobyService[] getServices()
-	throws MobyException {
+    /***************************************************************************
+     * 
+     **************************************************************************/
+    public MobyService[] getServices() throws MobyException {
 	if (servicesCache == null)
 	    return super.getServices();
 	synchronized (servicesCache) {
-	    Vector v = new Vector();
-	    if (isCacheEmpty (servicesCache)) {
+	    if (isCacheEmpty(servicesCache) || !rdfExists(servicesCache)) {
 		initCache();
-		if (! fillServicesCache())
+		if (!fillServicesCache())
 		    // callback stopped filling
 		    return new MobyService[] {};
 	    }
-	    File[] list = servicesCache.listFiles();
-	    if (list == null)
-		throw new MobyException (MSG_CACHE_NOT_DIR (servicesCache));
-	    Arrays.sort (list, getFileComparator());
-	    for (int i = 0; i < list.length; i++) {
-		try {
-		    if (ignored (list[i])) continue;
-		    MobyService[] servs = extractServices (load (list[i]));
-		    for (int j = 0; j < servs.length; j++) {
-			v.addElement (servs[j]);
-		    }
-		} catch (MobyException e) {
-		    log.error (MSG_CACHE_BAD_FILE (list[i], e));
-		}
-	    }
-	    MobyService[] result = new MobyService [v.size()];
-	    v.copyInto (result);
-	    return result;
+	    ArrayList<MobyService> services = new ArrayList<MobyService>();
+	    ServiceInstanceParser p = new ServiceInstanceParser("file:///"
+		    + servicesCache.getAbsolutePath() + fileSeparator
+		    + RDF_FILE);
+	    services.addAll(Arrays.asList(p.getMobyServicesFromRDF()));
+
+	    return services.toArray(new MobyService[] {});
 	}
     }
 
-    /*************************************************************************
-     *
-     *************************************************************************/
-    public MobyNamespace[] getFullNamespaces()
-	throws MobyException {
+    /***************************************************************************
+     * 
+     **************************************************************************/
+    public MobyNamespace[] getFullNamespaces() throws MobyException {
 	if (namespacesCache == null)
 	    return super.getFullNamespaces();
 	synchronized (namespacesCache) {
-	    if (isCacheEmpty (namespacesCache)) {
+	    if (isCacheEmpty(namespacesCache)) {
 		initCache();
 		fillNamespacesCache();
 	    }
 
 	    // get a list file (with all namespaces)
-	    String xmlList = getListFile (namespacesCache);
-	    if (xmlList == null) {
+	    String xmlList = getListFile(namespacesCache);
+	    if (xmlList == null || !isNamespacesLoaded()) {
 		initCache();
 		fillNamespacesCache();
-		xmlList = getListFile (namespacesCache);
+		xmlList = getListFile(namespacesCache);
 		if (xmlList == null)
 		    return new MobyNamespace[] {};
 	    }
-	    return createNamespacesFromXML (xmlList);
+	    String[] names = extractNamespacesFromXML(xmlList);
+	    ArrayList<MobyNamespace> types = new ArrayList<MobyNamespace>();
+	    for (String name : names) {
+		MobyNamespace namespace = MobyNamespace.getNamespace(name, reg);
+		if (namespace != null)
+		    types.add(namespace);
+		// else System.err.println(name + " is null for
+		// getNamespace()");
+	    }
+	    return types.toArray(new MobyNamespace[types.size()]);
 	}
     }
 
-    /*************************************************************************
-     *
-     *************************************************************************/
-    protected MobyServiceType[] readServiceTypes()
-	throws MobyException {
+    /***************************************************************************
+     * 
+     **************************************************************************/
+    protected MobyServiceType[] readServiceTypes() throws MobyException {
 	if (serviceTypesCache == null)
 	    return super.readServiceTypes();
 	synchronized (serviceTypesCache) {
-	    if (isCacheEmpty (serviceTypesCache)) {
+	    if (isCacheEmpty(serviceTypesCache)) {
 		initCache();
-		if (! fillServiceTypesCache())
+		if (!fillServiceTypesCache())
 		    // a callback stopped filling
 		    return new MobyServiceType[] {};
 	    }
 
 	    // get a list file (with all service type names)
-	    String xmlList = getListFile (serviceTypesCache);
-	    if (xmlList == null) {
-		if (! fillServiceTypesCache())
+	    String xmlList = getListFile(serviceTypesCache);
+	    if (xmlList == null || !isServiceTypesLoaded()) {
+		if (!fillServiceTypesCache())
 		    // a callback stopped filling
 		    return new MobyServiceType[] {};
 		else {
-		    xmlList = getListFile (serviceTypesCache);
+		    xmlList = getListFile(serviceTypesCache);
 		    if (xmlList == null)
 			return new MobyServiceType[] {};
 		}
 	    }
-	    MobyServiceType[] types = createServiceTypesFromXML (xmlList);
 
+	    ArrayList<MobyServiceType> list = new ArrayList<MobyServiceType>();
+	    MobyServiceType[] types = createServiceTypesFromXML(xmlList);
 	    // add details about relationship to get full service types
 	    for (int i = 0; i < types.length; i++) {
 		String name = types[i].getName();
-		File file = new File (serviceTypesCache, name);
-		try {
-		    types[i].setParentNames (createServiceTypeRelationshipsFromXML (load (file)));
-		} catch (MobyException e) {
-		    log.error (MSG_CACHE_BAD_FILE (file, e));
-		}
+		MobyServiceType type = MobyServiceType.getServiceType(name, reg); 
+		if (type != null)
+		    list.add(type);
 	    }
-	    return types;
+	    return list.toArray(new MobyServiceType[]{});
 	}
     }
 
-    /**************************************************************************
-     * A LIST_FILE is a TOC of a cache object (each cache part has its
-     * own LIST_FILE). Read it and return it. If it does not exist,
-     * return null.
-     *************************************************************************/
-    protected static String getListFile (File cache)
-	throws MobyException {
-	File listFile = new File (cache, LIST_FILE);
-	if (! listFile.exists())
+    /***************************************************************************
+     * parse list file for just the names of the Namespaces
+     **************************************************************************/
+    protected String[] extractNamespacesFromXML(String result)
+	    throws MobyException {
+
+	// parse returned XML
+	Document document = loadDocument(new ByteArrayInputStream(result
+		.getBytes()));
+	NodeList list = document.getElementsByTagName("Namespace");
+	if (list == null || list.getLength() == 0)
+	    return new String[] {};
+	String[] results = new String[list.getLength()];
+	for (int i = 0; i < list.getLength(); i++) {
+	    Element elem = (Element) list.item(i);
+	    results[i] = elem.getAttribute("name");
+	}
+	java.util.Arrays.sort(results);
+	return results;
+    }
+
+    /***************************************************************************
+     * A LIST_FILE is a TOC of a cache object (each cache part has its own
+     * LIST_FILE). Read it and return it. If it does not exist, return null.
+     **************************************************************************/
+    protected static String getListFile(File cache) throws MobyException {
+	File listFile = new File(cache, LIST_FILE);
+	if (!listFile.exists())
 	    return null;
-	return load (listFile);
+	return load(listFile);
+    }
+
+    protected static boolean rdfExists(File cache) {
+	File rdfFile = new File(cache, RDF_FILE);
+	return rdfFile.exists();
     }
 
-    /**************************************************************************
+    /***************************************************************************
      * Return a comparator for Files that compares in case-insensitive way.
-     *************************************************************************/
+     **************************************************************************/
     protected static Comparator getFileComparator() {
 	return new Comparator() {
-		public int compare (Object o1, Object o2) {
-		    return o1.toString().compareToIgnoreCase (o2.toString());
-		}
-	    };
+	    public int compare(Object o1, Object o2) {
+		return o1.toString().compareToIgnoreCase(o2.toString());
+	    }
+	};
     }
 
-    /**************************************************************************
+    /***************************************************************************
      * Some file (when being read from a cache directory) are ignored.
-     *************************************************************************/
-    protected static boolean ignored (File file) {
+     **************************************************************************/
+    protected static boolean ignored(File file) {
 	String path = file.getPath();
-	return
-	    path.endsWith ("~") ||
-	    path.endsWith (LIST_FILE);
+	return path.endsWith("~") || path.endsWith(LIST_FILE)
+		|| path.endsWith(RDF_FILE);
     }
 
-    /**************************************************************************
+    /***************************************************************************
      * Some file (when a cache is being tested fir emptyness) are ignored.
-     *************************************************************************/
-    protected static boolean ignoredForEmptiness (File file) {
+     **************************************************************************/
+    protected static boolean ignoredForEmptiness(File file) {
 	String path = file.getPath();
-	return
-	    path.endsWith ("~");
+	return path.endsWith("~") || path.endsWith(RDF_FILE);
     }
 
-    /**************************************************************************
-     *
-     *************************************************************************/
-    protected static String MSG_CACHE_NOT_DIR (File cache) {
-	return
-	    "Surprisingly, '" + cache.getAbsolutePath() +
-	    "' is not a directory. Strange...";
+    /***************************************************************************
+     * 
+     **************************************************************************/
+    protected static String MSG_CACHE_NOT_DIR(File cache) {
+	return "Surprisingly, '" + cache.getAbsolutePath()
+		+ "' is not a directory. Strange...";
     }
 
-    /**************************************************************************
-     *
-     *************************************************************************/
-    protected static String MSG_CACHE_BAD_FILE (File file, Exception e) {
-	return
-	    "Ignoring '" + file.getPath() +
-	    "'. It should not be in the cache directory:" +
-	    e.getMessage();
+    /***************************************************************************
+     * 
+     **************************************************************************/
+    protected static String MSG_CACHE_BAD_FILE(File file, Exception e) {
+	return "Ignoring '" + file.getPath()
+		+ "'. It should not be in the cache directory:"
+		+ e.getMessage();
     }
 
-    /**************************************************************************
-     * It always (if it functions as a cache which is when 'cacheDir'
-     * was given) disables caching in the parent (so no memory caching
-     * happens there).
+    /***************************************************************************
+     * It always (if it functions as a cache which is when 'cacheDir' was given)
+     * disables caching in the parent (so no memory caching happens there).
      **************************************************************************/
-    public void setCacheMode (boolean shouldCache) {
-	super.setCacheMode (cacheDir == null ? shouldCache : false);
+    public void setCacheMode(boolean shouldCache) {
+	super.setCacheMode(cacheDir == null ? shouldCache : false);
     }
 
-    /**************************************************************************
-     * It always (again, if it functions as a cache which is when
-     * 'cacheDir' is given) reports that caching is disabled (even
-     * though for the cumulative results is actually always enabled -
-     * but that is obvious from the name of this class, isn't it?).
+    /***************************************************************************
+     * It always (again, if it functions as a cache which is when 'cacheDir' is
+     * given) reports that caching is disabled (even though for the cumulative
+     * results is actually always enabled - but that is obvious from the name of
+     * this class, isn't it?).
      **************************************************************************/
-    public boolean getCacheMode(){
+    public boolean getCacheMode() {
 	return (cacheDir == null ? super.getCacheMode() : false);
     }
 
-    /**************************************************************************
-     * Return age of the current (whole) cache in millis from the
-     * beginning of the Epoch; or -1 if cache is empty, or the age is
-     * unknown. <p>
-     *
-     * @return the cache age which is taken as the oldest (but filled)
-     * cache part (part is considered e.g. 'services', or 'data
-     * types', not their individual entities)
+    /***************************************************************************
+     * Return age of the current (whole) cache in millis from the beginning of
+     * the Epoch; or -1 if cache is empty, or the age is unknown.
+     * <p>
+     * 
+     * @return the cache age which is taken as the oldest (but filled) cache
+     *         part (part is considered e.g. 'services', or 'data types', not
+     *         their individual entities)
      **************************************************************************/
     public long getCacheAge() {
 	try {
-	    long dataTypesCacheAge =
-		(isCacheEmpty (dataTypesCache)    ? Long.MAX_VALUE : dataTypesCache.lastModified());
-	    long servicesCacheAge =
-		(isCacheEmpty (servicesCache)     ? Long.MAX_VALUE : servicesCache.lastModified());
-	    long namespacesCacheAge =
-		(isCacheEmpty (namespacesCache)   ? Long.MAX_VALUE : namespacesCache.lastModified());
-	    long serviceTypesCacheAge =
-		(isCacheEmpty (serviceTypesCache) ? Long.MAX_VALUE : serviceTypesCache.lastModified());
-	    long age = Math.min (Math.min (dataTypesCacheAge, servicesCacheAge),
-				 Math.min (namespacesCacheAge, serviceTypesCacheAge));
+	    long dataTypesCacheAge = (isCacheEmpty(dataTypesCache) ? Long.MAX_VALUE
+		    : dataTypesCache.lastModified());
+	    long servicesCacheAge = (isCacheEmpty(servicesCache) ? Long.MAX_VALUE
+		    : servicesCache.lastModified());
+	    long namespacesCacheAge = (isCacheEmpty(namespacesCache) ? Long.MAX_VALUE
+		    : namespacesCache.lastModified());
+	    long serviceTypesCacheAge = (isCacheEmpty(serviceTypesCache) ? Long.MAX_VALUE
+		    : serviceTypesCache.lastModified());
+	    long age = Math.min(Math.min(dataTypesCacheAge, servicesCacheAge),
+		    Math.min(namespacesCacheAge, serviceTypesCacheAge));
 	    return (age == Long.MAX_VALUE ? -1 : age);
 	} catch (MobyException e) {
 	    return -1;
 	}
     }
 
-    /**************************************************************************
-     * Return as many properties describing the given part of a cache
-     * as possible. The key used for returned properties are publicly
-     * available from this class but other may be returned as well. <p>
-     *
+    /***************************************************************************
+     * Return as many properties describing the given part of a cache as
+     * possible. The key used for returned properties are publicly available
+     * from this class but other may be returned as well.
+     * <p>
+     * 
      * @return properties describing a cache
-     * @param id is a part of cache to be described, or null if the
-     * whole cache should be described (this may return different kind
-     * of properties than for individual cache parts)
+     * @param id
+     *                is a part of cache to be described, or null if the whole
+     *                cache should be described (this may return different kind
+     *                of properties than for individual cache parts)
      **************************************************************************/
-    public Properties getCacheInfo (String id) {
+    public Properties getCacheInfo(String id) {
 	Properties result = new Properties();
-	result.put (CACHE_PROP_REGISTRY_URL, getRegistryEndpoint());
-	result.put (CACHE_PROP_COUNT, new Integer (0));
-	result.put (CACHE_PROP_SIZE, new Long (0));
-	if (cacheDir == null) return result;
+	result.put(CACHE_PROP_REGISTRY_URL, getRegistryEndpoint());
+	result.put(CACHE_PROP_COUNT, new Integer(0));
+	result.put(CACHE_PROP_SIZE, new Long(0));
+	if (cacheDir == null)
+	    return result;
 	String realName = null;
 	try {
 	    File thisPart = null;
-	    if (CACHE_PART_SERVICES.equals (id)) {
+	    if (CACHE_PART_SERVICES.equals(id)) {
 		thisPart = servicesCache;
 		realName = "Cache for Services (authorities)";
-	    } else if (CACHE_PART_DATATYPES.equals (id)) {
+	    } else if (CACHE_PART_DATATYPES.equals(id)) {
 		thisPart = dataTypesCache;
 		realName = "Cache for Data Types";
-	    } else if (CACHE_PART_SERVICETYPES.equals (id)) {
+	    } else if (CACHE_PART_SERVICETYPES.equals(id)) {
 		thisPart = serviceTypesCache;
 		realName = "Cache for Service Types";
-	    } else if (CACHE_PART_NAMESPACES.equals (id)) {
+	    } else if (CACHE_PART_NAMESPACES.equals(id)) {
 		thisPart = namespacesCache;
 		realName = "Cache for Namespaces";
 	    }
-	    if (thisPart == null) return result;
-	    result.put (CACHE_PROP_NAME, realName);
+	    if (thisPart == null)
+		return result;
+	    result.put(CACHE_PROP_NAME, realName);
 
 	    File[] list = thisPart.listFiles();
-	    if (list == null) return result;
-	    result.put (CACHE_PROP_LOCATION, thisPart.getAbsolutePath());
+	    if (list == null)
+		return result;
+	    result.put(CACHE_PROP_LOCATION, thisPart.getAbsolutePath());
 	    int realCount = 0;
 	    long realSize = 0;
 	    long ageOfYoungest = -1;
 	    long ageOfOldest = Long.MAX_VALUE;
 	    for (int i = 0; i < list.length; i++) {
-		if (! ignored (list[i])) {
+		if (!ignored(list[i])) {
 		    realCount++;
 		    realSize += list[i].length();
 		    long age = list[i].lastModified();
-		    ageOfYoungest = Math.max (ageOfYoungest, age);
-		    ageOfOldest = Math.min (ageOfOldest, age);
+		    ageOfYoungest = Math.max(ageOfYoungest, age);
+		    ageOfOldest = Math.min(ageOfOldest, age);
 		}
 	    }
-	    if (! CACHE_PART_NAMESPACES.equals (id)) {
-		result.put (CACHE_PROP_COUNT, new Integer (realCount));
-		result.put (CACHE_PROP_SIZE, new Long (realSize));
+	    if (!CACHE_PART_NAMESPACES.equals(id)) {
+		result.put(CACHE_PROP_COUNT, new Integer(realCount));
+		result.put(CACHE_PROP_SIZE, new Long(realSize));
 	    }
 	    if (ageOfYoungest > 0)
-		result.put (CACHE_PROP_YOUNGEST, new Long (ageOfYoungest));
+		result.put(CACHE_PROP_YOUNGEST, new Long(ageOfYoungest));
 	    if (ageOfOldest < Long.MAX_VALUE)
-		result.put (CACHE_PROP_OLDEST, new Long (ageOfOldest));
+		result.put(CACHE_PROP_OLDEST, new Long(ageOfOldest));
 
 	} catch (Exception e) {
-	    log.error ("Getting cache info failed: " + e.toString());
+	    log.error("Getting cache info failed: " + e.toString());
 	}
 	return result;
     }
 
-    public String getCacheInfoFormatted (String id) {
-	Properties props = getCacheInfo (id);
+    public String getCacheInfoFormatted(String id) {
+	Properties props = getCacheInfo(id);
 	StringBuffer buf = new StringBuffer();
-	buf.append (props.getProperty (CACHE_PROP_NAME) + "\n");
-	add (buf, "Biomoby registry", props.get (CACHE_PROP_REGISTRY_URL));
-	if ( ((Integer)props.get (CACHE_PROP_COUNT)).intValue() > 0 )
-	    add (buf, "Number of entities", props.get (CACHE_PROP_COUNT));
-	Object value = props.get (CACHE_PROP_OLDEST);
+	buf.append(props.getProperty(CACHE_PROP_NAME) + "\n");
+	add(buf, "Biomoby registry", props.get(CACHE_PROP_REGISTRY_URL));
+	if (((Integer) props.get(CACHE_PROP_COUNT)).intValue() > 0)
+	    add(buf, "Number of entities", props.get(CACHE_PROP_COUNT));
+	Object value = props.get(CACHE_PROP_OLDEST);
 	if (value != null) {
-	    long age = ((Long)value).longValue();
-	    add (buf, "Oldest entry created", new Date (age));
-	    add (buf, "Oldest entry has age", Utils.ms2Human (new Date().getTime() - age));
+	    long age = ((Long) value).longValue();
+	    add(buf, "Oldest entry created", new Date(age));
+	    add(buf, "Oldest entry has age", Utils.ms2Human(new Date()
+		    .getTime()
+		    - age));
 	}
-	value = props.get (CACHE_PROP_YOUNGEST);
+	value = props.get(CACHE_PROP_YOUNGEST);
 	if (value != null) {
-	    long age = ((Long)value).longValue();
-	    add (buf, "Youngest entry created", new Date (age));
-	    add (buf, "Youngest entry has age", Utils.ms2Human (new Date().getTime() - age));
-	}
-	if ( ((Long)props.get (CACHE_PROP_SIZE)).longValue() > 0 )
-	    add (buf, "Size (in bytes)", props.get (CACHE_PROP_SIZE));
-	add (buf, "Location", props.get (CACHE_PROP_LOCATION));
-	return new String (buf);
+	    long age = ((Long) value).longValue();
+	    add(buf, "Youngest entry created", new Date(age));
+	    add(buf, "Youngest entry has age", Utils.ms2Human(new Date()
+		    .getTime()
+		    - age));
+	}
+	if (((Long) props.get(CACHE_PROP_SIZE)).longValue() > 0)
+	    add(buf, "Size (in bytes)", props.get(CACHE_PROP_SIZE));
+	add(buf, "Location", props.get(CACHE_PROP_LOCATION));
+	return new String(buf);
     }
 
-    private void add (StringBuffer buf, String name, Object value) {
+    private void add(StringBuffer buf, String name, Object value) {
 	if (value != null) {
-	    buf.append ("\t");
-	    buf.append (name);
-	    buf.append ("\t");
-	    buf.append (value.toString());
-	    buf.append ("\n");
+	    buf.append("\t");
+	    buf.append(name);
+	    buf.append("\t");
+	    buf.append(value.toString());
+	    buf.append("\n");
 	}
     }
 
+    public boolean isDatatypesLoaded() {
+	return datatypesLoaded;
+    }
+
+    public void setDatatypesLoaded(boolean datatypesLoaded) {
+	this.datatypesLoaded = datatypesLoaded;
+    }
+
+    public boolean isServicesLoaded() {
+	return servicesLoaded;
+    }
+
+    public void setServicesLoaded(boolean servicesLoaded) {
+	this.servicesLoaded = servicesLoaded;
+    }
+
+    public boolean isNamespacesLoaded() {
+	return namespacesLoaded;
+    }
+
+    public void setNamespacesLoaded(boolean namespacesLoaded) {
+	this.namespacesLoaded = namespacesLoaded;
+    }
+
+    public boolean isServiceTypesLoaded() {
+	return serviceTypesLoaded;
+    }
+
+    public void setServiceTypesLoaded(boolean serviceTypesLoaded) {
+	this.serviceTypesLoaded = serviceTypesLoaded;
+    }
+
 }




More information about the MOBY-guts mailing list