1 8 9 package mx4j.remote.rmi; 10 11 import java.io.IOException ; 12 import java.io.NotSerializableException ; 13 import java.rmi.MarshalledObject ; 14 import java.util.Set ; 15 import javax.management.Attribute ; 16 import javax.management.AttributeList ; 17 import javax.management.AttributeNotFoundException ; 18 import javax.management.InstanceAlreadyExistsException ; 19 import javax.management.InstanceNotFoundException ; 20 import javax.management.IntrospectionException ; 21 import javax.management.InvalidAttributeValueException ; 22 import javax.management.ListenerNotFoundException ; 23 import javax.management.MBeanException ; 24 import javax.management.MBeanInfo ; 25 import javax.management.MBeanRegistrationException ; 26 import javax.management.MBeanServerConnection ; 27 import javax.management.NotCompliantMBeanException ; 28 import javax.management.NotificationFilter ; 29 import javax.management.NotificationListener ; 30 import javax.management.ObjectInstance ; 31 import javax.management.ObjectName ; 32 import javax.management.QueryExp ; 33 import javax.management.ReflectionException ; 34 import javax.management.remote.rmi.RMIConnection ; 35 import javax.security.auth.Subject ; 36 37 import mx4j.remote.NotificationTuple; 38 import mx4j.remote.RemoteNotificationClientHandler; 39 40 47 public class ClientInvoker implements MBeanServerConnection 48 { 49 private final RMIConnection connection; 50 private final Subject delegate; 51 private final RemoteNotificationClientHandler notificationHandler; 52 53 public ClientInvoker(RMIConnection rmiConnection, RemoteNotificationClientHandler notificationHandler, Subject delegate) 54 { 55 this.connection = rmiConnection; 56 this.delegate = delegate; 57 this.notificationHandler = notificationHandler; 58 } 59 60 public void addNotificationListener(ObjectName observed, NotificationListener listener, NotificationFilter filter, Object handback) 61 throws InstanceNotFoundException , IOException 62 { 63 NotificationTuple tuple = new NotificationTuple(observed, listener, filter, handback); 64 if (notificationHandler.contains(tuple)) return; 65 66 MarshalledObject f = null; 67 try 68 { 69 f = RMIMarshaller.marshal(filter); 70 } 71 catch (NotSerializableException x) 72 { 73 tuple.setInvokeFilter(true); 75 } 76 Integer [] ids = connection.addNotificationListeners(new ObjectName []{observed}, new MarshalledObject []{f}, new Subject []{delegate}); 77 notificationHandler.addNotificationListener(ids[0], tuple); 78 } 79 80 public void removeNotificationListener(ObjectName observed, NotificationListener listener) 81 throws InstanceNotFoundException , ListenerNotFoundException , IOException 82 { 83 Integer [] ids = notificationHandler.getNotificationListeners(new NotificationTuple(observed, listener)); 84 if (ids == null) throw new ListenerNotFoundException ("Could not find listener " + listener); 85 try 86 { 87 connection.removeNotificationListeners(observed, ids, delegate); 88 notificationHandler.removeNotificationListeners(ids); 89 } 90 catch (InstanceNotFoundException x) 91 { 92 notificationHandler.removeNotificationListeners(ids); 93 throw x; 94 } 95 catch (ListenerNotFoundException x) 96 { 97 notificationHandler.removeNotificationListeners(ids); 98 throw x; 99 } 100 } 101 102 public void removeNotificationListener(ObjectName observed, NotificationListener listener, NotificationFilter filter, Object handback) 103 throws InstanceNotFoundException , ListenerNotFoundException , IOException 104 { 105 Integer id = notificationHandler.getNotificationListener(new NotificationTuple(observed, listener, filter, handback)); 106 if (id == null) throw new ListenerNotFoundException ("Could not find listener " + listener + " with filter " + filter + " and handback " + handback); 107 Integer [] ids = new Integer []{id}; 108 try 109 { 110 connection.removeNotificationListeners(observed, ids, delegate); 111 notificationHandler.removeNotificationListeners(ids); 112 } 113 catch (InstanceNotFoundException x) 114 { 115 notificationHandler.removeNotificationListeners(ids); 116 throw x; 117 } 118 catch (ListenerNotFoundException x) 119 { 120 notificationHandler.removeNotificationListeners(ids); 121 throw x; 122 } 123 } 124 125 public void addNotificationListener(ObjectName observed, ObjectName listener, NotificationFilter filter, Object handback) 126 throws InstanceNotFoundException , IOException 127 { 128 MarshalledObject f = RMIMarshaller.marshal(filter); 129 MarshalledObject h = RMIMarshaller.marshal(handback); 130 connection.addNotificationListener(observed, listener, f, h, delegate); 131 } 132 133 public void removeNotificationListener(ObjectName observed, ObjectName listener) 134 throws InstanceNotFoundException , ListenerNotFoundException , IOException 135 { 136 connection.removeNotificationListener(observed, listener, delegate); 137 } 138 139 public void removeNotificationListener(ObjectName observed, ObjectName listener, NotificationFilter filter, Object handback) 140 throws InstanceNotFoundException , ListenerNotFoundException , IOException 141 { 142 MarshalledObject f = RMIMarshaller.marshal(filter); 143 MarshalledObject h = RMIMarshaller.marshal(handback); 144 connection.removeNotificationListener(observed, listener, f, h, delegate); 145 } 146 147 public MBeanInfo getMBeanInfo(ObjectName objectName) 148 throws InstanceNotFoundException , IntrospectionException , ReflectionException , IOException 149 { 150 return connection.getMBeanInfo(objectName, delegate); 151 } 152 153 public boolean isInstanceOf(ObjectName objectName, String className) 154 throws InstanceNotFoundException , IOException 155 { 156 return connection.isInstanceOf(objectName, className, delegate); 157 } 158 159 public String [] getDomains() 160 throws IOException 161 { 162 return connection.getDomains(delegate); 163 } 164 165 public String getDefaultDomain() 166 throws IOException 167 { 168 return connection.getDefaultDomain(delegate); 169 } 170 171 public ObjectInstance createMBean(String className, ObjectName objectName) 172 throws ReflectionException , InstanceAlreadyExistsException , MBeanRegistrationException , MBeanException , NotCompliantMBeanException , IOException 173 { 174 return connection.createMBean(className, objectName, delegate); 175 } 176 177 public ObjectInstance createMBean(String className, ObjectName objectName, Object [] args, String [] parameters) 178 throws ReflectionException , InstanceAlreadyExistsException , MBeanRegistrationException , MBeanException , NotCompliantMBeanException , IOException 179 { 180 MarshalledObject arguments = RMIMarshaller.marshal(args); 181 return connection.createMBean(className, objectName, arguments, parameters, delegate); 182 } 183 184 public ObjectInstance createMBean(String className, ObjectName objectName, ObjectName loaderName) 185 throws ReflectionException , InstanceAlreadyExistsException , MBeanRegistrationException , MBeanException , NotCompliantMBeanException , InstanceNotFoundException , IOException 186 { 187 return connection.createMBean(className, objectName, loaderName, delegate); 188 } 189 190 public ObjectInstance createMBean(String className, ObjectName objectName, ObjectName loaderName, Object [] args, String [] parameters) 191 throws ReflectionException , InstanceAlreadyExistsException , MBeanRegistrationException , MBeanException , NotCompliantMBeanException , InstanceNotFoundException , IOException 192 { 193 MarshalledObject arguments = RMIMarshaller.marshal(args); 194 return connection.createMBean(className, objectName, loaderName, arguments, parameters, delegate); 195 } 196 197 public void unregisterMBean(ObjectName objectName) 198 throws InstanceNotFoundException , MBeanRegistrationException , IOException 199 { 200 connection.unregisterMBean(objectName, delegate); 201 } 202 203 public Object getAttribute(ObjectName objectName, String attribute) 204 throws MBeanException , AttributeNotFoundException , InstanceNotFoundException , ReflectionException , IOException 205 { 206 return connection.getAttribute(objectName, attribute, delegate); 207 } 208 209 public void setAttribute(ObjectName objectName, Attribute attribute) 210 throws InstanceNotFoundException , AttributeNotFoundException , InvalidAttributeValueException , MBeanException , ReflectionException , IOException 211 { 212 MarshalledObject attrib = RMIMarshaller.marshal(attribute); 213 connection.setAttribute(objectName, attrib, delegate); 214 } 215 216 public AttributeList getAttributes(ObjectName objectName, String [] attributes) 217 throws InstanceNotFoundException , ReflectionException , IOException 218 { 219 return connection.getAttributes(objectName, attributes, delegate); 220 } 221 222 public AttributeList setAttributes(ObjectName objectName, AttributeList attributes) 223 throws InstanceNotFoundException , ReflectionException , IOException 224 { 225 MarshalledObject attribs = RMIMarshaller.marshal(attributes); 226 return connection.setAttributes(objectName, attribs, delegate); 227 } 228 229 public Object invoke(ObjectName objectName, String methodName, Object [] args, String [] parameters) 230 throws InstanceNotFoundException , MBeanException , ReflectionException , IOException 231 { 232 MarshalledObject arguments = RMIMarshaller.marshal(args); 233 return connection.invoke(objectName, methodName, arguments, parameters, delegate); 234 } 235 236 public Integer getMBeanCount() 237 throws IOException 238 { 239 return connection.getMBeanCount(delegate); 240 } 241 242 public boolean isRegistered(ObjectName objectName) 243 throws IOException 244 { 245 return connection.isRegistered(objectName, delegate); 246 } 247 248 public ObjectInstance getObjectInstance(ObjectName objectName) 249 throws InstanceNotFoundException , IOException 250 { 251 return connection.getObjectInstance(objectName, delegate); 252 } 253 254 public Set queryMBeans(ObjectName patternName, QueryExp filter) 255 throws IOException 256 { 257 MarshalledObject query = RMIMarshaller.marshal(filter); 258 return connection.queryMBeans(patternName, query, delegate); 259 } 260 261 public Set queryNames(ObjectName patternName, QueryExp filter) 262 throws IOException 263 { 264 MarshalledObject query = RMIMarshaller.marshal(filter); 265 return connection.queryNames(patternName, query, delegate); 266 } 267 } 268 | Popular Tags |