001: /**
002: * Copyright 2004-2005 jManage.org
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */package org.jmanage.core.modules.jboss;
016:
017: import org.jmanage.core.management.ObjectName;
018: import org.jmanage.core.management.ObjectInfo;
019: import org.jmanage.core.management.ObjectNotificationListener;
020: import org.jmanage.core.management.ObjectNotificationFilter;
021: import org.jmanage.core.modules.JMXServerConnection;
022: import org.jmanage.core.util.Loggers;
023: import org.jboss.jmx.adaptor.rmi.RMIAdaptor;
024: import org.jboss.jmx.adaptor.rmi.RMINotificationListener;
025:
026: import javax.management.*;
027: import java.util.Set;
028: import java.util.List;
029: import java.util.logging.Logger;
030: import java.rmi.RemoteException;
031: import java.rmi.server.UnicastRemoteObject;
032:
033: /**
034: *
035: * date: Oct 30, 2004
036: * @author Prem
037: * @author Shashank Bellary
038: * @author Rakesh Kalra
039: */
040: public class JBossServerConnection extends JMXServerConnection {
041:
042: private static final Logger logger = Loggers
043: .getLogger(JBossServerConnection.class);
044:
045: private final RMIAdaptor rmiAdaptor;
046:
047: public JBossServerConnection(RMIAdaptor rmiAdaptor) {
048: super (rmiAdaptor, RMIAdaptor.class);
049: assert rmiAdaptor != null;
050: this .rmiAdaptor = rmiAdaptor;
051: }
052:
053: public ObjectInfo getObjectInfo(ObjectName objectName) {
054:
055: String existingProtocolHandler = System
056: .getProperty("java.protocol.handler.pkgs");
057: logger.fine("Existing value for java.protocol.handler.pkgs: "
058: + existingProtocolHandler);
059: try {
060: javax.management.ObjectName jmxObjName = toJMXObjectName(objectName);
061: // fix for Bug# 1211202
062: System.setProperty("java.protocol.handler.pkgs",
063: "org.jmanage.net.protocol");
064: MBeanInfo mbeanInfo = rmiAdaptor.getMBeanInfo(jmxObjName);
065: return toObjectInfo(objectName, mbeanInfo);
066: } catch (Exception e) {
067: throw new RuntimeException(e);
068: } finally {
069: // todo: there is a minor bug here. if the existing value was null, it won't be reset
070: if (existingProtocolHandler != null) {
071: System.setProperty("java.protocol.handler.pkgs",
072: existingProtocolHandler);
073: }
074: }
075: }
076:
077: public void addNotificationListener(ObjectName objectName,
078: ObjectNotificationListener listener,
079: ObjectNotificationFilter filter, Object handback) {
080:
081: try {
082: MyRMINotificationListener notifListener = toRMINotificationListener(listener);
083: notifListener.export();
084: notifications.put(listener, notifListener);
085: NotificationFilter notifFilter = toJMXNotificationFilter(filter);
086: rmiAdaptor.addNotificationListener(
087: toJMXObjectName(objectName), notifListener,
088: notifFilter, new String());// todo: handback is not used
089: } catch (Exception e) {
090: throw new RuntimeException(e);
091: }
092: }
093:
094: public void removeNotificationListener(ObjectName objectName,
095: ObjectNotificationListener listener,
096: ObjectNotificationFilter filter, Object handback) {
097:
098: MyRMINotificationListener notifListener = (MyRMINotificationListener) notifications
099: .remove(listener);
100: assert notifListener != null;
101: try {
102: rmiAdaptor.removeNotificationListener(
103: toJMXObjectName(objectName), notifListener);
104: notifListener.unexport();
105: } catch (Exception e) {
106: throw new RuntimeException(e);
107: }
108: }
109:
110: private static MyRMINotificationListener toRMINotificationListener(
111: final ObjectNotificationListener listener) {
112:
113: return new MyRMINotificationListener(listener);
114: }
115:
116: private static class MyRMINotificationListener implements
117: RMINotificationListener {
118:
119: private final ObjectNotificationListener listener;
120:
121: MyRMINotificationListener(ObjectNotificationListener listener) {
122: this .listener = listener;
123: }
124:
125: public void export() throws RemoteException {
126: UnicastRemoteObject.exportObject(this );
127: }
128:
129: public void unexport() throws RemoteException {
130: UnicastRemoteObject.unexportObject(this , true);
131: }
132:
133: public void handleNotification(Notification notification,
134: Object handback) throws RemoteException {
135: listener.handleNotification(
136: toObjectNotification(notification), handback);
137: }
138: }
139: }
|