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.assembler;
017:
018: import java.io.File;
019: import java.io.IOException;
020: import java.math.BigInteger;
021: import java.security.SecureRandom;
022: import java.util.Collection;
023: import java.util.Map;
024: import java.util.Properties;
025: import java.util.TreeMap;
026: import javax.ejb.Stateless;
027: import javax.ejb.Remote;
028:
029: import org.apache.openejb.NoSuchApplicationException;
030: import org.apache.openejb.OpenEJBException;
031: import org.apache.openejb.UndeployException;
032: import org.apache.openejb.ClassLoaderUtil;
033: import org.apache.openejb.assembler.classic.AppInfo;
034: import org.apache.openejb.assembler.classic.Assembler;
035: import org.apache.openejb.config.AppModule;
036: import org.apache.openejb.config.ConfigurationFactory;
037: import org.apache.openejb.config.DeploymentLoader;
038: import org.apache.openejb.config.DeploymentModule;
039: import org.apache.openejb.loader.SystemInstance;
040:
041: @Stateless(name="openejb/Deployer")
042: @Remote(Deployer.class)
043: public class DeployerEjb implements Deployer {
044: private final static File uniqueFile;
045:
046: static {
047: String uniqueName = "OpenEJB-"
048: + new BigInteger(128, new SecureRandom())
049: .toString(Character.MAX_RADIX);
050: String tempDir = System.getProperty("java.io.tmpdir");
051: try {
052: uniqueFile = new File(tempDir, uniqueName)
053: .getCanonicalFile();
054: uniqueFile.createNewFile();
055: } catch (IOException e) {
056: throw new RuntimeException(e);
057: }
058: uniqueFile.deleteOnExit();
059: }
060:
061: private final DeploymentLoader deploymentLoader;
062: private final ConfigurationFactory configurationFactory;
063: private final Assembler assembler;
064:
065: public DeployerEjb() {
066: deploymentLoader = new DeploymentLoader();
067: configurationFactory = new ConfigurationFactory();
068: assembler = (Assembler) SystemInstance.get().getComponent(
069: org.apache.openejb.spi.Assembler.class);
070: }
071:
072: public String getUniqueFile() {
073: return uniqueFile.getAbsolutePath();
074: }
075:
076: public Collection<AppInfo> getDeployedApps() {
077: return assembler.getDeployedApplications();
078: }
079:
080: public AppInfo deploy(String location) throws OpenEJBException {
081: return deploy(location, null);
082: }
083:
084: public AppInfo deploy(Properties properties)
085: throws OpenEJBException {
086: return deploy(null, properties);
087: }
088:
089: public AppInfo deploy(String location, Properties properties)
090: throws OpenEJBException {
091: if (location == null && properties == null) {
092: throw new NullPointerException(
093: "location and properties are null");
094: }
095: if (location == null) {
096: location = properties.getProperty(FILENAME);
097: }
098: if (properties == null) {
099: properties = new Properties();
100: }
101:
102: AppModule appModule = null;
103: try {
104: File file = new File(location);
105: appModule = deploymentLoader.load(file);
106:
107: // Add any alternate deployment descriptors to the modules
108: Map<String, DeploymentModule> modules = new TreeMap<String, DeploymentModule>();
109: for (DeploymentModule module : appModule.getEjbModules()) {
110: modules.put(module.getModuleId(), module);
111: }
112: for (DeploymentModule module : appModule.getClientModules()) {
113: modules.put(module.getModuleId(), module);
114: }
115: for (DeploymentModule module : appModule.getWebModules()) {
116: modules.put(module.getModuleId(), module);
117: }
118: for (DeploymentModule module : appModule
119: .getResourceModules()) {
120: modules.put(module.getModuleId(), module);
121: }
122:
123: for (Map.Entry<Object, Object> entry : properties
124: .entrySet()) {
125: String name = (String) entry.getKey();
126: if (name.startsWith(ALT_DD + "/")) {
127: name = name.substring(ALT_DD.length() + 1);
128:
129: DeploymentModule module;
130: int slash = name.indexOf('/');
131: if (slash > 0) {
132: String moduleId = name.substring(0, slash);
133: name = name.substring(slash + 1);
134: module = modules.get(moduleId);
135: } else {
136: module = appModule;
137: }
138:
139: if (module != null) {
140: String value = (String) entry.getValue();
141: File dd = new File(value);
142: if (dd.canRead()) {
143: module.getAltDDs().put(name, dd.toURL());
144: } else {
145: module.getAltDDs().put(name, value);
146: }
147: }
148: }
149: }
150:
151: AppInfo appInfo = configurationFactory
152: .configureApplication(appModule);
153: assembler.createApplication(appInfo);
154:
155: return appInfo;
156: } catch (Throwable e) {
157: // destroy the class loader for the failed application
158: if (appModule != null) {
159: ClassLoaderUtil.destroyClassLoader(appModule
160: .getJarLocation());
161: }
162:
163: e.printStackTrace();
164:
165: if (e instanceof OpenEJBException) {
166: throw (OpenEJBException) e;
167: }
168: if (e instanceof Error) {
169: throw (OpenEJBException) e;
170: }
171: throw new OpenEJBException(e);
172: }
173: }
174:
175: public void undeploy(String moduleId) throws UndeployException,
176: NoSuchApplicationException {
177: assembler.destroyApplication(moduleId);
178: }
179: }
|