1 8 9 package mx4j.remote; 10 11 import java.io.IOException ; 12 import java.util.Map ; 13 import java.util.StringTokenizer ; 14 15 import javax.management.remote.JMXConnectorServerFactory ; 16 import javax.management.remote.JMXServiceURL ; 17 18 import mx4j.log.Logger; 19 20 35 public abstract class ConnectionResolver extends ProviderHelper 36 { 37 40 public static ConnectionResolver newConnectionResolver(String proto, Map environment) 41 { 42 String protocol = normalizeProtocol(proto); 43 String resolverPackages = findResolverPackageList(); 44 ClassLoader classLoader = findResolverClassLoader(environment, JMXConnectorServerFactory.PROTOCOL_PROVIDER_CLASS_LOADER); 45 return loadResolver(resolverPackages, protocol, classLoader); 46 } 47 48 private static String findResolverPackageList() 49 { 50 String packages = findSystemPackageList(MX4JRemoteConstants.PROTOCOL_RESOLVER_PACKAGES); 51 if (packages == null) 52 packages = MX4JRemoteConstants.RESOLVER_PACKAGES; 53 else 54 packages += MX4JRemoteConstants.RESOLVER_PACKAGES_SEPARATOR + MX4JRemoteConstants.RESOLVER_PACKAGES; 55 Logger logger = getLogger(); 56 if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Resolver packages list is: " + packages); 57 return packages; 58 } 59 60 private static ClassLoader findResolverClassLoader(Map environment, String loaderKey) 61 { 62 if (environment == null) return Thread.currentThread().getContextClassLoader(); 63 Object object = environment.get(loaderKey); 64 if (object == null) return Thread.currentThread().getContextClassLoader(); 65 if (!(object instanceof ClassLoader )) throw new IllegalArgumentException ("Environment property " + loaderKey + " must be a ClassLoader"); 66 return (ClassLoader )object; 67 } 68 69 private static ConnectionResolver loadResolver(String packages, String protocol, ClassLoader loader) 70 { 71 Logger logger = getLogger(); 72 73 StringTokenizer tokenizer = new StringTokenizer (packages, MX4JRemoteConstants.RESOLVER_PACKAGES_SEPARATOR); 74 while (tokenizer.hasMoreTokens()) 75 { 76 String pkg = tokenizer.nextToken().trim(); 77 if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Resolver package: " + pkg); 78 if (pkg.length() == 0) continue; 79 80 String resolverClassName = constructClassName(pkg, protocol, MX4JRemoteConstants.RESOLVER_CLASS); 81 82 Class resolverClass = null; 83 try 84 { 85 resolverClass = loadClass(resolverClassName, loader); 86 } 87 catch (ClassNotFoundException x) 88 { 89 if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Resolver class " + resolverClassName + " not found, continuing with next package"); 90 continue; 91 } 92 catch (Exception x) 93 { 94 if (logger.isEnabledFor(Logger.TRACE)) logger.trace("Cannot load resolver class " + resolverClassName, x); 95 return null; 96 } 97 98 try 99 { 100 return (ConnectionResolver)resolverClass.newInstance(); 101 } 102 catch (Exception x) 103 { 104 if (logger.isEnabledFor(Logger.TRACE)) logger.trace("Cannot instantiate resolver class " + resolverClassName, x); 105 return null; 106 } 107 } 108 109 if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Could not find resolver for protocol " + protocol + " in package list '" + packages + "'"); 111 return null; 112 } 113 114 120 public abstract Object lookupClient(JMXServiceURL url, Map environment) throws IOException ; 121 122 127 public abstract Object bindClient(Object client, Map environment) throws IOException ; 128 129 138 public abstract Object createServer(JMXServiceURL url, Map environment) throws IOException ; 139 140 146 public abstract JMXServiceURL bindServer(Object server, JMXServiceURL url, Map environment) throws IOException ; 147 148 153 public abstract void unbindServer(Object server, JMXServiceURL address, Map environment) throws IOException ; 154 155 161 public abstract void destroyServer(Object server, JMXServiceURL url, Map environment) throws IOException ; 162 } 163 | Popular Tags |