1 8 9 package mx4j.tools.remote.http; 10 11 import java.io.IOException ; 12 import java.net.MalformedURLException ; 13 import java.util.HashMap ; 14 import java.util.Map ; 15 import java.util.Set ; 16 17 import javax.management.AttributeList ; 18 import javax.management.AttributeNotFoundException ; 19 import javax.management.InstanceAlreadyExistsException ; 20 import javax.management.InstanceNotFoundException ; 21 import javax.management.IntrospectionException ; 22 import javax.management.InvalidAttributeValueException ; 23 import javax.management.ListenerNotFoundException ; 24 import javax.management.MBeanException ; 25 import javax.management.MBeanInfo ; 26 import javax.management.MBeanRegistrationException ; 27 import javax.management.NotCompliantMBeanException ; 28 import javax.management.ObjectInstance ; 29 import javax.management.ObjectName ; 30 import javax.management.ReflectionException ; 31 import javax.management.remote.JMXServiceURL ; 32 import javax.management.remote.NotificationResult ; 33 import javax.security.auth.Subject ; 34 35 import mx4j.log.Log; 36 import mx4j.log.Logger; 37 import mx4j.tools.remote.Connection; 38 import mx4j.tools.remote.ConnectionManager; 39 40 43 public abstract class HTTPService implements HTTPConnection 44 { 45 private final Map connections = new HashMap (); 46 47 protected Logger getLogger() 48 { 49 return Log.getLogger(getClass().getName()); 50 } 51 52 public String connect(Object credentials) throws IOException , SecurityException 53 { 54 JMXServiceURL address = findJMXServiceURL(); 55 56 ConnectionManager connectionManager = HTTPConnectorServer.find(address); 58 if (connectionManager == null) throw new IOException ("Could not find ConnectionManager. Make sure a HTTPConnectorServer is in classloader scope and bound at this address " + address); 59 60 Connection connection = connectionManager.connect(credentials); 61 addConnection(connection); 62 return connection.getConnectionId(); 63 } 64 65 protected JMXServiceURL findJMXServiceURL() throws MalformedURLException 66 { 67 String url = findRequestURL(); 68 JMXServiceURL temp = new JMXServiceURL ("service:jmx:" + url); 69 int port = temp.getPort(); 70 if ("http".equals(temp.getProtocol()) && port == 0) 71 { 72 port = 80; 74 } 75 else if ("https".equals(temp.getProtocol()) && port == 0) 76 { 77 port = 443; 79 } 80 return new JMXServiceURL (getProtocol(), temp.getHost(), port, temp.getURLPath()); 81 } 82 83 protected abstract String findRequestURL(); 84 85 protected abstract String getProtocol(); 86 87 protected void addConnection(Connection connection) throws IOException 88 { 89 String connectionId = connection.getConnectionId(); 90 synchronized (this) 91 { 92 if (connections.containsKey(connectionId)) throw new IOException ("Connection '" + connection + "' already connected"); 93 connections.put(connectionId, connection); 94 95 Logger logger = getLogger(); 96 if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Added connection '" + connectionId + "', known connections are " + connections.keySet()); 97 } 98 } 99 100 protected void removeConnection(Connection connection) throws IOException 101 { 102 String connectionId = connection.getConnectionId(); 103 synchronized (this) 104 { 105 if (!connections.containsKey(connectionId)) throw new IOException ("Connection '" + connection + "' unknown"); 106 connections.remove(connectionId); 107 108 Logger logger = getLogger(); 109 if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Removed connection '" + connectionId + "', known connections are " + connections.keySet()); 110 } 111 } 112 113 protected Connection findConnection() throws IOException 114 { 115 String connectionId = findConnectionId(); 116 synchronized (this) 117 { 118 Connection connection = (Connection)connections.get(connectionId); 119 if (connection != null) return connection; 120 121 Logger logger = getLogger(); 122 if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Unknown connection '" + connectionId + "', known connections are " + connections.keySet()); 123 throw new IOException ("Connection ID '" + connectionId + "' unknown"); 124 } 125 } 126 127 protected abstract String findConnectionId(); 128 129 public void close() throws IOException 130 { 131 Connection connection = findConnection(); 132 removeConnection(connection); 133 connection.close(); 134 } 135 136 public String getConnectionId() throws IOException 137 { 138 Connection connection = findConnection(); 139 return connection.getConnectionId(); 140 } 141 142 public ObjectInstance createMBean(String className, ObjectName name, Object params, String [] signature, Subject delegate) throws ReflectionException , InstanceAlreadyExistsException , MBeanRegistrationException , MBeanException , NotCompliantMBeanException , IOException 143 { 144 HTTPConnection connection = (HTTPConnection)findConnection(); 145 return connection.createMBean(className, name, params, signature, delegate); 146 } 147 148 public ObjectInstance createMBean(String className, ObjectName name, ObjectName loaderName, Object params, String [] signature, Subject delegate) throws ReflectionException , InstanceAlreadyExistsException , MBeanRegistrationException , MBeanException , NotCompliantMBeanException , InstanceNotFoundException , IOException 149 { 150 HTTPConnection connection = (HTTPConnection)findConnection(); 151 return connection.createMBean(className, name, loaderName, params, signature, delegate); 152 } 153 154 public void unregisterMBean(ObjectName name, Subject delegate) throws InstanceNotFoundException , MBeanRegistrationException , IOException 155 { 156 HTTPConnection connection = (HTTPConnection)findConnection(); 157 connection.unregisterMBean(name, delegate); 158 } 159 160 public ObjectInstance getObjectInstance(ObjectName name, Subject delegate) throws InstanceNotFoundException , IOException 161 { 162 HTTPConnection connection = (HTTPConnection)findConnection(); 163 return connection.getObjectInstance(name, delegate); 164 } 165 166 public Set queryMBeans(ObjectName name, Object query, Subject delegate) throws IOException 167 { 168 HTTPConnection connection = (HTTPConnection)findConnection(); 169 return connection.queryMBeans(name, query, delegate); 170 } 171 172 public Set queryNames(ObjectName name, Object query, Subject delegate) throws IOException 173 { 174 HTTPConnection connection = (HTTPConnection)findConnection(); 175 return connection.queryNames(name, query, delegate); 176 } 177 178 public boolean isRegistered(ObjectName name, Subject delegate) throws IOException 179 { 180 HTTPConnection connection = (HTTPConnection)findConnection(); 181 return connection.isRegistered(name, delegate); 182 } 183 184 public Integer getMBeanCount(Subject delegate) throws IOException 185 { 186 HTTPConnection connection = (HTTPConnection)findConnection(); 187 return connection.getMBeanCount(delegate); 188 } 189 190 public Object getAttribute(ObjectName name, String attribute, Subject delegate) throws MBeanException , AttributeNotFoundException , InstanceNotFoundException , ReflectionException , IOException 191 { 192 HTTPConnection connection = (HTTPConnection)findConnection(); 193 return connection.getAttribute(name, attribute, delegate); 194 } 195 196 public AttributeList getAttributes(ObjectName name, String [] attributes, Subject delegate) throws InstanceNotFoundException , ReflectionException , IOException 197 { 198 HTTPConnection connection = (HTTPConnection)findConnection(); 199 return connection.getAttributes(name, attributes, delegate); 200 } 201 202 public void setAttribute(ObjectName name, Object attribute, Subject delegate) throws InstanceNotFoundException , AttributeNotFoundException , InvalidAttributeValueException , MBeanException , ReflectionException , IOException 203 { 204 HTTPConnection connection = (HTTPConnection)findConnection(); 205 connection.setAttribute(name, attribute, delegate); 206 } 207 208 public AttributeList setAttributes(ObjectName name, Object attributes, Subject delegate) throws InstanceNotFoundException , ReflectionException , IOException 209 { 210 HTTPConnection connection = (HTTPConnection)findConnection(); 211 return connection.setAttributes(name, attributes, delegate); 212 } 213 214 public Object invoke(ObjectName name, String operationName, Object params, String [] signature, Subject delegate) throws InstanceNotFoundException , MBeanException , ReflectionException , IOException 215 { 216 HTTPConnection connection = (HTTPConnection)findConnection(); 217 return connection.invoke(name, operationName, params, signature, delegate); 218 } 219 220 public String getDefaultDomain(Subject delegate) throws IOException 221 { 222 HTTPConnection connection = (HTTPConnection)findConnection(); 223 return connection.getDefaultDomain(delegate); 224 } 225 226 public String [] getDomains(Subject delegate) throws IOException 227 { 228 HTTPConnection connection = (HTTPConnection)findConnection(); 229 return connection.getDomains(delegate); 230 } 231 232 public MBeanInfo getMBeanInfo(ObjectName name, Subject delegate) throws InstanceNotFoundException , IntrospectionException , ReflectionException , IOException 233 { 234 HTTPConnection connection = (HTTPConnection)findConnection(); 235 return connection.getMBeanInfo(name, delegate); 236 } 237 238 public boolean isInstanceOf(ObjectName name, String className, Subject delegate) throws InstanceNotFoundException , IOException 239 { 240 HTTPConnection connection = (HTTPConnection)findConnection(); 241 return connection.isInstanceOf(name, className, delegate); 242 } 243 244 public void addNotificationListener(ObjectName name, ObjectName listener, Object filter, Object handback, Subject delegate) throws InstanceNotFoundException , IOException 245 { 246 HTTPConnection connection = (HTTPConnection)findConnection(); 247 connection.addNotificationListener(name, listener, filter, handback, delegate); 248 } 249 250 public void removeNotificationListener(ObjectName name, ObjectName listener, Subject delegate) throws InstanceNotFoundException , ListenerNotFoundException , IOException 251 { 252 HTTPConnection connection = (HTTPConnection)findConnection(); 253 connection.removeNotificationListener(name, listener, delegate); 254 } 255 256 public void removeNotificationListener(ObjectName name, ObjectName listener, Object filter, Object handback, Subject delegate) throws InstanceNotFoundException , ListenerNotFoundException , IOException 257 { 258 HTTPConnection connection = (HTTPConnection)findConnection(); 259 connection.removeNotificationListener(name, listener, filter, handback, delegate); 260 } 261 262 public Integer addNotificationListener(ObjectName name, Object filter, Subject delegate) throws InstanceNotFoundException , IOException 263 { 264 HTTPConnection connection = (HTTPConnection)findConnection(); 265 return connection.addNotificationListener(name, filter, delegate); 266 } 267 268 public void removeNotificationListeners(ObjectName name, Integer [] listenerIDs, Subject delegate) throws InstanceNotFoundException , ListenerNotFoundException , IOException 269 { 270 HTTPConnection connection = (HTTPConnection)findConnection(); 271 connection.removeNotificationListeners(name, listenerIDs, delegate); 272 } 273 274 public NotificationResult fetchNotifications(long clientSequenceNumber, int maxNotifications, long timeout) throws IOException 275 { 276 HTTPConnection connection = (HTTPConnection)findConnection(); 277 return connection.fetchNotifications(clientSequenceNumber, maxNotifications, timeout); 278 } 279 } 280 | Popular Tags |