KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > examples > remote > interception > SubjectTrackingMBeanServer


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.examples.remote.interception;
10
11 import java.io.ObjectInputStream JavaDoc;
12 import java.security.AccessController JavaDoc;
13 import java.util.Set JavaDoc;
14 import javax.management.Attribute JavaDoc;
15 import javax.management.AttributeList JavaDoc;
16 import javax.management.AttributeNotFoundException JavaDoc;
17 import javax.management.InstanceAlreadyExistsException JavaDoc;
18 import javax.management.InstanceNotFoundException JavaDoc;
19 import javax.management.IntrospectionException JavaDoc;
20 import javax.management.InvalidAttributeValueException JavaDoc;
21 import javax.management.ListenerNotFoundException JavaDoc;
22 import javax.management.MBeanException JavaDoc;
23 import javax.management.MBeanInfo JavaDoc;
24 import javax.management.MBeanRegistrationException JavaDoc;
25 import javax.management.MBeanServer JavaDoc;
26 import javax.management.NotCompliantMBeanException JavaDoc;
27 import javax.management.NotificationFilter JavaDoc;
28 import javax.management.NotificationListener JavaDoc;
29 import javax.management.ObjectInstance JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31 import javax.management.OperationsException JavaDoc;
32 import javax.management.QueryExp JavaDoc;
33 import javax.management.ReflectionException JavaDoc;
34 import javax.management.loading.ClassLoaderRepository JavaDoc;
35 import javax.management.remote.MBeanServerForwarder JavaDoc;
36 import javax.security.auth.Subject JavaDoc;
37
38 /**
39  * This class tracks the Subject of the current invocation, and prints it to System.out.
40  * It should be better implemented as JDK 1.3 dynamic proxy, but this is left as a simple
41  * exercise to the reader ;)
42  *
43  * @version $Revision: 1.3 $
44  */

45 public class SubjectTrackingMBeanServer implements MBeanServerForwarder JavaDoc
46 {
47    private MBeanServer JavaDoc server;
48
49    public synchronized MBeanServer JavaDoc getMBeanServer()
50    {
51       return server;
52    }
53
54    public synchronized void setMBeanServer(MBeanServer JavaDoc server) throws IllegalArgumentException JavaDoc
55    {
56       if (server == null) throw new IllegalArgumentException JavaDoc("Cannot forward to a null MBeanServer");
57       this.server = server;
58    }
59
60    private void trackSubject()
61    {
62       Subject JavaDoc subject = Subject.getSubject(AccessController.getContext());
63       System.out.println("Subject = " + subject);
64    }
65
66    public void addNotificationListener(ObjectName JavaDoc observed, NotificationListener JavaDoc listener, NotificationFilter JavaDoc filter, Object JavaDoc handback)
67            throws InstanceNotFoundException JavaDoc
68    {
69       trackSubject();
70       getMBeanServer().addNotificationListener(observed, listener, filter, handback);
71    }
72
73    public void addNotificationListener(ObjectName JavaDoc observed, ObjectName JavaDoc listener, NotificationFilter JavaDoc filter, Object JavaDoc handback)
74            throws InstanceNotFoundException JavaDoc
75    {
76       trackSubject();
77       getMBeanServer().addNotificationListener(observed, listener, filter, handback);
78    }
79
80    public void removeNotificationListener(ObjectName JavaDoc observed, ObjectName JavaDoc listener)
81            throws InstanceNotFoundException JavaDoc, ListenerNotFoundException JavaDoc
82    {
83       trackSubject();
84       getMBeanServer().removeNotificationListener(observed, listener);
85    }
86
87    public void removeNotificationListener(ObjectName JavaDoc observed, NotificationListener JavaDoc listener)
88            throws InstanceNotFoundException JavaDoc, ListenerNotFoundException JavaDoc
89    {
90       trackSubject();
91       getMBeanServer().removeNotificationListener(observed, listener);
92    }
93
94    public void removeNotificationListener(ObjectName JavaDoc observed, ObjectName JavaDoc listener, NotificationFilter JavaDoc filter, Object JavaDoc handback)
95            throws InstanceNotFoundException JavaDoc, ListenerNotFoundException JavaDoc
96    {
97       trackSubject();
98       getMBeanServer().removeNotificationListener(observed, listener, filter, handback);
99    }
100
101    public void removeNotificationListener(ObjectName JavaDoc observed, NotificationListener JavaDoc listener, NotificationFilter JavaDoc filter, Object JavaDoc handback)
102            throws InstanceNotFoundException JavaDoc, ListenerNotFoundException JavaDoc
103    {
104       trackSubject();
105       getMBeanServer().removeNotificationListener(observed, listener, filter, handback);
106    }
107
108    public MBeanInfo JavaDoc getMBeanInfo(ObjectName JavaDoc objectName)
109            throws InstanceNotFoundException JavaDoc, IntrospectionException JavaDoc, ReflectionException JavaDoc
110    {
111       trackSubject();
112       return getMBeanServer().getMBeanInfo(objectName);
113    }
114
115    public boolean isInstanceOf(ObjectName JavaDoc objectName, String JavaDoc className)
116            throws InstanceNotFoundException JavaDoc
117    {
118       trackSubject();
119       return getMBeanServer().isInstanceOf(objectName, className);
120    }
121
122    public String JavaDoc[] getDomains()
123    {
124       trackSubject();
125       return getMBeanServer().getDomains();
126    }
127
128    public String JavaDoc getDefaultDomain()
129    {
130       trackSubject();
131       return getMBeanServer().getDefaultDomain();
132    }
133
134    public ObjectInstance JavaDoc createMBean(String JavaDoc className, ObjectName JavaDoc objectName)
135            throws ReflectionException JavaDoc, InstanceAlreadyExistsException JavaDoc, MBeanRegistrationException JavaDoc, MBeanException JavaDoc, NotCompliantMBeanException JavaDoc
136    {
137       trackSubject();
138       return getMBeanServer().createMBean(className, objectName);
139    }
140
141    public ObjectInstance JavaDoc createMBean(String JavaDoc className, ObjectName JavaDoc objectName, ObjectName JavaDoc loaderName)
142            throws ReflectionException JavaDoc, InstanceAlreadyExistsException JavaDoc, MBeanRegistrationException JavaDoc, MBeanException JavaDoc, NotCompliantMBeanException JavaDoc, InstanceNotFoundException JavaDoc
143    {
144       trackSubject();
145       return getMBeanServer().createMBean(className, objectName, loaderName);
146    }
147
148    public ObjectInstance JavaDoc createMBean(String JavaDoc className, ObjectName JavaDoc objectName, Object JavaDoc[] args, String JavaDoc[] parameters)
149            throws ReflectionException JavaDoc, InstanceAlreadyExistsException JavaDoc, MBeanRegistrationException JavaDoc, MBeanException JavaDoc, NotCompliantMBeanException JavaDoc
150    {
151       trackSubject();
152       return getMBeanServer().createMBean(className, objectName, args, parameters);
153    }
154
155    public ObjectInstance JavaDoc createMBean(String JavaDoc className, ObjectName JavaDoc objectName, ObjectName JavaDoc loaderName, Object JavaDoc[] args, String JavaDoc[] parameters)
156            throws ReflectionException JavaDoc, InstanceAlreadyExistsException JavaDoc, MBeanRegistrationException JavaDoc, MBeanException JavaDoc, NotCompliantMBeanException JavaDoc, InstanceNotFoundException JavaDoc
157    {
158       trackSubject();
159       return getMBeanServer().createMBean(className, objectName, loaderName, args, parameters);
160    }
161
162    public void unregisterMBean(ObjectName JavaDoc objectName)
163            throws InstanceNotFoundException JavaDoc, MBeanRegistrationException JavaDoc
164    {
165       trackSubject();
166       getMBeanServer().unregisterMBean(objectName);
167    }
168
169    public Object JavaDoc getAttribute(ObjectName JavaDoc objectName, String JavaDoc attribute)
170            throws MBeanException JavaDoc, AttributeNotFoundException JavaDoc, InstanceNotFoundException JavaDoc, ReflectionException JavaDoc
171    {
172       trackSubject();
173       return getMBeanServer().getAttribute(objectName, attribute);
174    }
175
176    public void setAttribute(ObjectName JavaDoc objectName, Attribute JavaDoc attribute)
177            throws InstanceNotFoundException JavaDoc, AttributeNotFoundException JavaDoc, InvalidAttributeValueException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc
178    {
179       trackSubject();
180       getMBeanServer().setAttribute(objectName, attribute);
181    }
182
183    public AttributeList JavaDoc getAttributes(ObjectName JavaDoc objectName, String JavaDoc[] attributes)
184            throws InstanceNotFoundException JavaDoc, ReflectionException JavaDoc
185    {
186       trackSubject();
187       return getMBeanServer().getAttributes(objectName, attributes);
188    }
189
190    public AttributeList JavaDoc setAttributes(ObjectName JavaDoc objectName, AttributeList JavaDoc attributes)
191            throws InstanceNotFoundException JavaDoc, ReflectionException JavaDoc
192    {
193       trackSubject();
194       return getMBeanServer().setAttributes(objectName, attributes);
195    }
196
197    public Object JavaDoc invoke(ObjectName JavaDoc objectName, String JavaDoc methodName, Object JavaDoc[] args, String JavaDoc[] parameters)
198            throws InstanceNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc
199    {
200       trackSubject();
201       return getMBeanServer().invoke(objectName, methodName, args, parameters);
202    }
203
204    public Integer JavaDoc getMBeanCount()
205    {
206       trackSubject();
207       return getMBeanServer().getMBeanCount();
208    }
209
210    public boolean isRegistered(ObjectName JavaDoc objectname)
211    {
212       trackSubject();
213       return getMBeanServer().isRegistered(objectname);
214    }
215
216    public ObjectInstance JavaDoc getObjectInstance(ObjectName JavaDoc objectName)
217            throws InstanceNotFoundException JavaDoc
218    {
219       trackSubject();
220       return getMBeanServer().getObjectInstance(objectName);
221    }
222
223    public Set JavaDoc queryMBeans(ObjectName JavaDoc patternName, QueryExp JavaDoc filter)
224    {
225       trackSubject();
226       return getMBeanServer().queryMBeans(patternName, filter);
227    }
228
229    public Set JavaDoc queryNames(ObjectName JavaDoc patternName, QueryExp JavaDoc filter)
230    {
231       trackSubject();
232       return getMBeanServer().queryNames(patternName, filter);
233    }
234
235    public Object JavaDoc instantiate(String JavaDoc className)
236            throws ReflectionException JavaDoc, MBeanException JavaDoc
237    {
238       trackSubject();
239       return getMBeanServer().instantiate(className);
240    }
241
242    public Object JavaDoc instantiate(String JavaDoc className, ObjectName JavaDoc loaderName)
243            throws ReflectionException JavaDoc, MBeanException JavaDoc, InstanceNotFoundException JavaDoc
244    {
245       trackSubject();
246       return getMBeanServer().instantiate(className, loaderName);
247    }
248
249    public Object JavaDoc instantiate(String JavaDoc className, Object JavaDoc[] args, String JavaDoc[] parameters)
250            throws ReflectionException JavaDoc, MBeanException JavaDoc
251    {
252       trackSubject();
253       return getMBeanServer().instantiate(className, args, parameters);
254    }
255
256    public Object JavaDoc instantiate(String JavaDoc className, ObjectName JavaDoc loaderName, Object JavaDoc[] args, String JavaDoc[] parameters)
257            throws ReflectionException JavaDoc, MBeanException JavaDoc, InstanceNotFoundException JavaDoc
258    {
259       trackSubject();
260       return getMBeanServer().instantiate(className, loaderName, args, parameters);
261    }
262
263    public ObjectInstance JavaDoc registerMBean(Object JavaDoc mbean, ObjectName JavaDoc objectName)
264            throws InstanceAlreadyExistsException JavaDoc, MBeanRegistrationException JavaDoc, NotCompliantMBeanException JavaDoc
265    {
266       trackSubject();
267       return registerMBean(mbean, objectName);
268    }
269
270    public ObjectInputStream JavaDoc deserialize(String JavaDoc className, ObjectName JavaDoc loaderName, byte[] bytes)
271            throws InstanceNotFoundException JavaDoc, OperationsException JavaDoc, ReflectionException JavaDoc
272    {
273       trackSubject();
274       return getMBeanServer().deserialize(className, loaderName, bytes);
275    }
276
277    public ObjectInputStream JavaDoc deserialize(String JavaDoc className, byte[] bytes)
278            throws OperationsException JavaDoc, ReflectionException JavaDoc
279    {
280       trackSubject();
281       return getMBeanServer().deserialize(className, bytes);
282    }
283
284    public ObjectInputStream JavaDoc deserialize(ObjectName JavaDoc objectName, byte[] bytes)
285            throws InstanceNotFoundException JavaDoc, OperationsException JavaDoc
286    {
287       trackSubject();
288       return getMBeanServer().deserialize(objectName, bytes);
289    }
290
291    public ClassLoader JavaDoc getClassLoaderFor(ObjectName JavaDoc mbeanName)
292            throws InstanceNotFoundException JavaDoc
293    {
294       trackSubject();
295       return getMBeanServer().getClassLoaderFor(mbeanName);
296    }
297
298    public ClassLoader JavaDoc getClassLoader(ObjectName JavaDoc loaderName)
299            throws InstanceNotFoundException JavaDoc
300    {
301       trackSubject();
302       return getMBeanServer().getClassLoader(loaderName);
303    }
304
305    public ClassLoaderRepository JavaDoc getClassLoaderRepository()
306    {
307       trackSubject();
308       return getMBeanServer().getClassLoaderRepository();
309    }
310 }
311
Popular Tags