Search in sources :

Example 26 with ExtendedDeploymentContext

use of org.glassfish.internal.deployment.ExtendedDeploymentContext in project Payara by payara.

the class VirtualServer method removeContext.

/**
 * Stops the given <tt>context</tt> and removes it from this
 * <tt>VirtualServer</tt>.
 * @throws org.glassfish.embeddable.GlassFishException
 */
@Override
public void removeContext(Context context) throws GlassFishException {
    ActionReport report = services.getService(ActionReport.class, "plain");
    Deployment deployment = services.getService(Deployment.class);
    String name;
    if (context instanceof ContextFacade) {
        name = ((ContextFacade) context).getAppName();
    } else {
        name = context.getPath();
    }
    ApplicationInfo appInfo = deployment.get(name);
    if (appInfo == null) {
        report.setMessage("Cannot find deployed application of name " + name);
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        throw new GlassFishException("Cannot find deployed application of name " + name);
    }
    ReadableArchive source = appInfo.getSource();
    if (source == null) {
        report.setMessage("Cannot get source archive for undeployment");
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        throw new GlassFishException("Cannot get source archive for undeployment");
    }
    UndeployCommandParameters params = new UndeployCommandParameters(name);
    params.origin = UndeployCommandParameters.Origin.undeploy;
    params.target = "server";
    ExtendedDeploymentContext deploymentContext = null;
    try {
        deploymentContext = deployment.getBuilder(_logger, params, report).source(source).build();
        deployment.undeploy(name, deploymentContext);
        deployment.unregisterAppFromDomainXML(name, "server");
    } catch (IOException e) {
        _logger.log(Level.SEVERE, LogFacade.REMOVE_CONTEXT_ERROR, e);
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        throw new GlassFishException("Cannot create context for undeployment ", e);
    } catch (TransactionFailure e) {
        throw new GlassFishException(e);
    } finally {
        if (deploymentContext != null) {
            deploymentContext.clean();
        }
    }
    if (_logger.isLoggable(Level.FINE)) {
        _logger.log(Level.FINE, LogFacade.REMOVED_CONTEXT, name);
    }
}
Also used : GlassFishException(org.glassfish.embeddable.GlassFishException) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) UndeployCommandParameters(org.glassfish.api.deployment.UndeployCommandParameters) ApplicationInfo(org.glassfish.internal.data.ApplicationInfo) Deployment(org.glassfish.internal.deployment.Deployment) IOException(java.io.IOException) ActionReport(org.glassfish.api.ActionReport) ReadableArchive(org.glassfish.api.deployment.archive.ReadableArchive) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext)

Example 27 with ExtendedDeploymentContext

use of org.glassfish.internal.deployment.ExtendedDeploymentContext 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
            }
        }
    }
}
Also used : ArchiveHandler(org.glassfish.api.deployment.archive.ArchiveHandler) InputJarArchive(com.sun.enterprise.deployment.deploy.shared.InputJarArchive) IOException(java.io.IOException) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) Logger(java.util.logging.Logger) ActionReport(org.glassfish.api.ActionReport) ClassLoaderHierarchy(org.glassfish.internal.api.ClassLoaderHierarchy) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) DeployCommandParameters(org.glassfish.api.deployment.DeployCommandParameters) HTMLActionReporter(com.sun.enterprise.v3.common.HTMLActionReporter) PreDestroy(org.glassfish.hk2.api.PreDestroy) FileArchive(com.sun.enterprise.deploy.shared.FileArchive) File(java.io.File)

Example 28 with ExtendedDeploymentContext

use of org.glassfish.internal.deployment.ExtendedDeploymentContext in project Payara by payara.

the class EarDeployer method subContext.

private ExtendedDeploymentContext subContext(final Application application, final DeploymentContext context, final String moduleUri) {
    ExtendedDeploymentContext moduleContext = ((ExtendedDeploymentContext) context).getModuleDeploymentContexts().get(moduleUri);
    if (moduleContext != null) {
        return moduleContext;
    }
    final ReadableArchive subArchive;
    try {
        subArchive = context.getSource().getSubArchive(moduleUri);
        subArchive.setParentArchive(context.getSource());
    } catch (IOException ioe) {
        deplLogger.log(Level.WARNING, ERROR_OCCURRED, ioe);
        return null;
    }
    final Properties moduleProps = getModuleProps(context, moduleUri);
    ActionReport subReport = context.getActionReport().addSubActionsReport();
    moduleContext = new DeploymentContextImpl(subReport, context.getSource(), context.getCommandParameters(OpsParams.class), env) {

        @Override
        public ClassLoader getClassLoader() {
            try {
                if (context.getClassLoader() == null) {
                    return null;
                }
                EarClassLoader appCl = EarClassLoader.class.cast(context.getClassLoader());
                if (((ExtendedDeploymentContext) context).getPhase() == Phase.PREPARE) {
                    return appCl;
                } else {
                    return appCl.getModuleClassLoader(moduleUri);
                }
            } catch (ClassCastException e) {
                return context.getClassLoader();
            }
        }

        @Override
        public ClassLoader getFinalClassLoader() {
            try {
                EarClassLoader finalEarCL = (EarClassLoader) context.getFinalClassLoader();
                return finalEarCL.getModuleClassLoader(moduleUri);
            } catch (ClassCastException e) {
                return context.getClassLoader();
            }
        }

        @Override
        public ReadableArchive getSource() {
            return subArchive;
        }

        @Override
        public Properties getAppProps() {
            return context.getAppProps();
        }

        @Override
        public <U extends OpsParams> U getCommandParameters(Class<U> commandParametersType) {
            return context.getCommandParameters(commandParametersType);
        }

        @Override
        public void addTransientAppMetaData(String metaDataKey, Object metaData) {
            context.addTransientAppMetaData(metaDataKey, metaData);
        }

        @Override
        public <T> T getTransientAppMetaData(String metaDataKey, Class<T> metadataType) {
            return context.getTransientAppMetaData(metaDataKey, metadataType);
        }

        @Override
        public Properties getModuleProps() {
            return moduleProps;
        }

        @Override
        public ReadableArchive getOriginalSource() {
            try {
                File appRoot = context.getSourceDir();
                File origModuleFile = new File(appRoot, moduleUri);
                return archiveFactory.openArchive(origModuleFile);
            } catch (IOException ioe) {
                return null;
            }
        }

        @Override
        public File getScratchDir(String subDirName) {
            String modulePortion = Util.getURIName(getSource().getURI());
            return (new File(super.getScratchDir(subDirName), modulePortion));
        }

        @Override
        public <T> T getModuleMetaData(Class<T> metadataType) {
            try {
                return metadataType.cast(application.getModuleByUri(moduleUri));
            } catch (Exception e) {
                // let's first try the extensions mechanisms...
                if (RootDeploymentDescriptor.class.isAssignableFrom(metadataType)) {
                    for (RootDeploymentDescriptor extension : application.getModuleByUri(moduleUri).getExtensionsDescriptors((Class<RootDeploymentDescriptor>) metadataType)) {
                        // we assume there can only be one type of
                        if (extension != null) {
                            try {
                                return metadataType.cast(extension);
                            } catch (Exception e1) {
                            // next one...
                            }
                        }
                    }
                }
                return context.getModuleMetaData(metadataType);
            }
        }
    };
    ((ExtendedDeploymentContext) context).getModuleDeploymentContexts().put(moduleUri, moduleContext);
    moduleContext.setParentContext((ExtendedDeploymentContext) context);
    moduleContext.setModuleUri(moduleUri);
    ArchiveHandler subHandler = context.getModuleArchiveHandlers().get(moduleUri);
    moduleContext.setArchiveHandler(subHandler);
    return moduleContext;
}
Also used : ArchiveHandler(org.glassfish.api.deployment.archive.ArchiveHandler) IOException(java.io.IOException) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) ActionReport(org.glassfish.api.ActionReport) IOException(java.io.IOException) ReadableArchive(org.glassfish.api.deployment.archive.ReadableArchive) File(java.io.File)

Example 29 with ExtendedDeploymentContext

use of org.glassfish.internal.deployment.ExtendedDeploymentContext in project Payara by payara.

the class CreateLifecycleModuleCommand method execute.

public void execute(AdminCommandContext context) {
    ActionReport report = context.getActionReport();
    try {
        validateTarget(target, name);
    } catch (IllegalArgumentException ie) {
        report.setMessage(ie.getMessage());
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    DeployCommandParameters commandParams = new DeployCommandParameters();
    commandParams.name = name;
    commandParams.enabled = enabled;
    commandParams.description = description;
    commandParams.target = target;
    // create a dummy context to hold params and props
    ExtendedDeploymentContext deploymentContext = new DeploymentContextImpl(report, null, commandParams, null);
    Properties appProps = deploymentContext.getAppProps();
    if (property != null) {
        appProps.putAll(property);
    }
    // set to default "user", deployers can override it
    appProps.setProperty(ServerTags.OBJECT_TYPE, "user");
    appProps.setProperty(ServerTags.CLASS_NAME, classname);
    if (classpath != null) {
        appProps.setProperty(ServerTags.CLASSPATH, classpath);
    }
    if (loadorder != null) {
        appProps.setProperty(ServerTags.LOAD_ORDER, loadorder);
    }
    appProps.setProperty(ServerTags.IS_FAILURE_FATAL, failurefatal.toString());
    appProps.setProperty(ServerTags.IS_LIFECYCLE, "true");
    try {
        Transaction t = deployment.prepareAppConfigChanges(deploymentContext);
        deployment.registerAppInDomainXML(null, deploymentContext, t);
    } catch (Exception e) {
        report.setMessage("Failed to create lifecycle module: " + e);
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
}
Also used : DeployCommandParameters(org.glassfish.api.deployment.DeployCommandParameters) Transaction(org.jvnet.hk2.config.Transaction) ActionReport(org.glassfish.api.ActionReport) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) Properties(java.util.Properties) DeploymentContextImpl(org.glassfish.deployment.common.DeploymentContextImpl)

Example 30 with ExtendedDeploymentContext

use of org.glassfish.internal.deployment.ExtendedDeploymentContext in project Payara by payara.

the class DeleteApplicationRefCommand method execute.

/**
 * Entry point from the framework into the command execution
 * @param context context for the command.
 */
public void execute(AdminCommandContext context) {
    final ActionReport report = context.getActionReport();
    final Logger logger = context.getLogger();
    UndeployCommandParameters commandParams = new UndeployCommandParameters();
    if (server.isDas()) {
        commandParams.origin = Origin.unload;
    } else {
        // delete application ref on instance
        // is essentially an undeploy
        commandParams.origin = Origin.undeploy;
    }
    commandParams.command = Command.delete_application_ref;
    // for each matched version
    Iterator it = matchedVersions.iterator();
    while (it.hasNext()) {
        String appName = (String) it.next();
        Application application = applications.getApplication(appName);
        if (application == null) {
            if (env.isDas()) {
                // let's only do this check for DAS to be more
                // tolerable of the partial deployment case
                report.setMessage(localStrings.getLocalString("application.notreg", "Application {0} not registered", appName));
                report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            }
            return;
        }
        ApplicationRef applicationRef = domain.getApplicationRefInTarget(appName, target);
        if (applicationRef == null) {
            if (env.isDas()) {
                // let's only do this check for DAS to be more
                // tolerable of the partial deployment case
                report.setMessage(localStrings.getLocalString("appref.not.exists", "Target {1} does not have a reference to application {0}.", appName, target));
                report.setActionExitCode(ActionReport.ExitCode.WARNING);
            }
            return;
        }
        if (application.isLifecycleModule()) {
            try {
                deployment.unregisterAppFromDomainXML(appName, target, true);
            } catch (Exception e) {
                report.failure(logger, e.getMessage());
            }
            return;
        }
        try {
            ReadableArchive source = null;
            ApplicationInfo appInfo = deployment.get(appName);
            if (appInfo != null) {
                source = appInfo.getSource();
            } else {
                File location = new File(new URI(application.getLocation()));
                source = archiveFactory.openArchive(location);
            }
            commandParams.name = appName;
            commandParams.cascade = cascade;
            final ExtendedDeploymentContext deploymentContext = deployment.getBuilder(logger, commandParams, report).source(source).build();
            deploymentContext.getAppProps().putAll(application.getDeployProperties());
            deploymentContext.setModulePropsMap(application.getModulePropertiesMap());
            if (domain.isCurrentInstanceMatchingTarget(target, appName, server.getName(), null) && appInfo != null) {
                // stop and unload application if it's the target and the
                // the application is in enabled state
                deployment.unload(appInfo, deploymentContext);
            }
            if (report.getActionExitCode().equals(ActionReport.ExitCode.SUCCESS)) {
                try {
                    if (server.isInstance()) {
                        // if it's on instance, we should clean up
                        // the bits
                        deployment.undeploy(appName, deploymentContext);
                        deploymentContext.clean();
                        if (!Boolean.valueOf(application.getDirectoryDeployed()) && source.exists()) {
                            FileUtils.whack(new File(source.getURI()));
                        }
                        deployment.unregisterAppFromDomainXML(appName, target);
                    } else {
                        deployment.unregisterAppFromDomainXML(appName, target, true);
                    }
                } catch (TransactionFailure e) {
                    logger.warning("failed to delete application ref for " + appName);
                }
            }
        } catch (Exception e) {
            logger.log(Level.SEVERE, "Error during deleteing application ref ", e);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            report.setMessage(e.getMessage());
        }
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) ApplicationInfo(org.glassfish.internal.data.ApplicationInfo) ActionReport(org.glassfish.api.ActionReport) Logger(java.util.logging.Logger) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) ApplicationRef(com.sun.enterprise.config.serverbeans.ApplicationRef) URI(java.net.URI) VersioningException(org.glassfish.deployment.versioning.VersioningException) UndeployCommandParameters(org.glassfish.api.deployment.UndeployCommandParameters) Iterator(java.util.Iterator) ReadableArchive(org.glassfish.api.deployment.archive.ReadableArchive) Application(com.sun.enterprise.config.serverbeans.Application) File(java.io.File)

Aggregations

ExtendedDeploymentContext (org.glassfish.internal.deployment.ExtendedDeploymentContext)39 ActionReport (org.glassfish.api.ActionReport)23 IOException (java.io.IOException)17 DeployCommandParameters (org.glassfish.api.deployment.DeployCommandParameters)17 File (java.io.File)16 Logger (java.util.logging.Logger)11 ReadableArchive (org.glassfish.api.deployment.archive.ReadableArchive)11 ApplicationInfo (org.glassfish.internal.data.ApplicationInfo)10 Properties (java.util.Properties)9 URI (java.net.URI)7 Deployment (org.glassfish.internal.deployment.Deployment)7 DeploymentContext (org.glassfish.api.deployment.DeploymentContext)6 UndeployCommandParameters (org.glassfish.api.deployment.UndeployCommandParameters)6 ArchiveHandler (org.glassfish.api.deployment.archive.ArchiveHandler)6 DeploymentContextImpl (org.glassfish.deployment.common.DeploymentContextImpl)6 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)6 Application (com.sun.enterprise.deployment.Application)5 ArrayList (java.util.ArrayList)5 DeploymentProperties (org.glassfish.deployment.common.DeploymentProperties)5 VersioningException (org.glassfish.deployment.versioning.VersioningException)5