Commit in lcsim/src/org/lcsim/conditions on MAIN
ConditionsReader.java+282-3101.11 -> 1.12


lcsim/src/org/lcsim/conditions
ConditionsReader.java 1.11 -> 1.12
diff -u -r1.11 -r1.12
--- ConditionsReader.java	22 May 2008 23:14:15 -0000	1.11
+++ ConditionsReader.java	20 Aug 2008 01:00:45 -0000	1.12
@@ -28,314 +28,286 @@
  */
 public abstract class ConditionsReader
 {
-   private static Properties aliases;
-   private static final File home = new File(FileCache.getCacheRoot(),".lcsim");
-   private static FileCache cache;
-   private static String OLD_DETECTOR_RESOURCE = "/org/lcsim/detector/db/";
-   
-   /**
-    * Get a list of available detectors
-    */
-   public static List<String> getDetectorNames()
-   {
-      Set<String> set = new HashSet<String>();
-      if (aliases == null) aliases = loadAliases();
-      for (Object key : aliases.keySet()) set.add(key.toString());
-      
-      try
-      {
-         if (cache == null) cache = new FileCache(new File(home,"cache"));
-         File file = cache.getCachedFile(new URL("http://www.lcsim.org/detectors/taglist.txt"));
-         if (file != null)
-         {
-            BufferedReader reader = new BufferedReader(new FileReader(file));
-            for (;;)
-            {
-               String line = reader.readLine();
-               if (line == null) break;
-               set.add(line);
-            }
-            reader.close();
-         }
-      }
-      catch (Exception ex)
-      {
-         System.err.println("Error reading file taglist.txt: "+ex);
-      }
-      
-      List result =  new ArrayList(set);
-      Collections.sort(result);
-      return result;
-   }
-   public static void addAlias(String alias, String target)
-   {
-      if (aliases == null) aliases = loadAliases();
-      aliases.setProperty(alias,target);
-   }
-   
-   public static ConditionsReader createDummy()
-   {
-      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.
-    * For more details see @link http://confluence.slac.stanford.edu/display/ilc/Conditions+database
-    */
-   static ConditionsReader create(String detectorName, int run) throws ConditionsNotFoundException
-   {
-      String name = detectorName;
-      try
-      {
-         if (cache == null) cache = new FileCache(new File(home,"cache"));
-         if (aliases == null) aliases = loadAliases();
-
-         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))
-            {
-               File file = new File(url.getPath());
-               if (file.isDirectory()) return new DirectoryConditionsReader(file);
-               else return new ZipConditionsReader(file);
-            }
-            else
-            {
-               File file = downloadDetectorDescription(url);
-               return new ZipConditionsReader(file);
-            }
-         }
-         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);
-
-            // Finally, try to pull the detector conditions from the lcsim.org website.
-            try
-            {
-               URL url = new URL("http://www.lcsim.org/detectors/"+name+".zip");
-               File file = downloadDetectorDescription(url);
-               return new ZipConditionsReader(file);
-            }
-            catch (FileNotFoundException x)
-            {
-               throw new ConditionsNotFoundException(name,run);
-            }
-         }
-      }
-      catch (MalformedURLException x)
-      {
-         throw new ConditionsNotFoundException(name,run,x);
-      }
-      catch (IOException x)
-      {
-         throw new ConditionsNotFoundException(name,run,x);
-      }
-   }
-
-   abstract InputStream open(String name, String type) throws IOException;
-   abstract void close() throws IOException;
-   private static Properties loadAliases()
-   {
-      Properties result = new Properties();
-      try
-      {
-         File f = new File(home,"alias.properties");
-         InputStream in = new FileInputStream(f);
-         if (in != null)
-         {
-            try
-            {
-               result.load(in);
-            }
-            finally
-            {
-               in.close();
-            }
-         }
-      }
-      catch (IOException x)
-      {}
-      return result;
-   }
-   
-   private static File downloadDetectorDescription(URL url) throws IOException
-   {
-      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
-      {
-         // Check if the file looks good. It should contain a file called detector.properties
-         // in the root directory
-         ZipFile zip = new ZipFile(file,ZipFile.OPEN_READ);
-         try
-         {
-            ZipEntry header = zip.getEntry("detector.properties");
-            if (header == null) throw new IOException("No detector.properties entry in file downloaded from "+url);
-            Properties props = new Properties();
-            props.load(zip.getInputStream(header));
-         }
-         finally
-         {
-            zip.close();
-         }
-      }
-   }
-   private static class PropertiesFileValidator implements Validator
-   {
-      public void checkValidity(URL url, File file) throws IOException
-      {
-         InputStream in = new FileInputStream(file);
-         try
-         {
-            Properties props = new Properties();
-            props.load(in);
-            
-         }
-         finally
-         {
-            in.close();
-         }
-      }
-   }
-   private static class ZipConditionsReader extends ConditionsReader
-   {
-      private ZipFile zip;
-      ZipConditionsReader(File file) throws IOException
-      {
-         this.zip = new ZipFile(file,ZipFile.OPEN_READ);
-      }
-      InputStream open(String name, String type) throws IOException
-      {
-         ZipEntry entry = zip.getEntry(name+"."+type);
-         if (entry == null) throw new IOException("Conditions "+name+"."+type+" not found");
-         return zip.getInputStream(entry);
-      }
-      void close() throws IOException
-      {
-         zip.close();
-      }
-   }
-   private static class DirectoryConditionsReader extends ConditionsReader
-   {
-      private File dir;
-      DirectoryConditionsReader(File file) throws IOException
-      {
-         this.dir = file;
-      }
-      InputStream open(String name, String type) throws IOException
-      {
-         File file = new File(dir,name+"."+type);
-         if (!file.exists()) throw new IOException("Conditions "+name+"."+type+" not found");
-         return new BufferedInputStream(new FileInputStream(file));
-      }
-      void close() throws IOException
-      {
-      }
-   }
-
-   /**
-    * 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;
-      OldClasspathConditionsReader(String name) throws IOException
-      {
-         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
-      {
-         InputStream in = ConditionsReader.class.getResourceAsStream(base+name+"."+type);
-         if (in == null) throw new IOException("Conditions "+name+"."+type+" not found");
-         return in;
-      }
-      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
-      {
-         throw new IOException("Conditions "+name+"."+type+" not found");
-      }
-
-      void close() throws IOException
-      {
-      }
-   }
+	private static Properties aliases;
+	private static final File home = new File(FileCache.getCacheRoot(),".lcsim");
+	private static FileCache cache;
+
+	/**
+	 * Get a list of available detectors
+	 */
+	public static List<String> getDetectorNames()
+	{
+		Set<String> set = new HashSet<String>();
+		if (aliases == null) aliases = loadAliases();
+		for (Object key : aliases.keySet()) set.add(key.toString());
+
+		try
+		{
+			if (cache == null) cache = new FileCache(new File(home,"cache"));
+			File file = cache.getCachedFile(new URL("http://www.lcsim.org/detectors/taglist.txt"));
+			if (file != null)
+			{
+				BufferedReader reader = new BufferedReader(new FileReader(file));
+				for (;;)
+				{
+					String line = reader.readLine();
+					if (line == null) break;
+					set.add(line);
+				}
+				reader.close();
+			}
+		}
+		catch (Exception ex)
+		{
+			System.err.println("Error reading file taglist.txt: "+ex);
+		}
+
+		List result = new ArrayList(set);
+		Collections.sort(result);
+		return result;
+	}
+	
+	public static void addAlias(String alias, String target)
+	{
+		if (aliases == null) aliases = loadAliases();
+		aliases.setProperty(alias,target);
+	}
+
+	public static ConditionsReader createDummy()
+	{
+		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.
+	 * For more details see @link http://confluence.slac.stanford.edu/display/ilc/Conditions+database
+	 */
+	static ConditionsReader create(String detectorName, int run) throws ConditionsNotFoundException
+	{
+		String name = detectorName;
+		try
+		{
+			if (cache == null) cache = new FileCache(new File(home,"cache"));
+			if (aliases == null) aliases = loadAliases();
+
+			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))
+				{					
+					File file = new File(url.getPath());
+					if (file.isDirectory()) return new DirectoryConditionsReader(file);
+					else return new ZipConditionsReader(file);
+				}
+				else
+				{
+					File file = downloadDetectorDescription(url);
+					return new ZipConditionsReader(file);
+				}
+			}
+			else 
+			{
+				// 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);
+				}
+
+				// Search the classpath from the base package.
+				try { 					
+					return new BaseClasspathConditionsReader(name);
+				}
+				catch (IOException x)
+				{}
+				
+				// Finally, try to pull the detector conditions from the lcsim.org website.
+				try
+				{
+					URL url = new URL("http://www.lcsim.org/detectors/"+name+".zip");
+					File file = downloadDetectorDescription(url);
+					return new ZipConditionsReader(file);
+				}
+				catch (FileNotFoundException x)
+				{
+					throw new ConditionsNotFoundException(name,run);
+				}
+			}
+		}
+		catch (MalformedURLException x)
+		{
+			throw new ConditionsNotFoundException(name,run,x);
+		}
+		catch (IOException x)
+		{
+			throw new ConditionsNotFoundException(name,run,x);
+		}
+	}
+
+	abstract InputStream open(String name, String type) throws IOException;
+	abstract void close() throws IOException;
+	private static Properties loadAliases()
+	{
+		Properties result = new Properties();
+		try
+		{
+			File f = new File(home,"alias.properties");
+			InputStream in = new FileInputStream(f);
+			if (in != null)
+			{
+				try
+				{
+					result.load(in);
+				}
+				finally
+				{
+					in.close();
+				}
+			}
+		}
+		catch (IOException x)
+		{}
+		return result;
+	}
+
+	private static File downloadDetectorDescription(URL url) throws IOException
+	{
+		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
+		{
+			// Check if the file looks good. It should contain a file called detector.properties
+			// in the root directory
+			ZipFile zip = new ZipFile(file,ZipFile.OPEN_READ);
+			try
+			{
+				ZipEntry header = zip.getEntry("detector.properties");
+				if (header == null) throw new IOException("No detector.properties entry in file downloaded from "+url);
+				Properties props = new Properties();
+				props.load(zip.getInputStream(header));
+			}
+			finally
+			{
+				zip.close();
+			}
+		}
+	}
+	
+	/*
+	private static class PropertiesFileValidator implements Validator
+	{
+		public void checkValidity(URL url, File file) throws IOException
+		{
+			InputStream in = new FileInputStream(file);
+			try
+			{
+				Properties props = new Properties();
+				props.load(in);
+
+			}
+			finally
+			{
+				in.close();
+			}
+		}
+	}
+	*/
+	
+	private static class ZipConditionsReader extends ConditionsReader
+	{
+		private ZipFile zip;
+		ZipConditionsReader(File file) throws IOException
+		{
+			this.zip = new ZipFile(file,ZipFile.OPEN_READ);
+		}
+		InputStream open(String name, String type) throws IOException
+		{
+			ZipEntry entry = zip.getEntry(name+"."+type);
+			if (entry == null) throw new IOException("Conditions "+name+"."+type+" not found");
+			return zip.getInputStream(entry);
+		}
+		void close() throws IOException
+		{
+			zip.close();
+		}
+	}
+	private static class DirectoryConditionsReader extends ConditionsReader
+	{
+		private File dir;
+		DirectoryConditionsReader(File file) throws IOException
+		{
+			this.dir = file;
+		}
+		InputStream open(String name, String type) throws IOException
+		{
+			File file = new File(dir,name+"."+type);
+			if (!file.exists()) throw new IOException("Conditions "+name+"."+type+" not found");
+			return new BufferedInputStream(new FileInputStream(file));
+		}
+		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 lcsim-detector jar from the LCDetectors project is on the classpath.
+	 */
+	private static class BaseClasspathConditionsReader extends ConditionsReader
+	{
+		private String detectorName;
+		BaseClasspathConditionsReader(String detectorName) throws IOException
+		{
+			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
+		{
+			throw new IOException("Conditions "+name+"."+type+" not found");
+		}
+
+		void close() throws IOException
+		{
+		}
+	}
 }
CVSspam 0.2.8