org.glassfish.deployment.common.DeploymentContextImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of payara-micro Show documentation
Show all versions of payara-micro Show documentation
Micro Distribution of the Payara Project
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2006-2013 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2019-2022] Payara Foundation and/or affiliates
package org.glassfish.deployment.common;
import com.sun.enterprise.config.serverbeans.ServerTags;
import org.glassfish.deployment.versioning.VersioningUtils;
import java.lang.instrument.ClassFileTransformer;
import org.glassfish.api.ActionReport;
import org.glassfish.api.deployment.InstrumentableClassLoader;
import org.glassfish.api.deployment.OpsParams;
import org.glassfish.api.deployment.archive.ReadableArchive;
import org.glassfish.api.deployment.archive.ArchiveHandler;
import org.glassfish.api.admin.ServerEnvironment;
import org.glassfish.internal.api.ClassLoaderHierarchy;
import org.glassfish.internal.deployment.*;
import org.glassfish.loader.util.ASClassLoaderUtil;
import java.util.*;
import java.util.logging.Logger;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.MalformedURLException;
import java.net.URLClassLoader;
import org.glassfish.hk2.api.PreDestroy;
import com.sun.enterprise.util.LocalStringManagerImpl;
import com.sun.enterprise.util.io.FileUtils;
import fish.payara.nucleus.hotdeploy.ApplicationState;
import fish.payara.nucleus.hotdeploy.HotDeployService;
import java.io.Closeable;
import java.util.logging.Level;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.FINEST;
import org.glassfish.api.deployment.DeployCommandParameters;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.classmodel.reflect.Parser;
import org.glassfish.hk2.classmodel.reflect.Types;
import org.glassfish.internal.api.Globals;
import org.glassfish.logging.annotation.LoggerInfo;
import org.glassfish.logging.annotation.LogMessagesResourceBundle;
/**
*
* @author dochez
*/
public class DeploymentContextImpl implements ExtendedDeploymentContext, PreDestroy {
@LogMessagesResourceBundle
private static final String SHARED_LOGMESSAGE_RESOURCE = "org.glassfish.deployment.LogMessages";
// Reserve this range [NCLS-DEPLOYMENT-00001, NCLS-DEPLOYMENT-02000]
// for message ids used in this deployment common module
@LoggerInfo(subsystem = "DEPLOYMENT", description="Deployment logger for common module", publish=true)
private static final String DEPLOYMENT_LOGGER = "javax.enterprise.system.tools.deployment.common";
public static final Logger deplLogger =
Logger.getLogger(DEPLOYMENT_LOGGER, SHARED_LOGMESSAGE_RESOURCE);
final private static LocalStringManagerImpl localStrings = new LocalStringManagerImpl(DeploymentContextImpl.class);
private static final String INTERNAL_DIR_NAME = "__internal";
private static final String APP_TENANTS_SUBDIR_NAME = "__app-tenants";
ReadableArchive source;
ReadableArchive originalSource;
final OpsParams parameters;
ActionReport actionReport;
final ServerEnvironment env;
ClassLoader cloader;
ArchiveHandler archiveHandler;
Properties props;
Map modulesMetaData = new HashMap();
List transformers = new ArrayList();
Phase phase = Phase.UNKNOWN;
ClassLoader sharableTemp = null;
Map modulePropsMap = new HashMap();
Map transientAppMetaData = new HashMap();
Map moduleArchiveHandlers = new HashMap();
Map moduleDeploymentContexts = new HashMap();
ExtendedDeploymentContext parentContext = null;
String moduleUri = null;
private String tenant = null;
private String originalAppName = null;
private File tenantDir = null;
/**
* Creates a new instance of DeploymentContext
*
* @param builder
* @param env
*/
public DeploymentContextImpl(Deployment.DeploymentContextBuilder builder, ServerEnvironment env) {
this(builder.report(), builder.sourceAsArchive(), builder.params(), env);
}
public DeploymentContextImpl(ActionReport actionReport, Logger logger,
ReadableArchive source, OpsParams params, ServerEnvironment env) {
this(actionReport, source, params, env);
}
public DeploymentContextImpl(ActionReport actionReport,
ReadableArchive source, OpsParams params, ServerEnvironment env) {
this.originalSource = source;
this.source = source;
this.actionReport = actionReport;
this.parameters = params;
this.env = env;
}
public Phase getPhase()
{
return phase;
}
public void setPhase(Phase newPhase) {
this.phase = newPhase;
}
public ReadableArchive getSource() {
return source;
}
public void setSource(ReadableArchive source) {
this.source = source;
}
public U getCommandParameters(Class commandParametersType) {
try {
return commandParametersType.cast(parameters);
} catch (ClassCastException e) {
return null;
}
}
public Logger getLogger() {
return deplLogger;
}
@Override
public synchronized void preDestroy() {
boolean hotDeploy = getCommandParameters(DeployCommandParameters.class).hotDeploy;
if (!hotDeploy) {
try {
PreDestroy.class.cast(sharableTemp).preDestroy();
} catch (Exception e) {
// ignore, the classloader does not need to be destroyed
}
try {
PreDestroy.class.cast(cloader).preDestroy();
} catch (Exception e) {
// ignore, the classloader does not need to be destroyed
}
}
}
/**
* Returns the class loader associated to this deployment request.
* ClassLoader instances are usually obtained by the getClassLoader API on
* the associated ArchiveHandler for the archive type being deployed.
*
* This can return null and the container should allocate a ClassLoader
* while loading the application.
*
* @return a class loader capable of loading classes and resources from the
* source
* @link {org.jvnet.glassfish.apu.deployment.archive.ArchiveHandler.getClassLoader()}
*/
@Override
public ClassLoader getFinalClassLoader() {
return cloader;
}
/**
* Returns the class loader associated to this deployment request.
* ClassLoader instances are usually obtained by the getClassLoader API on
* the associated ArchiveHandler for the archive type being deployed.
*
* This can return null and the container should allocate a ClassLoader
* while loading the application.
*
* @return a class loader capable of loading classes and resources from the
* source
* @link {org.jvnet.glassfish.apu.deployment.archive.ArchiveHandler.getClassLoader()}
*/
@Override
public ClassLoader getClassLoader() {
/* TODO -- Replace this method with another that does not imply it is
* an accessor and conveys that the result may change depending on the
* current lifecycle. For instance contemporaryClassLoader()
* Problem was reported by findbug
*/
return getClassLoader(true);
}
@Override
public synchronized void setClassLoader(ClassLoader cloader) {
this.cloader = cloader;
}
// this classloader will be used for sniffer retrieval, metadata parsing
// and the prepare
@Override
public synchronized void createDeploymentClassLoader(ClassLoaderHierarchy clh, ArchiveHandler handler)
throws URISyntaxException, MalformedURLException {
this.addTransientAppMetaData(ExtendedDeploymentContext.IS_TEMP_CLASSLOADER, Boolean.TRUE);
boolean hotDeploy = getCommandParameters(DeployCommandParameters.class).hotDeploy;
if (hotDeploy && this.cloader != null) {
this.sharableTemp = this.cloader;
} else {
this.sharableTemp = createClassLoader(clh, handler, null);
}
}
// this classloader will used to load and start the application
@Override
public void createApplicationClassLoader(ClassLoaderHierarchy clh, ArchiveHandler handler)
throws URISyntaxException, MalformedURLException {
this.addTransientAppMetaData(ExtendedDeploymentContext.IS_TEMP_CLASSLOADER, Boolean.FALSE);
if (this.cloader == null) {
this.cloader = createClassLoader(clh, handler, parameters.name());
}
}
private ClassLoader createClassLoader(ClassLoaderHierarchy clh, ArchiveHandler handler, String appName)
throws URISyntaxException, MalformedURLException {
// first we create the appLib class loader, this is non shared libraries class loader
ClassLoader applibCL = clh.getAppLibClassLoader(appName, getAppLibs());
ClassLoader parentCL = clh.createApplicationParentCL(applibCL, this);
return handler.getClassLoader(parentCL, this);
}
public synchronized ClassLoader getClassLoader(boolean sharable) {
// if we are in prepare phase, we need to return our sharable temporary class loader
// otherwise, we return the final one.
if (phase == Phase.PREPARE) {
if (sharable) {
return sharableTemp;
} else {
InstrumentableClassLoader cl = InstrumentableClassLoader.class.cast(sharableTemp);
return cl.copy();
}
} else {
// we are out of the prepare phase, destroy the shareableTemp and
// return the final classloader
if (sharableTemp != null && sharableTemp != cloader) {
try {
PreDestroy.class.cast(sharableTemp).preDestroy();
} catch (Exception e) {
// ignore, the classloader does not need to be destroyed
}
sharableTemp = null;
}
return cloader;
}
}
/**
* Returns a scratch directory that can be used to store things in.
* The scratch directory will be persisted across server restart but
* not across redeployment of the same application
*
* @param subDirName the sub directory name of the scratch dir
* @return the scratch directory for this application based on
* passed in subDirName. Returns the root scratch dir if the
* passed in value is null.
*/
public File getScratchDir(String subDirName) {
File rootScratchDir = env.getApplicationStubPath();
if (tenant != null && originalAppName != null) {
// multi-tenant case
rootScratchDir = getRootScratchTenantDirForApp(originalAppName);
rootScratchDir = new File(rootScratchDir, tenant);
if (subDirName != null ) {
rootScratchDir = new File(rootScratchDir, subDirName);
}
return rootScratchDir;
} else {
// regular case
if (subDirName != null ) {
rootScratchDir = new File(rootScratchDir, subDirName);
}
String appDirName = VersioningUtils.getRepositoryName(parameters.name());
return new File(rootScratchDir, appDirName);
}
}
/**
* {@inheritDoc}
*/
@Override
public File getSourceDir() {
return new File(getSource().getURI());
}
@Override
public void addModuleMetaData(Object metaData) {
if (metaData!=null) {
modulesMetaData.put(metaData.getClass().getName(), metaData);
}
}
@Override
public void resetModuleMetaData() {
modulesMetaData.clear();
}
@Override
public T getModuleMetaData(Class metadataType) {
Object moduleMetaData = modulesMetaData.get(metadataType.getName());
if (moduleMetaData != null) {
return metadataType.cast(moduleMetaData);
} else {
for (Object metadata : modulesMetaData.values()) {
try {
return metadataType.cast(metadata);
} catch (ClassCastException e) {
}
}
return null;
}
}
@Override
public Collection