Search in sources :

Example 1 with UndeployCommandParameters

use of org.glassfish.api.deployment.UndeployCommandParameters 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);
    }
}
Also used : GlassFishException(org.glassfish.embeddable.GlassFishException) ArchiveHandler(org.glassfish.api.deployment.archive.ArchiveHandler) ApplicationInfo(org.glassfish.internal.data.ApplicationInfo) ConfigException(org.glassfish.embeddable.web.ConfigException) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) ApplicationRef(com.sun.enterprise.config.serverbeans.ApplicationRef) ServerEnvironment(org.glassfish.api.admin.ServerEnvironment) WebappClassLoader(org.glassfish.web.loader.WebappClassLoader) PlainTextActionReporter(com.sun.enterprise.v3.common.PlainTextActionReporter) Applications(com.sun.enterprise.config.serverbeans.Applications) ConfigException(org.glassfish.embeddable.web.ConfigException) IOException(java.io.IOException) GlassFishException(org.glassfish.embeddable.GlassFishException) DeploymentContextImpl(org.glassfish.deployment.common.DeploymentContextImpl) DeployCommandParameters(org.glassfish.api.deployment.DeployCommandParameters) UndeployCommandParameters(org.glassfish.api.deployment.UndeployCommandParameters) Transaction(org.jvnet.hk2.config.Transaction) ApplicationConfigInfo(org.glassfish.deployment.common.ApplicationConfigInfo) ReadableArchive(org.glassfish.api.deployment.archive.ReadableArchive) File(java.io.File) Application(com.sun.enterprise.deployment.Application)

Example 2 with UndeployCommandParameters

use of org.glassfish.api.deployment.UndeployCommandParameters in project Payara by payara.

the class ResourcesDeployer method event.

/**
 * Event listener to listen to </code>application undeploy validation</code> and
 * if <i>preserveResources</i> flag is set, cache the &lt;resources&gt;
 * config for persisting it in domain.xml
 * @param event
 */
@Override
public void event(Event event) {
    if (event.is(Deployment.DEPLOYMENT_BEFORE_CLASSLOADER_CREATION)) {
        DeploymentContext dc = (DeploymentContext) event.hook();
        final DeployCommandParameters deployParams = dc.getCommandParameters(DeployCommandParameters.class);
        processResources(dc, deployParams);
    } else if (event.is(Deployment.UNDEPLOYMENT_VALIDATION)) {
        DeploymentContext dc = (DeploymentContext) event.hook();
        final UndeployCommandParameters undeployCommandParameters = dc.getCommandParameters(UndeployCommandParameters.class);
        preserveResources(dc, undeployCommandParameters);
    } else if (Deployment.UNDEPLOYMENT_FAILURE.equals(event.type())) {
        DeploymentContext dc = (DeploymentContext) event.hook();
        cleanupPreservedResources(dc, event);
    } else if (Deployment.DEPLOYMENT_FAILURE.equals(event.type())) {
        DeploymentContext dc = (DeploymentContext) event.hook();
        String appName = getAppNameFromDeployCmdParams(dc);
        cleanupResources(appName, dc.getCommandParameters(DeployCommandParameters.class).origin);
        // TODO ASR call this only when the flag is on ? --properties preserveAppScopedResources=true
        cleanupPreservedResources(dc, event);
    } else if (Deployment.DEPLOYMENT_SUCCESS.equals(event.type())) {
        ApplicationInfo applicationInfo = (ApplicationInfo) event.hook();
        String appName = applicationInfo.getName();
        ResourcesRegistry.remove(appName);
    }
}
Also used : DeployCommandParameters(org.glassfish.api.deployment.DeployCommandParameters) DeploymentContext(org.glassfish.api.deployment.DeploymentContext) UndeployCommandParameters(org.glassfish.api.deployment.UndeployCommandParameters) ApplicationInfo(org.glassfish.internal.data.ApplicationInfo)

Example 3 with UndeployCommandParameters

use of org.glassfish.api.deployment.UndeployCommandParameters in project Payara by payara.

the class Java2DBProcessorHelper method init.

/**
 * Initializes the rest of the settings
 */
public void init() {
    if (deploy) {
        // DeployCommandParameters are available only on deploy or deploy
        // part of redeploy
        DeployCommandParameters cliOverrides = ctx.getCommandParameters(DeployCommandParameters.class);
        if (logger.isLoggable(Level.FINE)) {
            logger.fine("---> cliOverrides " + cliOverrides);
        }
        cliCreateTables = cliOverrides.createtables;
        cliDropAndCreateTables = cliOverrides.dropandcreatetables;
        Application application = ctx.getModuleMetaData(Application.class);
        appRegisteredName = application.getRegistrationName();
        deploymentContextProps.setProperty(APPLICATION_NAME, appRegisteredName);
    } else {
        // UndeployCommandParameters are available only on undeploy or undeploy
        // part of redeploy. In the latter case, cliOverrides.droptables
        // is set from cliOverrides.dropandcreatetables passed to redeploy.
        UndeployCommandParameters cliOverrides = ctx.getCommandParameters(UndeployCommandParameters.class);
        if (logger.isLoggable(Level.FINE)) {
            logger.fine("---> cliOverrides " + cliOverrides);
        }
        cliDropTables = cliOverrides.droptables;
        appRegisteredName = deploymentContextProps.getProperty(APPLICATION_NAME);
    }
    try {
        appGeneratedLocation = ctx.getScratchDir("ejb").getCanonicalPath() + File.separator;
    } catch (Exception e) {
        throw new RuntimeException(I18NHelper.getMessage(messages, // NOI18N
        "Java2DBProcessorHelper.generatedlocation", appRegisteredName), e);
    }
    appDeployedLocation = ctx.getSource().getURI().getSchemeSpecificPart() + File.separator;
    ActionReport report = ctx.getActionReport();
    subReport = report.addSubActionsReport();
    subReport.setActionExitCode(ActionReport.ExitCode.SUCCESS);
}
Also used : DeployCommandParameters(org.glassfish.api.deployment.DeployCommandParameters) UndeployCommandParameters(org.glassfish.api.deployment.UndeployCommandParameters) ActionReport(org.glassfish.api.ActionReport) Application(com.sun.enterprise.deployment.Application) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 4 with UndeployCommandParameters

use of org.glassfish.api.deployment.UndeployCommandParameters in project Payara by payara.

the class ConnectorDeployer method clean.

/**
 * Clean any files and artifacts that were created during the execution
 * of the prepare method.
 *
 * @param dc deployment context
 */
public void clean(DeploymentContext dc) {
    super.clean(dc);
    // delete resource configuration
    UndeployCommandParameters dcp = dc.getCommandParameters(UndeployCommandParameters.class);
    if (dcp != null && dcp.origin == OpsParams.Origin.undeploy) {
        if (dcp.cascade != null && dcp.cascade) {
            File sourceDir = dc.getSourceDir();
            String moduleName = sourceDir.getName();
            if (ConnectorsUtil.isEmbedded(dc)) {
                String applicationName = ConnectorsUtil.getApplicationName(dc);
                moduleName = ConnectorsUtil.getEmbeddedRarModuleName(applicationName, moduleName);
            }
            deleteAllResources(moduleName, dcp.target);
        }
    }
}
Also used : UndeployCommandParameters(org.glassfish.api.deployment.UndeployCommandParameters)

Example 5 with UndeployCommandParameters

use of org.glassfish.api.deployment.UndeployCommandParameters in project Payara by payara.

the class ConnectorDeployer method event.

public void event(Event event) {
    if (/*env.isDas() && */
    Deployment.UNDEPLOYMENT_VALIDATION.equals(event.type())) {
        // this is an application undeploy event
        DeploymentContext dc = (DeploymentContext) event.hook();
        UndeployCommandParameters dcp = dc.getCommandParameters(UndeployCommandParameters.class);
        String appName = dcp.name;
        Boolean cascade = dcp.cascade;
        Boolean ignoreCascade = dcp._ignoreCascade;
        if (cascade != null && ignoreCascade != null) {
            if (cascade || ignoreCascade) {
                return;
            }
        }
        com.sun.enterprise.config.serverbeans.Application app = domain.getApplications().getApplication(appName);
        boolean isRAR = false;
        if (app != null && Boolean.valueOf(app.getEnabled())) {
            isRAR = app.containsSnifferType(ConnectorConstants.CONNECTOR_MODULE);
        }
        if (!isRAR) {
            return;
        }
        boolean isAppRefEnabled = false;
        Server server = domain.getServers().getServer(env.getInstanceName());
        ApplicationRef appRef = server.getApplicationRef(appName);
        if (appRef != null && Boolean.valueOf(appRef.getEnabled())) {
            isAppRefEnabled = true;
        }
        if (isAppRefEnabled) {
            return;
        }
        boolean isEAR = app.containsSnifferType(EAR);
        String moduleName = appName;
        ResourcesUtil resourcesUtil = ResourcesUtil.createInstance();
        if (isEAR) {
            List<Module> modules = app.getModule();
            for (Module module : modules) {
                moduleName = module.getName();
                if (module.getEngine(ConnectorConstants.CONNECTOR_MODULE) != null) {
                    moduleName = appName + ConnectorConstants.EMBEDDEDRAR_NAME_DELIMITER + moduleName;
                    if (moduleName.toLowerCase(Locale.getDefault()).endsWith(".rar")) {
                        int index = moduleName.lastIndexOf(".rar");
                        moduleName = moduleName.substring(0, index);
                        if (resourcesUtil.filterConnectorResources(resourceManager.getAllResources(), moduleName, true).size() > 0) {
                            setFailureStatus(dc, moduleName);
                            return;
                        }
                    }
                }
            }
        } else {
            if (resourcesUtil.filterConnectorResources(resourceManager.getAllResources(), moduleName, true).size() > 0) {
                setFailureStatus(dc, moduleName);
            }
        }
    }
}
Also used : ResourcesUtil(com.sun.enterprise.connectors.util.ResourcesUtil) com.sun.enterprise.config.serverbeans(com.sun.enterprise.config.serverbeans) DeploymentContext(org.glassfish.api.deployment.DeploymentContext) UndeployCommandParameters(org.glassfish.api.deployment.UndeployCommandParameters)

Aggregations

UndeployCommandParameters (org.glassfish.api.deployment.UndeployCommandParameters)19 ActionReport (org.glassfish.api.ActionReport)10 IOException (java.io.IOException)7 ApplicationInfo (org.glassfish.internal.data.ApplicationInfo)7 ExtendedDeploymentContext (org.glassfish.internal.deployment.ExtendedDeploymentContext)7 DeployCommandParameters (org.glassfish.api.deployment.DeployCommandParameters)6 File (java.io.File)4 ReadableArchive (org.glassfish.api.deployment.archive.ReadableArchive)4 ApplicationRef (com.sun.enterprise.config.serverbeans.ApplicationRef)3 Application (com.sun.enterprise.deployment.Application)3 Deployment (org.glassfish.internal.deployment.Deployment)3 Application (com.sun.enterprise.config.serverbeans.Application)2 HTMLActionReporter (com.sun.enterprise.v3.common.HTMLActionReporter)2 URI (java.net.URI)2 Logger (java.util.logging.Logger)2 DeploymentContext (org.glassfish.api.deployment.DeploymentContext)2 DeploymentProperties (org.glassfish.deployment.common.DeploymentProperties)2 GlassFishException (org.glassfish.embeddable.GlassFishException)2 ConfigApiTest (org.glassfish.tests.utils.ConfigApiTest)2 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)2