lcsim-conditions/src/main/java/org/lcsim/conditions
diff -u -r1.2 -r1.3
--- ConditionsManagerImplementation.java 29 Feb 2012 00:13:37 -0000 1.2
+++ ConditionsManagerImplementation.java 19 Mar 2012 20:55:58 -0000 1.3
@@ -35,15 +35,35 @@
// 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.log(Level.FINE, "Detector changed: {0} {1}", new Object[]{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.log(Level.FINE, "Detector changed: {0} {1}", new Object[]{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;
}
+
+// this.run = run;
+// if (!name.equals(detectorName))
+// {
+// setConditionsReader(ConditionsReader.create(name,run),name);
+// logger.log(Level.FINE, "Detector changed: {0} {1}", new Object[]{name, run});
+// }
}
+
public void setConditionsReader(ConditionsReader newReader, String name)
{
ConditionsReader oldReader = reader;
@@ -59,6 +79,7 @@
}
}
+
public void removeConditionsConverter(ConditionsConverter conv)
{
converters.remove(conv.getType());
lcsim-conditions/src/main/java/org/lcsim/conditions
diff -u -r1.2 -r1.3
--- ConditionsReader.java 29 Feb 2012 00:13:37 -0000 1.2
+++ ConditionsReader.java 19 Mar 2012 20:55:58 -0000 1.3
@@ -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
@@ -162,10 +185,42 @@
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 public InputStream open(String name, String type) throws IOException;
- abstract void close() throws IOException;
+ abstract public void close() throws IOException;
private static Properties loadAliases() {
Properties result = new Properties();
@@ -238,7 +293,7 @@
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");
@@ -246,7 +301,7 @@
return zip.getInputStream(entry);
}
- void close() throws IOException {
+ public void close() throws IOException {
zip.close();
}
}
@@ -259,7 +314,7 @@
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.");
@@ -267,7 +322,7 @@
return new BufferedInputStream(new FileInputStream(file));
}
- void close() throws IOException {
+ public void close() throws IOException {
}
}
@@ -286,7 +341,7 @@
}
}
- 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");
@@ -294,17 +349,17 @@
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 {
}
}
}