Print

Print


Commit in lcsim/src/org/lcsim/conditions on MAIN
ConditionsReader.java+79-651.10 -> 1.11
JM: add support for generic classpath loading of detector conditions (old system with individual zip files should still work too)

lcsim/src/org/lcsim/conditions
ConditionsReader.java 1.10 -> 1.11
diff -u -r1.10 -r1.11
--- ConditionsReader.java	27 Apr 2007 23:32:00 -0000	1.10
+++ ConditionsReader.java	22 May 2008 23:14:15 -0000	1.11
@@ -31,7 +31,7 @@
    private static Properties aliases;
    private static final File home = new File(FileCache.getCacheRoot(),".lcsim");
    private static FileCache cache;
-   private static String DETECTOR_RESOURCE = "/org/lcsim/detector/db/";
+   private static String OLD_DETECTOR_RESOURCE = "/org/lcsim/detector/db/";
    
    /**
     * Get a list of available detectors
@@ -77,8 +77,22 @@
    {
       return new DummyConditionsReader();
    }
+
+   private static String resolveAlias(final String detectorName) throws IOException
+   {
+      String name = detectorName;
+      for (int i=0; ; i++)
+      {
+         String alias = aliases.getProperty(name);
+         if (alias == null) break;
+         if (i > 100) throw new IOException("Recursive name translation: "+name);
+         name = alias;
+      }
+      return name;
+   }
    
-   /** Try to find the conditions associated with this detector.
+   /** 
+    * Try to find the conditions associated with this detector.
     * For more details see @link http://confluence.slac.stanford.edu/display/ilc/Conditions+database
     */
    static ConditionsReader create(String detectorName, int run) throws ConditionsNotFoundException
@@ -88,17 +102,12 @@
       {
          if (cache == null) cache = new FileCache(new File(home,"cache"));
          if (aliases == null) aliases = loadAliases();
-         
-         for (int i=0; ; i++)
-         {
-            String alias = aliases.getProperty(name);
-            if (alias == null) break;
-            if (i > 100) throw new IOException("Recursive name translation: "+name);
-            name = alias;
-         }
+
+         name = resolveAlias(detectorName); 
          
          if (name.contains(":"))
          {
+            // Name is a URL.
             URL url = new URL(name);
             if (url.getProtocol().equals("file") && (url.getHost() == null || url.getHost().length() == 0))
             {
@@ -112,23 +121,30 @@
                return new ZipConditionsReader(file);
             }
          }
-         else // Not a URL, search for the file in special locations
+         else 
          {
+            // Search the classpath from the base package.
+            try { 
+               return new BaseClasspathConditionsReader(name);
+            }
+            catch (IOException x)
+            {}
+
+            // Search the classpath using old detector db location. (GeomConverter)
+            try {
+               return new OldClasspathConditionsReader(name);
+            }
+            catch (IOException x)
+            {}
+
+            // Search for a local, cached copy.
             File detectorDir = new File(home,"detectors");
             File zipFile = new File(detectorDir,name+".zip");
             if (zipFile.exists()) return new ZipConditionsReader(zipFile);
             File dirFile = new File(detectorDir,name);
             if (dirFile.exists() && dirFile.isDirectory()) return new DirectoryConditionsReader(dirFile);
-            
-            // Look on classpath
-            InputStream in = ConditionsReader.class.getResourceAsStream(DETECTOR_RESOURCE + name + "/detector.properties");
-            if (in != null)
-            {
-               //System.out.println("found conditions in classpath for " + name);
-               in.close();
-               return new ClasspathConditionsReader(name);
-            }
-            
+
+            // Finally, try to pull the detector conditions from the lcsim.org website.
             try
             {
                URL url = new URL("http://www.lcsim.org/detectors/"+name+".zip");
@@ -150,50 +166,14 @@
          throw new ConditionsNotFoundException(name,run,x);
       }
    }
+
    abstract InputStream open(String name, String type) throws IOException;
    abstract void close() throws IOException;
    private static Properties loadAliases()
    {
-      // Load the alias files
       Properties result = new Properties();
       try
       {
-         File f = downloadAliasFile(new URL("http://www.lcsim.org/detectors/alias.properties"));
-         InputStream in = new FileInputStream(f);
-         try
-         {
-            result.load(in);
-         }
-         finally
-         {
-            in.close();
-         }
-      }
-      catch (IOException x)
-      {
-         
-      }
-      try
-      {
-         InputStream in = ConditionsReader.class.getResourceAsStream(DETECTOR_RESOURCE + "alias.properties");
-         if (in != null)
-         {
-            try
-            {
-               result.load(in);
-            }
-            finally
-            {
-               in.close();
-            }
-         }
-      }
-      catch (IOException x)
-      {
-         
-      }
-      try
-      {
          File f = new File(home,"alias.properties");
          InputStream in = new FileInputStream(f);
          if (in != null)
@@ -209,9 +189,7 @@
          }
       }
       catch (IOException x)
-      {
-         
-      }
+      {}
       return result;
    }
    
@@ -219,10 +197,12 @@
    {
       return cache.getCachedFile(url,new DetectorFileValidator());
    }
+
    private static File downloadAliasFile(URL url) throws IOException
    {
       return cache.getCachedFile(url,new PropertiesFileValidator());
    }
+
    private static class DetectorFileValidator implements Validator
    {
       public void checkValidity(URL url, File file) throws IOException
@@ -295,12 +275,21 @@
       {
       }
    }
-   private static class ClasspathConditionsReader extends ConditionsReader
+
+   /**
+    * Search for conditions using the old detector db package from GeomConverter. 
+    * NOTE: Do not add new detectors to the detector db package in GeomConverter, as it is being phased out.
+    * @deprecated
+    */
+   private static class OldClasspathConditionsReader extends ConditionsReader
    {
       private String base;
-      ClasspathConditionsReader(String name) throws IOException
+      OldClasspathConditionsReader(String name) throws IOException
       {
-         base = DETECTOR_RESOURCE + name + "/";
+         base = OLD_DETECTOR_RESOURCE + name;
+         //System.out.println("returning OldClasspathConditionsReader: " + base);
+         if (ConditionsReader.class.getResourceAsStream(base + "detector.properties") == null)
+         throw new IOException("Unable to find " + base + "/detector.properties on the classpath!");
       }
       InputStream open(String name, String type) throws IOException
       {
@@ -310,8 +299,34 @@
       }
       void close() throws IOException
       {
+       }
+   }
+
+   /**
+    * This ConditionsReader finds detector conditions with the assumption that the conditions are located in a package called <code>detectorName</code>.
+    * This ConditionsReader will work if the new lcsim-detector jar is on the classpath at runtime.
+    */
+   private static class BaseClasspathConditionsReader extends ConditionsReader
+   {
+      private String detectorName;
+      BaseClasspathConditionsReader(String detectorName) throws IOException
+      {
+         //System.out.println("returning BaseClasspathConditionsReader: " + detectorName);
+         this.detectorName = detectorName; 
+         if (ConditionsReader.class.getResourceAsStream("/" + detectorName + "/detector.properties") == null)
+         throw new IOException("Unable to find " + detectorName + "/detector.properties on the classpath!");
+      }
+      InputStream open(String name, String type) throws IOException
+      {
+         InputStream in = ConditionsReader.class.getResourceAsStream("/" + detectorName + "/" + name + "." + type);
+         if (in == null) throw new IOException("Conditions "+detectorName + "." + name + "." + type + " not found");
+         return in;
+      }
+      void close() throws IOException
+      {
       }
    }
+   
    private static class DummyConditionsReader extends ConditionsReader
    {
       InputStream open(String name, String type) throws IOException
@@ -323,5 +338,4 @@
       {
       }
    }
-   
 }
CVSspam 0.2.8