1 /* |
|
2 * Copyright 2007-2008 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Sun designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Sun in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 * have any questions. |
|
24 */ |
|
25 |
|
26 package com.sun.jmx.mbeanserver; |
|
27 |
|
28 import javax.management.Attribute; |
|
29 import javax.management.AttributeList; |
|
30 import javax.management.AttributeNotFoundException; |
|
31 import javax.management.DynamicMBean; |
|
32 import javax.management.DynamicWrapperMBean; |
|
33 import javax.management.InvalidAttributeValueException; |
|
34 import javax.management.ListenerNotFoundException; |
|
35 import javax.management.MBeanException; |
|
36 import javax.management.MBeanInfo; |
|
37 import javax.management.MBeanNotificationInfo; |
|
38 import javax.management.MBeanRegistration; |
|
39 import javax.management.MBeanServer; |
|
40 import javax.management.NotificationBroadcasterSupport; |
|
41 import javax.management.NotificationEmitter; |
|
42 import javax.management.NotificationFilter; |
|
43 import javax.management.NotificationListener; |
|
44 import javax.management.ObjectName; |
|
45 import javax.management.ReflectionException; |
|
46 |
|
47 /** |
|
48 * Create wrappers for DynamicMBean that implement NotificationEmitter |
|
49 * and SendNotification. |
|
50 */ |
|
51 public class NotifySupport |
|
52 implements DynamicMBean2, NotificationEmitter, MBeanRegistration { |
|
53 |
|
54 private final DynamicMBean mbean; |
|
55 private final NotificationBroadcasterSupport nbs; |
|
56 |
|
57 public static DynamicMBean wrap( |
|
58 DynamicMBean mbean, NotificationBroadcasterSupport nbs) { |
|
59 return new NotifySupport(mbean, nbs); |
|
60 } |
|
61 |
|
62 private NotifySupport(DynamicMBean mbean, NotificationBroadcasterSupport nbs) { |
|
63 this.mbean = mbean; |
|
64 this.nbs = nbs; |
|
65 } |
|
66 |
|
67 public static NotificationBroadcasterSupport getNB(DynamicMBean mbean) { |
|
68 if (mbean instanceof NotifySupport) |
|
69 return ((NotifySupport) mbean).nbs; |
|
70 else |
|
71 return null; |
|
72 } |
|
73 |
|
74 public String getClassName() { |
|
75 if (mbean instanceof DynamicMBean2) |
|
76 return ((DynamicMBean2) mbean).getClassName(); |
|
77 Object w = mbean; |
|
78 if (w instanceof DynamicWrapperMBean) |
|
79 w = ((DynamicWrapperMBean) w).getWrappedObject(); |
|
80 return w.getClass().getName(); |
|
81 } |
|
82 |
|
83 public void preRegister2(MBeanServer mbs, ObjectName name) throws Exception { |
|
84 if (mbean instanceof DynamicMBean2) |
|
85 ((DynamicMBean2) mbean).preRegister2(mbs, name); |
|
86 } |
|
87 |
|
88 public void registerFailed() { |
|
89 if (mbean instanceof DynamicMBean2) |
|
90 ((DynamicMBean2) mbean).registerFailed(); |
|
91 } |
|
92 |
|
93 public Object getWrappedObject() { |
|
94 if (mbean instanceof DynamicWrapperMBean) |
|
95 return ((DynamicWrapperMBean) mbean).getWrappedObject(); |
|
96 else |
|
97 return mbean; |
|
98 } |
|
99 |
|
100 public ClassLoader getWrappedClassLoader() { |
|
101 if (mbean instanceof DynamicWrapperMBean) |
|
102 return ((DynamicWrapperMBean) mbean).getWrappedClassLoader(); |
|
103 else |
|
104 return mbean.getClass().getClassLoader(); |
|
105 } |
|
106 |
|
107 public Object getAttribute(String attribute) throws AttributeNotFoundException, |
|
108 MBeanException, |
|
109 ReflectionException { |
|
110 return mbean.getAttribute(attribute); |
|
111 } |
|
112 |
|
113 public void setAttribute(Attribute attribute) throws AttributeNotFoundException, |
|
114 InvalidAttributeValueException, |
|
115 MBeanException, |
|
116 ReflectionException { |
|
117 mbean.setAttribute(attribute); |
|
118 } |
|
119 |
|
120 public AttributeList setAttributes(AttributeList attributes) { |
|
121 return mbean.setAttributes(attributes); |
|
122 } |
|
123 |
|
124 public Object invoke(String actionName, Object[] params, String[] signature) |
|
125 throws MBeanException, ReflectionException { |
|
126 return mbean.invoke(actionName, params, signature); |
|
127 } |
|
128 |
|
129 public MBeanInfo getMBeanInfo() { |
|
130 return mbean.getMBeanInfo(); |
|
131 } |
|
132 |
|
133 public AttributeList getAttributes(String[] attributes) { |
|
134 return mbean.getAttributes(attributes); |
|
135 } |
|
136 |
|
137 public void removeNotificationListener(NotificationListener listener, |
|
138 NotificationFilter filter, |
|
139 Object handback) throws ListenerNotFoundException { |
|
140 nbs.removeNotificationListener(listener, filter, handback); |
|
141 } |
|
142 |
|
143 public void removeNotificationListener(NotificationListener listener) |
|
144 throws ListenerNotFoundException { |
|
145 nbs.removeNotificationListener(listener); |
|
146 } |
|
147 |
|
148 public MBeanNotificationInfo[] getNotificationInfo() { |
|
149 return nbs.getNotificationInfo(); |
|
150 } |
|
151 |
|
152 public void addNotificationListener(NotificationListener listener, |
|
153 NotificationFilter filter, |
|
154 Object handback) { |
|
155 nbs.addNotificationListener(listener, filter, handback); |
|
156 } |
|
157 |
|
158 public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception { |
|
159 if (mbr() != null) |
|
160 return mbr().preRegister(server, name); |
|
161 else |
|
162 return name; |
|
163 } |
|
164 |
|
165 public void postRegister(Boolean registrationDone) { |
|
166 if (mbr() != null) |
|
167 mbr().postRegister(registrationDone); |
|
168 } |
|
169 |
|
170 public void preDeregister() throws Exception { |
|
171 if (mbr() != null) |
|
172 mbr().preDeregister(); |
|
173 } |
|
174 |
|
175 public void postDeregister() { |
|
176 if (mbr() != null) |
|
177 mbr().postDeregister(); |
|
178 } |
|
179 |
|
180 private MBeanRegistration mbr() { |
|
181 if (mbean instanceof MBeanRegistration) |
|
182 return (MBeanRegistration) mbean; |
|
183 else |
|
184 return null; |
|
185 } |
|
186 } |
|