1 8 9 package mx4j.tools.remote.resolver.local; 10 11 import java.io.IOException ; 12 import java.util.HashMap ; 13 import java.util.List ; 14 import java.util.Map ; 15 16 import javax.management.JMException ; 17 import javax.management.MBeanServer ; 18 import javax.management.MBeanServerFactory ; 19 import javax.management.ObjectName ; 20 import javax.management.remote.JMXServiceURL ; 21 22 import mx4j.remote.ConnectionResolver; 23 import mx4j.tools.remote.local.LocalConnectorServer; 24 25 28 public class Resolver extends ConnectionResolver 29 { 30 private static final String ID_CONTEXT = "/id/"; 31 private static int connectorID; 32 33 private final Map mbeanServerIds = new HashMap (); 34 35 public Object createServer(JMXServiceURL url, Map environment) throws IOException 36 { 37 String connectorID = findConnectorID(url); 38 if (connectorID == null) return null; 39 40 String mbeanServerId = findMBeanServerId(connectorID); 41 if (mbeanServerId == null) return null; 42 43 List servers = MBeanServerFactory.findMBeanServer(mbeanServerId); 44 if (servers.size() == 1) return servers.get(0); 45 return null; 46 } 47 48 private String findConnectorID(JMXServiceURL url) 49 { 50 String path = url.getURLPath(); 51 if (path == null || !path.startsWith(ID_CONTEXT)) return null; 52 return path.substring(ID_CONTEXT.length()); 53 } 54 55 private String findMBeanServerId(String connectorID) 56 { 57 synchronized (mbeanServerIds) 58 { 59 return (String )mbeanServerIds.get(connectorID); 60 } 61 } 62 63 public JMXServiceURL bindServer(Object server, JMXServiceURL url, Map environment) throws IOException 64 { 65 String connectorID = findConnectorID(url); 66 if (connectorID == null) connectorID = generateConnectorID(); 67 68 MBeanServer mbeanServer = (MBeanServer )server; 69 try 70 { 71 String mbeanServerId = (String )mbeanServer.getAttribute(new ObjectName ("JMImplementation:type=MBeanServerDelegate"), "MBeanServerId"); 72 synchronized (mbeanServerIds) 73 { 74 String existing = findMBeanServerId(connectorID); 75 if (existing != null && !existing.equals(mbeanServerId)) throw new IOException ("LocalConnectorServer with ID " + connectorID + " is already attached to MBeanServer with ID " + existing); 76 mbeanServerIds.put(connectorID, mbeanServerId); 77 } 78 } 79 catch (JMException x) 80 { 81 throw new IOException ("Cannot retrieve MBeanServer ID " + x.toString()); 82 } 83 84 return new JMXServiceURL (url.getProtocol(), url.getHost(), url.getPort(), ID_CONTEXT + connectorID); 85 } 86 87 private String generateConnectorID() 88 { 89 synchronized (Resolver.class) 90 { 91 return String.valueOf(++connectorID); 92 } 93 } 94 95 public void unbindServer(Object server, JMXServiceURL address, Map environment) throws IOException 96 { 97 String connectorID = findConnectorID(address); 98 if (connectorID == null) throw new IOException ("Unknown LocalConnectorServer ID: " + address); 99 synchronized (mbeanServerIds) 100 { 101 mbeanServerIds.remove(connectorID); 102 } 103 } 104 105 public void destroyServer(Object server, JMXServiceURL url, Map environment) throws IOException 106 { 107 } 108 109 public Object lookupClient(JMXServiceURL url, Map environment) throws IOException 110 { 111 return LocalConnectorServer.find(url); 112 } 113 114 public Object bindClient(Object client, Map environment) throws IOException 115 { 116 return client; 117 } 118 } 119 | Popular Tags |