Commit in lcsim-conditions/src/main/java/org/lcsim/conditions on recordloop-dev | |||
ConditionsManagerImplementation.java | +23 | -9 | 1.1.1.1 -> 1.1.1.1.2.1 |
ConditionsReader.java | +65 | -10 | 1.1.1.1 -> 1.1.1.1.2.1 |
+88 | -19 |
Support for run-dependent conditions and custom ConditionsReaders.
diff -u -r1.1.1.1 -r1.1.1.1.2.1 --- ConditionsManagerImplementation.java 25 Jan 2010 22:23:07 -0000 1.1.1.1 +++ ConditionsManagerImplementation.java 13 Mar 2012 21:29:10 -0000 1.1.1.1.2.1 @@ -2,13 +2,11 @@
import java.io.IOException; import java.io.InputStream;
+import java.text.MessageFormat;
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;
@@ -25,6 +23,7 @@
private int run; private List<ConditionsListener> listenerList = new ArrayList<ConditionsListener>(); private Logger logger = Logger.getLogger(ConditionsManagerImplementation.class.getName());
+ private static final String msgCondChange = "Conditions changed for: detector {0} run {1}";
/** * The default implementation of ConditionsManager.
@@ -37,20 +36,34 @@
// handler.setLevel(Level.ALL); // logger.addHandler(handler); }
- public void setDetector(String name, int run) throws ConditionsNotFoundException
+ + public void setDetector(String detectorName, int run) throws ConditionsNotFoundException
{
- this.run = run; - if (!name.equals(detectorName)) - { - setConditionsReader(ConditionsReader.create(name,run),name); - logger.fine("Detector changed: "+name+" "+run);
+ if (this.run != run || !detectorName.equals(this.detectorName)) { + if (reader == null) { + setConditionsReader(ConditionsReader.create(this, detectorName, run), detectorName); + } else { + try { + if (reader.update(this, detectorName, run)) { + fireConditionsChanged(); + logger.fine(MessageFormat.format(msgCondChange, detectorName, run)); + } + } catch (IllegalArgumentException x) { + setConditionsReader(ConditionsReader.create(this, detectorName, run), detectorName); + } catch (IOException x) { + throw new ConditionsSetNotFoundException("Failed to update conditions reader, detector: "+ detectorName +", run: "+ run, x); + } + } + this.run = run;
} }
+
public void setConditionsReader(ConditionsReader newReader, String name) { ConditionsReader oldReader = reader; reader = newReader; detectorName = name;
+ logger.fine(MessageFormat.format(msgCondChange, detectorName, run));
fireConditionsChanged(); try {
@@ -61,6 +74,7 @@
} }
+
public void removeConditionsConverter(ConditionsConverter conv) { converters.remove(conv.getType());
diff -u -r1.1.1.1 -r1.1.1.1.2.1 --- ConditionsReader.java 25 Jan 2010 22:23:07 -0000 1.1.1.1 +++ ConditionsReader.java 13 Mar 2012 21:29:10 -0000 1.1.1.1.2.1 @@ -31,6 +31,29 @@
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. + * <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. + * + * @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(); + } + }
/** * Get a list of available detectors
@@ -168,9 +191,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. + */ + 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) { + 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 InputStream open(String name, String type) throws IOException; - abstract void close() throws IOException;
+ abstract public InputStream open(String name, String type) throws IOException; + abstract public void close() throws IOException;
private static Properties loadAliases() { Properties result = new Properties();
@@ -253,13 +308,13 @@
{ this.zip = new ZipFile(file,ZipFile.OPEN_READ); }
- InputStream open(String name, String type) throws IOException
+ public 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
+ public void close() throws IOException
{ zip.close(); }
@@ -271,13 +326,13 @@
{ this.dir = file; }
- InputStream open(String name, String type) throws IOException
+ public 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
+ public void close() throws IOException
{ } }
@@ -295,25 +350,25 @@
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
+ public 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
+ public void close() throws IOException
{ } } private static class DummyConditionsReader extends ConditionsReader {
- InputStream open(String name, String type) throws IOException
+ public InputStream open(String name, String type) throws IOException
{ throw new IOException("Conditions "+name+"."+type+" not found"); }
- void close() throws IOException
+ public void close() throws IOException
{} } }
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