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