1 8 9 package mx4j.tools.remote; 10 11 import java.io.IOException ; 12 import java.io.Serializable ; 13 import java.util.Map ; 14 import javax.management.ListenerNotFoundException ; 15 import javax.management.MBeanServerConnection ; 16 import javax.management.NotificationFilter ; 17 import javax.management.NotificationListener ; 18 import javax.management.remote.JMXConnector ; 19 import javax.management.remote.JMXServiceURL ; 20 import javax.security.auth.Subject ; 21 22 import mx4j.remote.ConnectionNotificationEmitter; 23 24 31 public abstract class AbstractJMXConnector implements JMXConnector , Serializable 32 { 33 36 private final JMXServiceURL address; 37 private transient boolean connected; 38 private transient boolean closed; 39 private transient ConnectionNotificationEmitter emitter; 40 41 44 protected AbstractJMXConnector(JMXServiceURL address) throws IOException 45 { 46 if (address == null) throw new IOException ("JMXServiceURL cannot be null"); 47 this.address = address; 48 } 49 50 53 protected JMXServiceURL getAddress() 54 { 55 return address; 56 } 57 58 public void connect() throws IOException , SecurityException 59 { 60 connect(null); 61 } 62 63 public void connect(Map environment) throws IOException , SecurityException 64 { 65 synchronized (this) 66 { 67 if (isConnected()) return; 68 if (isClosed()) throw new IOException ("This connector has already been closed"); 69 70 doConnect(environment); 71 72 connected = true; 73 } 74 75 sendConnectionNotificationOpened(); 76 } 77 78 protected abstract void doConnect(Map environment) throws IOException , SecurityException ; 79 80 public void close() throws IOException 81 { 82 synchronized (this) 83 { 84 if (isClosed()) return; 85 closed = true; 86 connected = false; 87 88 doClose(); 89 } 90 91 sendConnectionNotificationClosed(); 92 } 93 94 97 protected abstract void doClose() throws IOException ; 98 99 public MBeanServerConnection getMBeanServerConnection() throws IOException 100 { 101 return getMBeanServerConnection(null); 102 } 103 104 public MBeanServerConnection getMBeanServerConnection(Subject delegate) throws IOException 105 { 106 if (!isConnected()) throw new IOException ("Connection has not been established"); 107 return doGetMBeanServerConnection(delegate); 108 } 109 110 120 protected abstract MBeanServerConnection doGetMBeanServerConnection(Subject delegate) throws IOException ; 121 122 public void addConnectionNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) 123 { 124 getConnectionNotificationEmitter().addNotificationListener(listener, filter, handback); 125 } 126 127 public void removeConnectionNotificationListener(NotificationListener listener) throws ListenerNotFoundException 128 { 129 getConnectionNotificationEmitter().removeNotificationListener(listener); 130 } 131 132 public void removeConnectionNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) throws ListenerNotFoundException 133 { 134 getConnectionNotificationEmitter().removeNotificationListener(listener, filter, handback); 135 } 136 137 private void sendConnectionNotificationOpened() 138 { 139 getConnectionNotificationEmitter().sendConnectionNotificationOpened(); 140 } 141 142 protected void sendConnectionNotificationClosed() 143 { 144 getConnectionNotificationEmitter().sendConnectionNotificationClosed(); 145 } 146 147 151 protected ConnectionNotificationEmitter createConnectionNotificationEmitter() 152 { 153 return new ConnectionNotificationEmitter(this); 154 } 155 156 protected ConnectionNotificationEmitter getConnectionNotificationEmitter() 157 { 158 synchronized (this) 159 { 160 if (emitter == null) emitter = createConnectionNotificationEmitter(); 161 } 162 return emitter; 163 } 164 165 168 protected synchronized boolean isConnected() 169 { 170 return connected; 171 } 172 173 176 protected synchronized boolean isClosed() 177 { 178 return closed; 179 } 180 } 181 | Popular Tags |