KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > core > modules > jboss > JBossServerConnection


1 /**
2  * Copyright 2004-2005 jManage.org
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.jmanage.core.modules.jboss;
17
18 import org.jmanage.core.management.ObjectName;
19 import org.jmanage.core.management.ObjectInfo;
20 import org.jmanage.core.management.ObjectNotificationListener;
21 import org.jmanage.core.management.ObjectNotificationFilter;
22 import org.jmanage.core.modules.JMXServerConnection;
23 import org.jmanage.core.util.Loggers;
24 import org.jboss.jmx.adaptor.rmi.RMIAdaptor;
25 import org.jboss.jmx.adaptor.rmi.RMINotificationListener;
26
27 import javax.management.*;
28 import java.util.Set JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.logging.Logger JavaDoc;
31 import java.rmi.RemoteException JavaDoc;
32 import java.rmi.server.UnicastRemoteObject JavaDoc;
33
34 /**
35  *
36  * date: Oct 30, 2004
37  * @author Prem
38  * @author Shashank Bellary
39  * @author Rakesh Kalra
40  */

41 public class JBossServerConnection extends JMXServerConnection {
42
43     private static final Logger JavaDoc logger =
44             Loggers.getLogger(JBossServerConnection.class);
45
46     private final RMIAdaptor rmiAdaptor;
47
48     public JBossServerConnection(RMIAdaptor rmiAdaptor) {
49         super(rmiAdaptor, RMIAdaptor.class);
50         assert rmiAdaptor != null;
51         this.rmiAdaptor = rmiAdaptor;
52     }
53
54     public ObjectInfo getObjectInfo(ObjectName objectName) {
55
56         String JavaDoc existingProtocolHandler =
57                 System.getProperty("java.protocol.handler.pkgs");
58         logger.fine("Existing value for java.protocol.handler.pkgs: " +
59                 existingProtocolHandler);
60         try {
61             javax.management.ObjectName JavaDoc jmxObjName = toJMXObjectName(objectName);
62             // fix for Bug# 1211202
63
System.setProperty("java.protocol.handler.pkgs",
64                     "org.jmanage.net.protocol");
65             MBeanInfo mbeanInfo = rmiAdaptor.getMBeanInfo(jmxObjName);
66             return toObjectInfo(objectName, mbeanInfo);
67         } catch (Exception JavaDoc e) {
68             throw new RuntimeException JavaDoc(e);
69         } finally {
70             // todo: there is a minor bug here. if the existing value was null, it won't be reset
71
if (existingProtocolHandler != null) {
72                 System.setProperty("java.protocol.handler.pkgs",
73                         existingProtocolHandler);
74             }
75         }
76     }
77
78     public void addNotificationListener(ObjectName objectName,
79                                         ObjectNotificationListener listener,
80                                         ObjectNotificationFilter filter,
81                                         Object JavaDoc handback) {
82
83         try {
84             MyRMINotificationListener notifListener =
85                     toRMINotificationListener(listener);
86             notifListener.export();
87             notifications.put(listener, notifListener);
88             NotificationFilter notifFilter =
89                     toJMXNotificationFilter(filter);
90             rmiAdaptor.addNotificationListener(toJMXObjectName(objectName),
91                     notifListener, notifFilter, new String JavaDoc());// todo: handback is not used
92
} catch (Exception JavaDoc e) {
93             throw new RuntimeException JavaDoc(e);
94         }
95     }
96
97     public void removeNotificationListener(ObjectName objectName,
98                                            ObjectNotificationListener listener,
99                                            ObjectNotificationFilter filter,
100                                            Object JavaDoc handback) {
101
102         MyRMINotificationListener notifListener =
103                 (MyRMINotificationListener) notifications.remove(listener);
104         assert notifListener != null;
105         try {
106             rmiAdaptor.removeNotificationListener(toJMXObjectName(objectName),
107                     notifListener);
108             notifListener.unexport();
109         } catch (Exception JavaDoc e) {
110             throw new RuntimeException JavaDoc(e);
111         }
112     }
113
114     private static MyRMINotificationListener toRMINotificationListener(
115             final ObjectNotificationListener listener) {
116
117         return new MyRMINotificationListener(listener);
118     }
119
120     private static class MyRMINotificationListener
121             implements RMINotificationListener{
122
123         private final ObjectNotificationListener listener;
124
125         MyRMINotificationListener(ObjectNotificationListener listener) {
126             this.listener = listener;
127         }
128
129         public void export() throws RemoteException JavaDoc {
130             UnicastRemoteObject.exportObject(this);
131         }
132
133         public void unexport() throws RemoteException JavaDoc {
134             UnicastRemoteObject.unexportObject(this, true);
135         }
136
137         public void handleNotification(Notification notification, Object JavaDoc handback)
138                 throws RemoteException JavaDoc {
139             listener.handleNotification(toObjectNotification(notification),
140                     handback);
141         }
142     }
143 }
144
145
146
Popular Tags