lcsim-conditions/src/main/java/org/lcsim/conditions
diff -u -r1.1.1.1 -r1.2
--- ConditionsManager.java 25 Jan 2010 22:23:07 -0000 1.1.1.1
+++ ConditionsManager.java 29 Feb 2012 00:13:37 -0000 1.2
@@ -2,7 +2,7 @@
/**
* The conditions manager is the main interface to the conditions system. The conditions
- * manager allows conditions to be stored in a heirarchical structure, but places no
+ * manager allows conditions to be stored in a hierarchical structure, but places no
* restrictions on the type of data provided. The conditions manager makes no
* assumptions about how the data is stored or retrieved.
*/
lcsim-conditions/src/main/java/org/lcsim/conditions
diff -u -r1.1.1.1 -r1.2
--- ConditionsManagerImplementation.java 25 Jan 2010 22:23:07 -0000 1.1.1.1
+++ ConditionsManagerImplementation.java 29 Feb 2012 00:13:37 -0000 1.2
@@ -4,10 +4,8 @@
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
-import java.util.Iterator;
import java.util.List;
import java.util.Map;
-import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -24,7 +22,7 @@
private String detectorName;
private int run;
private List<ConditionsListener> listenerList = new ArrayList<ConditionsListener>();
- private Logger logger = Logger.getLogger(ConditionsManagerImplementation.class.getName());
+ private static final Logger logger = Logger.getLogger(ConditionsManagerImplementation.class.getName());
/**
* The default implementation of ConditionsManager.
@@ -43,7 +41,7 @@
if (!name.equals(detectorName))
{
setConditionsReader(ConditionsReader.create(name,run),name);
- logger.fine("Detector changed: "+name+" "+run);
+ logger.log(Level.FINE, "Detector changed: {0} {1}", new Object[]{name, run});
}
}
public void setConditionsReader(ConditionsReader newReader, String name)
@@ -75,7 +73,7 @@
{
CachedConditions cond = cache.get(name);
if (cond != null) return cond;
- logger.fine("Getting cached conditions "+name);
+ logger.log(Level.FINE, "Getting cached conditions {0}", name);
ConditionsConverter converter = converters.get(type);
if (converter == null) throw new ConditionsSetNotFoundException("No converter registered for type: "+type.getName());
cond = new CachedConditionsImplementation(this,name,converter);
@@ -122,7 +120,7 @@
{
try
{
- logger.fine("Reading raw conditions "+name);
+ logger.log(Level.FINE, "Reading raw conditions {0}", name);
return new ConditionsSetImplementation(this, name);
}
catch (IOException x)
@@ -133,7 +131,7 @@
public RawConditions getRawConditions(String name) throws ConditionsSetNotFoundException
{
- logger.fine("Reading raw conditions "+name);
+ logger.log(Level.FINE, "Reading raw conditions {0}", name);
return new RawConditionsImplementation(this, name);
}
}
\ No newline at end of file
lcsim-conditions/src/main/java/org/lcsim/conditions
diff -u -r1.1.1.1 -r1.2
--- ConditionsReader.java 25 Jan 2010 22:23:07 -0000 1.1.1.1
+++ ConditionsReader.java 29 Feb 2012 00:13:37 -0000 1.2
@@ -26,294 +26,285 @@
*
* @author tonyj
*/
-public abstract class ConditionsReader
-{
- 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());
- // Check if exists.
- if (!file.exists())
- {
- throw new RuntimeException("The URL " + url.toString() + " used by detector " + name + " does not exist.");
- }
- 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, because directory " + file.getAbsolutePath() + " does not exist.");
- 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 the 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
- {}
- }
+public abstract class ConditionsReader {
+
+ 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());
+ // Check if exists.
+ if (!file.exists()) {
+ throw new RuntimeException("The URL " + url.toString() + " used by detector " + name + " does not exist.");
+ }
+ 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, because directory " + file.getAbsolutePath() + " does not exist.");
+ }
+ 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 the 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 {
+ }
+ }
}