1 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 ; 29 import java.util.List ; 30 import java.util.logging.Logger ; 31 import java.rmi.RemoteException ; 32 import java.rmi.server.UnicastRemoteObject ; 33 34 41 public class JBossServerConnection extends JMXServerConnection { 42 43 private static final Logger 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 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 jmxObjName = toJMXObjectName(objectName); 62 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 e) { 68 throw new RuntimeException (e); 69 } finally { 70 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 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 ()); } catch (Exception e) { 93 throw new RuntimeException (e); 94 } 95 } 96 97 public void removeNotificationListener(ObjectName objectName, 98 ObjectNotificationListener listener, 99 ObjectNotificationFilter filter, 100 Object 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 e) { 110 throw new RuntimeException (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 { 130 UnicastRemoteObject.exportObject(this); 131 } 132 133 public void unexport() throws RemoteException { 134 UnicastRemoteObject.unexportObject(this, true); 135 } 136 137 public void handleNotification(Notification notification, Object handback) 138 throws RemoteException { 139 listener.handleNotification(toObjectNotification(notification), 140 handback); 141 } 142 } 143 } 144 145 146 | Popular Tags |