1 package org.apache.tools.ant.taskdefs.optional.jmx.connector.jboss.ejb; 2 3 52 53 54 import java.net.InetAddress ; 55 import java.net.UnknownHostException ; 56 import java.util.Hashtable ; 57 import javax.management.MBeanServer ; 58 import javax.naming.Context ; 59 import javax.naming.NamingException ; 60 import org.apache.tools.ant.BuildException; 61 import org.apache.tools.ant.taskdefs.optional.jmx.connector.AbstractJMXConnector; 62 import org.jboss.jmx.adaptor.interfaces.AdaptorHome; 63 import org.jboss.jmx.connector.ejb.EJBConnector; 64 import org.jboss.security.SecurityAssociation; 65 import org.jboss.security.SimplePrincipal; 66 import org.jnp.interfaces.NamingContextFactory; 67 68 69 70 78 79 public class Connector 80 extends AbstractJMXConnector 81 { 82 private static final String DEFAULT_PROTOCOL = "jnp"; 83 private static final String DEFAULT_HOST = "localhost"; 84 private static final int DEFAULT_PORT = 1099; 85 private static final String DEFAULT_JNDI_NAME = "ejb/jmx/ejb/Adaptor"; 86 87 88 public Hashtable getInitialContextProperties(Hashtable contextProps) 89 { 90 if (!contextProps.containsKey(Context.PROVIDER_URL)) 91 { 92 String hostname = null; 93 94 try 95 { 96 hostname = InetAddress.getLocalHost().getHostName(); 97 } 98 99 catch (UnknownHostException e) 100 { 101 hostname = DEFAULT_HOST; 102 } 103 104 contextProps.put(Context.PROVIDER_URL, 105 DEFAULT_PROTOCOL + "://" + hostname + ":" + DEFAULT_PORT); 106 } 107 108 contextProps.put(Context.INITIAL_CONTEXT_FACTORY, 109 NamingContextFactory.class.getName()); 110 111 contextProps.put(Context.URL_PKG_PREFIXES, 112 "org.jboss.naming:org.jnp.interfaces"); 113 114 return contextProps; 115 } 116 117 118 private String getJndiName(String jndiName) { 119 if (jndiName == null || jndiName.length() == 0) { 120 return DEFAULT_JNDI_NAME; 121 } 122 123 return jndiName; 124 } 125 126 public MBeanServer getMBeanServer(Hashtable contextProps, String jndiName) 127 throws BuildException { 128 setSecurity(contextProps); 129 return super.getMBeanServer(contextProps,jndiName); 130 } 131 132 133 public MBeanServer getMBeanServer(final Context context, String jndiName) throws NamingException , org.apache.tools.ant.BuildException { 134 String actualJndiName = getJndiName(jndiName); 135 Object jndiEntry = context.lookup(actualJndiName); 136 137 try { 138 if (jndiEntry == null) { 139 throw new NamingException ("JNDI entry ["+actualJndiName+"] does not exist. Please check jndiName property."); 140 } 141 142 if (!(jndiEntry instanceof AdaptorHome)) { 143 throw new NamingException ( 144 "JNDI entry [" 145 + actualJndiName 146 + "] is incorrect type. Was expecting [" 147 + AdaptorHome.class.getName() 148 + "], found [" 149 + jndiEntry.getClass().getName() 150 + "] Please check jndiName property."); 151 } 152 153 return new EJBConnector((AdaptorHome) jndiEntry); 154 } catch (Exception e) { 155 e.printStackTrace(); 156 throw new BuildException(e); 157 } 158 } 159 160 protected void setSecurity(Hashtable contextProps) { 161 String username = (String ) contextProps.get(Context.SECURITY_PRINCIPAL); 162 String password = (String ) contextProps.get(Context.SECURITY_CREDENTIALS); 163 SecurityAssociation.setPrincipal(username != null ? new SimplePrincipal(username) : null); 164 SecurityAssociation.setCredential(password != null ? password.toCharArray() : null); 165 166 } 167 } 168 169 179 180 181 | Popular Tags |