KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > jmx > CounterMonitorComponent


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.components.jmx;
18
19 import javax.jbi.JBIException;
20 import javax.jbi.component.ComponentContext;
21 import javax.jbi.management.DeploymentException;
22 import javax.jbi.messaging.InOnly;
23 import javax.jbi.messaging.MessageExchange;
24 import javax.jbi.messaging.MessagingException;
25 import javax.jbi.messaging.NormalizedMessage;
26 import javax.management.MBeanServer JavaDoc;
27 import javax.management.Notification JavaDoc;
28 import javax.management.NotificationListener JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30 import javax.management.monitor.CounterMonitor JavaDoc;
31 import javax.xml.transform.Source JavaDoc;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.apache.servicemix.MessageExchangeListener;
36 import org.apache.servicemix.components.util.ComponentSupport;
37 import org.apache.servicemix.jbi.jaxp.StringSource;
38
39 /**
40  * A JMX Counter Monitor as a Component to enable firing notifications
41  *
42  * @version $Revision: 426415 $
43  */

44 public class CounterMonitorComponent extends ComponentSupport implements NotificationListener JavaDoc, MessageExchangeListener {
45     
46     private static final Log log = LogFactory.getLog(ComponentSupport.class);
47     private String JavaDoc name;
48     private ObjectName JavaDoc ourName;
49     private String JavaDoc observedObjectName;
50     private String JavaDoc attributeName;
51     private long granularityPeriod =5000;
52     private Number JavaDoc threshold;
53     private Number JavaDoc offset;
54     private MBeanServer JavaDoc mbeanServer;
55     private CounterMonitor JavaDoc counterMonitor = new CounterMonitor JavaDoc();
56
57     /**
58      * Called when the Component is initialized
59      *
60      * @param cc
61      * @throws JBIException
62      */

63     public void init(ComponentContext cc) throws JBIException {
64         super.init(cc);
65         validate();
66         if (mbeanServer == null) {
67             mbeanServer = cc.getMBeanServer();
68         }
69         try {
70             ObjectName JavaDoc observedName = new ObjectName JavaDoc(observedObjectName);
71             if (name == null) {
72                 String JavaDoc type = observedName.getKeyProperty("type");
73                 type = type != null ? type : "UNKNOWN";
74                 name = mbeanServer.getDefaultDomain() + ":type=CounterMonitor_" + type;
75             }
76             ourName = new ObjectName JavaDoc(name);
77             counterMonitor.setNotify(true);
78             counterMonitor.addObservedObject(observedName);
79             counterMonitor.setObservedAttribute(attributeName);
80             counterMonitor.setGranularityPeriod(granularityPeriod);
81             counterMonitor.setDifferenceMode(false);
82             counterMonitor.setInitThreshold(threshold);
83             counterMonitor.setOffset(offset);
84             mbeanServer.registerMBean(counterMonitor, ourName);
85             mbeanServer.addNotificationListener(ourName, this, null, new Object JavaDoc());
86         } catch (Exception JavaDoc e) {
87             throw new DeploymentException(e);
88         }
89     }
90
91     /**
92      * Start the item.
93      *
94      * @exception javax.jbi.JBIException
95      * if the item fails to start.
96      */

97     public void start() throws javax.jbi.JBIException {
98         super.start();
99         counterMonitor.start();
100     }
101
102     /**
103      * Stop the item. This suspends current messaging activities.
104      *
105      * @exception javax.jbi.JBIException
106      * if the item fails to stop.
107      */

108     public void stop() throws javax.jbi.JBIException {
109         counterMonitor.stop();
110         super.stop();
111     }
112
113     /**
114      * Shut down the item. The releases resources, preparatory to uninstallation.
115      *
116      * @exception javax.jbi.JBIException
117      * if the item fails to shut down.
118      */

119     public void shutDown() throws javax.jbi.JBIException {
120         stop();
121         if (ourName != null && mbeanServer != null) {
122             try{
123                 mbeanServer.removeNotificationListener(ourName,this);
124             } catch(Exception JavaDoc e) {
125                 throw new JBIException(e);
126             }
127         }
128         super.shutDown();
129     }
130
131    
132     /**
133      * @see javax.management.NotificationListener#handleNotification(javax.management.Notification, java.lang.Object)
134      */

135     public void handleNotification(Notification JavaDoc notification,Object JavaDoc arg1) {
136         try {
137             Source JavaDoc source = new StringSource(notification.getMessage());
138             InOnly exchange = getExchangeFactory().createInOnlyExchange();
139             NormalizedMessage message = exchange.createMessage();
140             message.setContent(source);
141             exchange.setInMessage(message);
142             send(exchange);
143         }
144         catch (Exception JavaDoc e) {
145             log.error("Failed to send Notification message to the NMR");
146         }
147         
148     }
149
150     protected void validate() throws JBIException {
151         if (observedObjectName == null) {
152             throw new DeploymentException("observedObjectName is null");
153         }
154         if (attributeName == null) {
155             throw new DeploymentException("attributeName is null");
156         }
157         if (threshold == null) {
158             throw new DeploymentException("threshold is null");
159         }
160         if (offset == null) {
161             throw new DeploymentException("offset is null");
162         }
163     }
164
165     /**
166      * @return Returns the attributeName.
167      */

168     public String JavaDoc getAttributeName() {
169         return attributeName;
170     }
171
172     /**
173      * @param attributeName
174      * The attributeName to set.
175      */

176     public void setAttributeName(String JavaDoc attributeName) {
177         this.attributeName = attributeName;
178     }
179
180     /**
181      * @return Returns the counterMonitor.
182      */

183     public CounterMonitor JavaDoc getCounterMonitor() {
184         return counterMonitor;
185     }
186
187     /**
188      * @param counterMonitor The counterMonitor to set.
189      */

190     public void setCounterMonitor(CounterMonitor JavaDoc counterMonitor) {
191         this.counterMonitor = counterMonitor;
192     }
193
194     /**
195      * @return Returns the granularityPeriod.
196      */

197     public long getGranularityPeriod() {
198         return granularityPeriod;
199     }
200
201     /**
202      * @param granularityPeriod The granularityPeriod to set.
203      */

204     public void setGranularityPeriod(long granularityPeriod) {
205         this.granularityPeriod = granularityPeriod;
206     }
207
208     /**
209      * @return Returns the mbeanServer.
210      */

211     public MBeanServer JavaDoc getMbeanServer() {
212         return mbeanServer;
213     }
214
215     /**
216      * @param mbeanServer The mbeanServer to set.
217      */

218     public void setMbeanServer(MBeanServer JavaDoc mbeanServer) {
219         this.mbeanServer = mbeanServer;
220     }
221
222     /**
223      * @return Returns the name.
224      */

225     public String JavaDoc getName() {
226         return name;
227     }
228
229     /**
230      * @param name The name to set.
231      */

232     public void setName(String JavaDoc name) {
233         this.name = name;
234     }
235
236     /**
237      * @return Returns the observedObjectName.
238      */

239     public String JavaDoc getObservedObjectName() {
240         return observedObjectName;
241     }
242
243     /**
244      * @param observedObjectName The observedObjectName to set.
245      */

246     public void setObservedObjectName(String JavaDoc observedObjectName) {
247         this.observedObjectName = observedObjectName;
248     }
249
250     /**
251      * @return Returns the offset.
252      */

253     public Number JavaDoc getOffset() {
254         return offset;
255     }
256
257     /**
258      * @param offset The offset to set.
259      */

260     public void setOffset(Number JavaDoc offset) {
261         this.offset = offset;
262     }
263
264     /**
265      * @return Returns the threshold.
266      */

267     public Number JavaDoc getThreshold() {
268         return threshold;
269     }
270
271     /**
272      * @param threshold The threshold to set.
273      */

274     public void setThreshold(Number JavaDoc threshold) {
275         this.threshold = threshold;
276     }
277
278     public void onMessageExchange(MessageExchange exchange) throws MessagingException {
279         // We can only receive acks, so do nothing
280
}
281 }
282
Popular Tags