001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.deployment.plugin.factories;
017:
018: import java.io.IOException;
019: import java.util.Collection;
020: import java.util.Collections;
021: import java.util.HashMap;
022: import java.util.Map;
023:
024: import javax.enterprise.deploy.spi.DeploymentManager;
025: import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException;
026: import javax.enterprise.deploy.spi.factories.DeploymentFactory;
027: import javax.management.remote.JMXConnector;
028: import javax.management.remote.JMXConnectorFactory;
029: import javax.management.remote.JMXServiceURL;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033: import org.apache.geronimo.deployment.ModuleConfigurer;
034: import org.apache.geronimo.deployment.plugin.DisconnectedDeploymentManager;
035: import org.apache.geronimo.deployment.plugin.jmx.LocalDeploymentManager;
036: import org.apache.geronimo.deployment.plugin.jmx.RemoteDeploymentManager;
037: import org.apache.geronimo.kernel.KernelRegistry;
038:
039: /**
040: * Base implementation of JSR88 DeploymentFactory.
041: *
042: * This will create a DeploymentManager using a local Geronimo kernel
043: * to contain the GBeans that are responsible for deploying each module
044: * type.
045: *
046: * @version $Rev: 568304 $ $Date: 2007-08-21 15:10:04 -0700 (Tue, 21 Aug 2007) $
047: */
048: public class BaseDeploymentFactory implements DeploymentFactory {
049: private static final Log log = LogFactory
050: .getLog(BaseDeploymentFactory.class);
051:
052: public static final String URI_PREFIX = "deployer:geronimo:";
053: private static final int DEFAULT_PORT = 1099;
054:
055: public BaseDeploymentFactory() {
056: }
057:
058: public String getDisplayName() {
059: return "Apache Geronimo";
060: }
061:
062: public String getProductVersion() {
063: return "1.0";
064: }
065:
066: public boolean handlesURI(String uri) {
067: return parseURI(uri) != null;
068: }
069:
070: private ConnectParams parseURI(String uri) {
071: uri = uri.trim();
072: if (log.isDebugEnabled()) {
073: log.debug("Parsing URI=" + uri);
074: }
075: if (!uri.startsWith(URI_PREFIX)) {
076: return null;
077: }
078: uri = uri.substring(URI_PREFIX.length());
079: int pos = uri.indexOf(":");
080: String protocol = pos == -1 ? uri : uri.substring(0, pos);
081: uri = pos == -1 ? "" : uri.substring(pos + 1);
082: if (protocol.equals("jmx")) {
083: if (!uri.startsWith("//")) {
084: return new ConnectParams(protocol, "localhost",
085: DEFAULT_PORT);
086: }
087: uri = uri.substring(2);
088: pos = uri.indexOf(':');
089: if (pos == -1) {
090: return new ConnectParams(protocol,
091: uri.equals("") ? "localhost" : uri,
092: DEFAULT_PORT);
093: }
094: if (uri.indexOf('/', pos + 1) > -1) {
095: return null;
096: }
097: if (uri.indexOf(':', pos + 1) > -1) {
098: return null;
099: }
100: String host = uri.substring(0, pos);
101: String port = uri.substring(pos + 1);
102: try {
103: return new ConnectParams(protocol,
104: host.equals("") ? "localhost" : host, Integer
105: .parseInt(port));
106: } catch (NumberFormatException e) {
107: return null;
108: }
109: } else if (protocol.equals("inVM")) {
110: if (uri.startsWith("//")) {
111: String kernel = uri.substring(2);
112: return new ConnectParams(protocol, kernel, -1);
113: } else {
114: return new ConnectParams(protocol, KernelRegistry
115: .getSingleKernel() == null ? null
116: : KernelRegistry.getSingleKernel()
117: .getKernelName(), -1);
118: }
119: } else
120: return null;
121: }
122:
123: public DeploymentManager getDisconnectedDeploymentManager(String uri)
124: throws DeploymentManagerCreationException {
125: if (!handlesURI(uri)) {
126: return null;
127: }
128:
129: Collection<ModuleConfigurer> moduleConfigurers = getModuleConfigurers();
130: return new DisconnectedDeploymentManager(moduleConfigurers);
131: }
132:
133: public DeploymentManager getDeploymentManager(String uri,
134: String username, String password)
135: throws DeploymentManagerCreationException {
136: ConnectParams params = parseURI(uri);
137: if (params == null) {
138: return null;
139: }
140: if (log.isDebugEnabled()) {
141: log.debug("Using protocol=" + params.getProtocol()
142: + ", host=" + params.getHost() + ", port="
143: + params.getPort());
144: }
145:
146: try {
147: if (params.getProtocol().equals("jmx")) {
148: return newRemoteDeploymentManager(username, password,
149: params);
150: } else if (params.getProtocol().equals("inVM")) {
151: return new LocalDeploymentManager(KernelRegistry
152: .getKernel(params.getHost()));
153: } else {
154: throw new DeploymentManagerCreationException(
155: "Invalid URI: " + uri);
156: }
157: } catch (RuntimeException e) {
158: // some DeploymentManagerFactories suppress unchecked exceptions - log and rethrow
159: log.error(e.getMessage(), e);
160: throw e;
161: } catch (Error e) {
162: // some DeploymentManagerFactories suppress unchecked exceptions - log and rethrow
163: log.error(e.getMessage(), e);
164: throw e;
165: }
166: }
167:
168: protected Collection<ModuleConfigurer> getModuleConfigurers()
169: throws DeploymentManagerCreationException {
170: return Collections.EMPTY_LIST;
171: }
172:
173: protected DeploymentManager newRemoteDeploymentManager(
174: String username, String password, ConnectParams params)
175: throws DeploymentManagerCreationException,
176: AuthenticationFailedException {
177: Map environment = new HashMap();
178: String[] credentials = new String[] { username, password };
179: environment.put(JMXConnector.CREDENTIALS, credentials);
180: environment.put(JMXConnectorFactory.DEFAULT_CLASS_LOADER,
181: BaseDeploymentFactory.class.getClassLoader());
182: try {
183: // if ipv6 numeric address wrap with "[" "]"
184: String host = params.getHost();
185: if (host.indexOf(":") >= 0) {
186: host = "[" + host + "]";
187: }
188: if (log.isDebugEnabled()) {
189: log.debug("Using JMXServiceURL with host=" + host
190: + ", port=" + params.getPort());
191: }
192: JMXServiceURL address = new JMXServiceURL(
193: "service:jmx:rmi:///jndi/rmi://" + host + ":"
194: + params.getPort() + "/JMXConnector");
195: JMXConnector jmxConnector = JMXConnectorFactory.connect(
196: address, environment);
197: RemoteDeploymentManager manager = getRemoteDeploymentManager();
198: manager.init(jmxConnector, host);
199: if (!manager.isSameMachine()) {
200: manager.setAuthentication(username, password);
201: }
202: return manager;
203: } catch (IOException e) {
204: log.fatal("caught ", e);
205: DeploymentManagerCreationException deploymentManagerCreationException = (DeploymentManagerCreationException) new DeploymentManagerCreationException(
206: e.getMessage()).initCause(e);
207: log.fatal("throwing ", deploymentManagerCreationException);
208: throw deploymentManagerCreationException;
209: } catch (SecurityException e) {
210: if (log.isDebugEnabled()) {
211: log.debug("caught ", e);
212: }
213: AuthenticationFailedException authenticationFailedException = (AuthenticationFailedException) new AuthenticationFailedException(
214: "Invalid login.").initCause(e);
215: if (log.isDebugEnabled()) {
216: log.debug("throwing ", authenticationFailedException);
217: }
218: throw authenticationFailedException;
219: }
220: }
221:
222: protected RemoteDeploymentManager getRemoteDeploymentManager()
223: throws DeploymentManagerCreationException {
224: Collection<ModuleConfigurer> moduleConfigurers = getModuleConfigurers();
225: return new RemoteDeploymentManager(moduleConfigurers);
226: }
227:
228: private final static class ConnectParams {
229: private String protocol;
230: private String host;
231: private int port;
232:
233: public ConnectParams(String protocol, String host, int port) {
234: this .protocol = protocol;
235: this .host = host;
236: this .port = port;
237: }
238:
239: public String getProtocol() {
240: return protocol;
241: }
242:
243: public String getHost() {
244: return host;
245: }
246:
247: public int getPort() {
248: return port;
249: }
250:
251: public String toString() {
252: return protocol + " / " + host + " / " + port;
253: }
254: }
255:
256: }
|