Search in sources :

Example 1 with ConfigException

use of org.glassfish.embeddable.web.ConfigException 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 ConfigException

use of org.glassfish.embeddable.web.ConfigException in project Payara by payara.

the class WebContainerImpl method addVirtualServer.

/**
 * Adds the given <tt>VirtualServer</tt> to this
 * <tt>WebContainer</tt>.
 *
 * <p>If this <tt>WebContainer</tt> has already been started,
 * the given <tt>virtualServer</tt> will be started as well.
 *
 * @param virtualServer the <tt>VirtualServer</tt> to add
 *
 * @throws ConfigException if a <tt>VirtualServer</tt> with the
 * same id has already been registered with this
 * <tt>WebContainer</tt>
 * @throws GlassFishException if the given <tt>virtualServer</tt> fails
 * to be started
 */
public void addVirtualServer(VirtualServer virtualServer) throws ConfigException, GlassFishException {
    if (!initialized) {
        init();
    }
    if (log.isLoggable(Level.INFO)) {
        log.info("Adding virtual server " + virtualServer.getID());
    }
    com.sun.enterprise.web.VirtualServer vs = (com.sun.enterprise.web.VirtualServer) engine.findChild(virtualServer.getID());
    if (vs != null) {
        throw new ConfigException("VirtualServer with id " + virtualServer.getID() + " is already registered");
    }
    Collection<WebListener> webListeners = virtualServer.getWebListeners();
    List<String> names = new ArrayList<String>();
    if ((webListeners != null) && (!webListeners.isEmpty())) {
        for (WebListener listener : webListeners) {
            names.add(listener.getId());
        }
    } else {
        for (NetworkListener networkListener : networkConfig.getNetworkListeners().getNetworkListener()) {
            names.add(networkListener.getName());
        }
        webListeners = listeners;
    }
    StringBuffer networkListeners = new StringBuffer("");
    if (names.size() > 0) {
        networkListeners.append(names.get(0));
    }
    for (int i = 1; i < names.size(); i++) {
        networkListeners.append(",");
        networkListeners.append(names.get(i));
    }
    String docRoot = null;
    if (virtualServer.getDocRoot() != null) {
        docRoot = virtualServer.getDocRoot().getAbsolutePath();
    }
    String hostName = null;
    if (virtualServer.getConfig() != null) {
        hostName = virtualServer.getConfig().getHostNames();
    }
    final String root = docRoot;
    final String nl = networkListeners.toString();
    final String id = virtualServer.getID();
    final String hosts = hostName;
    try {
        ConfigSupport.apply(new SingleConfigCode<HttpService>() {

            public Object run(HttpService param) throws PropertyVetoException, TransactionFailure {
                com.sun.enterprise.config.serverbeans.VirtualServer newVirtualServer = param.createChild(com.sun.enterprise.config.serverbeans.VirtualServer.class);
                newVirtualServer.setId(id);
                newVirtualServer.setNetworkListeners(nl);
                if (hosts != null) {
                    newVirtualServer.setHosts(hosts);
                }
                Property property = newVirtualServer.createChild(Property.class);
                property.setName("docroot");
                property.setValue(root);
                newVirtualServer.getProperty().add(property);
                param.getVirtualServer().add(newVirtualServer);
                return newVirtualServer;
            }
        }, httpService);
    } catch (Exception ex) {
        throw new GlassFishException(ex);
    }
    if ((webListeners != null) && (!webListeners.isEmpty())) {
        for (WebListener listener : webListeners) {
            if (getWebListener(listener.getId()) == null) {
                addWebListener(listener, virtualServer.getID());
            }
        }
    }
    vs = (com.sun.enterprise.web.VirtualServer) engine.findChild(id);
    if (vs != null) {
        if (log.isLoggable(Level.INFO)) {
            log.info("Added virtual server " + id + " docroot " + docRoot + " networklisteners " + nl);
        }
        if (virtualServer instanceof VirtualServerFacade) {
            ((VirtualServerFacade) virtualServer).setVirtualServer(vs);
        }
        vs.setNetworkListenerNames(names.toArray(new String[names.size()]));
    } else {
        log.severe("Could not add virtual server " + id);
        throw new GlassFishException(new Exception("Cannot add virtual server " + id));
    }
}
Also used : GlassFishException(org.glassfish.embeddable.GlassFishException) ConfigException(org.glassfish.embeddable.web.ConfigException) VirtualServerFacade(com.sun.enterprise.web.VirtualServerFacade) Property(org.jvnet.hk2.config.types.Property) VirtualServer(org.glassfish.embeddable.web.VirtualServer) PropertyVetoException(java.beans.PropertyVetoException) ConfigException(org.glassfish.embeddable.web.ConfigException) GlassFishException(org.glassfish.embeddable.GlassFishException) PropertyVetoException(java.beans.PropertyVetoException) WebListener(org.glassfish.embeddable.web.WebListener) HttpService(com.sun.enterprise.config.serverbeans.HttpService) org.jvnet.hk2.config(org.jvnet.hk2.config) NetworkListener(org.glassfish.grizzly.config.dom.NetworkListener)

Example 3 with ConfigException

use of org.glassfish.embeddable.web.ConfigException in project Payara by payara.

the class WebContainerImpl method addWebListener.

private void addWebListener(WebListener webListener, String vsId) throws ConfigException, GlassFishException {
    if (!initialized) {
        init();
    }
    if (getWebListener(webListener.getId()) != null) {
        throw new ConfigException("Connector with name '" + webListener.getId() + "' already exsits");
    }
    listenerName = webListener.getId();
    try {
        Ports ports = habitat.getService(Ports.class);
        Port port = ports.createPort(webListener.getPort());
        bind(port, webListener, vsId);
    } catch (java.io.IOException ex) {
        throw new ConfigException(ex);
    }
    webListener.setWebContainer(this);
    if (log.isLoggable(Level.INFO)) {
        log.info("Added connector " + webListener.getId() + " port " + webListener.getPort() + " to virtual server " + vsId);
    }
}
Also used : Port(org.glassfish.internal.embedded.Port) Ports(org.glassfish.internal.embedded.Ports) ConfigException(org.glassfish.embeddable.web.ConfigException)

Aggregations

ConfigException (org.glassfish.embeddable.web.ConfigException)3 GlassFishException (org.glassfish.embeddable.GlassFishException)2 ApplicationRef (com.sun.enterprise.config.serverbeans.ApplicationRef)1 Applications (com.sun.enterprise.config.serverbeans.Applications)1 HttpService (com.sun.enterprise.config.serverbeans.HttpService)1 Application (com.sun.enterprise.deployment.Application)1 PlainTextActionReporter (com.sun.enterprise.v3.common.PlainTextActionReporter)1 VirtualServerFacade (com.sun.enterprise.web.VirtualServerFacade)1 PropertyVetoException (java.beans.PropertyVetoException)1 File (java.io.File)1 IOException (java.io.IOException)1 ServerEnvironment (org.glassfish.api.admin.ServerEnvironment)1 DeployCommandParameters (org.glassfish.api.deployment.DeployCommandParameters)1 UndeployCommandParameters (org.glassfish.api.deployment.UndeployCommandParameters)1 ArchiveHandler (org.glassfish.api.deployment.archive.ArchiveHandler)1 ReadableArchive (org.glassfish.api.deployment.archive.ReadableArchive)1 ApplicationConfigInfo (org.glassfish.deployment.common.ApplicationConfigInfo)1 DeploymentContextImpl (org.glassfish.deployment.common.DeploymentContextImpl)1 VirtualServer (org.glassfish.embeddable.web.VirtualServer)1 WebListener (org.glassfish.embeddable.web.WebListener)1