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