View Javadoc

1   package org.lcsim.conditions.readers;
2   
3   import java.io.IOException;
4   import java.io.InputStream;
5   
6   import org.lcsim.conditions.ConditionsManager;
7   import org.lcsim.conditions.ConditionsReader;
8   
9   /**
10   * This ConditionsReader finds conditions from embedded jar resources
11   * based on a resource path.
12   */
13  public class BaseClasspathConditionsReader extends ConditionsReader {
14  
15      private String _resourcePath;    
16      String propFileName = "detector.properties";
17      
18      int _runNumber = -1;
19      String _detectorName;
20  
21      public BaseClasspathConditionsReader() {
22      }
23      
24      public BaseClasspathConditionsReader(String resourcePath) throws IOException {
25          _resourcePath = resourcePath;
26          
27          // This shouldn't be here but some convoluted logic in ConditionsReader depends on it for now.
28          if (getClass().getResourceAsStream("/" + _resourcePath + "/" + propFileName) == null) {
29              throw new IOException("Unable to find " + _resourcePath + "/" + propFileName + " on the classpath.");
30          }
31      }
32  
33      /**
34       * Open an InputStream to conditions data by name and type.
35       * 
36       * To be found, the conditions data must exist as a resource on the classpath like: 
37       * 
38       * /[resourcePath]/[name].[type]
39       * 
40       * It will throw an <code>IOException</code> if the conditions do not exist.
41       * 
42       * @return An InputStream to the conditions data or null if does not exist.
43       */
44      public InputStream open(String name, String type) throws IOException {
45          InputStream in = getClass().getResourceAsStream("/" + _resourcePath + "/" + name + "." + type);
46          if (in == null) {
47          	throw new IOException("The conditions " + name + " of type " + type + " do not exist at path " + _resourcePath);
48          }
49          return in;
50      }
51      
52      /**
53       * Set the base path for this reader to search for classpath resources.
54       * @param resourcePath the resource path
55       */
56      public void setResourcePath(String resourcePath) {
57          _resourcePath = resourcePath;
58      }
59      
60      /**
61       * Update this reader for possibly new detector or run number.
62       * This reader caches the detector name and run number, and this method will
63       * return new if either is different from the cached values.
64       * @return true if new detector or run number
65       */
66      public boolean update(ConditionsManager manager, String detectorName, int run) throws IOException {
67          
68          boolean update = false;
69          
70          // Check detector name.
71          if (_detectorName == null || _detectorName != detectorName) {
72              _detectorName = detectorName;
73              update = true;
74          }
75          
76          // Check run number.
77          if (run != _runNumber) {
78              update = true;
79          } 
80                  
81          return update;
82      }
83      
84      /**
85       * Close the reader.  
86       * @throws IOException never
87       */
88      public void close() throws IOException {
89      }
90  }