Search in sources :

Example 1 with GlassFishException

use of org.glassfish.embeddable.GlassFishException in project Payara by payara.

the class EmbeddedTest method testEjb.

@Test
public void testEjb() throws GlassFishException {
    Deployer deployer = glassfish.getDeployer();
    URL source = SampleEjb.class.getClassLoader().getResource("org/glassfish/distributions/test/ejb/SampleEjb.class");
    String p = source.getPath().substring(0, source.getPath().length() - "org/glassfish/distributions/test/ejb/SimpleEjb.class".length());
    String appName = deployer.deploy(new File(p).toURI(), "--name=sample");
    Assert.assertNotNull("AppName is null from deployer of type " + deployer.getClass().getName(), appName);
    // ok now let's look up the EJB...
    try {
        InitialContext ic = new InitialContext();
        SampleEjb ejb = (SampleEjb) ic.lookup("java:global/sample/SampleEjb");
        if (ejb != null) {
            try {
                System.out.println(ejb.saySomething());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    } catch (NamingException e) {
        // To change body of catch statement use File | Settings | File Templates.
        e.printStackTrace();
    }
    deployer.undeploy(appName);
    System.out.println("Done with EJB");
}
Also used : NamingException(javax.naming.NamingException) SampleEjb(org.glassfish.distributions.test.ejb.SampleEjb) File(java.io.File) Deployer(org.glassfish.embeddable.Deployer) URL(java.net.URL) InitialContext(javax.naming.InitialContext) GlassFishException(org.glassfish.embeddable.GlassFishException) NamingException(javax.naming.NamingException) Test(org.junit.Test)

Example 2 with GlassFishException

use of org.glassfish.embeddable.GlassFishException in project Payara by payara.

the class EmbeddedTest method testWeb.

@Test
public void testWeb() throws GlassFishException {
    System.out.println("Starting testWeb " + glassfish);
    Deployer deployer = glassfish.getDeployer();
    URL source = SampleEjb.class.getClassLoader().getResource("org/glassfish/distributions/test/web/WebHello.class");
    String p = source.getPath().substring(0, source.getPath().length() - "org/glassfish/distributions/test/web/WebHello.class".length());
    File path = new File(p).getParentFile().getParentFile();
    String name = null;
    if (path.getName().lastIndexOf('.') != -1) {
        name = path.getName().substring(0, path.getName().lastIndexOf('.'));
    } else {
        name = path.getName();
    }
    System.out.println("Deploying " + path + ", name = " + name);
    String appName = deployer.deploy(path.toURI(), "--name=" + name);
    System.out.println("Deployed " + appName);
    Assert.assertTrue(appName != null);
    try {
        URL servlet = new URL("http://localhost:8080/test-classes/hello");
        URLConnection yc = servlet.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
        String inputLine = in.readLine();
        if (inputLine != null) {
            System.out.println(inputLine);
        }
        Assert.assertNotNull(inputLine);
        Assert.assertEquals(inputLine.trim(), "Hello World !");
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    // do not throw the exception for now, because this may break the build if,
    // for example, another instance of glassfish is running on 8080
    // throw e;
    }
    if (appName != null) {
        deployer.undeploy(appName);
        System.out.println("Undeployed " + appName);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) SampleEjb(org.glassfish.distributions.test.ejb.SampleEjb) File(java.io.File) Deployer(org.glassfish.embeddable.Deployer) URL(java.net.URL) URLConnection(java.net.URLConnection) GlassFishException(org.glassfish.embeddable.GlassFishException) NamingException(javax.naming.NamingException) Test(org.junit.Test)

Example 3 with GlassFishException

use of org.glassfish.embeddable.GlassFishException 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 4 with GlassFishException

use of org.glassfish.embeddable.GlassFishException in project Payara by payara.

the class PayaraContainer method deploy.

@Override
public ProtocolMetaData deploy(Archive<?> archive) throws DeploymentException {
    String deploymentName = createDeploymentName(archive.getName());
    try {
        glassfish.getDeployer().deploy(toURL(archive).toURI(), "--name", deploymentName);
    } catch (Exception e) {
        throw new DeploymentException("Could not deploy " + archive.getName(), e);
    }
    try {
        HTTPContext httpContext = new HTTPContext(ADDRESS, bindHttpPort);
        findServlets(httpContext, resolveWebArchiveNames(archive));
        return new ProtocolMetaData().addContext(httpContext);
    } catch (GlassFishException e) {
        throw new DeploymentException("Could not probe Payara embedded for environment", e);
    }
}
Also used : GlassFishException(org.glassfish.embeddable.GlassFishException) DeploymentException(org.jboss.arquillian.container.spi.client.container.DeploymentException) HTTPContext(org.jboss.arquillian.container.spi.client.protocol.metadata.HTTPContext) ProtocolMetaData(org.jboss.arquillian.container.spi.client.protocol.metadata.ProtocolMetaData) NamingException(javax.naming.NamingException) GlassFishException(org.glassfish.embeddable.GlassFishException) LifecycleException(org.jboss.arquillian.container.spi.client.container.LifecycleException) DeploymentException(org.jboss.arquillian.container.spi.client.container.DeploymentException)

Example 5 with GlassFishException

use of org.glassfish.embeddable.GlassFishException in project Payara by payara.

the class UberJarOSGiGlassFishRuntimeBuilder method destroy.

public void destroy() throws GlassFishException {
    if (framework != null) {
        try {
            framework.stop();
            framework.waitForStop(0);
            logger.info("EmbeddedOSGIRuntimeBuilder.destroy, stopped framework " + framework);
        } catch (InterruptedException ex) {
            throw new GlassFishException(ex);
        } catch (BundleException ex) {
            throw new GlassFishException(ex);
        }
    } else {
        logger.finer("EmbeddedOSGIRuntimeBuilder.destroy called");
    }
}
Also used : GlassFishException(org.glassfish.embeddable.GlassFishException) BundleException(org.osgi.framework.BundleException)

Aggregations

GlassFishException (org.glassfish.embeddable.GlassFishException)25 File (java.io.File)7 IOException (java.io.IOException)5 URL (java.net.URL)5 GlassFishProperties (org.glassfish.embeddable.GlassFishProperties)5 Properties (java.util.Properties)4 NamingException (javax.naming.NamingException)4 ActionReport (org.glassfish.api.ActionReport)3 BootstrapProperties (org.glassfish.embeddable.BootstrapProperties)3 BundleException (org.osgi.framework.BundleException)3 ModulesRegistry (com.sun.enterprise.module.ModulesRegistry)2 ModuleStartup (com.sun.enterprise.module.bootstrap.ModuleStartup)2 StartupContext (com.sun.enterprise.module.bootstrap.StartupContext)2 BindException (java.net.BindException)2 MalformedURLException (java.net.MalformedURLException)2 URISyntaxException (java.net.URISyntaxException)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 CommandException (org.glassfish.api.admin.CommandException)2