1 package org.apache.tools.ant.taskdefs.optional.jmx.connector; 2 3 52 53 import java.util.Hashtable ; 54 import javax.management.MBeanRegistrationException ; 55 import javax.management.MBeanServer ; 56 import javax.management.ObjectName ; 57 import javax.naming.Context ; 58 import javax.naming.NamingException ; 59 import org.apache.tools.ant.BuildException; 60 61 62 63 70 public abstract class AbstractJMXConnector implements JMXConnector { 71 72 73 public AbstractJMXConnector() { 74 } 75 76 public Hashtable getInitialContextProperties(String providerUrl, String user, String password) { 77 Hashtable properties = new Hashtable (); 78 79 if (providerUrl != null) { 80 properties.put(Context.PROVIDER_URL,providerUrl); 81 } 82 83 if (user != null) { 84 properties.put(Context.SECURITY_PRINCIPAL,user); 85 } 86 87 if (password != null) { 88 properties.put(Context.SECURITY_CREDENTIALS,password); 89 } 90 91 return getInitialContextProperties(properties); 92 93 } 94 95 96 99 public ObjectName createMBean(String type, ObjectName objectName, MBeanServer mbserver) throws MBeanRegistrationException { 100 try { 101 return mbserver.createMBean(type,objectName).getObjectName(); 102 } catch (Exception ex) { 103 throw new MBeanRegistrationException (ex); 104 } 105 } 106 107 112 public String getActiveDomain(MBeanServer mbserver) { 113 return mbserver.getDefaultDomain(); 114 } 115 116 120 public Hashtable getInitialContextProperties(Hashtable contextProps) { 121 return contextProps; 122 } 123 124 public MBeanServer getMBeanServer(Hashtable contextProps, String jndiLookupName) throws BuildException { 125 Context context = null; 126 try { 127 context = new javax.naming.InitialContext (contextProps); 128 129 return getMBeanServer(context,jndiLookupName); 130 } catch (NamingException x) { 131 String message = x.getMessage(); 132 if (message == null) { 133 if (x.getRootCause() != null) { 134 message = x.getRootCause().getMessage(); 135 } 136 } else { 137 message = x.toString(); 138 } 139 throw new BuildException(message); 140 } finally { 141 try { 142 if (context != null) { 143 context.close(); 144 } 145 } catch (NamingException eatMe) { 146 System.err.println("Warning! Could not close naming context. " + eatMe); 148 } 149 } 150 } 151 152 166 public abstract MBeanServer getMBeanServer(Context context, String jndiLookupName) throws NamingException , BuildException; 167 168 } 169 170 189 | Popular Tags |