1 17 18 package org.apache.geronimo.deployment.plugin.factories; 19 20 import org.apache.geronimo.deployment.plugin.DisconnectedDeploymentManager; 21 import org.apache.geronimo.deployment.plugin.jmx.LocalDeploymentManager; 22 import org.apache.geronimo.deployment.plugin.jmx.RemoteDeploymentManager; 23 import org.apache.geronimo.kernel.KernelRegistry; 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 27 import javax.enterprise.deploy.shared.factories.DeploymentFactoryManager ; 28 import javax.enterprise.deploy.spi.DeploymentManager ; 29 import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException ; 30 import javax.enterprise.deploy.spi.factories.DeploymentFactory ; 31 import javax.management.remote.JMXConnector ; 32 import javax.management.remote.JMXConnectorFactory ; 33 import javax.management.remote.JMXServiceURL ; 34 import java.io.IOException ; 35 import java.util.HashMap ; 36 import java.util.Map ; 37 38 47 public class DeploymentFactoryImpl implements DeploymentFactory { 48 private static final Log log = LogFactory.getLog(DeploymentFactoryImpl.class); 49 50 public static final String URI_PREFIX = "deployer:geronimo:"; 51 private static final int DEFAULT_PORT = 1099; 52 53 public String getDisplayName() { 54 return "Apache Geronimo"; 55 } 56 57 public String getProductVersion() { 58 return "1.0"; 59 } 60 61 public boolean handlesURI(String uri) { 62 return parseURI(uri) != null; 63 } 64 65 private ConnectParams parseURI(String uri) { 66 uri = uri.trim(); 67 if(!uri.startsWith(URI_PREFIX)) { 68 return null; 69 } 70 uri = uri.substring(URI_PREFIX.length()); 71 int pos = uri.indexOf(":"); 72 String protocol = pos == -1 ? uri : uri.substring(0, pos); 73 uri = pos == -1 ? "" : uri.substring(pos+1); 74 if(protocol.equals("jmx")) { 75 if(!uri.startsWith("//")) { 76 return new ConnectParams(protocol, "localhost", DEFAULT_PORT); 77 } 78 uri = uri.substring(2); 79 pos = uri.indexOf(':'); 80 if(pos == -1) { 81 return new ConnectParams(protocol, uri.equals("") ? "localhost" : uri, DEFAULT_PORT); 82 } 83 if(uri.indexOf('/', pos+1) > -1) { 84 return null; 85 } 86 if(uri.indexOf(':', pos+1) > -1) { 87 return null; 88 } 89 String host = uri.substring(0, pos); 90 String port = uri.substring(pos+1); 91 try { 92 return new ConnectParams(protocol, host.equals("") ? "localhost" : host, Integer.parseInt(port)); 93 } catch (NumberFormatException e) { 94 return null; 95 } 96 } else if(protocol.equals("inVM")) { 97 if(uri.startsWith("//")) { 98 String kernel = uri.substring(2); 99 return new ConnectParams(protocol, kernel, -1); 100 } else { 101 return new ConnectParams(protocol, 102 KernelRegistry.getSingleKernel() == null ? null : KernelRegistry.getSingleKernel().getKernelName(), 103 -1); 104 } 105 } else return null; 106 } 107 108 public DeploymentManager getDisconnectedDeploymentManager(String uri) throws DeploymentManagerCreationException { 109 if (!handlesURI(uri)) { 110 return null; 111 } 112 113 return new DisconnectedDeploymentManager(); 114 } 115 116 public DeploymentManager getDeploymentManager(String uri, String username, String password) throws DeploymentManagerCreationException { 117 ConnectParams params = parseURI(uri); 118 if (params == null) { 119 return null; 120 } 121 122 try { 123 if (params.getProtocol().equals("jmx")) { 124 Map environment = new HashMap (); 125 String [] credentials = new String []{username, password}; 126 environment.put(JMXConnector.CREDENTIALS, credentials); 127 try { 128 JMXServiceURL address = new JMXServiceURL ("service:jmx:rmi:///jndi/rmi://"+params.getHost()+":"+params.getPort()+"/JMXConnector"); 129 JMXConnector jmxConnector = JMXConnectorFactory.connect(address, environment); 130 RemoteDeploymentManager manager = new RemoteDeploymentManager(jmxConnector, params.getHost()); 131 if(!manager.isSameMachine()) { 132 manager.setAuthentication(username, password); 133 } 134 return manager; 135 } catch (IOException e) { 136 throw (DeploymentManagerCreationException )new DeploymentManagerCreationException (e.getMessage()).initCause(e); 137 } catch (SecurityException e) { 138 throw (AuthenticationFailedException) new AuthenticationFailedException("Invalid login.").initCause(e); 139 } 140 } else if(params.getProtocol().equals("inVM")) { 141 return new LocalDeploymentManager(KernelRegistry.getKernel(params.getHost())); 142 } else { 143 throw new DeploymentManagerCreationException ("Invalid URI: " + uri); 144 } 145 } catch (RuntimeException e) { 146 log.error(e.getMessage(), e); 148 throw e; 149 } catch (Error e) { 150 log.error(e.getMessage(), e); 152 throw e; 153 } 154 } 155 156 static { 157 DeploymentFactoryManager manager = DeploymentFactoryManager.getInstance(); 158 manager.registerDeploymentFactory(new DeploymentFactoryImpl()); 159 } 160 161 private final static class ConnectParams { 162 private String protocol; 163 private String host; 164 private int port; 165 166 public ConnectParams(String protocol, String host, int port) { 167 this.protocol = protocol; 168 this.host = host; 169 this.port = port; 170 } 171 172 public String getProtocol() { 173 return protocol; 174 } 175 176 public String getHost() { 177 return host; 178 } 179 180 public int getPort() { 181 return port; 182 } 183 184 public String toString() { 185 return protocol+" / "+host+" / "+port; 186 } 187 } 188 189 public static void main(String [] args) { 190 System.out.println("Parsed: "+new DeploymentFactoryImpl().parseURI("deployer:geronimo:inVM")); 191 } 192 } 193 | Popular Tags |