001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.servicemix.components.jmx;
018:
019: import javax.jbi.JBIException;
020: import javax.jbi.component.ComponentContext;
021: import javax.jbi.management.DeploymentException;
022: import javax.jbi.messaging.InOnly;
023: import javax.jbi.messaging.MessageExchange;
024: import javax.jbi.messaging.MessagingException;
025: import javax.jbi.messaging.NormalizedMessage;
026: import javax.management.MBeanServer;
027: import javax.management.Notification;
028: import javax.management.NotificationListener;
029: import javax.management.ObjectName;
030: import javax.management.monitor.CounterMonitor;
031: import javax.xml.transform.Source;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035: import org.apache.servicemix.MessageExchangeListener;
036: import org.apache.servicemix.components.util.ComponentSupport;
037: import org.apache.servicemix.jbi.jaxp.StringSource;
038:
039: /**
040: * A JMX Counter Monitor as a Component to enable firing notifications
041: *
042: * @version $Revision: 426415 $
043: */
044: public class CounterMonitorComponent extends ComponentSupport implements
045: NotificationListener, MessageExchangeListener {
046:
047: private static final Log log = LogFactory
048: .getLog(ComponentSupport.class);
049: private String name;
050: private ObjectName ourName;
051: private String observedObjectName;
052: private String attributeName;
053: private long granularityPeriod = 5000;
054: private Number threshold;
055: private Number offset;
056: private MBeanServer mbeanServer;
057: private CounterMonitor counterMonitor = new CounterMonitor();
058:
059: /**
060: * Called when the Component is initialized
061: *
062: * @param cc
063: * @throws JBIException
064: */
065: public void init(ComponentContext cc) throws JBIException {
066: super .init(cc);
067: validate();
068: if (mbeanServer == null) {
069: mbeanServer = cc.getMBeanServer();
070: }
071: try {
072: ObjectName observedName = new ObjectName(observedObjectName);
073: if (name == null) {
074: String type = observedName.getKeyProperty("type");
075: type = type != null ? type : "UNKNOWN";
076: name = mbeanServer.getDefaultDomain()
077: + ":type=CounterMonitor_" + type;
078: }
079: ourName = new ObjectName(name);
080: counterMonitor.setNotify(true);
081: counterMonitor.addObservedObject(observedName);
082: counterMonitor.setObservedAttribute(attributeName);
083: counterMonitor.setGranularityPeriod(granularityPeriod);
084: counterMonitor.setDifferenceMode(false);
085: counterMonitor.setInitThreshold(threshold);
086: counterMonitor.setOffset(offset);
087: mbeanServer.registerMBean(counterMonitor, ourName);
088: mbeanServer.addNotificationListener(ourName, this , null,
089: new Object());
090: } catch (Exception e) {
091: throw new DeploymentException(e);
092: }
093: }
094:
095: /**
096: * Start the item.
097: *
098: * @exception javax.jbi.JBIException
099: * if the item fails to start.
100: */
101: public void start() throws javax.jbi.JBIException {
102: super .start();
103: counterMonitor.start();
104: }
105:
106: /**
107: * Stop the item. This suspends current messaging activities.
108: *
109: * @exception javax.jbi.JBIException
110: * if the item fails to stop.
111: */
112: public void stop() throws javax.jbi.JBIException {
113: counterMonitor.stop();
114: super .stop();
115: }
116:
117: /**
118: * Shut down the item. The releases resources, preparatory to uninstallation.
119: *
120: * @exception javax.jbi.JBIException
121: * if the item fails to shut down.
122: */
123: public void shutDown() throws javax.jbi.JBIException {
124: stop();
125: if (ourName != null && mbeanServer != null) {
126: try {
127: mbeanServer.removeNotificationListener(ourName, this );
128: } catch (Exception e) {
129: throw new JBIException(e);
130: }
131: }
132: super .shutDown();
133: }
134:
135: /**
136: * @see javax.management.NotificationListener#handleNotification(javax.management.Notification, java.lang.Object)
137: */
138: public void handleNotification(Notification notification,
139: Object arg1) {
140: try {
141: Source source = new StringSource(notification.getMessage());
142: InOnly exchange = getExchangeFactory()
143: .createInOnlyExchange();
144: NormalizedMessage message = exchange.createMessage();
145: message.setContent(source);
146: exchange.setInMessage(message);
147: send(exchange);
148: } catch (Exception e) {
149: log.error("Failed to send Notification message to the NMR");
150: }
151:
152: }
153:
154: protected void validate() throws JBIException {
155: if (observedObjectName == null) {
156: throw new DeploymentException("observedObjectName is null");
157: }
158: if (attributeName == null) {
159: throw new DeploymentException("attributeName is null");
160: }
161: if (threshold == null) {
162: throw new DeploymentException("threshold is null");
163: }
164: if (offset == null) {
165: throw new DeploymentException("offset is null");
166: }
167: }
168:
169: /**
170: * @return Returns the attributeName.
171: */
172: public String getAttributeName() {
173: return attributeName;
174: }
175:
176: /**
177: * @param attributeName
178: * The attributeName to set.
179: */
180: public void setAttributeName(String attributeName) {
181: this .attributeName = attributeName;
182: }
183:
184: /**
185: * @return Returns the counterMonitor.
186: */
187: public CounterMonitor getCounterMonitor() {
188: return counterMonitor;
189: }
190:
191: /**
192: * @param counterMonitor The counterMonitor to set.
193: */
194: public void setCounterMonitor(CounterMonitor counterMonitor) {
195: this .counterMonitor = counterMonitor;
196: }
197:
198: /**
199: * @return Returns the granularityPeriod.
200: */
201: public long getGranularityPeriod() {
202: return granularityPeriod;
203: }
204:
205: /**
206: * @param granularityPeriod The granularityPeriod to set.
207: */
208: public void setGranularityPeriod(long granularityPeriod) {
209: this .granularityPeriod = granularityPeriod;
210: }
211:
212: /**
213: * @return Returns the mbeanServer.
214: */
215: public MBeanServer getMbeanServer() {
216: return mbeanServer;
217: }
218:
219: /**
220: * @param mbeanServer The mbeanServer to set.
221: */
222: public void setMbeanServer(MBeanServer mbeanServer) {
223: this .mbeanServer = mbeanServer;
224: }
225:
226: /**
227: * @return Returns the name.
228: */
229: public String getName() {
230: return name;
231: }
232:
233: /**
234: * @param name The name to set.
235: */
236: public void setName(String name) {
237: this .name = name;
238: }
239:
240: /**
241: * @return Returns the observedObjectName.
242: */
243: public String getObservedObjectName() {
244: return observedObjectName;
245: }
246:
247: /**
248: * @param observedObjectName The observedObjectName to set.
249: */
250: public void setObservedObjectName(String observedObjectName) {
251: this .observedObjectName = observedObjectName;
252: }
253:
254: /**
255: * @return Returns the offset.
256: */
257: public Number getOffset() {
258: return offset;
259: }
260:
261: /**
262: * @param offset The offset to set.
263: */
264: public void setOffset(Number offset) {
265: this .offset = offset;
266: }
267:
268: /**
269: * @return Returns the threshold.
270: */
271: public Number getThreshold() {
272: return threshold;
273: }
274:
275: /**
276: * @param threshold The threshold to set.
277: */
278: public void setThreshold(Number threshold) {
279: this .threshold = threshold;
280: }
281:
282: public void onMessageExchange(MessageExchange exchange)
283: throws MessagingException {
284: // We can only receive acks, so do nothing
285: }
286: }
|