Glassfish example source code file (DeploymentContextImpl.java)
This example Glassfish source code file (DeploymentContextImpl.java) is included in the DevDaily.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.
The Glassfish DeploymentContextImpl.java source code
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2006-2010 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.
*/
package org.glassfish.deployment.common;
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.DeployCommandParameters;
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.util.logging.Level;
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 org.jvnet.hk2.component.PreDestroy;
import com.sun.enterprise.util.io.FileUtils;
/**
*
* @author dochez
*/
public class DeploymentContextImpl implements ExtendedDeploymentContext, PreDestroy {
private static final String INTERNAL_DIR_NAME = "__internal";
ReadableArchive source;
ReadableArchive originalSource;
final OpsParams parameters;
final Logger logger;
final ActionReport actionReport;
final ServerEnvironment env;
ClassLoader cloader;
ArchiveHandler archiveHandler;
Properties props;
Map<String, Object> modulesMetaData = new HashMap();
List<ClassFileTransformer> transformers = new ArrayList();
Phase phase = Phase.UNKNOWN;
boolean finalClassLoaderAccessedDuringPrepare = false;
boolean tempClassLoaderInvalidated = false;
ClassLoader sharableTemp = null;
Map<String, Properties> modulePropsMap = new HashMap();
Map<String, Object> transientAppMetaData = new HashMap();
Map<String, ArchiveHandler> moduleArchiveHandlers = new HashMap();
Map<String, ExtendedDeploymentContext> moduleDeploymentContexts = new HashMap();
ExtendedDeploymentContext parentContext = null;
/** Creates a new instance of DeploymentContext */
public DeploymentContextImpl(Deployment.DeploymentContextBuilder builder, ServerEnvironment env) {
this(builder.report(), builder.logger(), builder.sourceAsArchive(), builder.params(), env);
}
public DeploymentContextImpl(ActionReport actionReport, Logger logger,
ReadableArchive source, OpsParams params, ServerEnvironment env) {
this.originalSource = source;
this.source = source;
this.logger = logger;
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 extends OpsParams> U getCommandParameters(Class commandParametersType) {
try {
return commandParametersType.cast(parameters);
} catch (ClassCastException e) {
return null;
}
}
public Logger getLogger() {
return logger;
}
public void preDestroy() {
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.
* <p/>
* 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()}
*/
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.
* <p/>
* 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()}
*/
public ClassLoader getClassLoader() {
return getClassLoader(true);
}
public void setClassLoader(ClassLoader cloader) {
this.cloader = cloader;
}
// this classloader will be used for sniffer retrieval, metadata parsing
// and the prepare
public void createDeploymentClassLoader(ClassLoaderHierarchy clh, ArchiveHandler handler)
throws URISyntaxException, MalformedURLException {
this.addTransientAppMetaData(ExtendedDeploymentContext.IS_TEMP_CLASSLOADER, Boolean.TRUE);
this.sharableTemp = createClassLoader(clh, handler, null);
}
// this classloader will used to load and start the application
public void createApplicationClassLoader(ClassLoaderHierarchy clh, ArchiveHandler handler)
throws URISyntaxException, MalformedURLException {
this.addTransientAppMetaData(ExtendedDeploymentContext.IS_TEMP_CLASSLOADER, Boolean.FALSE);
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) {
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 accross server restart but
* not accross 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 (subDirName != null )
rootScratchDir = new File(rootScratchDir, subDirName);
String appDirName = parameters.name();
appDirName = VersioningUtils.getRepositoryName(parameters.name());
return new File(rootScratchDir, appDirName);
}
/**
* {@inheritDoc}
*/
public File getSourceDir() {
return new File(getSource().getURI());
}
public void addModuleMetaData(Object metaData) {
if (metaData!=null) {
modulesMetaData.put(metaData.getClass().getName(), metaData);
}
}
public <T> 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;
}
}
public Collection<Object> getModuleMetadata() {
List<Object> copy = new ArrayList