View Javadoc

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  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
14  * ***             http://cc.in2p3.fr/             ***
15  * ***************************************************
16  * File:   MetricImpl
17  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
18  * Date:   17 sept. 2007
19  * ***************************************************
20  * Description:                                      */
21  /**
22   *
23   */
24  public class MetricImpl<E> extends AbstractAttributesImpl implements Metric {
25      private static Logger s_logger = Logger.getLogger(MetricImpl.class);
26      // attributes
27      protected E m_value;
28      // internal
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      /** constructor */
36      public MetricImpl(Monitorable mt, String name, String desc, MetricMode mode, String unit, MetricType type, E initialValue) {
37          super(null, true);   //not attached to a session, isExtensible=true
38          // set attributes
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; //do not notify
45          // internal
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      /** clone */
54      public MetricImpl<E> clone() throws CloneNotSupportedException {
55          // clone attributes
56          MetricImpl<E> clone = (MetricImpl<E>) super.clone();
57          clone.m_value = m_value;
58          // internal
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       * If new value if different from old value, then set it and invoke callbacks.
69       * The SAGA engine implementation SHOULD use this method instead of method setAttribute.
70       * @param value the new value
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       * The SAGA engine implementation SHOULD use this method instead of method getAttribute.
82       * @return the current value
83       */
84      public E getValue() {
85          return m_value;
86      }
87  
88      /**
89       * ReadWrite metrics MAY override this method if default behavior does not suit to your needs
90       * @param value the value as a string
91       * @return the value object
92       * @throws NotImplementedException if this method is not overrided
93       */
94      protected E getValuefromString(String value) throws NotImplementedException, DoesNotExistException {
95          return new AttributeSerializer<E>(m_type).fromString(value);
96      }
97  
98      /**
99       * ReadWrite metrics MAY override this method if default behavior does not suit to your needs
100      * @param values the value as a string array
101      * @return the value object
102      * @throws NotImplementedException if this method is not overrided
103      */
104     protected E getValuefromStringArray(String[] values) throws NotImplementedException, DoesNotExistException {
105         return new AttributeSerializer<E>(m_type).fromStringArray(values);
106     }
107 
108     /**
109      * Metrics MAY override this method if default behavior does not suit to your needs
110      * @return the value as a string
111      */
112     protected String getStringFromValue() throws DoesNotExistException {
113         return new AttributeSerializer<E>(m_type).toString(m_value);
114     }
115 
116     /**
117      * Metrics MAY override this method if default behavior does not suit to your needs
118      * @return the value as a string array
119      */
120     protected String[] getStringArrayFromValue() throws DoesNotExistException {
121         return new AttributeSerializer<E>(m_type).toStringArray(m_value);
122     }
123 
124     //////////////////////////////////////////// interface Metric ////////////////////////////////////////////
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");    //todo: Implement method fire()
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     //////////////////////////////////////////// interface Attributes ////////////////////////////////////////////
156 
157     /** override Attributes.setAttribute() */
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     /** override Attributes.getAttribute() */
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     /** override Attributes.setVectorAttribute() */
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     /** override Attributes.getVectorAttribute() */
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     //////////////////////////////////////////// private methods ////////////////////////////////////////////
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 }