Commit in lcsim-conditions/src/main/java/org/lcsim on MAIN | |||
conditions/CachedConditions.java | +4 | -4 | 1.1.1.1 -> 1.2 |
/CachedConditionsImplementation.java | +20 | -20 | 1.1.1.1 -> 1.2 |
/Conditions.java | +19 | -17 | 1.1.1.1 -> 1.2 |
/ConditionsConverter.java | +6 | -5 | 1.1.1.1 -> 1.2 |
/ConditionsEvent.java | +9 | -11 | 1.1.1.1 -> 1.2 |
/ConditionsImplementation.java | +38 | -46 | 1.1.1.1 -> 1.2 |
/ConditionsListener.java | +8 | -9 | 1.1.1.1 -> 1.2 |
/ConditionsManager.java | +139 | -127 | 1.2 -> 1.3 |
/ConditionsReader.java | +91 | -90 | 1.5 -> 1.6 |
/ConditionsSet.java | +28 | -22 | 1.1.1.1 -> 1.2 |
/ConditionsSetImplementation.java | +150 | -170 | 1.1.1.1 -> 1.2 |
/RawConditions.java | +5 | -5 | 1.1.1.1 -> 1.2 |
/RawConditionsImplementation.java | +23 | -27 | 1.1.1.1 -> 1.2 |
util/cache/ByteFormat.java | +52 | -58 | 1.1.1.1 -> 1.2 |
/CachingEntityResolver.java | +22 | -23 | 1.1.1.1 -> 1.2 |
/FileCache.java | +185 | -195 | 1.1.1.1 -> 1.2 |
+799 | -829 |
code formatting
diff -u -r1.1.1.1 -r1.2 --- CachedConditions.java 25 Jan 2010 22:23:07 -0000 1.1.1.1 +++ CachedConditions.java 18 Oct 2013 21:42:47 -0000 1.2 @@ -1,10 +1,10 @@
package org.lcsim.conditions; /**
- * @version $Id: CachedConditions.java,v 1.1.1.1 2010/01/25 22:23:07 jeremy Exp $
+ * @version $Id: CachedConditions.java,v 1.1.1.1 2010/01/25 22:23:07 jeremy Exp + * $
* @author tonyj */
-public interface CachedConditions<T> extends Conditions -{ - T getCachedData();
+public interface CachedConditions<T> extends Conditions { + T getCachedData();
}
\ No newline at end of file
diff -u -r1.1.1.1 -r1.2 --- CachedConditionsImplementation.java 25 Jan 2010 22:23:07 -0000 1.1.1.1 +++ CachedConditionsImplementation.java 18 Oct 2013 21:42:47 -0000 1.2 @@ -1,27 +1,27 @@
package org.lcsim.conditions; /**
- * @version $Id: CachedConditionsImplementation.java,v 1.1.1.1 2010/01/25 22:23:07 jeremy Exp $
+ * @version $Id: CachedConditionsImplementation.java,v 1.1.1.1 2010/01/25 + * 22:23:07 jeremy Exp $
* @author tonyj */
-class CachedConditionsImplementation<T> extends ConditionsImplementation implements CachedConditions<T>, ConditionsListener -{ - private ConditionsConverter<T> conv; - private T data; - CachedConditionsImplementation(ConditionsManagerImplementation manager, String name, ConditionsConverter<T> conv) - { - super(manager,name); - this.conv = conv; - manager.addConditionsListener(this); - } - public T getCachedData() - { - if (data == null) data = conv.getData(getManager(), getName()); - return data; - }
+class CachedConditionsImplementation<T> extends ConditionsImplementation implements CachedConditions<T>, ConditionsListener { + private ConditionsConverter<T> conv; + private T data;
- public void conditionsChanged(ConditionsEvent conditionsEvent) - { - data = null; - }
+ CachedConditionsImplementation(ConditionsManagerImplementation manager, String name, ConditionsConverter<T> conv) { + super(manager, name); + this.conv = conv; + manager.addConditionsListener(this); + } + + public T getCachedData() { + if (data == null) + data = conv.getData(getManager(), getName()); + return data; + } + + public void conditionsChanged(ConditionsEvent conditionsEvent) { + data = null; + }
}
diff -u -r1.1.1.1 -r1.2 --- Conditions.java 25 Jan 2010 22:23:07 -0000 1.1.1.1 +++ Conditions.java 18 Oct 2013 21:42:47 -0000 1.2 @@ -4,23 +4,25 @@
/** * A base class implemented by all types of conditions.
- * @version $Id: Conditions.java,v 1.1.1.1 2010/01/25 22:23:07 jeremy Exp $
+ * @version $Id: Conditions.java,v 1.2 2013/10/18 21:42:47 jeremy Exp $
* @author Tony Johnson */
-public interface Conditions -{ - ConditionsSet getSubConditions(String name) throws ConditionsSetNotFoundException; - RawConditions getRawSubConditions(String name) throws ConditionsSetNotFoundException; - <T> CachedConditions<T> getCachedSubConditions(Class<T> type, String name) throws ConditionsSetNotFoundException; - - /** - * Add a listener to be notified about changes to these conditions. - * @param listener The listener to add. - */ - void addConditionsListener(ConditionsListener listener); - /** - * Remove a change listener from these conditions. - * @param listener The listener to remove. - */ - void removeConditionsListener(ConditionsListener listener);
+public interface Conditions { + ConditionsSet getSubConditions(String name) throws ConditionsSetNotFoundException; + + RawConditions getRawSubConditions(String name) throws ConditionsSetNotFoundException; + + <T> CachedConditions<T> getCachedSubConditions(Class<T> type, String name) throws ConditionsSetNotFoundException; + + /** + * Add a listener to be notified about changes to these conditions. + * @param listener The listener to add. + */ + void addConditionsListener(ConditionsListener listener); + + /** + * Remove a change listener from these conditions. + * @param listener The listener to remove. + */ + void removeConditionsListener(ConditionsListener listener);
}
diff -u -r1.1.1.1 -r1.2 --- ConditionsConverter.java 25 Jan 2010 22:23:07 -0000 1.1.1.1 +++ ConditionsConverter.java 18 Oct 2013 21:42:47 -0000 1.2 @@ -1,11 +1,12 @@
package org.lcsim.conditions; /**
- * @version $Id: ConditionsConverter.java,v 1.1.1.1 2010/01/25 22:23:07 jeremy Exp $
+ * @version $Id: ConditionsConverter.java,v 1.1.1.1 2010/01/25 22:23:07 jeremy + * Exp $
* @author tonyj */
-public interface ConditionsConverter<T> -{ - Class<T> getType(); - T getData(ConditionsManager manager, String name);
+public interface ConditionsConverter<T> { + Class<T> getType(); + + T getData(ConditionsManager manager, String name);
}
\ No newline at end of file
diff -u -r1.1.1.1 -r1.2 --- ConditionsEvent.java 25 Jan 2010 22:23:07 -0000 1.1.1.1 +++ ConditionsEvent.java 18 Oct 2013 21:42:47 -0000 1.2 @@ -3,17 +3,15 @@
import java.util.EventObject; /**
- * @version $Id: ConditionsEvent.java,v 1.1.1.1 2010/01/25 22:23:07 jeremy Exp $
+ * @version $Id: ConditionsEvent.java,v 1.2 2013/10/18 21:42:47 jeremy Exp $
* @author Tony Johnson */
-public class ConditionsEvent extends EventObject -{ - ConditionsEvent(ConditionsManager source) - { - super(source); - } - public ConditionsManager getConditionsManager() - { - return (ConditionsManager) getSource(); - }
+public class ConditionsEvent extends EventObject { + ConditionsEvent(ConditionsManager source) { + super(source); + } + + public ConditionsManager getConditionsManager() { + return (ConditionsManager) getSource(); + }
}
diff -u -r1.1.1.1 -r1.2 --- ConditionsImplementation.java 25 Jan 2010 22:23:07 -0000 1.1.1.1 +++ ConditionsImplementation.java 18 Oct 2013 21:42:47 -0000 1.2 @@ -1,52 +1,44 @@
package org.lcsim.conditions; /**
- *
+ *
* @author Tony Johnson */
-abstract class ConditionsImplementation implements Conditions -{ - private ConditionsManagerImplementation manager; - private String name; - /** Creates a new instance of ConditionsImplementation */ - ConditionsImplementation(ConditionsManagerImplementation manager, String name) - { - this.manager = manager; - this.name = name; - } - - public void addConditionsListener(ConditionsListener listener) - { - manager.addConditionsListener(listener); - } - - public void removeConditionsListener(ConditionsListener listener) - { - manager.removeConditionsListener(listener); - } - - public RawConditions getRawSubConditions(String name) - { - return manager.getRawConditions(this.name+"/"+name); - } - - public ConditionsSet getSubConditions(String name) - { - return manager.getConditions(this.name+"/"+name); - } - - public <T> CachedConditions<T> getCachedSubConditions(Class<T> type, String name) - { - return manager.getCachedConditions(type,this.name+"/"+name); - } - - String getName() - { - return name; - } - - ConditionsManagerImplementation getManager() - { - return manager; - }
+abstract class ConditionsImplementation implements Conditions { + private ConditionsManagerImplementation manager; + private String name; + + /** Creates a new instance of ConditionsImplementation */ + ConditionsImplementation(ConditionsManagerImplementation manager, String name) { + this.manager = manager; + this.name = name; + } + + public void addConditionsListener(ConditionsListener listener) { + manager.addConditionsListener(listener); + } + + public void removeConditionsListener(ConditionsListener listener) { + manager.removeConditionsListener(listener); + } + + public RawConditions getRawSubConditions(String name) { + return manager.getRawConditions(this.name + "/" + name); + } + + public ConditionsSet getSubConditions(String name) { + return manager.getConditions(this.name + "/" + name); + } + + public <T> CachedConditions<T> getCachedSubConditions(Class<T> type, String name) { + return manager.getCachedConditions(type, this.name + "/" + name); + } + + String getName() { + return name; + } + + ConditionsManagerImplementation getManager() { + return manager; + }
}
\ No newline at end of file
diff -u -r1.1.1.1 -r1.2 --- ConditionsListener.java 25 Jan 2010 22:23:07 -0000 1.1.1.1 +++ ConditionsListener.java 18 Oct 2013 21:42:47 -0000 1.2 @@ -3,15 +3,14 @@
import java.util.EventListener; /**
- * A conditions listener can be registered by objects wishing to be notified when - * a specific set of conditions changes.
+ * A conditions listener can be registered by objects wishing to be notified + * when a specific set of conditions changes.
* @author Tony Johnson */
-public interface ConditionsListener extends EventListener -{ - /** - * Called when the conditions associated with this listener change. - * @param event The event associated with the change. - */ - void conditionsChanged(ConditionsEvent event);
+public interface ConditionsListener extends EventListener { + /** + * Called when the conditions associated with this listener change. + * @param event The event associated with the change. + */ + void conditionsChanged(ConditionsEvent event);
}
diff -u -r1.2 -r1.3 --- ConditionsManager.java 29 Feb 2012 00:13:37 -0000 1.2 +++ ConditionsManager.java 18 Oct 2013 21:42:47 -0000 1.3 @@ -1,132 +1,144 @@
package org.lcsim.conditions; /**
- * The conditions manager is the main interface to the conditions system. The conditions - * 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.
+ * The conditions manager is the main interface to the conditions system. The + * conditions 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.
*/
-public abstract class ConditionsManager -{ - private static ConditionsManager theConditionsManager; - /** - * Get the default (shared) condition manager implementation. - * @return The default conditions manager. - */ - public static ConditionsManager defaultInstance() - { - if (theConditionsManager == null) theConditionsManager = new ConditionsManagerImplementation(); - return theConditionsManager; - } - public static void setDefaultConditionsManager(ConditionsManager manager) - { - theConditionsManager = manager; - } - /** - * Normally called automatically by the framework to set the - * detector name and run number, - * @param name The current detector name. - * @param run The current run number. - * @throws org.lcsim.conditions.ConditionsManager.ConditionsNotFoundException If the conditions associated with this detector/run number can - * not be found. - */ - public abstract void setDetector(String name, int run) throws ConditionsNotFoundException; - /** - * Set the current run number. Normally called automatically by the framework. - * @param run The current run number - * @throws org.lcsim.conditions.ConditionsManager.ConditionsNotFoundException If the conditions associated with the specified run number - * can not be found. - */ - public abstract void setRun(int run) throws ConditionsNotFoundException; - /** - * Get the current detector name - * @return The detector name. - */ - public abstract String getDetector(); - /** - * Get the current run number. - * @return The run number. - */ - public abstract int getRun(); - /** - * Get the conditions associated with the given name. - * @param name The name of the conditions to search for. - * @throws org.lcsim.conditions.ConditionsManager.ConditionsSetNotFoundException If the named conditions can not be found. - * @return The requested conditions. - */ - public abstract ConditionsSet getConditions(String name) throws ConditionsSetNotFoundException; - /** - * Access conditions converted to a java object using a conditions converter. The - * conditions are cached so that they do not need to be re-read each time the same - * object is requested. - * @param type The type of conditions requested (used to select an appropriate conditions - * converter). - * @param name The name of the conditions requested. - * @throws org.lcsim.conditions.ConditionsManager.ConditionsSetNotFoundException If the specified conditions can not be found. - * @return The converted conditions. - */ - public abstract <T> CachedConditions<T> getCachedConditions(Class<T> type, String name) throws ConditionsSetNotFoundException; - /** - * Get an input stream to directly read raw conditions from the database. The database - * makes no assumptions about the format of the data when this method is used. - * @param name The name of the conditions requested. - * @throws org.lcsim.conditions.ConditionsManager.ConditionsSetNotFoundException If the requested conditions can not be found. - * @return The input stream from which the data can be read. The called should - * close thsi input stream when they have finished using it. - */ - public abstract RawConditions getRawConditions(String name) throws ConditionsSetNotFoundException; - - /** - * Adds a conditions converter. A conditions converter can be used to convert data - * requested by the user into a specific Java object. - * @param conv The converter to add. - */ - public abstract void registerConditionsConverter(ConditionsConverter conv); - /** - * Removes a conditions converter. - * @param conv The converter to remove. - */ - public abstract void removeConditionsConverter(ConditionsConverter conv); - - /** - * Add a listener to be notified about changes to ANY conditions. - * @param listener The listener to add. - */ - public abstract void addConditionsListener(ConditionsListener listener); - - /** - * Remove a global change listener. - * @param listener The listener to remove. - */ - public abstract void removeConditionsListener(ConditionsListener listener); - - /** - * Thrown if conditions associated with a given detector can not be found. - */ - public static class ConditionsNotFoundException extends Exception - { - public ConditionsNotFoundException(String name, int run) - { - super("Conditions not found for detector "+name); - } - public ConditionsNotFoundException(String name, int run, Throwable t) - { - super("Conditions not found for detector "+name,t); - } - } - - /** - * Thrown if specific set of conditions can not be found. - */ - public static class ConditionsSetNotFoundException extends RuntimeException - { - public ConditionsSetNotFoundException(String message) - { - super(message); - } - public ConditionsSetNotFoundException(String message, Throwable t) - { - super(message,t); - } - }
+public abstract class ConditionsManager { + private static ConditionsManager theConditionsManager; + + /** + * Get the default (shared) condition manager implementation. + * @return The default conditions manager. + */ + public static ConditionsManager defaultInstance() { + if (theConditionsManager == null) + theConditionsManager = new ConditionsManagerImplementation(); + return theConditionsManager; + } + + public static void setDefaultConditionsManager(ConditionsManager manager) { + theConditionsManager = manager; + } + + /** + * Normally called automatically by the framework to set the detector name + * and run number, + * @param name The current detector name. + * @param run The current run number. + * @throws org.lcsim.conditions.ConditionsManager.ConditionsNotFoundException + * If the conditions associated with this detector/run number + * can not be found. + */ + public abstract void setDetector(String name, int run) throws ConditionsNotFoundException; + + /** + * Set the current run number. Normally called automatically by the + * framework. + * @param run The current run number + * @throws org.lcsim.conditions.ConditionsManager.ConditionsNotFoundException + * If the conditions associated with the specified run number + * can not be found. + */ + public abstract void setRun(int run) throws ConditionsNotFoundException; + + /** + * Get the current detector name + * @return The detector name. + */ + public abstract String getDetector(); + + /** + * Get the current run number. + * @return The run number. + */ + public abstract int getRun(); + + /** + * Get the conditions associated with the given name. + * @param name The name of the conditions to search for. + * @throws org.lcsim.conditions.ConditionsManager.ConditionsSetNotFoundException + * If the named conditions can not be found. + * @return The requested conditions. + */ + public abstract ConditionsSet getConditions(String name) throws ConditionsSetNotFoundException; + + /** + * Access conditions converted to a java object using a conditions + * converter. The conditions are cached so that they do not need to be + * re-read each time the same object is requested. + * @param type The type of conditions requested (used to select an + * appropriate conditions converter). + * @param name The name of the conditions requested. + * @throws org.lcsim.conditions.ConditionsManager.ConditionsSetNotFoundException + * If the specified conditions can not be found. + * @return The converted conditions. + */ + public abstract <T> CachedConditions<T> getCachedConditions(Class<T> type, String name) throws ConditionsSetNotFoundException; + + /** + * Get an input stream to directly read raw conditions from the database. + * The database makes no assumptions about the format of the data when this + * method is used. + * @param name The name of the conditions requested. + * @throws org.lcsim.conditions.ConditionsManager.ConditionsSetNotFoundException + * If the requested conditions can not be found. + * @return The input stream from which the data can be read. The called + * should close thsi input stream when they have finished using it. + */ + public abstract RawConditions getRawConditions(String name) throws ConditionsSetNotFoundException; + + /** + * Adds a conditions converter. A conditions converter can be used to + * convert data requested by the user into a specific Java object. + * @param conv The converter to add. + */ + public abstract void registerConditionsConverter(ConditionsConverter conv); + + /** + * Removes a conditions converter. + * @param conv The converter to remove. + */ + public abstract void removeConditionsConverter(ConditionsConverter conv); + + /** + * Add a listener to be notified about changes to ANY conditions. + * @param listener The listener to add. + */ + public abstract void addConditionsListener(ConditionsListener listener); + + /** + * Remove a global change listener. + * @param listener The listener to remove. + */ + public abstract void removeConditionsListener(ConditionsListener listener); + + /** + * Thrown if conditions associated with a given detector can not be found. + */ + public static class ConditionsNotFoundException extends Exception { + public ConditionsNotFoundException(String name, int run) { + super("Conditions not found for detector " + name); + } + + public ConditionsNotFoundException(String name, int run, Throwable t) { + super("Conditions not found for detector " + name, t); + } + } + + /** + * Thrown if specific set of conditions can not be found. + */ + public static class ConditionsSetNotFoundException extends RuntimeException { + public ConditionsSetNotFoundException(String message) { + super(message); + } + + public ConditionsSetNotFoundException(String message, Throwable t) { + super(message, t); + } + }
}
\ No newline at end of file
diff -u -r1.5 -r1.6 --- ConditionsReader.java 10 Jul 2012 18:03:56 -0000 1.5 +++ ConditionsReader.java 18 Oct 2013 21:42:47 -0000 1.6 @@ -23,7 +23,7 @@
import org.lcsim.util.cache.FileCache.Validator; /**
- *
+ *
* @author tonyj */ public abstract class ConditionsReader {
@@ -31,29 +31,33 @@
private static Properties aliases; private static final File home = new File(FileCache.getCacheRoot(), ".lcsim"); private static FileCache cache;
-
+
/**
- * Called by {@link ConditionsManager#setDetector} to tell this reader that it needs to prepare - * itself for reading conditions for the specified detector and run.
+ * Called by {@link ConditionsManager#setDetector} to tell this reader that + * it needs to prepare itself for reading conditions for the specified + * detector and run.
* <p>
- * The implementation provided by this class returns <tt>false</tt> if the specified detector - * name is equal to the name of the current detector known to the specified {@link ConditionsManager}, - * and throws <tt>IllegalArgumentException</tt> if it is not. Subclasses need to override this method - * if conditions might be different for different runs with the same detector.
+ * The implementation provided by this class returns <tt>false</tt> if the + * specified detector name is equal to the name of the current detector + * known to the specified {@link ConditionsManager}, and throws + * <tt>IllegalArgumentException</tt> if it is not. Subclasses need to + * override this method if conditions might be different for different runs + * with the same detector.
*
- * @return <tt>true</tt> if conditions for the specified detector/run may be different - * from conditions for the previous detector/run combination; <tt>false</tt> otherwise. - * @throws IllegalArgumentException if this <tt>ConditionsReader</tt> cannot handle the - * specified detector/run.
+ * @return <tt>true</tt> if conditions for the specified detector/run may be + * different from conditions for the previous detector/run + * combination; <tt>false</tt> otherwise. + * @throws IllegalArgumentException if this <tt>ConditionsReader</tt> cannot + * handle the specified detector/run.
* @throws IOException if the reader fails to update for any other reason. */
- protected boolean update(ConditionsManager manager, String detectorName, int run) throws IOException { - if (detectorName.equals(manager.getDetector())) { - return false; - } else { - throw new IllegalArgumentException(); - } - }
+ protected boolean update(ConditionsManager manager, String detectorName, int run) throws IOException { + if (detectorName.equals(manager.getDetector())) { + return false; + } else { + throw new IllegalArgumentException(); + } + }
/** * Get a list of available detectors
@@ -118,17 +122,18 @@
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
+ /** + * 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 {
- //boolean debug = true; - //if (debug) - // System.out.println("ConditionsReader.create - " + detectorName); - //System.out.println("ConditionsReader.create - " + detectorName); - //System.out.println("Called from ..."); - //(new RuntimeException()).printStackTrace();
+ // boolean debug = true; + // if (debug) + // System.out.println("ConditionsReader.create - " + detectorName); + // System.out.println("ConditionsReader.create - " + detectorName); + // System.out.println("Called from ..."); + // (new RuntimeException()).printStackTrace();
String name = detectorName; try { if (cache == null) {
@@ -141,7 +146,7 @@
name = resolveAlias(detectorName); if (name.contains(":")) {
- // Name is a URL.
+ // 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());
@@ -159,14 +164,14 @@
return new ZipConditionsReader(file); } } else {
- - // Search the classpath for conditions.
+ + // Search the classpath for conditions.
try { return new BaseClasspathConditionsReader(name); } catch (IOException x) {
- //System.out.println(x.getLocalizedMessage());
+ // System.out.println(x.getLocalizedMessage());
}
-
+
// Search for a local, cached copy. File detectorDir = new File(home, "detectors"); File zipFile = new File(detectorDir, name + ".zip");
@@ -178,7 +183,8 @@
return new DirectoryConditionsReader(dirFile); }
- // Finally, try to pull the detector conditions from the lcsim.org website.
+ // 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);
@@ -193,37 +199,41 @@
throw new ConditionsNotFoundException(name, run, x); } }
-
+
/**
- * Creates <tt>ConditionsReader</tt> to handle the specified detector and run. - * @throws ConditionsNotFoundException if creation of the reader fails for any reason.
+ * Creates <tt>ConditionsReader</tt> to handle the specified detector and + * run. + * @throws ConditionsNotFoundException if creation of the reader fails for + * any reason.
*/ static ConditionsReader create(ConditionsManager manager, String detectorName, int run) throws ConditionsNotFoundException {
- ConditionsReader reader = create(detectorName, run); - Properties prop = new Properties(); - try { - InputStream in = reader.open("detector", "properties"); - try { - prop.load(in); - } finally { - in.close(); - } - } catch (IOException x) { - // For now: having failed to find or load detector.properties, use unmodified reader - // Uncomment the line below if we decide this should be treated as an error - //throw new ConditionsNotFoundException(detectorName, run, x); - } - String readerClassName = prop.getProperty("ConditionsReader"); - if (readerClassName != null) {
+ ConditionsReader reader = create(detectorName, run); + Properties prop = new Properties();
try {
- Class readerClass = Class.forName(readerClassName); - reader = (ConditionsReader) readerClass.getDeclaredConstructor(ConditionsReader.class).newInstance(reader); - reader.update(manager, detectorName, run); - } catch (Exception x) { - throw new ConditionsNotFoundException(detectorName, run, x);
+ InputStream in = reader.open("detector", "properties"); + try { + prop.load(in); + } finally { + in.close(); + } + } catch (IOException x) { + // For now: having failed to find or load detector.properties, use + // unmodified reader + // Uncomment the line below if we decide this should be treated as + // an error + // throw new ConditionsNotFoundException(detectorName, run, x);
}
- } - return reader;
+ String readerClassName = prop.getProperty("ConditionsReader"); + if (readerClassName != null) { + try { + Class readerClass = Class.forName(readerClassName); + reader = (ConditionsReader) readerClass.getDeclaredConstructor(ConditionsReader.class).newInstance(reader); + reader.update(manager, detectorName, run); + } catch (Exception x) { + throw new ConditionsNotFoundException(detectorName, run, x); + } + } + return reader;
} abstract public InputStream open(String name, String type) throws IOException;
@@ -251,14 +261,15 @@
return cache.getCachedFile(url, new DetectorFileValidator()); }
- //private static File downloadAliasFile(URL url) throws IOException - //{ - // return cache.getCachedFile(url,new PropertiesFileValidator()); - //}
+ // 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
+ // 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 {
@@ -275,23 +286,12 @@
} /*
- 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 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 {
@@ -335,20 +335,21 @@
} /**
- * This ConditionsReader finds detector conditions with the assumption that they - * are located in a package called <code>detectorName</code>, which must be - * accessible to the ContextClassLoader of the current thread.
+ * This ConditionsReader finds detector conditions with the assumption that + * they are located in a package called <code>detectorName</code>, which + * must be accessible to the ContextClassLoader of the current thread.
*/ private static class BaseClasspathConditionsReader extends ConditionsReader { private String detectorName;
- //private ClassLoader classLoader;
+ // private ClassLoader classLoader;
String propFileName = "detector.properties"; BaseClasspathConditionsReader(String detectorName) throws IOException { this.detectorName = detectorName;
- //classLoader = Thread.currentThread().getContextClassLoader(); - //if (ConditionsReader.class.getResourceAsStream("/" + detectorName + "/detector.properties") == null) {
+ // classLoader = Thread.currentThread().getContextClassLoader(); + // if (ConditionsReader.class.getResourceAsStream("/" + detectorName + // + "/detector.properties") == null) {
if (getClass().getResourceAsStream("/" + detectorName + "/" + propFileName) == null) { throw new IOException("Unable to find " + detectorName + "/" + propFileName + " on the classpath."); }
@@ -362,8 +363,8 @@
return in; }
- public void close() throws IOException - {}
+ public void close() throws IOException { + }
} private static class DummyConditionsReader extends ConditionsReader {
diff -u -r1.1.1.1 -r1.2 --- ConditionsSet.java 25 Jan 2010 22:23:07 -0000 1.1.1.1 +++ ConditionsSet.java 18 Oct 2013 21:42:47 -0000 1.2 @@ -3,29 +3,35 @@
import java.util.Set; /**
- * - * @author Tony Johnson - * $Id: ConditionsSet.java,v 1.1.1.1 2010/01/25 22:23:07 jeremy Exp $
+ * + * @author Tony Johnson $Id: ConditionsSet.java,v 1.1.1.1 2010/01/25 22:23:07 + * jeremy Exp $
*/
-public interface ConditionsSet extends Conditions -{ - double getDouble(String key) throws IllegalArgumentException; - double getDouble(String key, double defValue); - double[] getDoubleArray(String key) throws IllegalArgumentException; - - int getInt(String key) throws IllegalArgumentException; - int getInt(String key, int defValue); - - String getString(String key) throws IllegalArgumentException; - String getString(String key, String defValue); - - boolean getBoolean(String key) throws IllegalArgumentException; - boolean getBoolean(String key, boolean defValue); - - int size(); - Set keySet(); - boolean containsKey(String key); - Class getType(String key);
+public interface ConditionsSet extends Conditions { + double getDouble(String key) throws IllegalArgumentException;
+ double getDouble(String key, double defValue); + + double[] getDoubleArray(String key) throws IllegalArgumentException; + + int getInt(String key) throws IllegalArgumentException; + + int getInt(String key, int defValue); + + String getString(String key) throws IllegalArgumentException; + + String getString(String key, String defValue); + + boolean getBoolean(String key) throws IllegalArgumentException; + + boolean getBoolean(String key, boolean defValue); + + int size(); + + Set keySet(); + + boolean containsKey(String key); + + Class getType(String key);
}
diff -u -r1.1.1.1 -r1.2 --- ConditionsSetImplementation.java 25 Jan 2010 22:23:07 -0000 1.1.1.1 +++ ConditionsSetImplementation.java 18 Oct 2013 21:42:47 -0000 1.2 @@ -9,175 +9,155 @@
import java.util.StringTokenizer; /**
- * - * @author Tony Johnson - * $Id: ConditionsSetImplementation.java,v 1.1.1.1 2010/01/25 22:23:07 jeremy Exp $
+ * + * @author Tony Johnson $Id: ConditionsSetImplementation.java,v 1.1.1.1 + * 2010/01/25 22:23:07 jeremy Exp $
*/
-class ConditionsSetImplementation extends ConditionsImplementation implements ConditionsSet -{ - ConditionsSetImplementation(ConditionsManagerImplementation manager, String name) throws IOException - { - super(manager,name); - // We assume this points to a properties file - InputStream in = manager.open(name,"properties"); - try - { - props = new Properties(); - props.load(in); - } - finally - { - in.close(); - } - } - - public int getInt(String name) - { - String value = props.getProperty(name); - if (value == null) throw new IllegalArgumentException("Missing value for "+name); - return parseInt(value,name); - } - public int getInt(String name, int defaultValue) - { - String value = props.getProperty(name); - if (value == null) return defaultValue; - return parseInt(value,name); - } - public double getDouble(String name) - { - String value = props.getProperty(name); - if (value == null) throw new IllegalArgumentException("Missing value for "+name); - return parseDouble(value,name); - } - public double getDouble(String name, double defaultValue) - { - String value = props.getProperty(name); - if (value == null) return defaultValue; - return parseDouble(value,name); - } - - public double[] getDoubleArray(String name) - { - String value = props.getProperty(name); - if (value == null) throw new IllegalArgumentException("Missing value for "+name); - return parseDoubleArray(value,name); - } - private int parseInt(String value, String name) - { - try - { - return Integer.parseInt(value); - } - catch (NumberFormatException e) - { - throw new RuntimeException("Error reading conditions: Illegal value "+value+" for "+name); - } - } - private double parseDouble(String value, String name) - { - try - { - return Double.parseDouble(value); - } - catch (NumberFormatException e) - { - throw new RuntimeException("Error reading conditions: Illegal value "+value+" for "+name); - } - } - private double[] parseDoubleArray(String value, String name) - { - List<Double> doubles = new ArrayList<Double>(); - StringTokenizer st = new StringTokenizer(value,","); - while(st.hasMoreTokens()) - { - String doubleValue = st.nextToken(); - try - { - doubles.add( Double.parseDouble(doubleValue) ); - } - catch (NumberFormatException e) - { - throw new RuntimeException("Error reading conditions: Illegal value "+value+" for "+name); - } - } - // should be an easier way to do this... - double[] returnDoubles = new double[doubles.size()]; - int i=0; - for(Double d : doubles) - { - returnDoubles[i++] = d; - } - return returnDoubles; - } - public String getString(String name) - { - String value = props.getProperty(name); - if (value == null) throw new IllegalArgumentException("Missing value for "+name); - return value; - } - public String getString(String name, String defValue) - { - return props.getProperty(name,defValue); - } - - public boolean getBoolean(String name) throws IllegalArgumentException - { - String value = props.getProperty(name); - if (value == null) throw new IllegalArgumentException("Missing value for "+name); - return parseBoolean(value,name); - } - - public boolean getBoolean(String name, boolean defaultValue) - { - String value = props.getProperty(name); - if (value == null) return defaultValue; - return parseBoolean(value,name); - } - - private boolean parseBoolean(String value, String name) - { - if ("true".equalsIgnoreCase(value) || "yes".equalsIgnoreCase(value)) return true; - if ("false".equalsIgnoreCase(value) || "no".equalsIgnoreCase(value)) return false; - throw new RuntimeException("Error reading conditions: Illegal value "+value+" for "+name); - } - - public Set keySet() - { - return props.keySet(); - } - - public Class getType(String key) - { - String value = props.getProperty(key); - if (value == null) throw new IllegalArgumentException("Missing value for "+key); - try - { - Integer.parseInt(value); - return Integer.TYPE; - } - catch (NumberFormatException x) - { - try - { - Double.parseDouble(value); - return Double.TYPE; - } - catch (NumberFormatException xx) - { - return String.class; - } - } - } - - public int size() - { - return props.size(); - } - - public boolean containsKey(String key) - { - return props.containsKey(key); - } - - private Properties props;
+class ConditionsSetImplementation extends ConditionsImplementation implements ConditionsSet { + ConditionsSetImplementation(ConditionsManagerImplementation manager, String name) throws IOException { + super(manager, name); + // We assume this points to a properties file + InputStream in = manager.open(name, "properties"); + try { + props = new Properties(); + props.load(in); + } finally { + in.close(); + } + } + + public int getInt(String name) { + String value = props.getProperty(name); + if (value == null) + throw new IllegalArgumentException("Missing value for " + name); + return parseInt(value, name); + } + + public int getInt(String name, int defaultValue) { + String value = props.getProperty(name); + if (value == null) + return defaultValue; + return parseInt(value, name); + } + + public double getDouble(String name) { + String value = props.getProperty(name); + if (value == null) + throw new IllegalArgumentException("Missing value for " + name); + return parseDouble(value, name); + } + + public double getDouble(String name, double defaultValue) { + String value = props.getProperty(name); + if (value == null) + return defaultValue; + return parseDouble(value, name); + } + + public double[] getDoubleArray(String name) { + String value = props.getProperty(name); + if (value == null) + throw new IllegalArgumentException("Missing value for " + name); + return parseDoubleArray(value, name); + } + + private int parseInt(String value, String name) { + try { + return Integer.parseInt(value); + } catch (NumberFormatException e) { + throw new RuntimeException("Error reading conditions: Illegal value " + value + " for " + name); + } + } + + private double parseDouble(String value, String name) { + try { + return Double.parseDouble(value); + } catch (NumberFormatException e) { + throw new RuntimeException("Error reading conditions: Illegal value " + value + " for " + name); + } + } + + private double[] parseDoubleArray(String value, String name) { + List<Double> doubles = new ArrayList<Double>(); + StringTokenizer st = new StringTokenizer(value, ","); + while (st.hasMoreTokens()) { + String doubleValue = st.nextToken(); + try { + doubles.add(Double.parseDouble(doubleValue)); + } catch (NumberFormatException e) { + throw new RuntimeException("Error reading conditions: Illegal value " + value + " for " + name); + } + } + // should be an easier way to do this... + double[] returnDoubles = new double[doubles.size()]; + int i = 0; + for (Double d : doubles) { + returnDoubles[i++] = d; + } + return returnDoubles; + } + + public String getString(String name) { + String value = props.getProperty(name); + if (value == null) + throw new IllegalArgumentException("Missing value for " + name); + return value; + } + + public String getString(String name, String defValue) { + return props.getProperty(name, defValue); + } + + public boolean getBoolean(String name) throws IllegalArgumentException { + String value = props.getProperty(name); + if (value == null) + throw new IllegalArgumentException("Missing value for " + name); + return parseBoolean(value, name); + } + + public boolean getBoolean(String name, boolean defaultValue) { + String value = props.getProperty(name); + if (value == null) + return defaultValue; + return parseBoolean(value, name); + } + + private boolean parseBoolean(String value, String name) { + if ("true".equalsIgnoreCase(value) || "yes".equalsIgnoreCase(value)) + return true; + if ("false".equalsIgnoreCase(value) || "no".equalsIgnoreCase(value)) + return false; + throw new RuntimeException("Error reading conditions: Illegal value " + value + " for " + name); + } + + public Set keySet() { + return props.keySet(); + } + + public Class getType(String key) { + String value = props.getProperty(key); + if (value == null) + throw new IllegalArgumentException("Missing value for " + key); + try { + Integer.parseInt(value); + return Integer.TYPE; + } catch (NumberFormatException x) { + try { + Double.parseDouble(value); + return Double.TYPE; + } catch (NumberFormatException xx) { + return String.class; + } + } + } + + public int size() { + return props.size(); + } + + public boolean containsKey(String key) { + return props.containsKey(key); + } + + private Properties props;
}
diff -u -r1.1.1.1 -r1.2 --- RawConditions.java 25 Jan 2010 22:23:07 -0000 1.1.1.1 +++ RawConditions.java 18 Oct 2013 21:42:47 -0000 1.2 @@ -5,11 +5,11 @@
import java.io.Reader; /**
- *
+ *
* @author Tony Johnson */
-public interface RawConditions extends Conditions -{ - InputStream getInputStream() throws IOException; - Reader getReader() throws IOException;
+public interface RawConditions extends Conditions { + InputStream getInputStream() throws IOException; + + Reader getReader() throws IOException;
}
diff -u -r1.1.1.1 -r1.2 --- RawConditionsImplementation.java 25 Jan 2010 22:23:07 -0000 1.1.1.1 +++ RawConditionsImplementation.java 18 Oct 2013 21:42:47 -0000 1.2 @@ -6,33 +6,29 @@
import java.io.Reader; /**
- *
+ *
* @author Tony Johnson */
-class RawConditionsImplementation extends ConditionsImplementation implements RawConditions -{ - /** Creates a new instance of RawConditionsImplementation */ - RawConditionsImplementation(ConditionsManagerImplementation manager, String name) - { - super(manager,name); - } - - public InputStream getInputStream() throws IOException - { - String type; - String name = getName(); - int pos = name.lastIndexOf('.'); - if (pos < 0) type = "ini"; - else - { - type = name.substring(pos+1); - name = name.substring(0,pos); - } - return getManager().open(name,type); - } - - public Reader getReader() throws IOException - { - return new InputStreamReader(getInputStream()); - }
+class RawConditionsImplementation extends ConditionsImplementation implements RawConditions { + /** Creates a new instance of RawConditionsImplementation */ + RawConditionsImplementation(ConditionsManagerImplementation manager, String name) { + super(manager, name); + } + + public InputStream getInputStream() throws IOException { + String type; + String name = getName(); + int pos = name.lastIndexOf('.'); + if (pos < 0) + type = "ini"; + else { + type = name.substring(pos + 1); + name = name.substring(0, pos); + } + return getManager().open(name, type); + } + + public Reader getReader() throws IOException { + return new InputStreamReader(getInputStream()); + }
}
diff -u -r1.1.1.1 -r1.2 --- ByteFormat.java 25 Jan 2010 22:23:07 -0000 1.1.1.1 +++ ByteFormat.java 18 Oct 2013 21:42:47 -0000 1.2 @@ -6,66 +6,60 @@
import java.text.ParsePosition; /**
- * A formatter for formatting byte sizes. For example, formatting 12345 byes results in - * "12.1 kB" and 1234567 results in "1.18 MB". - *
+ * A formatter for formatting byte sizes. For example, formatting 12345 byes + * results in "12.1 kB" and 1234567 results in "1.18 MB". + *
* @author Bill Lynch * @author Tony Johnson */
-public class ByteFormat extends Format -{ - - private final static String[] mags = {" B"," kB"," MB"," GB"," TB"," PB"}; - private final static DecimalFormat formatter = new DecimalFormat("#,##0.0"); - /** - * Formats a long which represent a number of bytes. - */ - public String format(long bytes) - { - return format(new Long(bytes)); - } - - /** - * Format the given object (must be a Long). - * - * @param obj assumed to be the number of bytes as a Long. - * @param buf the StringBuffer to append to. - * @param pos - * @return A formatted string representing the given bytes in more human-readable form. - */ - public StringBuffer format(Object obj, StringBuffer buf, FieldPosition pos) - { - if (obj instanceof Long) - { - long numBytes = ((Long)obj).longValue(); - if (numBytes > 1024) - { - int mag = 1; - for (; mag<mags.length; mag++) - { - if (numBytes < 1024*1024) break; - numBytes /= 1024;
+public class ByteFormat extends Format { + + private final static String[] mags = { " B", " kB", " MB", " GB", " TB", " PB" }; + private final static DecimalFormat formatter = new DecimalFormat("#,##0.0"); + + /** + * Formats a long which represent a number of bytes. + */ + public String format(long bytes) { + return format(new Long(bytes)); + } + + /** + * Format the given object (must be a Long). + * + * @param obj assumed to be the number of bytes as a Long. + * @param buf the StringBuffer to append to. + * @param pos + * @return A formatted string representing the given bytes in more + * human-readable form. + */ + public StringBuffer format(Object obj, StringBuffer buf, FieldPosition pos) { + if (obj instanceof Long) { + long numBytes = ((Long) obj).longValue(); + if (numBytes > 1024) { + int mag = 1; + for (; mag < mags.length; mag++) { + if (numBytes < 1024 * 1024) + break; + numBytes /= 1024; + } + + buf.append(formatter.format((double) numBytes / 1024.0)).append(mags[mag]); + } else { + buf.append(numBytes).append(mags[0]);
}
- - buf.append(formatter.format((double)numBytes / 1024.0)).append(mags[mag]); - } - else - { - buf.append(numBytes).append(mags[0]); - } - } - return buf; - } - - /** - * In this implementation, returns null always. - * - * @param source - * @param pos - * @return returns null in this implementation. - */ - public Object parseObject(String source, ParsePosition pos) - { - return null; - }
+ } + return buf; + } + + /** + * In this implementation, returns null always. + * + * @param source + * @param pos + * @return returns null in this implementation. + */ + public Object parseObject(String source, ParsePosition pos) { + return null; + }
}
diff -u -r1.1.1.1 -r1.2 --- CachingEntityResolver.java 25 Jan 2010 22:23:07 -0000 1.1.1.1 +++ CachingEntityResolver.java 18 Oct 2013 21:42:47 -0000 1.2 @@ -9,29 +9,28 @@
import org.xml.sax.SAXException; /**
- * An entity resolver which caches schemas locally. This makes it possible to run - * the tests without a network connection, provided the cache has been previously - * seeded.
+ * An entity resolver which caches schemas locally. This makes it possible to + * run the tests without a network connection, provided the cache has been + * previously seeded.
* @author tonyj
- * @version $Id: CachingEntityResolver.java,v 1.1.1.1 2010/01/25 22:23:07 jeremy Exp $
+ * @version $Id: CachingEntityResolver.java,v 1.1.1.1 2010/01/25 22:23:07 jeremy + * Exp $
*/
-public class CachingEntityResolver implements EntityResolver -{ - private FileCache fileCache; - - public CachingEntityResolver() throws IOException - { - fileCache = new FileCache(); - } - public CachingEntityResolver(File cacheDirectory) throws IOException - { - fileCache = new FileCache(cacheDirectory); - } - public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException - { - File file = fileCache.getCachedFile(new URL(systemId)); - InputSource result = new InputSource(new FileInputStream(file)); - result.setSystemId(systemId); - return result; - }
+public class CachingEntityResolver implements EntityResolver { + private FileCache fileCache; + + public CachingEntityResolver() throws IOException { + fileCache = new FileCache(); + } + + public CachingEntityResolver(File cacheDirectory) throws IOException { + fileCache = new FileCache(cacheDirectory); + } + + public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { + File file = fileCache.getCachedFile(new URL(systemId)); + InputSource result = new InputSource(new FileInputStream(file)); + result.setSystemId(systemId); + return result; + }
}
\ No newline at end of file
diff -u -r1.1.1.1 -r1.2 --- FileCache.java 25 Jan 2010 22:23:07 -0000 1.1.1.1 +++ FileCache.java 18 Oct 2013 21:42:47 -0000 1.2 @@ -14,200 +14,190 @@
* A utility for downloading and caching files * @author tonyj */
-public class FileCache -{ - private static final File home = new File(getCacheRoot(),".cache"); - private static final ByteFormat format = new ByteFormat(); - private File cache; - private PrintStream print = System.out; - - public static File getCacheRoot() - { - String cacheDir = System.getProperty("org.lcsim.cacheDir"); - if (cacheDir == null) cacheDir = System.getProperty("user.home"); - return new File(cacheDir); - } - - /** - * Create a FileCache using the default cache location - * @throws java.io.IOException If the cache directory cannot be created - */ - public FileCache() throws IOException - { - this(home); - } - - /** - * Create a file cache using an explicit cache location - * @param cacheDirectory The directory to use for storing cached files - * @throws java.io.IOException If the cache directory cannot be created - */ - public FileCache(File cacheDirectory) throws IOException - { - setCacheDirectory(cacheDirectory); - } - /** - * Get a file from the cache. If the file is already in the cache and up-to-date the existing - * file will be returned. Otherwise the file will be downloaded first, and then moved - * to the cache. - * @param url Teh URL of the file to download - * @throws java.io.IOException If the file cannot be downloaded, or if an error occurs writing to the cache. - * @return The location of the cached file - */ - public File getCachedFile(URL url) throws IOException - { - return getCachedFile(url,null); - } - - /** - * Get a file from the cache. If the file is already in the cache and up-to-date the existing - * file will be returned. Otherwise the file will be downloaded first, validated, and then moved - * to the cache. - * @param url Teh URL of the file to download - * @param validator A Validator that will be used to check the integrity of the downloaded file before it is - * placed in the cache - * @throws java.io.IOException If the file cannot be downloaded, or if an error occurs writing to the cache, or if the file - * fails validation. - * @return The location of the cached file - */ - public File getCachedFile(URL url, Validator validator) throws IOException - { - File cacheFile = new File(cache,URLEncoder.encode(url.toExternalForm(),"UTF-8")); - boolean downloadRequired = !cacheFile.exists(); - if (cacheFile.exists() && !Boolean.getBoolean("org.lcsim.offline")) - { - // If we can access the URL, check if the cachefile is up-to-date - try - { - URLConnection connection = url.openConnection(); - connection.setConnectTimeout(5000); - connection.setReadTimeout(5000); - long updated = connection.getLastModified(); - long cached = cacheFile.lastModified(); - if (updated > cached) downloadRequired = true; - } - catch (IOException x) - { - //if (print != null) print.println("Warning: file cache could not access "+url+", "+x.getMessage()); - } - } - if (downloadRequired) - { - URLConnection connection = url.openConnection(); - connection.setConnectTimeout(10000); - connection.setReadTimeout(10000); - long size = connection.getContentLength(); - InputStream in = connection.getInputStream(); - if (in != null) - { - if (print != null) print.println("Downloading..."+url); - File temp = File.createTempFile("det",null,cache); - temp.deleteOnExit(); - OutputStream out = new FileOutputStream(temp); - byte[] buffer = new byte[8096]; - try - { - long got = 0; - for (;;) - { - int l = in.read(buffer); - if (l < 0) break; - out.write(buffer,0,l); - if (out != null) - { - got += l; - if (print != null) - { - print.print("Got "); - print.print(format.format(got)); - print.print('/'); - print.print(format.format(size)); - print.print('\r'); - print.flush(); - } - } - } - } - finally - { - in.close(); - out.close();
+public class FileCache { + private static final File home = new File(getCacheRoot(), ".cache"); + private static final ByteFormat format = new ByteFormat(); + private File cache; + private PrintStream print = System.out; + + public static File getCacheRoot() { + String cacheDir = System.getProperty("org.lcsim.cacheDir"); + if (cacheDir == null) + cacheDir = System.getProperty("user.home"); + return new File(cacheDir); + } + + /** + * Create a FileCache using the default cache location + * @throws java.io.IOException If the cache directory cannot be created + */ + public FileCache() throws IOException { + this(home); + } + + /** + * Create a file cache using an explicit cache location + * @param cacheDirectory The directory to use for storing cached files + * @throws java.io.IOException If the cache directory cannot be created + */ + public FileCache(File cacheDirectory) throws IOException { + setCacheDirectory(cacheDirectory); + } + + /** + * Get a file from the cache. If the file is already in the cache and + * up-to-date the existing file will be returned. Otherwise the file will be + * downloaded first, and then moved to the cache. + * @param url The URL of the file to download + * @throws java.io.IOException If the file cannot be downloaded, or if an + * error occurs writing to the cache. + * @return The location of the cached file + */ + public File getCachedFile(URL url) throws IOException { + return getCachedFile(url, null); + } + + /** + * Get a file from the cache. If the file is already in the cache and + * up-to-date the existing file will be returned. Otherwise the file will be + * downloaded first, validated, and then moved to the cache. + * @param url Teh URL of the file to download + * @param validator A Validator that will be used to check the integrity of + * the downloaded file before it is placed in the cache + * @throws java.io.IOException If the file cannot be downloaded, or if an + * error occurs writing to the cache, or if the file fails + * validation. + * @return The location of the cached file + */ + public File getCachedFile(URL url, Validator validator) throws IOException { + File cacheFile = new File(cache, URLEncoder.encode(url.toExternalForm(), "UTF-8")); + boolean downloadRequired = !cacheFile.exists(); + if (cacheFile.exists() && !Boolean.getBoolean("org.lcsim.offline")) { + // If we can access the URL, check if the cachefile is up-to-date + try { + URLConnection connection = url.openConnection(); + connection.setConnectTimeout(5000); + connection.setReadTimeout(5000); + long updated = connection.getLastModified(); + long cached = cacheFile.lastModified(); + if (updated > cached) + downloadRequired = true; + } catch (IOException x) { + // if (print != null) + // print.println("Warning: file cache could not access "+url+", "+x.getMessage());
}
- if (validator != null) validator.checkValidity(url,temp); - // File looks ok, move it into the cache - if (cacheFile.exists()) - { - boolean ok = cacheFile.delete(); - if (!ok) throw new IOException("Error while deleting old cache file"); - } - boolean ok = temp.renameTo(cacheFile); - if (!ok) throw new IOException("Error while moving file to cache"); - } - else throw new IOException("Could not open "+url); - } - return cacheFile; - } - - - /** - * Get the directory used for caching - * @return The cache directory - */ - public File getCacheDirectory() - { - return this.cache; - } - - /** - * Set a new cache directory location - * @param cacheDirectory The directory to use. - * @throws java.io.IOException If the specified directory cannot be created, or already exists and is not a directory. - */ - public void setCacheDirectory(File cacheDirectory) throws IOException - { - if (!cacheDirectory.exists()) - { - boolean ok = cacheDirectory.mkdirs(); - if (!ok) throw new IOException("Unable to create: "+cacheDirectory.getAbsoluteFile()); - } - else if (!cacheDirectory.isDirectory()) - { - throw new IOException("Not a directory"); - } - this.cache = cacheDirectory; - } - - /** - * Gets the print stream used for diagnostic messages if a file is downloaded. - * @return The current diagnostic print stream, or <CODE>null</CODE> if none exists. - */ - public PrintStream getPrintStream() - { - - return this.print; - } - - /** - * Set the print stream to be used for diagnostic messages - * @param printStream The print stream to use, or <CODE>null</CODE> to disable diagnostic messages - */ - public void setPrintStream(PrintStream printStream) - { - this.print = printStream; - } - /** - * An interface to be implemented by cache validators - * @see #getCachedFile(URL,Validator) - */ - public static interface Validator - { - /** - * Called after a file has been downloaded but before it is placed in the cache, - * This method should throw an IOException if the file fails to validate. - * @param url The URL being downloaded - * @param file The file that needs to be validated - * @throws java.io.IOException If validation fails - */ - void checkValidity(URL url, File file) throws IOException; - }
+ } + if (downloadRequired) { + URLConnection connection = url.openConnection(); + connection.setConnectTimeout(10000); + connection.setReadTimeout(10000); + long size = connection.getContentLength(); + InputStream in = connection.getInputStream(); + if (in != null) { + if (print != null) + print.println("Downloading..." + url); + File temp = File.createTempFile("det", null, cache); + temp.deleteOnExit(); + OutputStream out = new FileOutputStream(temp); + byte[] buffer = new byte[8096]; + try { + long got = 0; + for (;;) { + int l = in.read(buffer); + if (l < 0) + break; + out.write(buffer, 0, l); + if (out != null) { + got += l; + if (print != null) { + print.print("Got "); + print.print(format.format(got)); + print.print('/'); + print.print(format.format(size)); + print.print('\r'); + print.flush(); + } + } + } + } finally { + in.close(); + out.close(); + } + if (validator != null) + validator.checkValidity(url, temp); + // File looks ok, move it into the cache + if (cacheFile.exists()) { + boolean ok = cacheFile.delete(); + if (!ok) + throw new IOException("Error while deleting old cache file"); + } + boolean ok = temp.renameTo(cacheFile); + if (!ok) + throw new IOException("Error while moving file to cache"); + } else + throw new IOException("Could not open " + url); + } + return cacheFile; + } + + /** + * Get the directory used for caching + * @return The cache directory + */ + public File getCacheDirectory() { + return this.cache; + } + + /** + * Set a new cache directory location + * @param cacheDirectory The directory to use. + * @throws java.io.IOException If the specified directory cannot be created, + * or already exists and is not a directory. + */ + public void setCacheDirectory(File cacheDirectory) throws IOException { + if (!cacheDirectory.exists()) { + boolean ok = cacheDirectory.mkdirs(); + if (!ok) + throw new IOException("Unable to create: " + cacheDirectory.getAbsoluteFile()); + } else if (!cacheDirectory.isDirectory()) { + throw new IOException("Not a directory"); + } + this.cache = cacheDirectory; + } + + /** + * Gets the print stream used for diagnostic messages if a file is + * downloaded. + * @return The current diagnostic print stream, or <CODE>null</CODE> if none + * exists. + */ + public PrintStream getPrintStream() { + + return this.print; + } + + /** + * Set the print stream to be used for diagnostic messages + * @param printStream The print stream to use, or <CODE>null</CODE> to + * disable diagnostic messages + */ + public void setPrintStream(PrintStream printStream) { + this.print = printStream; + } + + /** + * An interface to be implemented by cache validators + * @see #getCachedFile(URL,Validator) + */ + public static interface Validator { + /** + * Called after a file has been downloaded but before it is placed in + * the cache, This method should throw an IOException if the file fails + * to validate. + * @param url The URL being downloaded + * @param file The file that needs to be validated + * @throws java.io.IOException If validation fails + */ + void checkValidity(URL url, File file) throws IOException; + }
}
\ No newline at end of file
Use REPLY-ALL to reply to list
To unsubscribe from the LCD-CVS list, click the following link:
https://listserv.slac.stanford.edu/cgi-bin/wa?SUBED1=LCD-CVS&A=1