KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > plugin > factories > DeploymentFactoryImpl


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

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 JavaDoc;
28 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
29 import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException JavaDoc;
30 import javax.enterprise.deploy.spi.factories.DeploymentFactory JavaDoc;
31 import javax.management.remote.JMXConnector JavaDoc;
32 import javax.management.remote.JMXConnectorFactory JavaDoc;
33 import javax.management.remote.JMXServiceURL JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.Map JavaDoc;
37
38 /**
39  * Implementation of JSR88 DeploymentFactory.
40  *
41  * This will create a DeploymentManager using a local Geronimo kernel
42  * to contain the GBeans that are responsible for deploying each module
43  * type.
44  *
45  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
46  */

47 public class DeploymentFactoryImpl implements DeploymentFactory JavaDoc {
48     private static final Log log = LogFactory.getLog(DeploymentFactoryImpl.class);
49
50     public static final String JavaDoc URI_PREFIX = "deployer:geronimo:";
51     private static final int DEFAULT_PORT = 1099;
52
53     public String JavaDoc getDisplayName() {
54         return "Apache Geronimo";
55     }
56
57     public String JavaDoc getProductVersion() {
58         return "1.0";
59     }
60
61     public boolean handlesURI(String JavaDoc uri) {
62         return parseURI(uri) != null;
63     }
64
65     private ConnectParams parseURI(String JavaDoc 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 JavaDoc 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 JavaDoc host = uri.substring(0, pos);
90             String JavaDoc port = uri.substring(pos+1);
91             try {
92                 return new ConnectParams(protocol, host.equals("") ? "localhost" : host, Integer.parseInt(port));
93             } catch (NumberFormatException JavaDoc e) {
94                 return null;
95             }
96         } else if(protocol.equals("inVM")) {
97             if(uri.startsWith("//")) {
98                 String JavaDoc 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 JavaDoc uri) throws DeploymentManagerCreationException JavaDoc {
109         if (!handlesURI(uri)) {
110             return null;
111         }
112
113         return new DisconnectedDeploymentManager();
114     }
115
116     public DeploymentManager getDeploymentManager(String JavaDoc uri, String JavaDoc username, String JavaDoc password) throws DeploymentManagerCreationException JavaDoc {
117         ConnectParams params = parseURI(uri);
118         if (params == null) {
119             return null;
120         }
121
122         try {
123             if (params.getProtocol().equals("jmx")) {
124                 Map JavaDoc environment = new HashMap JavaDoc();
125                 String JavaDoc[] credentials = new String JavaDoc[]{username, password};
126                 environment.put(JMXConnector.CREDENTIALS, credentials);
127                 try {
128                     JMXServiceURL JavaDoc address = new JMXServiceURL JavaDoc("service:jmx:rmi:///jndi/rmi://"+params.getHost()+":"+params.getPort()+"/JMXConnector");
129                     JMXConnector JavaDoc 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 JavaDoc e) {
136                     throw (DeploymentManagerCreationException JavaDoc)new DeploymentManagerCreationException JavaDoc(e.getMessage()).initCause(e);
137                 } catch (SecurityException JavaDoc 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 JavaDoc("Invalid URI: " + uri);
144             }
145         } catch (RuntimeException JavaDoc e) {
146             // some DeploymentManagerFactories suppress unchecked exceptions - log and rethrow
147
log.error(e.getMessage(), e);
148             throw e;
149         } catch (Error JavaDoc e) {
150             // some DeploymentManagerFactories suppress unchecked exceptions - log and rethrow
151
log.error(e.getMessage(), e);
152             throw e;
153         }
154     }
155
156     static {
157         DeploymentFactoryManager JavaDoc manager = DeploymentFactoryManager.getInstance();
158         manager.registerDeploymentFactory(new DeploymentFactoryImpl());
159     }
160
161     private final static class ConnectParams {
162         private String JavaDoc protocol;
163         private String JavaDoc host;
164         private int port;
165
166         public ConnectParams(String JavaDoc protocol, String JavaDoc host, int port) {
167             this.protocol = protocol;
168             this.host = host;
169             this.port = port;
170         }
171
172         public String JavaDoc getProtocol() {
173             return protocol;
174         }
175
176         public String JavaDoc getHost() {
177             return host;
178         }
179
180         public int getPort() {
181             return port;
182         }
183
184         public String JavaDoc toString() {
185             return protocol+" / "+host+" / "+port;
186         }
187     }
188
189     public static void main(String JavaDoc[] args) {
190         System.out.println("Parsed: "+new DeploymentFactoryImpl().parseURI("deployer:geronimo:inVM"));
191     }
192 }
193
Popular Tags