001    /**
002     * Copyright (c) 2000-present 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                               MBeanRegistrationException, NotCompliantMBeanException,
080                               ReflectionException {
081    
082                    return _mBeanServer.createMBean(className, objectName);
083            }
084    
085            @Override
086            public ObjectInstance createMBean(
087                            String className, ObjectName objectName, Object[] params,
088                            String[] signature)
089                    throws InstanceAlreadyExistsException, MBeanException,
090                               MBeanRegistrationException, NotCompliantMBeanException,
091                               ReflectionException {
092    
093                    return _mBeanServer.createMBean(
094                            className, objectName, params, signature);
095            }
096    
097            @Override
098            public ObjectInstance createMBean(
099                            String className, ObjectName objectName, ObjectName loaderName)
100                    throws InstanceAlreadyExistsException, InstanceNotFoundException,
101                               MBeanException, MBeanRegistrationException,
102                               NotCompliantMBeanException, ReflectionException {
103    
104                    return _mBeanServer.createMBean(className, objectName, loaderName);
105            }
106    
107            @Override
108            public ObjectInstance createMBean(
109                            String className, ObjectName objectName,
110                            ObjectName loaderObjectName, Object[] params, String[] signature)
111                    throws InstanceAlreadyExistsException, InstanceNotFoundException,
112                               MBeanException, MBeanRegistrationException,
113                               NotCompliantMBeanException, ReflectionException {
114    
115                    return _mBeanServer.createMBean(
116                            className, objectName, loaderObjectName, params, signature);
117            }
118    
119            /**
120             * @deprecated As of 6.1.0
121             */
122            @Deprecated
123            @Override
124            public ObjectInputStream deserialize(ObjectName objectName, byte[] data)
125                    throws InstanceNotFoundException, OperationsException {
126    
127                    ObjectName platformObjectName = getPlatformObjectName(objectName);
128    
129                    return _mBeanServer.deserialize(platformObjectName, data);
130            }
131    
132            /**
133             * @deprecated As of 6.1.0
134             */
135            @Deprecated
136            @Override
137            public ObjectInputStream deserialize(String className, byte[] data)
138                    throws OperationsException, ReflectionException {
139    
140                    return _mBeanServer.deserialize(className, data);
141            }
142    
143            /**
144             * @deprecated As of 6.1.0
145             */
146            @Deprecated
147            @Override
148            public ObjectInputStream deserialize(
149                            String className, ObjectName loaderObjectName, byte[] data)
150                    throws InstanceNotFoundException, OperationsException,
151                               ReflectionException {
152    
153                    return _mBeanServer.deserialize(className, loaderObjectName, data);
154            }
155    
156            @Override
157            public Object getAttribute(ObjectName objectName, String attribute)
158                    throws AttributeNotFoundException, InstanceNotFoundException,
159                               MBeanException, ReflectionException {
160    
161                    ObjectName platformObjectName = getPlatformObjectName(objectName);
162    
163                    return _mBeanServer.getAttribute(platformObjectName, attribute);
164            }
165    
166            @Override
167            public AttributeList getAttributes(
168                            ObjectName objectName, String[] attributes)
169                    throws InstanceNotFoundException, ReflectionException {
170    
171                    ObjectName platformObjectName = getPlatformObjectName(objectName);
172    
173                    return _mBeanServer.getAttributes(platformObjectName, attributes);
174            }
175    
176            @Override
177            public ClassLoader getClassLoader(ObjectName loaderObjectName)
178                    throws InstanceNotFoundException {
179    
180                    return _mBeanServer.getClassLoader(loaderObjectName);
181            }
182    
183            @Override
184            public ClassLoader getClassLoaderFor(ObjectName objectName)
185                    throws InstanceNotFoundException {
186    
187                    ObjectName platformObjectName = getPlatformObjectName(objectName);
188    
189                    return _mBeanServer.getClassLoaderFor(platformObjectName);
190            }
191    
192            @Override
193            public ClassLoaderRepository getClassLoaderRepository() {
194                    return _mBeanServer.getClassLoaderRepository();
195            }
196    
197            @Override
198            public String getDefaultDomain() {
199                    return _mBeanServer.getDefaultDomain();
200            }
201    
202            @Override
203            public String[] getDomains() {
204                    return _mBeanServer.getDomains();
205            }
206    
207            @Override
208            public Integer getMBeanCount() {
209                    return _mBeanServer.getMBeanCount();
210            }
211    
212            @Override
213            public MBeanInfo getMBeanInfo(ObjectName objectName)
214                    throws InstanceNotFoundException, IntrospectionException,
215                               ReflectionException {
216    
217                    ObjectName platformObjectName = getPlatformObjectName(objectName);
218    
219                    return _mBeanServer.getMBeanInfo(platformObjectName);
220            }
221    
222            @Override
223            public ObjectInstance getObjectInstance(ObjectName objectName)
224                    throws InstanceNotFoundException {
225    
226                    ObjectName platformObjectName = getPlatformObjectName(objectName);
227    
228                    return _mBeanServer.getObjectInstance(platformObjectName);
229            }
230    
231            @Override
232            public Object instantiate(String className)
233                    throws MBeanException, ReflectionException {
234    
235                    return _mBeanServer.instantiate(className);
236            }
237    
238            @Override
239            public Object instantiate(
240                            String className, Object[] params, String[] signature)
241                    throws MBeanException, ReflectionException {
242    
243                    return _mBeanServer.instantiate(className, params, signature);
244            }
245    
246            @Override
247            public Object instantiate(String className, ObjectName loaderObjectName)
248                    throws InstanceNotFoundException, MBeanException, ReflectionException {
249    
250                    return _mBeanServer.instantiate(className, loaderObjectName);
251            }
252    
253            @Override
254            public Object instantiate(
255                            String className, ObjectName loaderName, Object[] params,
256                            String[] signature)
257                    throws InstanceNotFoundException, MBeanException, ReflectionException {
258    
259                    return _mBeanServer.instantiate(
260                            className, loaderName, params, signature);
261            }
262    
263            @Override
264            public Object invoke(
265                            ObjectName objectName, String operationName, Object[] params,
266                            String[] signature)
267                    throws InstanceNotFoundException, MBeanException, ReflectionException {
268    
269                    ObjectName platformObjectName = getPlatformObjectName(objectName);
270    
271                    return _mBeanServer.invoke(
272                            platformObjectName, operationName, params, signature);
273            }
274    
275            @Override
276            public boolean isInstanceOf(ObjectName objectName, String className)
277                    throws InstanceNotFoundException {
278    
279                    ObjectName platformObjectName = getPlatformObjectName(objectName);
280    
281                    return _mBeanServer.isInstanceOf(platformObjectName, className);
282            }
283    
284            @Override
285            public boolean isRegistered(ObjectName objectName) {
286                    ObjectName platformObjectName = getPlatformObjectName(objectName);
287    
288                    return _mBeanServer.isRegistered(platformObjectName);
289            }
290    
291            @Override
292            public Set<ObjectInstance> queryMBeans(
293                    ObjectName objectName, QueryExp queryExp) {
294    
295                    return _mBeanServer.queryMBeans(objectName, queryExp);
296            }
297    
298            @Override
299            public Set<ObjectName> queryNames(
300                    ObjectName objectName, QueryExp queryExp) {
301    
302                    return _mBeanServer.queryNames(objectName, queryExp);
303            }
304    
305            @Override
306            public ObjectInstance registerMBean(Object object, ObjectName objectName)
307                    throws InstanceAlreadyExistsException, MBeanRegistrationException,
308                               NotCompliantMBeanException {
309    
310                    return _mBeanRegistry.register(
311                            objectName.getCanonicalName(), object, objectName);
312            }
313    
314            @Override
315            public void removeNotificationListener(
316                            ObjectName name, NotificationListener notificationListener)
317                    throws InstanceNotFoundException, ListenerNotFoundException {
318    
319                    ObjectName platformObjectName = getPlatformObjectName(name);
320    
321                    _mBeanServer.removeNotificationListener(
322                            platformObjectName, notificationListener);
323            }
324    
325            @Override
326            public void removeNotificationListener(
327                            ObjectName objectName, NotificationListener notificationListener,
328                            NotificationFilter notificationFilter, Object handback)
329                    throws InstanceNotFoundException, ListenerNotFoundException {
330    
331                    ObjectName platformObjectName = getPlatformObjectName(objectName);
332    
333                    _mBeanServer.removeNotificationListener(
334                            platformObjectName, notificationListener, notificationFilter,
335                            handback);
336            }
337    
338            @Override
339            public void removeNotificationListener(
340                            ObjectName objectName, ObjectName listenerObjectName)
341                    throws InstanceNotFoundException, ListenerNotFoundException {
342    
343                    ObjectName platformObjectName = getPlatformObjectName(objectName);
344                    ObjectName platformListenerObjectName = getPlatformObjectName(
345                            listenerObjectName);
346    
347                    _mBeanServer.removeNotificationListener(
348                            platformObjectName, platformListenerObjectName);
349            }
350    
351            @Override
352            public void removeNotificationListener(
353                            ObjectName objectName, ObjectName listenerObjectName,
354                            NotificationFilter notificationFilter, Object handback)
355                    throws InstanceNotFoundException, ListenerNotFoundException {
356    
357                    ObjectName platformObjectName = getPlatformObjectName(objectName);
358                    ObjectName platformListenerObjectName = getPlatformObjectName(
359                            listenerObjectName);
360    
361                    _mBeanServer.removeNotificationListener(
362                            platformObjectName, platformListenerObjectName, notificationFilter,
363                            handback);
364            }
365    
366            @Override
367            public void setAttribute(ObjectName objectName, Attribute attribute)
368                    throws AttributeNotFoundException, InstanceNotFoundException,
369                               InvalidAttributeValueException, MBeanException,
370                               ReflectionException {
371    
372                    ObjectName platformObjectName = getPlatformObjectName(objectName);
373    
374                    _mBeanServer.setAttribute(platformObjectName, attribute);
375            }
376    
377            @Override
378            public AttributeList setAttributes(
379                            ObjectName objectName, AttributeList attributeList)
380                    throws InstanceNotFoundException, ReflectionException {
381    
382                    ObjectName platformObjectName = getPlatformObjectName(objectName);
383    
384                    return _mBeanServer.setAttributes(platformObjectName, attributeList);
385            }
386    
387            public void setMBeanRegistry(MBeanRegistry mBeanRegistry) {
388                    _mBeanRegistry = mBeanRegistry;
389            }
390    
391            public void setMBeanServer(MBeanServer mBeanServer) {
392                    _mBeanServer = mBeanServer;
393            }
394    
395            @Override
396            public void unregisterMBean(ObjectName objectName)
397                    throws InstanceNotFoundException, MBeanRegistrationException {
398    
399                    _mBeanRegistry.unregister(objectName.getCanonicalName(), objectName);
400            }
401    
402            protected ObjectName getPlatformObjectName(ObjectName objectName) {
403                    ObjectName platformObjectName = _mBeanRegistry.getObjectName(
404                            objectName.getCanonicalName());
405    
406                    if (platformObjectName == null) {
407                            platformObjectName = objectName;
408                    }
409    
410                    return platformObjectName;
411            }
412    
413            private MBeanRegistry _mBeanRegistry;
414            private MBeanServer _mBeanServer;
415    
416    }