1 package fr.in2p3.jsaga.impl.monitoring;
2
3 import fr.in2p3.jsaga.helpers.AttributeSerializer;
4 import fr.in2p3.jsaga.helpers.cloner.ObjectCloner;
5 import fr.in2p3.jsaga.impl.attributes.AbstractAttributesImpl;
6 import org.apache.log4j.Logger;
7 import org.ogf.saga.error.*;
8 import org.ogf.saga.monitoring.*;
9
10 import java.util.*;
11
12
13
14
15
16
17
18
19
20
21
22
23
24 public class MetricImpl<E> extends AbstractAttributesImpl implements Metric {
25 private static Logger s_logger = Logger.getLogger(MetricImpl.class);
26
27 protected E m_value;
28
29 private Monitorable m_monitorable;
30 private MetricMode m_mode;
31 private MetricType m_type;
32 private Map<Integer,Callback> m_callbacks;
33 private int m_cookieGenerator;
34
35
36 public MetricImpl(Monitorable mt, String name, String desc, MetricMode mode, String unit, MetricType type, E initialValue) {
37 super(null, true);
38
39 super._addReadOnlyAttribute(Metric.NAME, name);
40 super._addReadOnlyAttribute(Metric.DESCRIPTION, desc);
41 super._addReadOnlyAttribute(Metric.MODE, mode.name());
42 super._addReadOnlyAttribute(Metric.UNIT, unit);
43 super._addReadOnlyAttribute(Metric.TYPE, type.name());
44 m_value = initialValue;
45
46 m_monitorable = mt;
47 m_mode = mode;
48 m_type = type;
49 m_callbacks = new HashMap<Integer,Callback>();
50 m_cookieGenerator = 1;
51 }
52
53
54 public MetricImpl<E> clone() throws CloneNotSupportedException {
55
56 MetricImpl<E> clone = (MetricImpl<E>) super.clone();
57 clone.m_value = m_value;
58
59 clone.m_monitorable = m_monitorable;
60 clone.m_mode = m_mode;
61 clone.m_type = m_type;
62 clone.m_callbacks = new ObjectCloner<Integer,Callback>().cloneMap(m_callbacks);
63 clone.m_cookieGenerator = m_cookieGenerator;
64 return clone;
65 }
66
67
68
69
70
71
72 public void setValue(E value) {
73 boolean isModified = (m_value==null && value!=null) || (m_value!=null && !m_value.equals(value));
74 if (isModified) {
75 m_value = value;
76 this.invokeCallbacks();
77 }
78 }
79
80
81
82
83
84 public E getValue() {
85 return m_value;
86 }
87
88
89
90
91
92
93
94 protected E getValuefromString(String value) throws NotImplementedException, DoesNotExistException {
95 return new AttributeSerializer<E>(m_type).fromString(value);
96 }
97
98
99
100
101
102
103
104 protected E getValuefromStringArray(String[] values) throws NotImplementedException, DoesNotExistException {
105 return new AttributeSerializer<E>(m_type).fromStringArray(values);
106 }
107
108
109
110
111
112 protected String getStringFromValue() throws DoesNotExistException {
113 return new AttributeSerializer<E>(m_type).toString(m_value);
114 }
115
116
117
118
119
120 protected String[] getStringArrayFromValue() throws DoesNotExistException {
121 return new AttributeSerializer<E>(m_type).toStringArray(m_value);
122 }
123
124
125
126 public int addCallback(Callback cb) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectStateException, TimeoutException, NoSuccessException {
127 switch (m_mode) {
128 case ReadWrite:
129 case ReadOnly:
130 m_callbacks.put(m_cookieGenerator, cb);
131 return m_cookieGenerator++;
132 case Final:
133 throw new IncorrectStateException("Can not add callback to a metric with mode: "+m_mode.name(), this);
134 default:
135 throw new NoSuccessException("INTERNAL ERROR: unexpected exception");
136 }
137 }
138
139 public void removeCallback(int cookie) throws NotImplementedException, BadParameterException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, TimeoutException, NoSuccessException {
140 m_callbacks.remove(cookie);
141 }
142
143 public void fire() throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectStateException, TimeoutException, NoSuccessException {
144 switch(m_mode) {
145 case ReadWrite:
146 throw new NotImplementedException("Not implemented yet");
147 case ReadOnly:
148 case Final:
149 throw new IncorrectStateException("Can not fire callback on a metric with mode: "+m_mode.name(), this);
150 default:
151 throw new NoSuccessException("INTERNAL ERROR: unexpected exception");
152 }
153 }
154
155
156
157
158 public void setAttribute(String key, String value) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectStateException, BadParameterException, DoesNotExistException, TimeoutException, NoSuccessException {
159 if (Metric.VALUE.equals(key)) {
160 switch(m_mode) {
161 case ReadWrite:
162 this.setValue(this.getValuefromString(value));
163 break;
164 case ReadOnly:
165 case Final:
166 throw new IncorrectStateException("Can not set attributes of a metric with mode: "+m_mode.name(), this);
167 default:
168 throw new NoSuccessException("INTERNAL ERROR: unexpected exception");
169 }
170 } else {
171 super.setAttribute(key, value);
172 }
173 }
174
175
176 public String getAttribute(String key) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectStateException, DoesNotExistException, TimeoutException, NoSuccessException {
177 if (Metric.VALUE.equals(key)) {
178 return this.getStringFromValue();
179 } else {
180 return super.getAttribute(key);
181 }
182 }
183
184
185 public void setVectorAttribute(String key, String[] values) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectStateException, BadParameterException, DoesNotExistException, TimeoutException, NoSuccessException {
186 if (Metric.VALUE.equals(key)) {
187 switch(m_mode) {
188 case ReadWrite:
189 this.setValue(this.getValuefromStringArray(values));
190 case ReadOnly:
191 case Final:
192 throw new IncorrectStateException("Can not set attributes of a metric with mode: "+m_mode.name(), this);
193 default:
194 throw new NoSuccessException("INTERNAL ERROR: unexpected exception");
195 }
196 } else {
197 super.setVectorAttribute(key, values);
198 }
199 }
200
201
202 public String[] getVectorAttribute(String key) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectStateException, DoesNotExistException, TimeoutException, NoSuccessException {
203 if (Metric.VALUE.equals(key)) {
204 return this.getStringArrayFromValue();
205 } else {
206 return super.getVectorAttribute(key);
207 }
208 }
209
210
211
212 protected int getNumberOfCallbacks() {
213 return m_callbacks.size();
214 }
215
216 protected void invokeCallbacks() {
217 Collection<Map.Entry<Integer,Callback>> callbacks = new HashSet<Map.Entry<Integer,Callback>>(m_callbacks.entrySet());
218 for (Map.Entry<Integer,Callback> entry : callbacks) {
219 Integer cookie = entry.getKey();
220 Callback callback = entry.getValue();
221 try {
222 boolean stayRegistered = callback.cb(m_monitorable, this, null);
223 if (!stayRegistered) {
224 this.removeCallback(cookie);
225 }
226 } catch (Throwable e) {
227 s_logger.warn("Failed to invoke callback: "+callback.getClass().getName(), e);
228 }
229 }
230 }
231 }