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.openejb.config;
017:
018: import javax.enterprise.deploy.spi.DeploymentManager;
019: import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException;
020: import javax.enterprise.deploy.spi.factories.DeploymentFactory;
021: import java.net.URI;
022: import java.net.URISyntaxException;
023:
024: /**
025: * @version $Rev: 602704 $ $Date: 2007-12-09 09:58:22 -0800 $
026: */
027: public class OpenEJBDeploymentFactory implements DeploymentFactory {
028: public static final String URI_SCHEME = "openejb";
029: private static org.apache.openejb.config.DeploymentFactory factory = new DeploymentImpl.DeploymentFactoryImpl();
030:
031: // static {
032: // DeploymentFactoryManager manager = DeploymentFactoryManager.getInstance();
033: // manager.registerDeploymentFactory(new OpenEJBDeploymentFactory());
034: //
035: // try {
036: // ResourceFinder resourceFinder = new ResourceFinder("META-INF", OpenEJBDeploymentFactory.class.getClassLoader());
037: // Class impl = resourceFinder.findImplementation(org.apache.openejb.config.DeploymentFactory.class);
038: // factory = (org.apache.openejb.config.DeploymentFactory) impl.newInstance();
039: // } catch (Exception ignored) {
040: // // todo maybe log this
041: // }
042: // }
043:
044: public static org.apache.openejb.config.DeploymentFactory getFactory() {
045: return factory;
046: }
047:
048: public static void setFactory(
049: org.apache.openejb.config.DeploymentFactory factory) {
050: OpenEJBDeploymentFactory.factory = factory;
051: }
052:
053: public String getDisplayName() {
054: return "OpenEJB";
055: }
056:
057: public String getProductVersion() {
058: return "3.0";
059: }
060:
061: public boolean handlesURI(String uri) {
062: try {
063: URI fullUri = new URI(uri);
064: return OpenEJBDeploymentFactory.URI_SCHEME.equals(fullUri
065: .getScheme());
066: } catch (URISyntaxException e) {
067: return false;
068: }
069: }
070:
071: public DeploymentManager getDisconnectedDeploymentManager(String uri)
072: throws DeploymentManagerCreationException {
073: if (!handlesURI(uri)) {
074: throw new DeploymentManagerCreationException(
075: "Invalid URI: " + uri);
076: }
077:
078: return new OpenEJBDeploymentManager();
079: }
080:
081: public DeploymentManager getDeploymentManager(String uri,
082: String username, String password)
083: throws DeploymentManagerCreationException {
084: URI protocolUri = getProtocolUri(uri);
085: if (protocolUri == null) {
086: throw new DeploymentManagerCreationException(
087: "Invalid URI: " + uri);
088: }
089:
090: try {
091: Deployment deployment = factory.createDeployment(
092: protocolUri, username, password);
093: return new OpenEJBDeploymentManager(deployment);
094: } catch (RuntimeException e) {
095: // some DeploymentManagerFactories suppress unchecked exceptions - log and rethrow
096: DeploymentManagerCreationException creationException = new DeploymentManagerCreationException(
097: "Unexpected exception while creating deployment manager");
098: creationException.initCause(e);
099: throw creationException;
100: } catch (AssertionError e) {
101: // some DeploymentManagerFactories suppress unchecked exceptions - log and rethrow
102: DeploymentManagerCreationException creationException = new DeploymentManagerCreationException(
103: "Assertion error while creating deployment manager");
104: creationException.initCause(e);
105: throw creationException;
106: }
107: }
108:
109: private URI getProtocolUri(String uri) {
110: try {
111: URI fullUri = new URI(uri);
112: if (!OpenEJBDeploymentFactory.URI_SCHEME.equals(fullUri
113: .getScheme())) {
114: return null;
115: }
116:
117: URI protocolUri = new URI(fullUri.getSchemeSpecificPart());
118: return protocolUri;
119: } catch (URISyntaxException e) {
120: return null;
121: }
122: }
123:
124: // public static void main(String[] args) {
125: // System.out.println("Parsed: "+new DeploymentFactoryImpl().parseURI("deployer:geronimo:inVM"));
126: // }
127: }
|