1 16 package org.jmanage.core.modules.jboss; 17 18 import org.jmanage.core.management.ServerConnection; 19 import org.jmanage.core.management.ConnectionFailedException; 20 import org.jmanage.core.management.ServerConnectionFactory; 21 import org.jmanage.core.config.ApplicationConfig; 22 import org.jboss.jmx.adaptor.rmi.RMIAdaptor; 23 import org.jboss.security.SecurityAssociation; 24 import org.jboss.security.SimplePrincipal; 25 26 import javax.naming.NamingException ; 27 import javax.naming.Context ; 28 import javax.naming.InitialContext ; 29 30 import java.util.Hashtable ; 31 32 37 public class JBossServerConnectionFactory implements ServerConnectionFactory{ 38 39 43 public ServerConnection getServerConnection(ApplicationConfig config) 44 throws ConnectionFailedException { 45 46 try { 47 RMIAdaptor rmiAdaptor = findExternal(config.getURL(), 48 config.getUsername(), config.getPassword()); 49 return new JBossServerConnection(rmiAdaptor); 50 } catch (Throwable e) { 51 throw new ConnectionFailedException(e); 52 } 53 } 54 55 61 private static RMIAdaptor findExternal(String url, String username, String password) 62 throws NamingException { 63 64 Hashtable props = new Hashtable (); 65 props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); 66 props.put(Context.PROVIDER_URL, url); 67 props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces"); 68 69 if(username != null){ 70 SecurityAssociation.setPrincipal(new SimplePrincipal(username)); 71 SecurityAssociation.setCredential(password); 72 } 73 74 Context ctx = new InitialContext (props); 75 RMIAdaptor rmiAdaptor = (RMIAdaptor)ctx.lookup("jmx/rmi/RMIAdaptor"); 76 ctx.close(); 77 return rmiAdaptor; 78 } 79 80 } 81 | Popular Tags |