KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > tools > remote > proxy > RemoteMBeanProxy


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.tools.remote.proxy;
10
11 import java.io.IOException JavaDoc;
12 import java.util.Map JavaDoc;
13 import javax.management.Attribute JavaDoc;
14 import javax.management.AttributeList JavaDoc;
15 import javax.management.AttributeNotFoundException JavaDoc;
16 import javax.management.DynamicMBean JavaDoc;
17 import javax.management.InstanceNotFoundException JavaDoc;
18 import javax.management.InvalidAttributeValueException JavaDoc;
19 import javax.management.ListenerNotFoundException JavaDoc;
20 import javax.management.MBeanException JavaDoc;
21 import javax.management.MBeanInfo JavaDoc;
22 import javax.management.MBeanNotificationInfo JavaDoc;
23 import javax.management.MBeanRegistration JavaDoc;
24 import javax.management.MBeanServer JavaDoc;
25 import javax.management.MBeanServerConnection JavaDoc;
26 import javax.management.NotificationEmitter JavaDoc;
27 import javax.management.NotificationFilter JavaDoc;
28 import javax.management.NotificationListener JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30 import javax.management.ReflectionException JavaDoc;
31 import javax.management.remote.JMXConnector JavaDoc;
32 import javax.management.remote.JMXConnectorFactory JavaDoc;
33 import javax.management.remote.JMXServiceURL JavaDoc;
34 import javax.security.auth.Subject JavaDoc;
35
36 /**
37  * @version $Revision: 1.4 $
38  */

39 public class RemoteMBeanProxy implements DynamicMBean JavaDoc, NotificationEmitter JavaDoc, MBeanRegistration JavaDoc
40 {
41    private final ObjectName JavaDoc remoteObjectName;
42    private final JMXConnector JavaDoc connector;
43    private final MBeanServerConnection JavaDoc connection;
44
45    public RemoteMBeanProxy(ObjectName JavaDoc remoteObjectName, JMXServiceURL JavaDoc url, Map JavaDoc environment, Subject JavaDoc delegate) throws IOException JavaDoc
46    {
47       this(remoteObjectName, JMXConnectorFactory.newJMXConnector(url, environment), environment, delegate);
48    }
49
50    public RemoteMBeanProxy(ObjectName JavaDoc remoteObjectName, JMXConnector JavaDoc connector, Map JavaDoc environment, Subject JavaDoc delegate) throws IOException JavaDoc
51    {
52       this.remoteObjectName = remoteObjectName;
53       this.connector = connector;
54       this.connector.connect(environment);
55       this.connection = connector.getMBeanServerConnection(delegate);
56    }
57
58    public RemoteMBeanProxy(ObjectName JavaDoc remoteObjectName, MBeanServerConnection JavaDoc connection)
59    {
60       this.remoteObjectName = remoteObjectName;
61       this.connector = null;
62       this.connection = connection;
63    }
64
65    public ObjectName JavaDoc preRegister(MBeanServer JavaDoc server, ObjectName JavaDoc name) throws Exception JavaDoc
66    {
67       return name;
68    }
69
70    public void postRegister(Boolean JavaDoc registrationDone)
71    {
72    }
73
74    public void preDeregister() throws Exception JavaDoc
75    {
76       JMXConnector JavaDoc cntor = getJMXConnector();
77       if (cntor != null) cntor.close();
78    }
79
80    public void postDeregister()
81    {
82    }
83
84    protected ObjectName JavaDoc getRemoteObjectName()
85    {
86       return remoteObjectName;
87    }
88
89    protected MBeanServerConnection JavaDoc getMBeanServerConnection()
90    {
91       return connection;
92    }
93
94    protected JMXConnector JavaDoc getJMXConnector()
95    {
96       return connector;
97    }
98
99    public MBeanInfo JavaDoc getMBeanInfo()
100    {
101       try
102       {
103          return getMBeanServerConnection().getMBeanInfo(getRemoteObjectName());
104       }
105       catch (Exception JavaDoc x)
106       {
107          throw new RemoteMBeanProxyException(x);
108       }
109    }
110
111    public Object JavaDoc getAttribute(String JavaDoc attribute) throws AttributeNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc
112    {
113       try
114       {
115          return getMBeanServerConnection().getAttribute(getRemoteObjectName(), attribute);
116       }
117       catch (InstanceNotFoundException JavaDoc x)
118       {
119          throw new RemoteMBeanProxyException(x);
120       }
121       catch (IOException JavaDoc x)
122       {
123          throw new RemoteMBeanProxyException(x);
124       }
125    }
126
127    public void setAttribute(Attribute JavaDoc attribute) throws AttributeNotFoundException JavaDoc, InvalidAttributeValueException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc
128    {
129       try
130       {
131          getMBeanServerConnection().setAttribute(getRemoteObjectName(), attribute);
132       }
133       catch (InstanceNotFoundException JavaDoc x)
134       {
135          throw new RemoteMBeanProxyException(x);
136       }
137       catch (IOException JavaDoc x)
138       {
139          throw new RemoteMBeanProxyException(x);
140       }
141    }
142
143    public AttributeList JavaDoc getAttributes(String JavaDoc[] attributes)
144    {
145       try
146       {
147          return getMBeanServerConnection().getAttributes(getRemoteObjectName(), attributes);
148       }
149       catch (InstanceNotFoundException JavaDoc x)
150       {
151          throw new RemoteMBeanProxyException(x);
152       }
153       catch (ReflectionException JavaDoc x)
154       {
155          throw new RemoteMBeanProxyException(x);
156       }
157       catch (IOException JavaDoc x)
158       {
159          throw new RemoteMBeanProxyException(x);
160       }
161    }
162
163    public AttributeList JavaDoc setAttributes(AttributeList JavaDoc attributes)
164    {
165       try
166       {
167          return getMBeanServerConnection().setAttributes(getRemoteObjectName(), attributes);
168       }
169       catch (InstanceNotFoundException JavaDoc x)
170       {
171          throw new RemoteMBeanProxyException(x);
172       }
173       catch (ReflectionException JavaDoc x)
174       {
175          throw new RemoteMBeanProxyException(x);
176       }
177       catch (IOException JavaDoc x)
178       {
179          throw new RemoteMBeanProxyException(x);
180       }
181    }
182
183    public Object JavaDoc invoke(String JavaDoc method, Object JavaDoc[] arguments, String JavaDoc[] params) throws MBeanException JavaDoc, ReflectionException JavaDoc
184    {
185       try
186       {
187          return getMBeanServerConnection().invoke(getRemoteObjectName(), method, arguments, params);
188       }
189       catch (InstanceNotFoundException JavaDoc x)
190       {
191          throw new RemoteMBeanProxyException(x);
192       }
193       catch (IOException JavaDoc x)
194       {
195          throw new RemoteMBeanProxyException(x);
196       }
197    }
198
199    public MBeanNotificationInfo JavaDoc[] getNotificationInfo()
200    {
201       return getMBeanInfo().getNotifications();
202    }
203
204    public void addNotificationListener(NotificationListener JavaDoc listener, NotificationFilter JavaDoc filter, Object JavaDoc handback) throws IllegalArgumentException JavaDoc
205    {
206       try
207       {
208          getMBeanServerConnection().addNotificationListener(getRemoteObjectName(), listener, filter, handback);
209       }
210       catch (InstanceNotFoundException JavaDoc x)
211       {
212          throw new RemoteMBeanProxyException(x);
213       }
214       catch (IOException JavaDoc x)
215       {
216          throw new RemoteMBeanProxyException(x);
217       }
218    }
219
220    public void removeNotificationListener(NotificationListener JavaDoc listener) throws ListenerNotFoundException JavaDoc
221    {
222       try
223       {
224          getMBeanServerConnection().removeNotificationListener(getRemoteObjectName(), listener);
225       }
226       catch (InstanceNotFoundException JavaDoc x)
227       {
228          throw new RemoteMBeanProxyException(x);
229       }
230       catch (IOException JavaDoc x)
231       {
232          throw new RemoteMBeanProxyException(x);
233       }
234    }
235
236    public void removeNotificationListener(NotificationListener JavaDoc listener, NotificationFilter JavaDoc filter, Object JavaDoc handback) throws ListenerNotFoundException JavaDoc
237    {
238       try
239       {
240          getMBeanServerConnection().removeNotificationListener(getRemoteObjectName(), listener, filter, handback);
241       }
242       catch (InstanceNotFoundException JavaDoc x)
243       {
244          throw new RemoteMBeanProxyException(x);
245       }
246       catch (IOException JavaDoc x)
247       {
248          throw new RemoteMBeanProxyException(x);
249       }
250    }
251 }
252
Popular Tags