1 package org.lcsim.conditions;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.ArrayList;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.logging.Level;
10 import java.util.logging.Logger;
11
12
13
14
15 public class ConditionsManagerImplementation extends ConditionsManager {
16
17 protected Map<Class, ConditionsConverter> converters = new HashMap<Class, ConditionsConverter>();
18 protected Map<String, CachedConditions> cache = new HashMap<String, CachedConditions>();
19 protected ConditionsReader reader;
20 protected String detectorName;
21 protected int run;
22 protected List<ConditionsListener> listenerList = new ArrayList<ConditionsListener>();
23 protected static final Logger logger = Logger.getLogger(ConditionsManagerImplementation.class.getName());
24
25
26
27
28
29 protected ConditionsManagerImplementation() {
30
31
32
33
34 }
35
36 public void setDetector(String detectorName, int run) throws ConditionsNotFoundException {
37 if ((this.run != run) || !detectorName.equals(this.detectorName)) {
38 ConditionsReader newReader = null;
39 if (reader == null) {
40 newReader = ConditionsReader.create(this, detectorName, run);
41 } else {
42 try {
43 if (reader.update(this, detectorName, run))
44 newReader = reader;
45 } catch (IllegalArgumentException x) {
46 newReader = ConditionsReader.create(this, detectorName, run);
47 } catch (IOException x) {
48 throw new ConditionsSetNotFoundException("Failed to update conditions reader, detector: " + detectorName + ", run: " + run, x);
49 }
50 }
51 this.run = run;
52 if (newReader != null)
53 setConditionsReader(newReader, detectorName);
54 }
55 }
56
57 public void setConditionsReader(ConditionsReader newReader, String name) {
58 detectorName = name;
59 if (newReader != reader) {
60 ConditionsReader oldReader = reader;
61 reader = newReader;
62 if (oldReader != null) {
63 try {
64 oldReader.close();
65 } catch (IOException x) {
66 }
67 }
68 }
69 fireConditionsChanged();
70 logger.log(Level.FINE, "Detector changed: {0} {1}", new Object[] { detectorName, run });
71 }
72
73 public void removeConditionsConverter(ConditionsConverter conv) {
74 converters.remove(conv.getType());
75 }
76
77 public void registerConditionsConverter(ConditionsConverter conv) {
78 converters.put(conv.getType(), conv);
79 }
80
81 public <T> CachedConditions<T> getCachedConditions(Class<T> type, String name) throws ConditionsSetNotFoundException {
82 if (name == null)
83 throw new IllegalArgumentException("Name argument points to null.");
84 if (name.equals(""))
85 throw new IllegalArgumentException("Name argument has length zero.");
86 CachedConditions cond = cache.get(name);
87 if (cond != null)
88 return cond;
89 logger.log(Level.FINE, "Getting cached conditions {0}", name);
90 ConditionsConverter converter = converters.get(type);
91 if (converter == null)
92 throw new ConditionsSetNotFoundException("No converter registered for type: " + type.getName());
93 cond = new CachedConditionsImplementation(this, name, converter);
94 cache.put(name, cond);
95 return cond;
96 }
97
98 public void setRun(int run) {
99 this.run = run;
100 }
101
102 public int getRun() {
103 return run;
104 }
105
106 public String getDetector() {
107 return detectorName;
108 }
109
110 public void addConditionsListener(ConditionsListener l) {
111 listenerList.add(l);
112 }
113
114 public void removeConditionsListener(ConditionsListener l) {
115 listenerList.remove(l);
116 }
117
118 protected void fireConditionsChanged() {
119 ConditionsEvent event = new ConditionsEvent(this);
120 ArrayList<ConditionsListener> listeners = new ArrayList<ConditionsListener>(listenerList);
121 for (ConditionsListener cl : listeners)
122 cl.conditionsChanged(event);
123 }
124
125 protected InputStream open(String name, String type) throws IOException {
126 if (reader == null)
127 throw new IOException("Detector description has not been set");
128 return reader.open(name, type);
129 }
130
131 public ConditionsSet getConditions(String name) throws ConditionsSetNotFoundException {
132 try {
133 logger.log(Level.FINE, "Reading raw conditions {0}", name);
134 return new ConditionsSetImplementation(this, name);
135 } catch (IOException x) {
136 throw new ConditionsSetNotFoundException("Conditions set not found: " + name, x);
137 }
138 }
139
140 public RawConditions getRawConditions(String name) throws ConditionsSetNotFoundException {
141 logger.log(Level.FINE, "Reading raw conditions {0}", name);
142 return new RawConditionsImplementation(this, name);
143 }
144
145 protected ConditionsReader getConditionsReader() {
146 return reader;
147 }
148
149 protected void clearCache() {
150 this.cache.clear();
151 }
152 }