001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.jmx;
016    
017    import java.io.ObjectInputStream;
018    
019    import java.util.Set;
020    
021    import javax.management.Attribute;
022    import javax.management.AttributeList;
023    import javax.management.AttributeNotFoundException;
024    import javax.management.InstanceAlreadyExistsException;
025    import javax.management.InstanceNotFoundException;
026    import javax.management.IntrospectionException;
027    import javax.management.InvalidAttributeValueException;
028    import javax.management.ListenerNotFoundException;
029    import javax.management.MBeanException;
030    import javax.management.MBeanInfo;
031    import javax.management.MBeanRegistrationException;
032    import javax.management.MBeanServer;
033    import javax.management.NotCompliantMBeanException;
034    import javax.management.NotificationFilter;
035    import javax.management.NotificationListener;
036    import javax.management.ObjectInstance;
037    import javax.management.ObjectName;
038    import javax.management.OperationsException;
039    import javax.management.QueryExp;
040    import javax.management.ReflectionException;
041    import javax.management.loading.ClassLoaderRepository;
042    
043    /**
044     * @author Michael C. Han
045     */
046    public class RegistryAwareMBeanServer implements MBeanServer {
047    
048            @Override
049            public void addNotificationListener(
050                            ObjectName objectName, NotificationListener notificationListener,
051                            NotificationFilter notificationFilter, Object handback)
052                    throws InstanceNotFoundException {
053    
054                    ObjectName platformObjectName = getPlatformObjectName(objectName);
055    
056                    _mBeanServer.addNotificationListener(
057                            platformObjectName, notificationListener, notificationFilter,
058                            handback);
059            }
060    
061            @Override
062            public void addNotificationListener(
063                            ObjectName objectName, ObjectName listenerObjectName,
064                            NotificationFilter notificationFilter, Object handback)
065                    throws InstanceNotFoundException {
066    
067                    ObjectName platformObjectName = getPlatformObjectName(objectName);
068                    ObjectName platformListenerObjectName = getPlatformObjectName(
069                            listenerObjectName);
070    
071                    _mBeanServer.addNotificationListener(
072                            platformObjectName, platformListenerObjectName, notificationFilter,
073                            handback);
074            }
075    
076            @Override
077            public ObjectInstance createMBean(String className, ObjectName objectName)
078                    throws InstanceAlreadyExistsException, MBeanException,
079                               NotCompliantMBeanException, ReflectionException {
080    
081                    return _mBeanServer.createMBean(className, objectName);
082            }
083    
084            @Override
085            public ObjectInstance createMBean(
086                            String className, ObjectName objectName, Object[] params,
087                            String[] signature)
088                    throws InstanceAlreadyExistsException, MBeanException,
089                               NotCompliantMBeanException, ReflectionException {
090    
091                    return _mBeanServer.createMBean(
092                            className, objectName, params, signature);
093            }
094    
095            @Override
096            public ObjectInstance createMBean(
097                            String className, ObjectName objectName, ObjectName loaderName)
098                    throws InstanceAlreadyExistsException, InstanceNotFoundException,
099                               MBeanException, NotCompliantMBeanException, ReflectionException {
100    
101                    return _mBeanServer.createMBean(className, objectName, loaderName);
102            }
103    
104            @Override
105            public ObjectInstance createMBean(
106                            String className, ObjectName objectName,
107                            ObjectName loaderObjectName, Object[] params, String[] signature)
108                    throws InstanceAlreadyExistsException, InstanceNotFoundException,
109                               MBeanException, NotCompliantMBeanException, ReflectionException {
110    
111                    return _mBeanServer.createMBean(
112                            className, objectName, loaderObjectName, params, signature);
113            }
114    
115            /**
116             * @deprecated As of 6.1.0
117             */
118            @Override
119            public ObjectInputStream deserialize(ObjectName objectName, byte[] data)
120                    throws OperationsException {
121    
122                    ObjectName platformObjectName = getPlatformObjectName(objectName);
123    
124                    return _mBeanServer.deserialize(platformObjectName, data);
125            }
126    
127            /**
128             * @deprecated As of 6.1.0
129             */
130            @Override
131            public ObjectInputStream deserialize(String className, byte[] data)
132                    throws OperationsException, ReflectionException {
133    
134                    return _mBeanServer.deserialize(className, data);
135            }
136    
137            /**
138             * @deprecated As of 6.1.0
139             */
140            @Override
141            public ObjectInputStream deserialize(
142                            String className, ObjectName loaderObjectName, byte[] data)
143                    throws OperationsException, ReflectionException {
144    
145                    return _mBeanServer.deserialize(className, loaderObjectName, data);
146            }
147    
148            @Override
149            public Object getAttribute(ObjectName objectName, String attribute)
150                    throws AttributeNotFoundException, InstanceNotFoundException,
151                               MBeanException, ReflectionException {
152    
153                    ObjectName platformObjectName = getPlatformObjectName(objectName);
154    
155                    return _mBeanServer.getAttribute(platformObjectName, attribute);
156            }
157    
158            @Override
159            public AttributeList getAttributes(
160                            ObjectName objectName, String[] attributes)
161                    throws InstanceNotFoundException, ReflectionException {
162    
163                    ObjectName platformObjectName = getPlatformObjectName(objectName);
164    
165                    return _mBeanServer.getAttributes(platformObjectName, attributes);
166            }
167    
168            @Override
169            public ClassLoader getClassLoader(ObjectName loaderObjectName)
170                    throws InstanceNotFoundException {
171    
172                    return _mBeanServer.getClassLoader(loaderObjectName);
173            }
174    
175            @Override
176            public ClassLoader getClassLoaderFor(ObjectName objectName)
177                    throws InstanceNotFoundException {
178    
179                    ObjectName platformObjectName = getPlatformObjectName(objectName);
180    
181                    return _mBeanServer.getClassLoaderFor(platformObjectName);
182            }
183    
184            @Override
185            public ClassLoaderRepository getClassLoaderRepository() {
186                    return _mBeanServer.getClassLoaderRepository();
187            }
188    
189            @Override
190            public String getDefaultDomain() {
191                    return _mBeanServer.getDefaultDomain();
192            }
193    
194            @Override
195            public String[] getDomains() {
196                    return _mBeanServer.getDomains();
197            }
198    
199            @Override
200            public Integer getMBeanCount() {
201                    return _mBeanServer.getMBeanCount();
202            }
203    
204            @Override
205            public MBeanInfo getMBeanInfo(ObjectName objectName)
206                    throws InstanceNotFoundException, IntrospectionException,
207                               ReflectionException {
208    
209                    ObjectName platformObjectName = getPlatformObjectName(objectName);
210    
211                    return _mBeanServer.getMBeanInfo(platformObjectName);
212            }
213    
214            @Override
215            public ObjectInstance getObjectInstance(ObjectName objectName)
216                    throws InstanceNotFoundException {
217    
218                    ObjectName platformObjectName = getPlatformObjectName(objectName);
219    
220                    return _mBeanServer.getObjectInstance(platformObjectName);
221            }
222    
223            @Override
224            public Object instantiate(String className)
225                    throws MBeanException, ReflectionException {
226    
227                    return _mBeanServer.instantiate(className);
228            }
229    
230            @Override
231            public Object instantiate(
232                            String className, Object[] params, String[] signature)
233                    throws MBeanException, ReflectionException {
234    
235                    return _mBeanServer.instantiate(className, params, signature);
236            }
237    
238            @Override
239            public Object instantiate(String className, ObjectName loaderObjectName)
240                    throws InstanceNotFoundException, MBeanException, ReflectionException {
241    
242                    return _mBeanServer.instantiate(className, loaderObjectName);
243            }
244    
245            @Override
246            public Object instantiate(
247                            String className, ObjectName loaderName, Object[] params,
248                            String[] signature)
249                    throws InstanceNotFoundException, MBeanException, ReflectionException {
250    
251                    return _mBeanServer.instantiate(
252                            className, loaderName, params, signature);
253            }
254    
255            @Override
256            public Object invoke(
257                            ObjectName objectName, String operationName, Object[] params,
258                            String[] signature)
259                    throws InstanceNotFoundException, MBeanException, ReflectionException {
260    
261                    ObjectName platformObjectName = getPlatformObjectName(objectName);
262    
263                    return _mBeanServer.invoke(
264                            platformObjectName, operationName, params, signature);
265            }
266    
267            @Override
268            public boolean isInstanceOf(ObjectName objectName, String className)
269                    throws InstanceNotFoundException {
270    
271                    ObjectName platformObjectName = getPlatformObjectName(objectName);
272    
273                    return _mBeanServer.isInstanceOf(platformObjectName, className);
274            }
275    
276            @Override
277            public boolean isRegistered(ObjectName objectName) {
278                    ObjectName platformObjectName = getPlatformObjectName(objectName);
279    
280                    return _mBeanServer.isRegistered(platformObjectName);
281            }
282    
283            @Override
284            public Set<ObjectInstance> queryMBeans(
285                    ObjectName objectName, QueryExp queryExp) {
286    
287                    return _mBeanServer.queryMBeans(objectName, queryExp);
288            }
289    
290            @Override
291            public Set<ObjectName> queryNames(
292                    ObjectName objectName, QueryExp queryExp) {
293    
294                    return _mBeanServer.queryNames(objectName, queryExp);
295            }
296    
297            @Override
298            public ObjectInstance registerMBean(Object object, ObjectName objectName)
299                    throws InstanceAlreadyExistsException, MBeanRegistrationException,
300                               NotCompliantMBeanException {
301    
302                    return _mBeanRegistry.register(
303                            objectName.getCanonicalName(), object, objectName);
304            }
305    
306            @Override
307            public void removeNotificationListener(
308                            ObjectName name, NotificationListener notificationListener)
309                    throws InstanceNotFoundException, ListenerNotFoundException {
310    
311                    ObjectName platformObjectName = getPlatformObjectName(name);
312    
313                    _mBeanServer.removeNotificationListener(
314                            platformObjectName, notificationListener);
315            }
316    
317            @Override
318            public void removeNotificationListener(
319                            ObjectName objectName, NotificationListener notificationListener,
320                            NotificationFilter notificationFilter, Object handback)
321                    throws InstanceNotFoundException, ListenerNotFoundException {
322    
323                    ObjectName platformObjectName = getPlatformObjectName(objectName);
324    
325                    _mBeanServer.removeNotificationListener(
326                            platformObjectName, notificationListener, notificationFilter,
327                            handback);
328            }
329    
330            @Override
331            public void removeNotificationListener(
332                            ObjectName objectName, ObjectName listenerObjectName)
333                    throws InstanceNotFoundException, ListenerNotFoundException {
334    
335                    ObjectName platformObjectName = getPlatformObjectName(objectName);
336                    ObjectName platformListenerObjectName = getPlatformObjectName(
337                            listenerObjectName);
338    
339                    _mBeanServer.removeNotificationListener(
340                            platformObjectName, platformListenerObjectName);
341            }
342    
343            @Override
344            public void removeNotificationListener(
345                            ObjectName objectName, ObjectName listenerObjectName,
346                            NotificationFilter notificationFilter, Object handback)
347                    throws InstanceNotFoundException, ListenerNotFoundException {
348    
349                    ObjectName platformObjectName = getPlatformObjectName(objectName);
350                    ObjectName platformListenerObjectName = getPlatformObjectName(
351                            listenerObjectName);
352    
353                    _mBeanServer.removeNotificationListener(
354                            platformObjectName, platformListenerObjectName, notificationFilter,
355                            handback);
356            }
357    
358            @Override
359            public void setAttribute(ObjectName objectName, Attribute attribute)
360                    throws AttributeNotFoundException, InstanceNotFoundException,
361                               InvalidAttributeValueException, MBeanException,
362                               ReflectionException {
363    
364                    ObjectName platformObjectName = getPlatformObjectName(objectName);
365    
366                    _mBeanServer.setAttribute(platformObjectName, attribute);
367            }
368    
369            @Override
370            public AttributeList setAttributes(
371                            ObjectName objectName, AttributeList attributeList)
372                    throws InstanceNotFoundException, ReflectionException {
373    
374                    ObjectName platformObjectName = getPlatformObjectName(objectName);
375    
376                    return _mBeanServer.setAttributes(platformObjectName, attributeList);
377            }
378    
379            public void setMBeanRegistry(MBeanRegistry mBeanRegistry) {
380                    _mBeanRegistry = mBeanRegistry;
381            }
382    
383            public void setMBeanServer(MBeanServer mBeanServer) {
384                    _mBeanServer = mBeanServer;
385            }
386    
387            @Override
388            public void unregisterMBean(ObjectName objectName)
389                    throws InstanceNotFoundException, MBeanRegistrationException {
390    
391                    _mBeanRegistry.unregister(objectName.getCanonicalName(), objectName);
392            }
393    
394            protected ObjectName getPlatformObjectName(ObjectName objectName) {
395                    ObjectName platformObjectName = _mBeanRegistry.getObjectName(
396                            objectName.getCanonicalName());
397    
398                    if (platformObjectName == null) {
399                            platformObjectName = objectName;
400                    }
401    
402                    return platformObjectName;
403            }
404    
405            private MBeanRegistry _mBeanRegistry;
406            private MBeanServer _mBeanServer;
407    
408    }