use of org.glassfish.api.deployment.archive.ArchiveHandler in project Payara by payara.
the class VirtualServer method addContext.
/**
* Registers the given <tt>Context</tt> with this <tt>VirtualServer</tt>
* at the given context root.
*
* <p>If this <tt>VirtualServer</tt> has already been started, the
* given <tt>context</tt> will be started as well.
* @throws org.glassfish.embeddable.GlassFishException
*/
@Override
public void addContext(Context context, String contextRoot) throws ConfigException, GlassFishException {
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, LogFacade.VS_ADDED_CONTEXT);
}
if (!(context instanceof ContextFacade)) {
// embedded context should always be created via ContextFacade
return;
}
if (!contextRoot.startsWith("/")) {
contextRoot = "/" + contextRoot;
}
ExtendedDeploymentContext deploymentContext = null;
try {
if (factory == null)
factory = services.getService(ArchiveFactory.class);
ContextFacade facade = (ContextFacade) context;
File docRoot = facade.getDocRoot();
ClassLoader classLoader = facade.getClassLoader();
ReadableArchive archive = factory.openArchive(docRoot);
if (report == null)
report = new PlainTextActionReporter();
ServerEnvironment env = services.getService(ServerEnvironment.class);
DeployCommandParameters params = new DeployCommandParameters();
params.contextroot = contextRoot;
params.enabled = Boolean.FALSE;
params.origin = OpsParams.Origin.deploy;
params.virtualservers = getName();
params.target = "server";
ExtendedDeploymentContext initialContext = new DeploymentContextImpl(report, archive, params, env);
if (deployment == null)
deployment = services.getService(Deployment.class);
ArchiveHandler archiveHandler = deployment.getArchiveHandler(archive);
if (archiveHandler == null) {
throw new RuntimeException("Cannot find archive handler for source archive");
}
params.name = archiveHandler.getDefaultApplicationName(archive, initialContext);
Applications apps = domain.getApplications();
ApplicationInfo appInfo = deployment.get(params.name);
ApplicationRef appRef = domain.getApplicationRefInServer(params.target, params.name);
if (appInfo != null && appRef != null) {
if (appRef.getVirtualServers().contains(getName())) {
throw new ConfigException("Context with name " + params.name + " is already registered on virtual server " + getName());
} else {
String virtualServers = appRef.getVirtualServers();
virtualServers = virtualServers + "," + getName();
params.virtualservers = virtualServers;
params.force = Boolean.TRUE;
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "Virtual server " + getName() + " added to context " + params.name);
}
return;
}
}
deploymentContext = deployment.getBuilder(_logger, params, report).source(archive).archiveHandler(archiveHandler).build(initialContext);
Properties properties = new Properties();
deploymentContext.getAppProps().putAll(properties);
if (classLoader != null) {
ClassLoader parentCL = clh.createApplicationParentCL(classLoader, deploymentContext);
ClassLoader cl = archiveHandler.getClassLoader(parentCL, deploymentContext);
deploymentContext.setClassLoader(cl);
}
ApplicationConfigInfo savedAppConfig = new ApplicationConfigInfo(apps.getModule(com.sun.enterprise.config.serverbeans.Application.class, params.name));
Properties appProps = deploymentContext.getAppProps();
String appLocation = DeploymentUtils.relativizeWithinDomainIfPossible(deploymentContext.getSource().getURI());
appProps.setProperty(ServerTags.LOCATION, appLocation);
appProps.setProperty(ServerTags.OBJECT_TYPE, "user");
appProps.setProperty(ServerTags.CONTEXT_ROOT, contextRoot);
savedAppConfig.store(appProps);
Transaction t = deployment.prepareAppConfigChanges(deploymentContext);
appInfo = deployment.deploy(deploymentContext);
if (appInfo != null) {
facade.setAppName(appInfo.getName());
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, LogFacade.VS_ADDED_CONTEXT, new Object[] { getName(), appInfo.getName() });
}
deployment.registerAppInDomainXML(appInfo, deploymentContext, t);
} else {
if (report.getActionExitCode().equals(ActionReport.ExitCode.FAILURE)) {
throw new ConfigException(report.getMessage());
}
}
// Update web.xml with programmatically added servlets, filters, and listeners
File file = null;
boolean delete = true;
com.sun.enterprise.config.serverbeans.Application appBean = apps.getApplication(params.name);
if (appBean != null) {
file = new File(deploymentContext.getSource().getURI().getPath(), "/WEB-INF/web.xml");
if (file.exists()) {
delete = false;
}
updateWebXml(facade, file);
} else {
_logger.log(Level.SEVERE, LogFacade.APP_NOT_FOUND);
}
ReadableArchive source = appInfo.getSource();
UndeployCommandParameters undeployParams = new UndeployCommandParameters(params.name);
undeployParams.origin = UndeployCommandParameters.Origin.undeploy;
undeployParams.target = "server";
ExtendedDeploymentContext undeploymentContext = deployment.getBuilder(_logger, undeployParams, report).source(source).build();
deployment.undeploy(params.name, undeploymentContext);
params.origin = DeployCommandParameters.Origin.load;
params.enabled = Boolean.TRUE;
archive = factory.openArchive(docRoot);
deploymentContext = deployment.getBuilder(_logger, params, report).source(archive).build();
if (classLoader != null) {
ClassLoader parentCL = clh.createApplicationParentCL(classLoader, deploymentContext);
archiveHandler = deployment.getArchiveHandler(archive);
ClassLoader cl = archiveHandler.getClassLoader(parentCL, deploymentContext);
deploymentContext.setClassLoader(cl);
}
deployment.deploy(deploymentContext);
// Enable the app using the modified web.xml
// We can't use Deployment.enable since it doesn't take DeploymentContext with custom class loader
deployment.updateAppEnabledAttributeInDomainXML(params.name, params.target, true);
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, LogFacade.VS_ENABLED_CONTEXT, new Object[] { getName(), params.name() });
}
if (delete) {
if (file != null) {
if (file.exists() && !file.delete()) {
String path = file.toString();
_logger.log(Level.WARNING, LogFacade.UNABLE_TO_DELETE, path);
}
}
}
if (contextRoot.equals("/")) {
contextRoot = "";
}
WebModule wm = (WebModule) findChild(contextRoot);
if (wm != null) {
facade.setUnwrappedContext(wm);
wm.setEmbedded(true);
if (config != null) {
wm.setDefaultWebXml(config.getDefaultWebXml());
}
} else {
throw new ConfigException("Deployed app not found " + contextRoot);
}
if (deploymentContext != null) {
deploymentContext.postDeployClean(true);
}
} catch (Exception ex) {
if (deployment != null && deploymentContext != null) {
deploymentContext.clean();
}
throw new GlassFishException(ex);
}
}
use of org.glassfish.api.deployment.archive.ArchiveHandler in project Payara by payara.
the class DOLUtils method getSniffersForModule.
/**
* get sniffer list for sub modules of an ear application
*/
private static Collection<Sniffer> getSniffersForModule(ServiceLocator habitat, ReadableArchive archive, ModuleDescriptor md, Application app) throws Exception {
ArchiveHandler handler = habitat.getService(ArchiveHandler.class, md.getModuleType().toString());
SnifferManager snifferManager = habitat.getService(SnifferManager.class);
List<URI> classPathURIs = handler.getClassPathURIs(archive);
classPathURIs.addAll(getLibraryJarURIs(app, archive));
Types types = archive.getParentArchive().getExtraData(Types.class);
DeployCommandParameters parameters = archive.getParentArchive().getArchiveMetaData(DeploymentProperties.COMMAND_PARAMS, DeployCommandParameters.class);
Properties appProps = archive.getParentArchive().getArchiveMetaData(DeploymentProperties.APP_PROPS, Properties.class);
ExtendedDeploymentContext context = new DeploymentContextImpl(null, archive, parameters, habitat.<ServerEnvironment>getService(ServerEnvironment.class));
if (appProps != null) {
context.getAppProps().putAll(appProps);
}
context.setArchiveHandler(handler);
context.addTransientAppMetaData(Types.class.getName(), types);
Collection<Sniffer> sniffers = snifferManager.getSniffers(context, classPathURIs, types);
context.postDeployClean(true);
String type = getTypeFromModuleType(md.getModuleType());
Sniffer mainSniffer = null;
for (Sniffer sniffer : sniffers) {
if (sniffer.getModuleType().equals(type)) {
mainSniffer = sniffer;
}
}
// to add the appropriate sniffer
if (mainSniffer == null) {
mainSniffer = snifferManager.getSniffer(type);
sniffers.add(mainSniffer);
}
String[] incompatibleTypes = mainSniffer.getIncompatibleSnifferTypes();
List<String> allIncompatTypes = addAdditionalIncompatTypes(mainSniffer, incompatibleTypes);
List<Sniffer> sniffersToRemove = new ArrayList<Sniffer>();
for (Sniffer sniffer : sniffers) {
for (String incompatType : allIncompatTypes) {
if (sniffer.getModuleType().equals(incompatType)) {
deplLogger.log(Level.WARNING, INCOMPATIBLE_TYPE, new Object[] { type, md.getArchiveUri(), incompatType });
sniffersToRemove.add(sniffer);
}
}
}
sniffers.removeAll(sniffersToRemove);
// store the module sniffer information so we don't need to
// recalculate them later
Hashtable sniffersTable = archive.getParentArchive().getExtraData(Hashtable.class);
if (sniffersTable == null) {
sniffersTable = new Hashtable<String, Collection<Sniffer>>();
archive.getParentArchive().setExtraData(Hashtable.class, sniffersTable);
}
sniffersTable.put(md.getArchiveUri(), sniffers);
return sniffers;
}
use of org.glassfish.api.deployment.archive.ArchiveHandler in project Payara by payara.
the class ApplicationLoaderService method postConstruct.
/**
* Starts the application loader service.
*
* Look at the list of applications installed in our local repository
* Get a Deployer capable for each application found
* Invoke the deployer load() method for each application.
*/
public void postConstruct() {
assert env != null;
try {
logger.fine("Satisfying Optional Packages dependencies...");
InstalledLibrariesResolver.initializeInstalledLibRegistry(env.getLibPath().getAbsolutePath());
} catch (Exception e) {
logger.log(Level.WARNING, KernelLoggerInfo.exceptionOptionalDepend, e);
}
DeploymentLifecycleStatsProvider dlsp = new DeploymentLifecycleStatsProvider();
StatsProviderManager.register("deployment", PluginPoint.SERVER, "deployment/lifecycle", dlsp);
deploymentTracingEnabled = System.getProperty("org.glassfish.deployment.trace");
domain = habitat.getService(Domain.class);
/*
* Build a map that associates an application with its
* order in domain.xml. If the deployment-order attribute
* is not used for any application, then the applications
* are loaded in the order they occur in domain.xml. Also, for
* applications with the same deployment-order attribute,
* the applications are loaded in the order they occur in domain.xml.
* Otherwise, applications are loaded according to the
* deploynment-order attribute.
*/
systemApplications = domain.getSystemApplications();
for (Application systemApp : systemApplications.getApplications()) {
appOrderInfoMap.put(systemApp.getName(), Integer.valueOf(appOrder++));
}
List<Application> standaloneAdapters = applications.getApplicationsWithSnifferType(ServerTags.CONNECTOR, true);
for (Application standaloneAdapter : standaloneAdapters) {
appOrderInfoMap.put(standaloneAdapter.getName(), Integer.valueOf(appOrder++));
}
List<Application> allApplications = applications.getApplications();
for (Application app : allApplications) {
appOrderInfoMap.put(app.getName(), Integer.valueOf(appOrder++));
}
for (Application systemApp : systemApplications.getApplications()) {
// check to see if we need to load up this system application
if (Boolean.valueOf(systemApp.getDeployProperties().getProperty(ServerTags.LOAD_SYSTEM_APP_ON_STARTUP))) {
if (deployment.isAppEnabled(systemApp) || loadAppOnDAS(systemApp.getName())) {
Integer order = appOrderInfoMap.get(systemApp.getName());
ApplicationOrderInfo info = new ApplicationOrderInfo(systemApp, order);
DeploymentOrder.addApplicationDeployment(info);
}
}
}
// load standalone resource adapters first
for (Application standaloneAdapter : standaloneAdapters) {
// information is available on DAS
if (deployment.isAppEnabled(standaloneAdapter) || loadAppOnDAS(standaloneAdapter.getName())) {
DeploymentOrder.addApplicationDeployment(new ApplicationOrderInfo(standaloneAdapter, appOrderInfoMap.get(standaloneAdapter.getName()).intValue()));
}
}
// then the rest of the applications
for (Application app : allApplications) {
if (app.isStandaloneModule() && app.containsSnifferType(ServerTags.CONNECTOR)) {
continue;
}
// information is available on DAS
if (Boolean.valueOf(app.getEnabled()) || loadAppOnDAS(app.getName())) {
DeploymentOrder.addApplicationDeployment(new ApplicationOrderInfo(app, appOrderInfoMap.get(app.getName()).intValue()));
}
}
List<Deployment.ApplicationDeployment> appDeployments = new ArrayList<>();
// process the deployed applications
Iterator iter = DeploymentOrder.getApplicationDeployments();
while (iter.hasNext()) {
Application app = (Application) iter.next();
ApplicationRef appRef = server.getApplicationRef(app.getName());
appDeployments.addAll(processApplication(app, appRef));
}
// does the user want us to run a particular application
String defaultParam = env.getStartupContext().getArguments().getProperty("default");
if (defaultParam != null) {
initializeRuntimeDependencies();
File sourceFile;
if (defaultParam.equals(".")) {
sourceFile = new File(System.getProperty("user.dir"));
} else {
sourceFile = new File(defaultParam);
}
if (sourceFile.exists()) {
sourceFile = sourceFile.getAbsoluteFile();
ReadableArchive sourceArchive = null;
try {
sourceArchive = archiveFactoryProvider.get().openArchive(sourceFile);
DeployCommandParameters parameters = new DeployCommandParameters(sourceFile);
parameters.name = sourceFile.getName();
parameters.enabled = Boolean.TRUE;
parameters.origin = DeployCommandParameters.Origin.deploy;
ActionReport report = new HTMLActionReporter();
if (!sourceFile.isDirectory()) {
// ok we need to explode the directory somwhere and remember to delete it on shutdown
final File tmpFile = File.createTempFile(sourceFile.getName(), "");
final String path = tmpFile.getAbsolutePath();
if (!tmpFile.delete()) {
logger.log(Level.WARNING, KernelLoggerInfo.cantDeleteTempFile, path);
}
File tmpDir = new File(path);
tmpDir.deleteOnExit();
events.register(new org.glassfish.api.event.EventListener() {
public void event(Event event) {
if (event.is(EventTypes.SERVER_SHUTDOWN)) {
if (tmpFile.exists()) {
FileUtils.whack(tmpFile);
}
}
}
});
if (tmpDir.mkdirs()) {
ArchiveHandler handler = deployment.getArchiveHandler(sourceArchive);
final String appName = handler.getDefaultApplicationName(sourceArchive);
DeploymentContextImpl dummyContext = new DeploymentContextImpl(report, logger, sourceArchive, parameters, env);
handler.expand(sourceArchive, archiveFactoryProvider.get().createArchive(tmpDir), dummyContext);
sourceArchive = archiveFactoryProvider.get().openArchive(tmpDir);
logger.log(Level.INFO, KernelLoggerInfo.sourceNotDirectory, tmpDir.getAbsolutePath());
parameters.name = appName;
}
}
ExtendedDeploymentContext depContext = deployment.getBuilder(logger, parameters, report).source(sourceArchive).build();
Deployment.ApplicationDeployment appDeployment = deployment.prepare(null, depContext);
if (appDeployment == null) {
logger.log(Level.SEVERE, KernelLoggerInfo.cantFindApplicationInfo, sourceFile.getAbsolutePath());
} else {
appDeployments.add(appDeployment);
}
} catch (RuntimeException | IOException e) {
logger.log(Level.SEVERE, KernelLoggerInfo.deployException, e);
} finally {
if (sourceArchive != null) {
try {
sourceArchive.close();
} catch (IOException ioe) {
// ignore
}
}
}
}
}
events.send(new Event<>(Deployment.ALL_APPLICATIONS_LOADED, null), false);
for (Deployment.ApplicationDeployment depl : appDeployments) {
deployment.initialize(depl.appInfo, depl.appInfo.getSniffers(), depl.context);
}
events.send(new Event<>(Deployment.ALL_APPLICATIONS_PROCESSED, null));
}
use of org.glassfish.api.deployment.archive.ArchiveHandler in project Payara by payara.
the class DescriptorFactory method createApplicationDescriptor.
/**
* Returns the parsed DOL object from archive
*
* @param archiveFile original archive file
* @param destRootDir root destination directory where the application
* should be expanded under in case of archive deployment
* @param parentCl parent classloader
*
* @return the parsed DOL object
*/
public ResultHolder createApplicationDescriptor(File archiveFile, File destRootDir, ClassLoader parentCl) throws IOException {
ReadableArchive archive = null;
Application application = null;
try {
Descriptor.setBoundsChecking(false);
archive = archiveFactory.openArchive(archiveFile);
ArchiveHandler archiveHandler = deployment.getArchiveHandler(archive);
ActionReport dummyReport = new HTMLActionReporter();
String appName = DeploymentUtils.getDefaultEEName(archiveFile.getName());
DeployCommandParameters params = new DeployCommandParameters();
params.name = appName;
ExtendedDeploymentContext context = new DeploymentContextImpl(dummyReport, archive, params, env);
context.setArchiveHandler(archiveHandler);
if (!archiveFile.isDirectory()) {
// expand archive
File destDir = new File(destRootDir, appName);
if (destDir.exists()) {
FileUtils.whack(destDir);
}
destDir.mkdirs();
archiveHandler.expand(archive, archiveFactory.createArchive(destDir), context);
archive.close();
archive = archiveFactory.openArchive(destDir);
context.setSource(archive);
}
// issue 14564
context.addTransientAppMetaData(ExtendedDeploymentContext.IS_TEMP_CLASSLOADER, Boolean.TRUE);
String archiveType = context.getArchiveHandler().getArchiveType();
ClassLoader cl = archiveHandler.getClassLoader(parentCl, context);
Archivist archivist = archivistFactory.getArchivist(archiveType, cl);
if (archivist == null) {
throw new IOException("Cannot determine the Java EE module type for " + archive.getURI());
}
archivist.setAnnotationProcessingRequested(true);
String xmlValidationLevel = dasConfig.getDeployXmlValidation();
archivist.setXMLValidationLevel(xmlValidationLevel);
if (xmlValidationLevel.equals("none")) {
archivist.setXMLValidation(false);
}
archivist.setRuntimeXMLValidation(false);
try {
application = applicationFactory.openArchive(appName, archivist, archive, true);
} catch (SAXParseException e) {
throw new IOException(e);
}
if (application != null) {
application.setClassLoader(cl);
application.visit((ApplicationVisitor) new ApplicationValidator());
}
} finally {
if (archive != null) {
archive.close();
}
// We need to reset it after descriptor building
Descriptor.setBoundsChecking(true);
}
ResultHolder result = new ResultHolder();
result.application = application;
result.archive = archive;
return result;
}
use of org.glassfish.api.deployment.archive.ArchiveHandler in project Payara by payara.
the class DolProvider method processDeploymentMetaData.
/**
* This method populates the Application object from a ReadableArchive
* @param archive the archive for the application
*/
public Application processDeploymentMetaData(ReadableArchive archive) throws Exception {
FileArchive expandedArchive = null;
File tmpFile = null;
ExtendedDeploymentContext context = null;
Logger logger = Logger.getAnonymousLogger();
ClassLoader cl = null;
try {
String archiveName = Util.getURIName(archive.getURI());
ArchiveHandler archiveHandler = deployment.getArchiveHandler(archive);
if (archiveHandler == null) {
throw new IllegalArgumentException(localStrings.getLocalString("deploy.unknownarchivetype", "Archive type of {0} was not recognized", archiveName));
}
DeployCommandParameters parameters = new DeployCommandParameters(new File(archive.getURI()));
ActionReport report = new HTMLActionReporter();
context = new DeploymentContextImpl(report, archive, parameters, env);
context.setArchiveHandler(archiveHandler);
String appName = archiveHandler.getDefaultApplicationName(archive, context);
parameters.name = appName;
if (archive instanceof InputJarArchive) {
// we need to expand the archive first in this case
tmpFile = File.createTempFile(archiveName, "");
String path = tmpFile.getAbsolutePath();
if (!tmpFile.delete()) {
logger.log(Level.WARNING, "cannot.delete.temp.file", new Object[] { path });
}
File tmpDir = new File(path);
tmpDir.deleteOnExit();
if (!tmpDir.exists() && !tmpDir.mkdirs()) {
throw new IOException("Unable to create directory " + tmpDir.getAbsolutePath());
}
expandedArchive = (FileArchive) archiveFactory.createArchive(tmpDir);
archiveHandler.expand(archive, expandedArchive, context);
context.setSource(expandedArchive);
}
context.setPhase(DeploymentContextImpl.Phase.PREPARE);
ClassLoaderHierarchy clh = clhProvider.get();
context.createDeploymentClassLoader(clh, archiveHandler);
cl = context.getClassLoader();
deployment.getDeployableTypes(context);
deployment.getSniffers(archiveHandler, null, context);
return processDOL(context);
} finally {
if (cl != null && cl instanceof PreDestroy) {
try {
PreDestroy.class.cast(cl).preDestroy();
} catch (Exception e) {
// ignore
}
}
if (context != null) {
context.postDeployClean(true);
}
if (expandedArchive != null) {
try {
expandedArchive.close();
} catch (Exception e) {
// ignore
}
}
if (tmpFile != null && tmpFile.exists()) {
try {
FileUtils.whack(tmpFile);
} catch (Exception e) {
// ignore
}
}
}
}
Aggregations