1 package org.apache.tools.ant.taskdefs.optional.jmx.connector.jboss; 2 3 52 53 54 import java.net.InetAddress ; 55 import java.net.UnknownHostException ; 56 import java.util.Hashtable ; 57 import javax.naming.Context ; 58 import javax.naming.NamingException ; 59 import org.jboss.jmx.adaptor.rmi.RMIAdaptor; 60 61 62 63 72 public class Connector extends org.apache.tools.ant.taskdefs.optional.jmx.connector.AbstractJMXConnector { 73 74 public Connector() { 75 } 76 77 private static final String DEFAULT_PROTOCOL = "jnp"; 78 private static final String DEFAULT_HOST = "localhost"; 79 private static final int DEFAULT_PORT = 1099; 80 private static final String DEFAULT_JNDI_NAME= "jmx:localhost:rmi"; 81 82 public Hashtable getInitialContextProperties(Hashtable contextProps) { 83 if (!(contextProps.containsKey(Context.PROVIDER_URL))) { 84 try { 85 contextProps.put(Context.PROVIDER_URL,DEFAULT_PROTOCOL + "://"+InetAddress.getLocalHost().getHostName()+":"+DEFAULT_PORT); 86 } catch (UnknownHostException eatMe) { 87 contextProps.put(Context.PROVIDER_URL,DEFAULT_PROTOCOL + "://"+DEFAULT_HOST+":"+DEFAULT_PORT); 88 } 89 } 90 contextProps.put(Context.INITIAL_CONTEXT_FACTORY, org.jnp.interfaces.NamingContextFactory.class.getName()); 91 contextProps.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces"); 92 return contextProps; 93 } 94 95 private String getJndiName(String jndiName) { 96 if ( (jndiName == null) || (jndiName.length() == 0)) { 97 try { 98 return "jmx:"+InetAddress.getLocalHost().getHostName()+":rmi"; 99 } catch (UnknownHostException eatMe) { 100 return DEFAULT_JNDI_NAME; 101 } 102 } 103 return jndiName; 104 } 105 106 public javax.management.MBeanServer getMBeanServer(final Context context, String jndiName) throws NamingException , org.apache.tools.ant.BuildException { 107 String actualJndiName = getJndiName(jndiName); 108 109 Object jndiEntry = context.lookup(actualJndiName); 110 111 if (jndiEntry == null) { 112 throw new NamingException ("JNDI entry ["+actualJndiName+"] does not exist. Please check jndiName property."); 113 } 114 115 if (!(jndiEntry instanceof RMIAdaptor)) { 116 throw new NamingException ("JNDI entry ["+actualJndiName+"] is incorrect type. Was expecting [RMIAdaptor], found ["+jndiEntry.getClass().getName()+"] Please check jndiName property."); 117 } 118 119 org.jboss.jmx.connector.RemoteMBeanServer server = new org.jboss.jmx.connector.rmi.RMIConnectorImpl((RMIAdaptor)jndiEntry); 120 return new JBossMBeanServer(server); 121 122 } 123 124 } 125 126 | Popular Tags |