1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.jmx;
16  
17  import java.io.ObjectInputStream;
18  
19  import java.util.Set;
20  
21  import javax.management.Attribute;
22  import javax.management.AttributeList;
23  import javax.management.AttributeNotFoundException;
24  import javax.management.InstanceAlreadyExistsException;
25  import javax.management.InstanceNotFoundException;
26  import javax.management.IntrospectionException;
27  import javax.management.InvalidAttributeValueException;
28  import javax.management.ListenerNotFoundException;
29  import javax.management.MBeanException;
30  import javax.management.MBeanInfo;
31  import javax.management.MBeanRegistrationException;
32  import javax.management.MBeanServer;
33  import javax.management.NotCompliantMBeanException;
34  import javax.management.NotificationFilter;
35  import javax.management.NotificationListener;
36  import javax.management.ObjectInstance;
37  import javax.management.ObjectName;
38  import javax.management.OperationsException;
39  import javax.management.QueryExp;
40  import javax.management.ReflectionException;
41  import javax.management.loading.ClassLoaderRepository;
42  
43  /**
44   * <a href="RegistryAwareMBeanServer.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Michael C. Han
47   */
48  public class RegistryAwareMBeanServer implements MBeanServer {
49  
50      public void addNotificationListener(
51              ObjectName objectName, NotificationListener notificationListener,
52              NotificationFilter notificationFilter, Object handback)
53          throws InstanceNotFoundException {
54  
55          ObjectName platformObjectName = getPlatformObjectName(objectName);
56  
57          _mBeanServer.addNotificationListener(
58              platformObjectName, notificationListener, notificationFilter,
59              handback);
60      }
61  
62      public void addNotificationListener(
63              ObjectName objectName, ObjectName listenerObjectName,
64              NotificationFilter notificationFilter, Object handback)
65          throws InstanceNotFoundException {
66  
67          ObjectName platformObjectName = getPlatformObjectName(objectName);
68          ObjectName platformListenerObjectName = getPlatformObjectName(
69              listenerObjectName);
70  
71          _mBeanServer.addNotificationListener(
72              platformObjectName, platformListenerObjectName, notificationFilter,
73              handback);
74  
75      }
76  
77      public ObjectInstance createMBean(String className, ObjectName objectName)
78          throws InstanceAlreadyExistsException, MBeanException,
79                 MBeanRegistrationException, NotCompliantMBeanException,
80                 ReflectionException {
81  
82          return _mBeanServer.createMBean(className, objectName);
83      }
84  
85      public ObjectInstance createMBean(
86              String className, ObjectName objectName, Object[] params,
87              String[] signature)
88          throws InstanceAlreadyExistsException, MBeanException,
89                 MBeanRegistrationException, NotCompliantMBeanException,
90                 ReflectionException {
91  
92          return _mBeanServer.createMBean(
93              className, objectName, params, signature);
94      }
95  
96      public ObjectInstance createMBean(
97              String className, ObjectName objectName, ObjectName loaderName)
98          throws InstanceAlreadyExistsException, InstanceNotFoundException,
99                 MBeanException, MBeanRegistrationException,
100                NotCompliantMBeanException, ReflectionException {
101 
102         return _mBeanServer.createMBean(className, objectName, loaderName);
103     }
104 
105     public ObjectInstance createMBean(
106             String className, ObjectName objectName,
107             ObjectName loaderObjectName, Object[] params, String[] signature)
108         throws InstanceAlreadyExistsException, InstanceNotFoundException,
109                MBeanException, MBeanRegistrationException,
110                NotCompliantMBeanException, ReflectionException {
111 
112         return _mBeanServer.createMBean(
113             className, objectName, loaderObjectName, params, signature);
114     }
115 
116     /**
117      * @deprecated
118      */
119     public ObjectInputStream deserialize(ObjectName objectName, byte[] data)
120         throws InstanceNotFoundException, OperationsException {
121 
122         ObjectName platformObjectName = getPlatformObjectName(objectName);
123 
124         return _mBeanServer.deserialize(platformObjectName, data);
125     }
126 
127     /**
128      * @deprecated
129      */
130     public ObjectInputStream deserialize(String className, byte[] data)
131         throws OperationsException, ReflectionException {
132 
133         return _mBeanServer.deserialize(className, data);
134     }
135 
136     /**
137      * @deprecated
138      */
139     public ObjectInputStream deserialize(
140             String className, ObjectName loaderObjectName, byte[] data)
141         throws InstanceNotFoundException, OperationsException,
142                ReflectionException {
143 
144         return _mBeanServer.deserialize(className, loaderObjectName, data);
145     }
146 
147     public Object getAttribute(ObjectName objectName, String attribute)
148         throws AttributeNotFoundException, InstanceNotFoundException,
149                MBeanException, ReflectionException {
150 
151         ObjectName platformObjectName = getPlatformObjectName(objectName);
152 
153         return _mBeanServer.getAttribute(platformObjectName, attribute);
154     }
155 
156     public AttributeList getAttributes(
157             ObjectName objectName, String[] attributes)
158         throws InstanceNotFoundException, ReflectionException {
159 
160         ObjectName platformObjectName = getPlatformObjectName(objectName);
161 
162         return _mBeanServer.getAttributes(platformObjectName, attributes);
163     }
164 
165     public ClassLoader getClassLoader(ObjectName loaderObjectName)
166         throws InstanceNotFoundException {
167 
168         return _mBeanServer.getClassLoader(loaderObjectName);
169     }
170 
171     public ClassLoader getClassLoaderFor(ObjectName objectName)
172         throws InstanceNotFoundException {
173 
174         ObjectName platformObjectName = getPlatformObjectName(objectName);
175 
176         return _mBeanServer.getClassLoaderFor(platformObjectName);
177     }
178 
179     public ClassLoaderRepository getClassLoaderRepository() {
180         return _mBeanServer.getClassLoaderRepository();
181     }
182 
183     public String getDefaultDomain() {
184         return _mBeanServer.getDefaultDomain();
185     }
186 
187     public String[] getDomains() {
188         return _mBeanServer.getDomains();
189     }
190 
191     public Integer getMBeanCount() {
192         return _mBeanServer.getMBeanCount();
193     }
194 
195     public MBeanInfo getMBeanInfo(ObjectName objectName)
196         throws InstanceNotFoundException, IntrospectionException,
197                ReflectionException {
198 
199         ObjectName platformObjectName = getPlatformObjectName(objectName);
200 
201         return _mBeanServer.getMBeanInfo(platformObjectName);
202     }
203 
204     public ObjectInstance getObjectInstance(ObjectName objectName)
205         throws InstanceNotFoundException {
206 
207         ObjectName platformObjectName = getPlatformObjectName(objectName);
208 
209         return _mBeanServer.getObjectInstance(platformObjectName);
210     }
211 
212     public Object instantiate(String className)
213         throws MBeanException, ReflectionException {
214 
215         return _mBeanServer.instantiate(className);
216     }
217 
218     public Object instantiate(
219             String className, Object[] params, String[] signature)
220         throws MBeanException, ReflectionException {
221 
222         return _mBeanServer.instantiate(className, params, signature);
223     }
224 
225     public Object instantiate(String className, ObjectName loaderObjectName)
226         throws InstanceNotFoundException, MBeanException, ReflectionException {
227 
228         return _mBeanServer.instantiate(className, loaderObjectName);
229     }
230 
231     public Object instantiate(
232             String className, ObjectName loaderName, Object[] params,
233             String[] signature)
234         throws InstanceNotFoundException, MBeanException, ReflectionException {
235 
236         return _mBeanServer.instantiate(
237             className, loaderName, params, signature);
238     }
239 
240     public Object invoke(
241             ObjectName objectName, String operationName, Object[] params,
242             String[] signature)
243         throws InstanceNotFoundException, MBeanException, ReflectionException {
244 
245         ObjectName platformObjectName = getPlatformObjectName(objectName);
246 
247         return _mBeanServer.invoke(
248             platformObjectName, operationName, params, signature);
249     }
250 
251     public boolean isInstanceOf(ObjectName objectName, String className)
252         throws InstanceNotFoundException {
253 
254         ObjectName platformObjectName = getPlatformObjectName(objectName);
255 
256         return _mBeanServer.isInstanceOf(platformObjectName, className);
257     }
258 
259     public boolean isRegistered(ObjectName objectName) {
260         ObjectName platformObjectName = getPlatformObjectName(objectName);
261 
262         return _mBeanServer.isRegistered(platformObjectName);
263     }
264 
265     public Set<ObjectInstance> queryMBeans(
266         ObjectName objectName, QueryExp queryExp) {
267 
268         return _mBeanServer.queryMBeans(objectName, queryExp);
269     }
270 
271     public Set<ObjectName> queryNames(
272         ObjectName objectName, QueryExp queryExp) {
273 
274         return _mBeanServer.queryNames(objectName, queryExp);
275     }
276 
277     public ObjectInstance registerMBean(Object object, ObjectName objectName)
278         throws InstanceAlreadyExistsException, MBeanRegistrationException,
279                NotCompliantMBeanException {
280 
281         return _mBeanRegistry.register(
282             objectName.getCanonicalName(), object, objectName);
283     }
284 
285     public void removeNotificationListener(
286             ObjectName name, NotificationListener notificationListener)
287         throws InstanceNotFoundException, ListenerNotFoundException {
288 
289         ObjectName platformObjectName = getPlatformObjectName(name);
290 
291         _mBeanServer.removeNotificationListener(
292             platformObjectName, notificationListener);
293     }
294 
295     public void removeNotificationListener(
296             ObjectName objectName, NotificationListener notificationListener,
297             NotificationFilter notificationFilter, Object handback)
298         throws InstanceNotFoundException, ListenerNotFoundException {
299 
300         ObjectName platformObjectName = getPlatformObjectName(objectName);
301 
302         _mBeanServer.removeNotificationListener(
303             platformObjectName, notificationListener, notificationFilter,
304             handback);
305     }
306 
307     public void removeNotificationListener(
308             ObjectName objectName, ObjectName listenerObjectName)
309         throws InstanceNotFoundException, ListenerNotFoundException {
310 
311         ObjectName platformObjectName = getPlatformObjectName(objectName);
312         ObjectName platformListenerObjectName = getPlatformObjectName(
313             listenerObjectName);
314 
315         _mBeanServer.removeNotificationListener(
316             platformObjectName, platformListenerObjectName);
317     }
318 
319     public void removeNotificationListener(
320             ObjectName objectName, ObjectName listenerObjectName,
321             NotificationFilter notificationFilter, Object handback)
322         throws InstanceNotFoundException, ListenerNotFoundException {
323 
324         ObjectName platformObjectName = getPlatformObjectName(objectName);
325         ObjectName platformListenerObjectName = getPlatformObjectName(
326             listenerObjectName);
327 
328         _mBeanServer.removeNotificationListener(
329             platformObjectName, platformListenerObjectName, notificationFilter,
330             handback);
331 
332     }
333 
334     public void setAttribute(ObjectName objectName, Attribute attribute)
335         throws AttributeNotFoundException, InstanceNotFoundException,
336                InvalidAttributeValueException, MBeanException,
337                ReflectionException {
338 
339         ObjectName platformObjectName = getPlatformObjectName(objectName);
340 
341         _mBeanServer.setAttribute(platformObjectName, attribute);
342     }
343 
344     public AttributeList setAttributes(
345             ObjectName objectName, AttributeList attributeList)
346         throws InstanceNotFoundException, ReflectionException {
347 
348         ObjectName platformObjectName = getPlatformObjectName(objectName);
349 
350         return _mBeanServer.setAttributes(platformObjectName, attributeList);
351     }
352 
353     public void setMBeanRegistry(MBeanRegistry mBeanRegistry) {
354         _mBeanRegistry = mBeanRegistry;
355     }
356 
357     public void setMBeanServer(MBeanServer mBeanServer) {
358         _mBeanServer = mBeanServer;
359     }
360 
361     public void unregisterMBean(ObjectName objectName)
362         throws InstanceNotFoundException, MBeanRegistrationException {
363 
364         _mBeanRegistry.unregister(objectName.getCanonicalName(), objectName);
365     }
366 
367     protected ObjectName getPlatformObjectName(ObjectName objectName) {
368         ObjectName platformObjectName = _mBeanRegistry.getObjectName(
369             objectName.getCanonicalName());
370 
371         if (platformObjectName == null) {
372             platformObjectName = objectName;
373         }
374 
375         return platformObjectName;
376     }
377 
378     private MBeanRegistry _mBeanRegistry;
379     private MBeanServer _mBeanServer;
380 
381 }