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