[MOBY-guts] biomoby commit

Eddie Kawas kawas at dev.open-bio.org
Tue Feb 26 20:49:18 UTC 2008


kawas
Tue Feb 26 15:49:18 EST 2008
Update of /home/repository/moby/moby-live/Java/src/main/org/biomoby/client
In directory dev.open-bio.org:/tmp/cvs-serv23421/Java/src/main/org/biomoby/client

Modified Files:
	CentralDigestCachedImpl.java 
Log Message:
re-did the implementation of fillServiceCache and fillDatatypeCache
* on empty cache, RDF is downloaded from the registry
* on updates, if the cache is stale to a certain degree, the RDF is downloded and parsed, otherwise multiple calls to central occur
* no extra files are created or stored when caching services/datatypes
moby-live/Java/src/main/org/biomoby/client CentralDigestCachedImpl.java,1.26,1.27
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/CentralDigestCachedImpl.java,v
retrieving revision 1.26
retrieving revision 1.27
diff -u -r1.26 -r1.27
--- /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/CentralDigestCachedImpl.java	2008/02/26 15:40:01	1.26
+++ /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/CentralDigestCachedImpl.java	2008/02/26 20:49:18	1.27
@@ -35,14 +35,23 @@
 import org.biomoby.client.rdf.builder.ServiceInstanceRDF;
 import org.biomoby.registry.meta.Registry;
 import org.biomoby.shared.CentralCached;
+import org.biomoby.shared.Central;
 import org.biomoby.shared.MobyDataType;
 import org.biomoby.shared.MobyException;
+import org.biomoby.shared.NoSuccessException;
 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.biomoby.shared.extended.DataTypeParser;
+import org.biomoby.shared.MobyPrimaryDataSet;
+import org.biomoby.shared.MobyPrimaryData;
+import org.biomoby.shared.MobyPrimaryDataSimple;
+import org.biomoby.shared.MobySecondaryData;
+import org.biomoby.shared.MobyService;
+import org.biomoby.shared.MobyRelationship;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.NodeList;
@@ -78,10 +87,12 @@
 
     // for optimalization
     private Registry reg = null;
-    private boolean datatypesLoaded = false;
     private boolean namespacesLoaded = false;
     private boolean serviceTypesLoaded = false;
 
+    private int datatype_threshold ;
+    private int service_threshold ;
+    
     /***************************************************************************
      * Create an instance that will access a default Moby registry and
      * will cache results in the 'cacheDir' directory.  <p>
@@ -102,6 +113,14 @@
 	super (endpoint, namespace, cacheDir);
 	reg = new Registry(getRegistryEndpoint(), getRegistryEndpoint(),
 		getRegistryNamespace());
+	
+	datatype_threshold = Integer.getInteger("cache.threshold.datatypes", 7).intValue();
+	if (datatype_threshold < 0 || datatype_threshold > 100)
+	    datatype_threshold = 7;
+	service_threshold = Integer.getInteger("cache.threshold.services", 7).intValue();
+	if (service_threshold < 0 || service_threshold > 100)
+	    service_threshold = 30;
+	
 
     }
 
@@ -112,96 +131,136 @@
      * the cache and remove them, or fetched missing ones if success add there
      * new LIST_FILE
      **************************************************************************/
-    @Override protected boolean fillDataTypesCache()
+    protected boolean fillDataTypesCache()
 	throws MobyException {
 	try {
-	    fireEvent(DATA_TYPES_START);
-	    // XML from API
+	    
+	    if (isCacheEmpty(dataTypesCache)) {
+		fireEvent (DATA_TYPES_START);
+		// download RDF and parse it into individual datatypes
+		DataTypeParser sip = new DataTypeParser(getResourceURL(DATA_TYPES_RESOURCE_NAME));
+                MobyDataType[] datatypes = sip.getMobyDataTypesFromRDF();
+                fireEvent (DATA_TYPES_COUNT, new Integer (datatypes.length));
+                // create a map of name => to String representation as is with retrieveObjectDefinition
+                Map<String,String> map = createRetrieveObjectXML(datatypes);
+                for (String datatype : map.keySet()) {
+                    fireEvent (DATA_TYPE_LOADING, datatype);
+                    String filecontents = map.get(datatype);
+                    store(dataTypesCache, datatype, filecontents);
+                    fireEvent (DATA_TYPE_LOADED, datatype);
+                }
+                // finally, put there the new LIST_FILE
+                store(dataTypesCache, LIST_FILE, getDataTypeNamesAsXML());
+                // done
+                return true;
+	    }
+	    
+	    fireEvent (DATA_TYPES_START);
 	    String typesAsXML = getDataTypeNamesAsXML();
 
 	    // get a list file with all data type names currently in
 	    // the cache...
-	    Map<String, MobyDataType> cachedTypes = new HashMap<String, MobyDataType>();
-	    // XML from Cache
-	    String xmlList = getListFile(dataTypesCache);
+	    Map cachedTypes = new HashMap();
+	    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 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;
-		    }
-		    // are the lsids the same?
-		    MobyDataType md = cachedTypes.remove(name);
-		    if (!md.getLSID().trim().equals(
-			    types.get(name).getLSID().trim())) {
-			isStale = true;
-			break;
+	    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());
+	    }
+	    // a list of datatypes needed to fetch from the registry
+	    ArrayList<String> datatypesToFetch = new ArrayList<String>();
+	    
+	    // 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;
 		    }
 		}
-		// 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;
+		if (needToFetch) {
+		    // missing file: add to fetch it from a registry list 
+		    datatypesToFetch.add(name);
 		}
+		currentFiles.remove (name);
 	    }
-
-	    // 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(dataTypesCache, RDF_FILE));
-	    } catch (Exception e) {
-		isStale = true;
-	    }
-	    if (isStale) {
-		// store the RDF_FILE
-		String rdf = getResourceAsString(DATA_TYPES_RESOURCE_NAME);
-		store(dataTypesCache, RDF_FILE, rdf.toString());
+	    
+	    if (((datatypesToFetch.size()*100) / types.size()) > datatype_threshold) {
+		// download rdf instead of making individual calls to central
+		DataTypeParser sip = new DataTypeParser(getResourceURL(DATA_TYPES_RESOURCE_NAME));
+                MobyDataType[] datatypes = sip.getMobyDataTypesFromRDF();
+                // create a map of name => to String representation as is with retrieveObjectDefinition
+                Map<String,String> map = createRetrieveObjectXML(datatypes);
+                for (String datatype : datatypesToFetch) {
+                    if (!map.containsKey(datatype)) {
+                	log.warn("'" + datatype + "' was not found in the RDF ...");
+                	continue;
+                    }
+                    fireEvent (DATA_TYPE_LOADING, datatype);
+                    String filecontents = map.get(datatype);
+                    store(dataTypesCache, datatype, filecontents);
+                    fireEvent (DATA_TYPE_LOADED, datatype);
+                    if (stopDT) {
+			return false;
+		    }
+                }
+	    } else {
+		// use api to get datatypes
+		for (String name : datatypesToFetch) {
+		    fireEvent (DATA_TYPE_LOADING, name);
+		    String xml = getDataTypeAsXML (name);
+		    store (dataTypesCache, name, xml);
+		    fireEvent (DATA_TYPE_LOADED, name);
+		    if (stopDT) {
+			return false;
+		    }
+		}
 	    }
-	    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() + File.separator
-		    + 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");
+	    
+	    // remove files that are not any more needed
+	    for (Iterator it = currentFiles.iterator(); it.hasNext(); )
+		remove (dataTypesCache, (String)it.next());
 
-	    // finally, store the the new LIST_FILE
-	    store(dataTypesCache, LIST_FILE, typesAsXML);
+	    // finally, put there 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;
 	}
-    }
+}
 
     /**
      * @return a string of text as obtained from the url
@@ -230,102 +289,84 @@
      * contain all services mentioned in the LIST_FILE if success add there new
      * LIST_FILE
      **************************************************************************/
-    @Override protected boolean fillServicesCache()
+    protected boolean fillServicesCache()
 	throws MobyException {
 	try {
-	    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,
-			    getResourceAsString(SERVICE_INSTANCES_RESOURCE_NAME));
-		    // extract the services
-		    ServiceInstanceParser sip = new ServiceInstanceParser(
-			    "file:///" + servicesCache.getAbsolutePath()
-				    + File.separator + 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 rdf
-			store(servicesCache, authURI+".__r__d__f__", s);
-			// TODO store Service XML too
-		    }
-		    // 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);
-		}
+	    if (isCacheEmpty(servicesCache)) {
+		fireEvent (AUTHORITIES_START);
+		// get a list file
+                String byAuthorityAsXML = getServiceNamesByAuthorityAsXML();
+                
+		// download RDF and fill it up
+		ServiceInstanceParser sip = new ServiceInstanceParser(getResourceURL(SERVICE_INSTANCES_RESOURCE_NAME));
+                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);
+                }
+                fireEvent (AUTHORITIES_COUNT, new Integer (sorted.size()));
+                // free memory
+                services = null;
+                for (String authURI : sorted.keySet()) {
+                    fireEvent (AUTHORITY_LOADING, authURI);
+                    String s = createServiceXML(sorted.get(authURI).toArray(
+                                    new MobyService[] {}));
+                    store(servicesCache, authURI, s);
+                    fireEvent (AUTHORITY_LOADED, authURI);
+                }
+                // free more memory
+                sorted.clear();
+                
+                // finally, store the new LIST_FILE
+                store(servicesCache, LIST_FILE, byAuthorityAsXML);
+                // done
+                return true;
 	    }
-
-	    // RDF exists ... are there changes?
-
+	    
+	    fireEvent (AUTHORITIES_START);
 	    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<String> currentFiles = new HashSet<String>();
+	    HashSet currentFiles = new HashSet();
 	    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]) && !list[i].getName().endsWith(".__r__d__f__"))
-		    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;
+	    // a list of authorities with services needed to fetch from the registry
+	    ArrayList<String> servicesToFetch = new ArrayList<String>();
+
 	    // 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;
 			    }
@@ -336,95 +377,191 @@
 		    }
 		    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);
-		    // save the Service xml
-		    store(servicesCache, authority, xml);
-		    MobyService[] services = extractServices(xml);
-		    ServiceInstanceRDF sRdf = new ServiceInstanceRDF(reg);
-		    xml = sRdf.serializeModel(sRdf.createRDFModel(null,
-			    services, true));
-		    // save the rdf
-		    store(servicesCache, authority+".__r__d__f__", xml);
-		    hasChanged = true;
-		    fireEvent(AUTHORITY_LOADED, authority);
+		    servicesToFetch.add(authority);
+		} else {
+		    currentFiles.remove (authority);
+		}
+	    }
+	    
+	    if (((servicesToFetch.size() *100) / authorities.size()) > service_threshold) {
+		// download and process rdf
+		ServiceInstanceParser sip = new ServiceInstanceParser(getResourceURL(SERVICE_INSTANCES_RESOURCE_NAME));
+                MobyService[] services = sip.getMobyServicesFromRDF();
+                // sort the services
+                Map<String, ArrayList<MobyService>> sorted = new HashMap<String, ArrayList<MobyService>>();
+                for (MobyService s : services) {
+                    ArrayList<MobyService> al = (sorted.containsKey(s
+                            .getAuthority()) ? sorted.remove(s
+                            .getAuthority()) : new ArrayList<MobyService>());
+                    al.add(s);
+                    sorted.put(s.getAuthority(), al);
+                }
+                // free memory
+                services = null;
+                for (String authURI : servicesToFetch) {
+                    if (!sorted.containsKey(authURI)) {
+                	log.warn("'" + authURI + "' was not found in the RDF oddly enough ...");
+                	continue;
+                    }
+                    fireEvent (AUTHORITY_LOADING, authURI);
+                    String s = createServiceXML(sorted.get(authURI).toArray(new MobyService[] {}));
+                    store(servicesCache, authURI, s);
+                    fireEvent (AUTHORITY_LOADED, authURI);
+                    if (stopS) {
+			return false;
+		    }
+                }
+	    } else {
+		// process authorities one by one
+		for (String authority : servicesToFetch) {
+		    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);
 		    if (stopS) {
 			return false;
 		    }
-		} else {
-		    currentFiles.remove(authority);
 		}
 	    }
+	    
 
 	    // remove files that are not any more needed
-	    for (String it : currentFiles) {
-		log.debug("Removing the authority '" + it+"' from the cache");
-		remove(servicesCache, it);
-		// remove the RDF file too if it exists
-		remove(servicesCache, it+".__r__d__f__");
-		hasChanged = true;
-	    }
+	    for (Iterator it = currentFiles.iterator(); it.hasNext(); )
+		remove (servicesCache, (String)it.next());
 
-	    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]) && list[i].getName().endsWith(".__r__d__f__")) {
-		try {
-		    log.debug("loading: " + list[i].getName());
-		    p.setUrl("file:///" + servicesCache.getAbsolutePath()
-			    + File.separator + 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);
+}
+    
+    private URL getResourceURL (String resourceName)
+	throws MobyException {
+	MobyResourceRef[] resourceRefs = getResourceRefs();
+	for (int i = 0; i < resourceRefs.length; i++) {
+	    if (resourceName.equalsIgnoreCase (resourceRefs[i].getResourceName())) {
+		URL url = resourceRefs[i].getResourceLocation();
+		    return url;
+	    }
+	}
+	throw new MobyException ("No resource found for '" + resourceName + "'.");
+    }
+    private String createServiceXML(MobyService[] services) {
+	StringBuffer sb = new StringBuffer();
+	sb.append("<Services>\n");
+	for (MobyService s : services) {
+	    sb.append("<Service authURI=\""+s.getAuthority()+"\" serviceName=\""+s.getName()+"\" lsid=\""+s.getLSID()+"\">\n");
+	    sb.append("<serviceType lsid=\""+s.getServiceType().getLSID()+"\">"+ s.getServiceType().getName()+"</serviceType>\n");
+	    sb.append("<authoritative>"+(s.isAuthoritative() ? "1" : "0")+"</authoritative>\n" + 
+	    		"<Category>"+s.getCategory()+"</Category>\n" + 
+	    		"<Description><![CDATA["+s.getDescription()+"]]></Description>\n" + 
+	    		"<contactEmail>"+s.getEmailContact()+"</contactEmail>\n" + 
+	    		"<signatureURL>"+s.getSignatureURL().replaceAll("&","&amp;")+"</signatureURL>\n" + 
+	    		"<URL>"+s.getURL().replaceAll("&","&amp;")+"</URL>\n");
+	    // process inputs
+	    sb.append("<Input>\n");
+	    for (MobyPrimaryData in : s.getPrimaryInputs()) {
+		if (in instanceof MobyPrimaryDataSimple) {
+		    sb.append("<Simple articleName=\""+in.getName()+"\">\n");
+		    sb.append("<objectType lsid=\"" + in.getDataType().getLSID() + "\">" + in.getDataType().getName() + "</objectType>\n");
+		    for (MobyNamespace namespace : in.getNamespaces()) {
+			sb.append("<Namespace lsid=\""+namespace.getLSID()+"\">"+namespace.getName()+"</Namespace>\n");
+		    }
+		    sb.append("</Simple>\n");
+		} else {
+		    sb.append("<Collection articleName=\""+ in.getName() +"\">\n");
+		    for (MobyPrimaryDataSimple sim : ((MobyPrimaryDataSet)in).getElements()) {
+			sb.append("<Simple articleName=\"\">\n");
+			sb.append("<objectType lsid=\"" + sim.getDataType().getLSID() + "\">" + sim.getDataType().getName() + "</objectType>\n");
+			for (MobyNamespace namespace : sim.getNamespaces()) {
+			    sb.append("<Namespace lsid=\""+namespace.getLSID()+"\">"+namespace.getName()+"</Namespace>\n");
+			}
+			sb.append("</Simple>\n");
+		    }
+		    sb.append("</Collection>\n");
 		}
 	    }
+	    sb.append("</Input>\n");
+	    // process outputs
+	    sb.append("<Output>\n");
+	    for (MobyPrimaryData out : s.getPrimaryOutputs()) {
+		if (out instanceof MobyPrimaryDataSimple) {
+		    sb.append("<Simple articleName=\""+out.getName()+"\">\n");
+		    sb.append("<objectType lsid=\"" + out.getDataType().getLSID() + "\">" + out.getDataType().getName() + "</objectType>\n");
+		    for (MobyNamespace namespace : out.getNamespaces()) {
+			sb.append("<Namespace lsid=\""+namespace.getLSID()+"\">"+namespace.getName()+"</Namespace>\n");
+		    }
+		    sb.append("</Simple>\n");
+		} else {
+		    sb.append("<Collection articleName=\""+ out.getName() +"\">\n");
+		    for (MobyPrimaryDataSimple sim : ((MobyPrimaryDataSet)out).getElements()) {
+			sb.append("<Simple articleName=\"\">\n");
+			sb.append("<objectType lsid=\"" + sim.getDataType().getLSID() + "\">" + sim.getDataType().getName() + "</objectType>\n");
+			for (MobyNamespace namespace : sim.getNamespaces()) {
+			    sb.append("<Namespace lsid=\""+namespace.getLSID()+"\">"+namespace.getName()+"</Namespace>\n");
+			}
+			sb.append("</Simple>\n");
+		    }
+		    sb.append("</Collection>\n");
+		}
+	    }
+	    sb.append("</Output>\n");
+	    // process secondaries
+	    sb.append("<secondaryArticles>\n");
+	    for (MobySecondaryData sec : s.getSecondaryInputs()) {
+		sb.append(sec.toXML());
+	    }
+	    sb.append("</secondaryArticles>\n");
+	    
+	    // finished with the service
+	    sb.append("</Service>\n");
 	}
-	if (m != null)
-	    store(servicesCache, RDF_FILE, siRdf.serializeModel(m));
-
+	sb.append("</Services>");
+	return sb.toString();
     }
-
+    
+    private Map<String, String> createRetrieveObjectXML(MobyDataType[] datatypes) {
+	HashMap<String, String> map = new HashMap<String,String>();
+	
+	
+	for (MobyDataType d : datatypes) {
+	    StringBuffer sb = new StringBuffer();
+	    sb.append("<retrieveObjectDefinition>\n");
+	    sb.append("<objectType lsid=\'"+d.getLSID()+"\'>"+d.getName()+"</objectType>\n" + 
+	    		"<Description><![CDATA[" + d.getDescription() + "]]></Description>\n" + 
+	    		"<authURI>"+d.getAuthority()+"</authURI>\n" + 
+	    		"<contactEmail>"+d.getEmailContact()+"</contactEmail>\n");
+	    // process HAS/HASA
+	    for (MobyRelationship relationship : d.getChildren()) {
+		sb.append("<Relationship relationshipType=\'urn:lsid:biomoby.org:objectrelation:"+(relationship.getRelationshipType() == Central.iHAS ? "has" : "hasa")+"\'>\n" + 
+				"<objectType articleName=\'"+relationship.getName()+"\' lsid=\'\'>"+relationship.getDataTypeName()+"</objectType>\n" + 
+				"</Relationship>\n");
+	    }
+	    // set the isa - if it exists
+	    if (!d.getParentName().equals(""))
+		sb.append("<Relationship relationshipType=\'urn:lsid:biomoby.org:objectrelation:isa\'>\n" + 
+			  "<objectType articleName=\'\' lsid=\'\'>" + d.getParentName() + "</objectType>\n" + 
+	    		  "</Relationship>\n");
+	    sb.append("</retrieveObjectDefinition>\n");
+	    map.put(d.getName(), sb.toString());
+	}
+	return map;
+    }
+    
     /***************************************************************************
      * 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) -
@@ -618,53 +755,72 @@
 	    fireEvent(NAMESPACES_END);
 	}
     }
+    
+    /*************************************************************************
+    *
+    *************************************************************************/
+   public Map getDataTypeNames()
+	throws MobyException {
+	if (dataTypesCache == null)
+	    return super.getDataTypeNames();
+	synchronized (dataTypesCache) {
+	    if (isCacheEmpty (dataTypesCache)) {
+		initCache();
+		if (! fillDataTypesCache())
+		    // callback stopped filling
+		    return new TreeMap();
+	    }
 
+	    // get a list file (with all data type names)
+	    String xmlList = getListFile (dataTypesCache);
+	    if (xmlList == null) {
+		initCache();
+		if (! fillDataTypesCache())
+		    // callback stopped filling
+		    return new TreeMap();
+		else {
+		    xmlList = getListFile (dataTypesCache);
+		    if (xmlList == null)
+			return new TreeMap();
+		}
+	    }
+	    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())
-		    // 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 || !rdfExists(dataTypesCache)) {
-		if (!fillDataTypesCache())
+		if (! fillDataTypesCache())
 		    // callback stopped filling
 		    return new MobyDataType[] {};
 	    }
-	    try {
-        	    if (!isDatatypesLoaded()) {
-        		MobyDataType.loadDataTypes(new URL("file:///"
-        			    + dataTypesCache.getAbsolutePath() + File.separator
-        			    + RDF_FILE), reg);
-        		setDatatypesLoaded(true);
-        	    }
-	    } catch (Exception e) {
-		    throw new MobyException(formatException(e), 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);
+	    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));
+		}
 	    }
-	    MobyDataType[] result = new MobyDataType[v.size()];
-	    v.copyInto(result);
+	    MobyDataType[] result = new MobyDataType [v.size()];
+	    v.copyInto (result);
 	    return result;
 	}
-    }
+}
 
     /***************************************************************************
      * 
@@ -700,25 +856,38 @@
     /***************************************************************************
      * 
      **************************************************************************/
-    public MobyService[] getServices() throws MobyException {
+    public MobyService[] getServices()
+	throws MobyException {
 	if (servicesCache == null)
 	    return super.getServices();
 	synchronized (servicesCache) {
-	    if (isCacheEmpty(servicesCache) || !rdfExists(servicesCache)) {
+	    Vector v = new Vector();
+	    if (isCacheEmpty (servicesCache)) {
 		initCache();
-		if (!fillServicesCache())
+		if (! fillServicesCache())
 		    // callback stopped filling
 		    return new MobyService[] {};
 	    }
-	    ArrayList<MobyService> services = new ArrayList<MobyService>();
-	    ServiceInstanceParser p = new ServiceInstanceParser("file:///"
-		    + servicesCache.getAbsolutePath() + File.separator
-		    + RDF_FILE);
-	    services.addAll(Arrays.asList(p.getMobyServicesFromRDF()));
-
-	    return services.toArray(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;
 	}
-    }
+}
 
     /***************************************************************************
      * 
@@ -847,15 +1016,7 @@
      **************************************************************************/
     @Override protected static boolean ignoredForEmptiness (File file) {
 	String path = file.getPath();
-	return path.endsWith("~") || path.endsWith(RDF_FILE);
-    }
-
-    private boolean isDatatypesLoaded() {
-	return datatypesLoaded;
-    }
-
-    private void setDatatypesLoaded(boolean datatypesLoaded) {
-	this.datatypesLoaded = datatypesLoaded;
+	return path.endsWith("~") || path.endsWith("__R__D__F__");
     }
 
     private boolean isNamespacesLoaded() {




More information about the MOBY-guts mailing list