001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008:
009: package test.javax.management.support;
010:
011: import javax.management.InstanceNotFoundException;
012: import javax.management.MBeanOperationInfo;
013: import javax.management.MBeanParameterInfo;
014: import javax.management.MBeanRegistration;
015: import javax.management.MBeanServer;
016: import javax.management.Notification;
017: import javax.management.NotificationListener;
018: import javax.management.ObjectName;
019:
020: import mx4j.AbstractDynamicMBean;
021: import test.MutableBoolean;
022: import test.MutableObject;
023:
024: /**
025: * @version $Revision: 1.7 $
026: */
027: public class RegistrationSupport {
028: public interface NullObjectNameMBean {
029: }
030:
031: public static class NullObjectName implements NullObjectNameMBean,
032: MBeanRegistration {
033: public ObjectName preRegister(MBeanServer server,
034: ObjectName name) throws Exception {
035: return null;
036: }
037:
038: public void postRegister(Boolean registrationDone) {
039: }
040:
041: public void preDeregister() throws Exception {
042: }
043:
044: public void postDeregister() {
045: }
046: }
047:
048: public interface PreRegisterExceptionMBean {
049: }
050:
051: public static class PreRegisterException implements
052: PreRegisterExceptionMBean, MBeanRegistration {
053: public ObjectName preRegister(MBeanServer server,
054: ObjectName name) throws Exception {
055: throw new Exception();
056: }
057:
058: public void postRegister(Boolean registrationDone) {
059: }
060:
061: public void preDeregister() throws Exception {
062: }
063:
064: public void postDeregister() {
065: }
066: }
067:
068: public interface PostRegisterExceptionMBean {
069: }
070:
071: public static class PostRegisterException implements
072: PostRegisterExceptionMBean, MBeanRegistration {
073: public ObjectName preRegister(MBeanServer server,
074: ObjectName name) throws Exception {
075: return name;
076: }
077:
078: public void postRegister(Boolean registrationDone) {
079: throw new RuntimeException();
080: }
081:
082: public void preDeregister() throws Exception {
083: }
084:
085: public void postDeregister() {
086: }
087: }
088:
089: public interface PreDeregisterExceptionMBean {
090: }
091:
092: public static class PreDeregisterException implements
093: PreDeregisterExceptionMBean, MBeanRegistration {
094: public ObjectName preRegister(MBeanServer server,
095: ObjectName name) throws Exception {
096: return name;
097: }
098:
099: public void postRegister(Boolean registrationDone) {
100: }
101:
102: public void preDeregister() throws Exception {
103: throw new Exception();
104: }
105:
106: public void postDeregister() {
107: }
108: }
109:
110: public interface PostDeregisterExceptionMBean {
111: }
112:
113: public static class PostDeregisterException implements
114: PostDeregisterExceptionMBean, MBeanRegistration {
115: public ObjectName preRegister(MBeanServer server,
116: ObjectName name) throws Exception {
117: return name;
118: }
119:
120: public void postRegister(Boolean registrationDone) {
121: }
122:
123: public void preDeregister() throws Exception {
124: }
125:
126: public void postDeregister() {
127: throw new RuntimeException();
128: }
129: }
130:
131: public interface EmptyMBean {
132: }
133:
134: public static class Empty implements EmptyMBean, MBeanRegistration {
135: private MutableBoolean m_bool1;
136: private MutableBoolean m_bool2;
137:
138: public Empty(MutableBoolean bool1, MutableBoolean bool2) {
139: m_bool1 = bool1;
140: m_bool2 = bool2;
141: }
142:
143: public ObjectName preRegister(MBeanServer server,
144: ObjectName name) throws Exception {
145: return name;
146: }
147:
148: public void postRegister(Boolean registrationDone) {
149: m_bool1.set(registrationDone.booleanValue());
150: }
151:
152: public void preDeregister() throws Exception {
153: m_bool1.set(false);
154: }
155:
156: public void postDeregister() {
157: m_bool2.set(false);
158: }
159: }
160:
161: public interface EmptyDuplicateMBean {
162: }
163:
164: public static class EmptyDuplicate implements EmptyDuplicateMBean,
165: MBeanRegistration {
166: private ObjectName m_name;
167: private MutableBoolean m_bool1;
168:
169: public EmptyDuplicate(ObjectName name, MutableBoolean bool1) {
170: m_name = name;
171: m_bool1 = bool1;
172: }
173:
174: public ObjectName preRegister(MBeanServer server,
175: ObjectName name) throws Exception {
176: return m_name;
177: }
178:
179: public void postRegister(Boolean registrationDone) {
180: m_bool1.set(registrationDone.booleanValue());
181: }
182:
183: public void preDeregister() throws Exception {
184: }
185:
186: public void postDeregister() {
187: }
188: }
189:
190: public interface StdMBean {
191: public void method();
192: }
193:
194: public static class Std implements StdMBean, MBeanRegistration {
195: public ObjectName preRegister(MBeanServer server,
196: ObjectName name) throws Exception {
197: return name;
198: }
199:
200: public void postRegister(Boolean registrationDone) {
201: }
202:
203: public void preDeregister() throws Exception {
204: }
205:
206: public void postDeregister() {
207: }
208:
209: public void method() {
210: }
211: }
212:
213: public static class Dyn extends AbstractDynamicMBean implements
214: MBeanRegistration {
215: public ObjectName preRegister(MBeanServer server,
216: ObjectName name) throws Exception {
217: return name;
218: }
219:
220: public void postRegister(Boolean registrationDone) {
221: }
222:
223: public void preDeregister() throws Exception {
224: }
225:
226: public void postDeregister() {
227: }
228:
229: public void method() {
230: }
231:
232: protected MBeanOperationInfo[] createMBeanOperationInfo() {
233: return new MBeanOperationInfo[] { new MBeanOperationInfo(
234: StdMBean.class.getMethods()[0].getName(), null,
235: new MBeanParameterInfo[0], "void",
236: MBeanOperationInfo.UNKNOWN) };
237: }
238: }
239:
240: public interface ListenerRegistrarMBean {
241: }
242:
243: public static class ListenerRegistrar implements
244: ListenerRegistrarMBean, MBeanRegistration,
245: NotificationListener {
246: private final MutableObject holder;
247: private MBeanServer server;
248: private ObjectName delegate;
249:
250: public ListenerRegistrar(MutableObject holder) {
251: this .holder = holder;
252: }
253:
254: public ObjectName preRegister(MBeanServer server,
255: ObjectName name) throws Exception {
256: this .server = server;
257: delegate = ObjectName
258: .getInstance("JMImplementation:type=MBeanServerDelegate");
259: return name;
260: }
261:
262: public void postRegister(Boolean registrationDone) {
263: try {
264: server.addNotificationListener(delegate, this , null,
265: null);
266: } catch (InstanceNotFoundException x) {
267: throw new Error(x.toString());
268: }
269: }
270:
271: public void preDeregister() throws Exception {
272: }
273:
274: public void postDeregister() {
275: try {
276: server.removeNotificationListener(delegate, this );
277: } catch (Exception x) {
278: throw new Error(x.toString());
279: }
280: }
281:
282: public void handleNotification(Notification notification,
283: Object handback) {
284: holder.set(notification);
285: }
286: }
287: }
|