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