org.apache.openejb.assembler.DeployerEjb Maven / Gradle / Ivy
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.openejb.assembler;
import org.apache.openejb.ClassLoaderUtil;
import org.apache.openejb.NoSuchApplicationException;
import org.apache.openejb.OpenEJBException;
import org.apache.openejb.OpenEJBRuntimeException;
import org.apache.openejb.UndeployException;
import org.apache.openejb.assembler.classic.AppInfo;
import org.apache.openejb.assembler.classic.Assembler;
import org.apache.openejb.config.AppModule;
import org.apache.openejb.config.ConfigurationFactory;
import org.apache.openejb.config.DeploymentLoader;
import org.apache.openejb.config.DeploymentModule;
import org.apache.openejb.config.WebModule;
import org.apache.openejb.config.sys.AdditionalDeployments;
import org.apache.openejb.config.sys.Deployments;
import org.apache.openejb.config.sys.JaxbOpenejb;
import org.apache.openejb.loader.Files;
import org.apache.openejb.loader.IO;
import org.apache.openejb.loader.ProvisioningUtil;
import org.apache.openejb.loader.SystemInstance;
import org.apache.openejb.util.LogCategory;
import org.apache.openejb.util.Logger;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.enterprise.inject.Alternative;
import javax.validation.ValidationException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;
import static javax.ejb.TransactionManagementType.BEAN;
import static org.apache.openejb.config.ConfigurationFactory.ADDITIONAL_DEPLOYMENTS;
import static org.apache.openejb.loader.ProvisioningUtil.realLocation;
@SuppressWarnings("EjbProhibitedPackageUsageInspection")
@Stateless(name = "openejb/Deployer")
@Remote(Deployer.class)
@TransactionManagement(BEAN)
@Alternative
public class DeployerEjb implements Deployer {
private static final Logger LOGGER = Logger.getInstance(LogCategory.OPENEJB, DeployerEjb.class);
public static final String OPENEJB_DEPLOYER_FORCED_APP_ID_PROP = "openejb.deployer.forced.appId";
public static final String OPENEJB_DEPLOYER_HOST = "openejb.deployer.host";
public static final String OPENEJB_USE_BINARIES = "openejb.deployer.binaries.use";
public static final String OPENEJB_PATH_BINARIES = "openejb.deployer.binaries.path";
public static final String OPENEJB_VALUE_BINARIES = "openejb.deployer.binaries.value";
public static final String OPENEJB_APP_AUTODEPLOY = "openejb.app.autodeploy";
public static final ThreadLocal AUTO_DEPLOY = new ThreadLocal();
private static final File uniqueFile;
private static final boolean oldWarDeployer = "old".equalsIgnoreCase(SystemInstance.get().getOptions().get("openejb.deployer.war", "new"));
private static final String OPENEJB_DEPLOYER_SAVE_DEPLOYMENTS = "openejb.deployer.save-deployments";
private static final boolean SAVE_DEPLOYMENTS = SystemInstance.get().getOptions().get(OPENEJB_DEPLOYER_SAVE_DEPLOYMENTS, false);
static {
final String uniqueName = "OpenEJB-" + new BigInteger(128, new SecureRandom()).toString(Character.MAX_RADIX);
final String tempDir = System.getProperty("java.io.tmpdir");
File unique;
try {
unique = new File(tempDir, uniqueName).getCanonicalFile();
if (!unique.createNewFile()) {
throw new IOException("Failed to create file in temp: " + unique);
}
} catch (final IOException e) {
// same trying in work directory
unique = new File(SystemInstance.get().getBase().getDirectory(), "work");
if (unique.exists()) {
try {
unique = new File(unique, uniqueName).getCanonicalFile();
if (!unique.createNewFile()) {
throw new IOException("Failed to create file in work: " + unique);
}
} catch (final IOException e1) {
throw new OpenEJBRuntimeException(e);
}
} else {
throw new OpenEJBRuntimeException("cannot create unique file, please set java.io.tmpdir to a writable folder or create work folder", e);
}
}
uniqueFile = unique;
uniqueFile.deleteOnExit();
}
private final DeploymentLoader deploymentLoader;
private final ConfigurationFactory configurationFactory;
private final Assembler assembler;
public DeployerEjb() {
deploymentLoader = new DeploymentLoader();
final ConfigurationFactory component = SystemInstance.get().getComponent(ConfigurationFactory.class);
configurationFactory = component == null ? new ConfigurationFactory() : component;
assembler = (Assembler) SystemInstance.get().getComponent(org.apache.openejb.spi.Assembler.class);
}
@Override
public String getUniqueFile() {
return uniqueFile.getAbsolutePath();
}
@Override
public Collection getDeployedApps() {
return assembler.getDeployedApplications();
}
@Override
public AppInfo deploy(final String location) throws OpenEJBException {
return deploy(location, null);
}
@Override
public AppInfo deploy(final Properties properties) throws OpenEJBException {
return deploy(null, properties);
}
@Override
public AppInfo deploy(final String inLocation, Properties properties) throws OpenEJBException {
String rawLocation = inLocation;
if (rawLocation == null && properties == null) {
throw new NullPointerException("location and properties are null");
}
if (rawLocation == null) {
rawLocation = properties.getProperty(FILENAME);
}
if (properties == null) {
properties = new Properties();
}
AppModule appModule = null;
final File file;
if ("true".equalsIgnoreCase(properties.getProperty(OPENEJB_USE_BINARIES, "false"))) {
file = copyBinaries(properties);
} else {
file = new File(realLocation(rawLocation));
}
final boolean autoDeploy = Boolean.parseBoolean(properties.getProperty(OPENEJB_APP_AUTODEPLOY, "false"));
final String host = properties.getProperty(OPENEJB_DEPLOYER_HOST, null);
if (WebAppDeployer.Helper.isWebApp(file) && !oldWarDeployer) {
AUTO_DEPLOY.set(autoDeploy);
try {
final AppInfo appInfo = SystemInstance.get().getComponent(WebAppDeployer.class)
.deploy(host, contextRoot(properties, file.getAbsolutePath()), file);
if (appInfo != null) {
saveIfNeeded(properties, file, appInfo);
return appInfo;
}
throw new OpenEJBException("can't deploy " + file.getAbsolutePath());
} finally {
AUTO_DEPLOY.remove();
}
}
AppInfo appInfo = null;
try {
appModule = deploymentLoader.load(file);
// Add any alternate deployment descriptors to the modules
final Map modules = new TreeMap();
for (final DeploymentModule module : appModule.getEjbModules()) {
modules.put(module.getModuleId(), module);
}
for (final DeploymentModule module : appModule.getClientModules()) {
modules.put(module.getModuleId(), module);
}
for (final WebModule module : appModule.getWebModules()) {
final String contextRoot = contextRoot(properties, module.getJarLocation());
if (contextRoot != null) {
module.setContextRoot(contextRoot);
module.setHost(host);
}
modules.put(module.getModuleId(), module);
}
for (final DeploymentModule module : appModule.getConnectorModules()) {
modules.put(module.getModuleId(), module);
}
for (final Map.Entry
© 2015 - 2025 Weber Informatics LLC | Privacy Policy