001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package javax.management.modelmbean;
023:
024: import java.util.ArrayList;
025: import java.util.Arrays;
026:
027: import javax.management.Attribute;
028: import javax.management.AttributeChangeNotification;
029: import javax.management.AttributeList;
030: import javax.management.AttributeNotFoundException;
031: import javax.management.InstanceNotFoundException;
032: import javax.management.InvalidAttributeValueException;
033: import javax.management.ListenerNotFoundException;
034: import javax.management.MBeanException;
035: import javax.management.MBeanInfo;
036: import javax.management.MBeanNotificationInfo;
037: import javax.management.MBeanRegistration;
038: import javax.management.MBeanServer;
039: import javax.management.Notification;
040: import javax.management.NotificationEmitter;
041: import javax.management.NotificationFilter;
042: import javax.management.NotificationListener;
043: import javax.management.ObjectName;
044: import javax.management.ReflectionException;
045: import javax.management.RuntimeOperationsException;
046: import javax.management.loading.ClassLoaderRepository;
047:
048: import org.jboss.mx.modelmbean.RequiredModelMBeanInstantiator;
049:
050: /**
051: * Mandatory Model MBean implementation. The Model MBean implementation
052: * can be configured by setting a <tt>jbossmx.required.modelmbean.class</tt>
053: * system property.
054: *
055: * @see javax.management.modelmbean.ModelMBean
056: *
057: * @author <a href="mailto:juha@jboss.org">Juha Lindfors</a>.
058: * @author <a href="mailto:thomas.diesler@jboss.com">Thomas Diesler</a>.
059: * @version $Revision: 57200 $
060: */
061: public class RequiredModelMBean implements ModelMBean,
062: MBeanRegistration, NotificationEmitter {
063: // Attributes ----------------------------------------------------
064: private ModelMBean delegate = null;
065: private MBeanRegistration registrationInterface = null;
066: private Boolean isRegistered = new Boolean(false);
067:
068: // Constructors --------------------------------------------------
069: public RequiredModelMBean() throws MBeanException,
070: RuntimeOperationsException {
071: delegate = RequiredModelMBeanInstantiator.instantiate();
072: if (delegate instanceof MBeanRegistration)
073: registrationInterface = (MBeanRegistration) delegate;
074: }
075:
076: /**
077: * Constructs a RequiredModelMBean object using ModelMBeanInfo passed in.
078: * As long as the RequiredModelMBean is not registered with the MBeanServer yet,
079: * the RequiredModelMBean's MBeanInfo and Descriptors can be customized using the
080: * setModelMBeanInfo(javax.management.modelmbean.ModelMBeanInfo) method.
081: *
082: * After the RequiredModelMBean's MBeanInfo and Descriptors are customized,
083: * the RequiredModelMBean can be registered with the MBeanServer.
084: */
085: public RequiredModelMBean(ModelMBeanInfo info)
086: throws MBeanException, RuntimeOperationsException {
087: if (info == null)
088: throw new RuntimeOperationsException(
089: new IllegalArgumentException(
090: "Null model mbean info"));
091:
092: delegate = RequiredModelMBeanInstantiator.instantiate(info);
093: if (delegate instanceof MBeanRegistration)
094: registrationInterface = (MBeanRegistration) delegate;
095: }
096:
097: // ModelMBean implementation -------------------------------------
098: public void setModelMBeanInfo(ModelMBeanInfo info)
099: throws MBeanException, RuntimeOperationsException {
100: if (isRegistered.booleanValue())
101: throw new RuntimeOperationsException(
102: new IllegalStateException(
103: "Cannot set ModelMBeanInfo, mbean already registered"));
104: delegate.setModelMBeanInfo(info);
105: }
106:
107: public void setManagedResource(Object mr, String mr_type)
108: throws MBeanException, RuntimeOperationsException,
109: InstanceNotFoundException, InvalidTargetObjectTypeException {
110: delegate.setManagedResource(mr, mr_type);
111: }
112:
113: // PersistentMBean implementation --------------------------------
114: public void load() throws MBeanException,
115: RuntimeOperationsException, InstanceNotFoundException {
116: delegate.load();
117: }
118:
119: public void store() throws MBeanException,
120: RuntimeOperationsException, InstanceNotFoundException {
121: delegate.store();
122: }
123:
124: // DynamicMBean implementation -----------------------------------
125: public MBeanInfo getMBeanInfo() {
126: return delegate.getMBeanInfo();
127: }
128:
129: public Object invoke(String opName, Object[] opArgs, String[] sig)
130: throws MBeanException, ReflectionException {
131: return delegate.invoke(opName, opArgs, sig);
132: }
133:
134: public Object getAttribute(String attrName)
135: throws AttributeNotFoundException, MBeanException,
136: ReflectionException {
137: return delegate.getAttribute(attrName);
138: }
139:
140: public AttributeList getAttributes(String[] attrNames) {
141: return delegate.getAttributes(attrNames);
142: }
143:
144: public void setAttribute(Attribute attribute)
145: throws AttributeNotFoundException,
146: InvalidAttributeValueException, MBeanException,
147: ReflectionException {
148: delegate.setAttribute(attribute);
149: }
150:
151: public AttributeList setAttributes(AttributeList attributes) {
152: return delegate.setAttributes(attributes);
153: }
154:
155: // ModelMBeanNotificationBroadcaster implementation --------------
156: public void addNotificationListener(
157: NotificationListener inlistener,
158: NotificationFilter infilter, Object inhandback)
159: throws IllegalArgumentException {
160: delegate.addNotificationListener(inlistener, infilter,
161: inhandback);
162: }
163:
164: public void removeNotificationListener(
165: NotificationListener inlistener)
166: throws ListenerNotFoundException {
167: delegate.removeNotificationListener(inlistener);
168: }
169:
170: public void removeNotificationListener(
171: NotificationListener listener, NotificationFilter filter,
172: Object handback) throws ListenerNotFoundException {
173: if (delegate instanceof NotificationEmitter)
174: ((NotificationEmitter) delegate)
175: .removeNotificationListener(listener, filter,
176: handback);
177: else
178: throw new UnsupportedOperationException(
179: "Delegate is not a NotificationEmitter "
180: + delegate.getClass());
181: }
182:
183: public void sendNotification(Notification ntfyObj)
184: throws MBeanException, RuntimeOperationsException {
185: delegate.sendNotification(ntfyObj);
186: }
187:
188: public void sendNotification(String ntfyText)
189: throws MBeanException, RuntimeOperationsException {
190: delegate.sendNotification(ntfyText);
191: }
192:
193: /** Returns the array of Notifications generated by the RequiredModelMBean.
194: * RequiredModelMBean may always send also two additional notifications:
195: * One with descriptor "name=GENERIC,descriptorType=notification,log=T,severity=5,displayName=jmx.modelmbean.generic"
196: * Second is a standard attribute change notification with descriptor "name=ATTRIBUTE_CHANGE,descriptorType=notification,log=T,severity=5,displayName=jmx.attribute.change"
197: * Thus these two notifications are always added to those specified by the application.
198: *
199: * @return
200: */
201: public MBeanNotificationInfo[] getNotificationInfo() {
202: MBeanNotificationInfo generic = null;
203: MBeanNotificationInfo attrChange = null;
204: MBeanNotificationInfo[] resourceInfo = delegate
205: .getNotificationInfo();
206: for (int n = 0; n < resourceInfo.length; n++) {
207: MBeanNotificationInfo notify = resourceInfo[n];
208: String name = notify.getName();
209: if (name.equals("GENERIC"))
210: generic = notify;
211: else if (name.equals("ATTRIBUTE_CHANGE"))
212: attrChange = notify;
213: }
214: if (generic == null || attrChange == null) {
215: ArrayList tmp = new ArrayList(Arrays.asList(resourceInfo));
216: if (attrChange == null) {
217: String[] types = { "jmx.attribute.change" };
218: ModelMBeanNotificationInfo n = new ModelMBeanNotificationInfo(
219: types, "ATTRIBUTE_CHANGE",
220: "Attribute change notfication");
221: n.getDescriptor().setField("log", "T");
222: n.getDescriptor().setField("displayName",
223: "jmx.attribute.change");
224: tmp.add(0, n);
225: }
226: if (generic == null) {
227: String[] types = { "jmx.modelmbean.generic" };
228: ModelMBeanNotificationInfo n = new ModelMBeanNotificationInfo(
229: types, "GENERIC", "Generic text msg");
230: n.getDescriptor().setField("log", "T");
231: n.getDescriptor().setField("displayName",
232: "jmx.modelmbean.generic");
233: tmp.add(0, n);
234: }
235: resourceInfo = new MBeanNotificationInfo[tmp.size()];
236: tmp.toArray(resourceInfo);
237: }
238:
239: return resourceInfo;
240: }
241:
242: public void addAttributeChangeNotificationListener(
243: NotificationListener inlistener, String inAttributeName,
244: Object inhandback) throws MBeanException,
245: RuntimeOperationsException, IllegalArgumentException {
246: delegate.addAttributeChangeNotificationListener(inlistener,
247: inAttributeName, inhandback);
248: }
249:
250: public void removeAttributeChangeNotificationListener(
251: NotificationListener inlistener, String inAttributeName)
252: throws MBeanException, RuntimeOperationsException,
253: ListenerNotFoundException {
254: delegate.removeAttributeChangeNotificationListener(inlistener,
255: inAttributeName);
256: }
257:
258: public void sendAttributeChangeNotification(
259: AttributeChangeNotification ntfyObj) throws MBeanException,
260: RuntimeOperationsException {
261: delegate.sendAttributeChangeNotification(ntfyObj);
262: }
263:
264: public void sendAttributeChangeNotification(Attribute inOldVal,
265: Attribute inNewVal) throws MBeanException,
266: RuntimeOperationsException {
267: delegate.sendAttributeChangeNotification(inOldVal, inNewVal);
268: }
269:
270: // MBeanRegistration implementation ------------------------------
271: public ObjectName preRegister(MBeanServer server, ObjectName name)
272: throws Exception {
273: if (registrationInterface != null)
274: return registrationInterface.preRegister(server, name);
275:
276: return name;
277: }
278:
279: public void postRegister(Boolean registrationDone) {
280: if (registrationInterface != null)
281: registrationInterface.postRegister(registrationDone);
282: isRegistered = registrationDone;
283: }
284:
285: public void preDeregister() throws Exception {
286: if (registrationInterface != null)
287: registrationInterface.preDeregister();
288: }
289:
290: public void postDeregister() {
291: isRegistered = new Boolean(false);
292: if (registrationInterface != null)
293: registrationInterface.postDeregister();
294: }
295:
296: protected ClassLoaderRepository getClassLoaderRepository() {
297: return null;
298: }
299: }
|