Search in sources :

Example 1 with PlainTextActionReporter

use of com.sun.enterprise.v3.common.PlainTextActionReporter 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 PlainTextActionReporter

use of com.sun.enterprise.v3.common.PlainTextActionReporter in project Payara by payara.

the class RuntimeRootImpl method restartDomain.

public void restartDomain() {
    final ModulesRegistry registry = InjectedValues.getInstance().getModulesRegistry();
    final AdminCommandContext ctx = new AdminCommandContextImpl(AMXLoggerInfo.getLogger(), new PlainTextActionReporter());
    final AdminCommand cmd = new RestartDomainCommand(registry);
    cmd.execute(ctx);
}
Also used : AdminCommandContextImpl(org.glassfish.api.admin.AdminCommandContextImpl) ModulesRegistry(com.sun.enterprise.module.ModulesRegistry) AdminCommand(org.glassfish.api.admin.AdminCommand) RestartDomainCommand(com.sun.enterprise.v3.admin.RestartDomainCommand) AdminCommandContext(org.glassfish.api.admin.AdminCommandContext) PlainTextActionReporter(com.sun.enterprise.v3.common.PlainTextActionReporter)

Example 3 with PlainTextActionReporter

use of com.sun.enterprise.v3.common.PlainTextActionReporter in project Payara by payara.

the class CommandResource method fixActionReporterSpecialCases.

/**
 * Some ActionReporters has special logic which must be reflected here
 */
private void fixActionReporterSpecialCases(ActionReporter ar) {
    if (ar == null) {
        return;
    }
    if (ar instanceof PlainTextActionReporter) {
        PlainTextActionReporter par = (PlainTextActionReporter) ar;
        StringBuilder finalOutput = new StringBuilder();
        par.getCombinedMessages(par, finalOutput);
        String outs = finalOutput.toString();
        if (!StringUtils.ok(outs)) {
            par.getTopMessagePart().setMessage(strings.getLocalString("get.mon.no.data", "No monitoring data to report.") + "\n");
        }
    }
}
Also used : PlainTextActionReporter(com.sun.enterprise.v3.common.PlainTextActionReporter)

Example 4 with PlainTextActionReporter

use of com.sun.enterprise.v3.common.PlainTextActionReporter in project Payara by payara.

the class EmbeddedAdminCtrImpl method execute.

public CommandExecution execute(String commandName, CommandParameters params) {
    ParameterMap props = params.getOptions();
    if (params.getOperands().size() > 0) {
        for (String op : params.getOperands()) props.add("DEFAULT", op);
    }
    final ActionReport report = new PlainTextActionReporter();
    CommandExecution ce = new CommandExecution() {

        public ActionReport getActionReport() {
            return report;
        }

        public ActionReport.ExitCode getExitCode() {
            return report.getActionExitCode();
        }

        public String getMessage() {
            return report.getMessage();
        }
    };
    runner.getCommandInvocation(commandName, report, kernelIdentity.getSubject()).parameters(props).execute();
    return ce;
}
Also used : CommandExecution(org.glassfish.internal.embedded.admin.CommandExecution) ParameterMap(org.glassfish.api.admin.ParameterMap) ActionReport(org.glassfish.api.ActionReport) PlainTextActionReporter(com.sun.enterprise.v3.common.PlainTextActionReporter)

Example 5 with PlainTextActionReporter

use of com.sun.enterprise.v3.common.PlainTextActionReporter in project Payara by payara.

the class EmbeddedDeployerImpl method deploy.

@Override
public String deploy(ReadableArchive archive, DeployCommandParameters params) {
    // ensure server is started. start it if not started.
    try {
        server.start();
    } catch (LifecycleException e) {
        throw new RuntimeException(e);
    }
    ActionReport report = new PlainTextActionReporter();
    if (params == null) {
        params = new DeployCommandParameters();
    }
    ExtendedDeploymentContext initialContext = new DeploymentContextImpl(report, archive, params, env);
    ArchiveHandler archiveHandler = null;
    try {
        archiveHandler = deployment.getArchiveHandler(archive);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    if (archiveHandler == null) {
        throw new RuntimeException("Cannot find archive handler for source archive");
    }
    if (params.name == null) {
        params.name = archiveHandler.getDefaultApplicationName(archive, initialContext);
    }
    ExtendedDeploymentContext context = null;
    try {
        context = deployment.getBuilder(logger, params, report).source(archive).archiveHandler(archiveHandler).build(initialContext);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    if (params.property != null) {
        context.getAppProps().putAll(params.property);
    }
    if (params.properties != null) {
        context.getAppProps().putAll(params.properties);
    }
    ApplicationInfo appInfo = null;
    try {
        appInfo = deployment.deploy(context);
    } catch (Exception e) {
        logger.log(Level.SEVERE, KernelLoggerInfo.deployException, e);
    }
    if (appInfo != null) {
        boolean isDirectory = new File(archive.getURI().getPath()).isDirectory();
        EmbeddedDeployedInfo info = new EmbeddedDeployedInfo(appInfo, context.getModulePropsMap(), context.getAppProps(), isDirectory);
        deployedApps.put(appInfo.getName(), info);
        return appInfo.getName();
    }
    return null;
}
Also used : IOException(java.io.IOException) ActionReport(org.glassfish.api.ActionReport) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) PropertyVetoException(java.beans.PropertyVetoException) IOException(java.io.IOException) DeployCommandParameters(org.glassfish.api.deployment.DeployCommandParameters) PlainTextActionReporter(com.sun.enterprise.v3.common.PlainTextActionReporter) File(java.io.File)

Aggregations

PlainTextActionReporter (com.sun.enterprise.v3.common.PlainTextActionReporter)7 ActionReport (org.glassfish.api.ActionReport)4 File (java.io.File)2 IOException (java.io.IOException)2 AdminCommandContext (org.glassfish.api.admin.AdminCommandContext)2 AdminCommandContextImpl (org.glassfish.api.admin.AdminCommandContextImpl)2 DeployCommandParameters (org.glassfish.api.deployment.DeployCommandParameters)2 ExtendedDeploymentContext (org.glassfish.internal.deployment.ExtendedDeploymentContext)2 ApplicationRef (com.sun.enterprise.config.serverbeans.ApplicationRef)1 Applications (com.sun.enterprise.config.serverbeans.Applications)1 Application (com.sun.enterprise.deployment.Application)1 ModulesRegistry (com.sun.enterprise.module.ModulesRegistry)1 RestartDomainCommand (com.sun.enterprise.v3.admin.RestartDomainCommand)1 PropertyVetoException (java.beans.PropertyVetoException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 AdminCommand (org.glassfish.api.admin.AdminCommand)1 ParameterMap (org.glassfish.api.admin.ParameterMap)1