Search in sources :

Example 1 with GlassFishProperties

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

the class PayaraContainer method setup.

@Override
public void setup(PayaraConfiguration configuration) {
    this.configuration = configuration;
    BootstrapProperties bootstrapProps = new BootstrapProperties();
    if (configuration.getInstallRoot() != null) {
        bootstrapProps.setInstallRoot(configuration.getInstallRoot());
    }
    try {
        glassfishRuntime = GlassFishRuntime.bootstrap(bootstrapProps);
    } catch (Exception e) {
        throw new RuntimeException("Could not setup GlassFish Embedded Bootstrap", e);
    }
    boolean cleanup = configuration.getCleanup();
    GlassFishProperties serverProps = new GlassFishProperties();
    if (configuration.getInstanceRoot() != null) {
        File instanceRoot = new File(configuration.getInstanceRoot());
        if (instanceRoot.exists()) {
            cleanup = false;
        }
        serverProps.setInstanceRoot(configuration.getInstanceRoot());
        shouldSetPort = false;
    }
    if (configuration.getConfigurationXml() != null) {
        serverProps.setConfigFileURI(configuration.getConfigurationXml());
        shouldSetPort = false;
    }
    serverProps.setConfigFileReadOnly(configuration.isConfigurationReadOnly());
    if (shouldSetPort) {
        bindHttpPort = configuration.getBindHttpPort();
        serverProps.setPort("http-listener", bindHttpPort);
        bindHttpsPort = configuration.getBindHttpsPort();
        serverProps.setPort("https-listener", bindHttpsPort);
    }
    try {
        glassfish = glassfishRuntime.newGlassFish(serverProps);
    } catch (Exception e) {
        throw new RuntimeException("Could not setup GlassFish Embedded Runtime", e);
    }
    if (cleanup) {
        getRuntime().addShutdownHook(new Thread() {

            @Override
            public void run() {
                deleteRecursive(new File(getProperty(INSTANCE_ROOT_PROPERTY)));
            }
        });
    }
}
Also used : BootstrapProperties(org.glassfish.embeddable.BootstrapProperties) File(java.io.File) 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) GlassFishProperties(org.glassfish.embeddable.GlassFishProperties)

Example 2 with GlassFishProperties

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

the class CoffeeTest method test.

@Test
public void test() throws Exception {
    // 1. Bootstrap GlassFish DAS in embedded mode.
    GlassFishProperties glassFishProperties = new GlassFishProperties();
    glassFishProperties.setInstanceRoot(System.getenv("S1AS_HOME") + "/domains/domain1");
    glassFishProperties.setConfigFileReadOnly(false);
    GlassFish glassfish = GlassFishRuntime.bootstrap().newGlassFish(glassFishProperties);
    PrintStream sysout = System.out;
    glassfish.start();
    System.setOut(sysout);
    // 2. Deploy the PaaS application.
    File archive = new File(System.getProperty("basedir") + "/target/basic_db_with_resources_xml.war");
    // TODO :: use mvn apis to get the archive location.
    Assert.assertTrue(archive.exists());
    Deployer deployer = null;
    String appName = null;
    CommandRunner commandRunner = glassfish.getCommandRunner();
    try {
        // 2.1. Create the shared DB service
        CommandResult createSharedServiceResult = commandRunner.run("create-shared-service", "--characteristics", "service-type=Database", "--configuration", "database.name=foobar", "--servicetype", "Database", "coffee-service");
        System.out.println("\ncreate-shared-service command output [ " + createSharedServiceResult.getOutput() + "]");
        // 2.2. List services to check for the shared service
        CommandResult listSharedServicesResult = commandRunner.run("list-services", "--scope", "shared", "--output", "service-name, state");
        System.out.println("\nlist-services command output [ " + listSharedServicesResult.getOutput() + "]");
        // 2.3. Deploy app
        deployer = glassfish.getDeployer();
        appName = deployer.deploy(archive);
        System.err.println("Deployed [" + appName + "]");
        Assert.assertNotNull(appName);
        CommandResult result = commandRunner.run("list-services");
        System.out.println("\nlist-services command output [ " + result.getOutput() + "]");
        // 3. Access the app to make sure PaaS app is correctly provisioned.
        String HTTP_PORT = (System.getProperty("http.port") != null) ? System.getProperty("http.port") : "28080";
        String instanceIP = getLBIPAddress(glassfish);
        get("http://" + instanceIP + ":" + HTTP_PORT + "/basic_db_with_resources_xml/CoffeeServlet", "Coffee ID");
    // 4. Undeploy the PaaS application .
    } finally {
        if (appName != null) {
            deployer.undeploy(appName);
            System.err.println("Undeployed [" + appName + "]");
            System.out.println("Destroying the resources created");
            // 4.1. Delete Shared DB Service.
            CommandResult deleteResult = commandRunner.run("delete-shared-service", "coffee-service");
            System.out.println("\ndelete-shared-service command output [" + deleteResult.getOutput() + "]");
        }
    }
}
Also used : PrintStream(java.io.PrintStream) GlassFish(org.glassfish.embeddable.GlassFish) JarFile(java.util.jar.JarFile) File(java.io.File) CommandRunner(org.glassfish.embeddable.CommandRunner) GlassFishProperties(org.glassfish.embeddable.GlassFishProperties) Deployer(org.glassfish.embeddable.Deployer) CommandResult(org.glassfish.embeddable.CommandResult) Test(org.junit.Test)

Example 3 with GlassFishProperties

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

the class BasicDBPaaSTest method test.

@Test
public void test() throws Exception {
    // 1. Bootstrap GlassFish DAS in embedded mode.
    GlassFishProperties glassFishProperties = new GlassFishProperties();
    glassFishProperties.setInstanceRoot(System.getenv("S1AS_HOME") + "/domains/domain1");
    glassFishProperties.setConfigFileReadOnly(false);
    GlassFish glassfish = GlassFishRuntime.bootstrap().newGlassFish(glassFishProperties);
    PrintStream sysout = System.out;
    glassfish.start();
    System.setOut(sysout);
    // 2. Deploy the PaaS application.
    File archive = new File(System.getProperty("basedir") + // TODO :: use mvn apis
    "/target/basic_db_paas_sample.war");
    // to get the archive
    // location.
    Assert.assertTrue(archive.exists());
    Deployer deployer = null;
    String appName = null;
    try {
        deployer = glassfish.getDeployer();
        appName = deployer.deploy(archive);
        System.err.println("Deployed [" + appName + "]");
        Assert.assertNotNull(appName);
        CommandRunner commandRunner = glassfish.getCommandRunner();
        CommandResult result = commandRunner.run("list-services");
        System.out.println("\nlist-services command output [ " + result.getOutput() + "]");
        // 3. Access the app to make sure PaaS app is correctly provisioned.
        String HTTP_PORT = (System.getProperty("http.port") != null) ? System.getProperty("http.port") : "28080";
        /**
         * Look for a table entry by name SYSFILES in the output to ensure
         * DB is provisioned
         */
        String instanceIP = getLBIPAddress(glassfish);
        get("http://" + instanceIP + ":" + HTTP_PORT + "/basic_db_paas_sample/BasicDBPaaSServlet", "SYSFILES");
    // 4. Undeploy the PaaS-DB application using undeploy.
    } finally {
        if (appName != null) {
            deployer.undeploy(appName);
            System.err.println("Undeployed [" + appName + "]");
            try {
                boolean undeployClean = false;
                CommandResult commandResult = glassfish.getCommandRunner().run("list-services");
                if (commandResult.getOutput().contains("Nothing to list.")) {
                    undeployClean = true;
                }
                Assert.assertTrue(undeployClean);
            } catch (Exception e) {
                System.err.println("Couldn't varify whether undeploy succeeded");
            }
        }
    }
}
Also used : PrintStream(java.io.PrintStream) GlassFish(org.glassfish.embeddable.GlassFish) JarFile(java.util.jar.JarFile) File(java.io.File) CommandRunner(org.glassfish.embeddable.CommandRunner) GlassFishProperties(org.glassfish.embeddable.GlassFishProperties) Deployer(org.glassfish.embeddable.Deployer) IOException(java.io.IOException) CommandResult(org.glassfish.embeddable.CommandResult) Test(org.junit.Test)

Example 4 with GlassFishProperties

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

the class BasicCDITest method test.

@Test
public void test() throws Exception {
    GlassFishProperties props = new GlassFishProperties();
    BootstrapProperties bootstrapProperties = new BootstrapProperties();
    props.setPort("http-listener", 8080);
    GlassFish glassfish = GlassFishRuntime.bootstrap(bootstrapProperties).newGlassFish(props);
    glassfish.start();
    // Test Scattered Web Archive
    ScatteredArchive sa = new ScatteredArchive("cdi_ejb_jpa", ScatteredArchive.Type.WAR, new File("src/main/webapp"));
    sa.addClassPath(new File("target/classes"));
    sa.addClassPath(new File("src/main/resources"));
    URI warURI = sa.toURI();
    printContents(warURI);
    // Deploy archive
    Deployer deployer = glassfish.getDeployer();
    String appname = deployer.deploy(warURI);
    System.out.println("Deployed [" + appname + "]");
    Assert.assertEquals(appname, "cdi_ejb_jpa");
    // Now create a http listener and access the app.
    WebContainer webcontainer = glassfish.getService(WebContainer.class);
    HttpListener listener = new HttpListener();
    listener.setId("my-listener");
    listener.setPort(9090);
    webcontainer.addWebListener(listener);
    get("http://localhost:8080/cdi_ejb_jpa/BasicCDITestServlet", "All CDI beans have been injected.");
    deployer.undeploy(appname);
    glassfish.dispose();
}
Also used : ScatteredArchive(org.glassfish.embeddable.archive.ScatteredArchive) BootstrapProperties(org.glassfish.embeddable.BootstrapProperties) WebContainer(org.glassfish.embeddable.web.WebContainer) HttpListener(org.glassfish.embeddable.web.HttpListener) GlassFish(org.glassfish.embeddable.GlassFish) JarFile(java.util.jar.JarFile) File(java.io.File) URI(java.net.URI) GlassFishProperties(org.glassfish.embeddable.GlassFishProperties) Deployer(org.glassfish.embeddable.Deployer) Test(org.junit.Test)

Example 5 with GlassFishProperties

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

the class MySqlTest method test.

@Test
public void test() throws Exception {
    GlassFishProperties glassFishProperties = new GlassFishProperties();
    glassFishProperties.setPort("http-listener", 8080);
    GlassFish glassFish = GlassFishRuntime.bootstrap().newGlassFish(glassFishProperties);
    glassFish.start();
    Deployer deployer = glassFish.getDeployer();
    String appName = deployer.deploy(new File("target/mysqltest.war"));
    System.out.println("Deployed [" + appName + "]");
    // Access the app
    get("http://localhost:8080/mysqltest/mysqlTestServlet", "connection = ");
    glassFish.dispose();
}
Also used : GlassFish(org.glassfish.embeddable.GlassFish) File(java.io.File) GlassFishProperties(org.glassfish.embeddable.GlassFishProperties) Deployer(org.glassfish.embeddable.Deployer)

Aggregations

GlassFishProperties (org.glassfish.embeddable.GlassFishProperties)35 GlassFish (org.glassfish.embeddable.GlassFish)30 File (java.io.File)24 PrintStream (java.io.PrintStream)21 Deployer (org.glassfish.embeddable.Deployer)21 Test (org.junit.Test)21 CommandRunner (org.glassfish.embeddable.CommandRunner)18 CommandResult (org.glassfish.embeddable.CommandResult)17 JarFile (java.util.jar.JarFile)13 IOException (java.io.IOException)6 BootstrapProperties (org.glassfish.embeddable.BootstrapProperties)6 GlassFishException (org.glassfish.embeddable.GlassFishException)4 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)4 URI (java.net.URI)3 ScatteredArchive (org.glassfish.embeddable.archive.ScatteredArchive)3 HttpListener (org.glassfish.embeddable.web.HttpListener)3 WebContainer (org.glassfish.embeddable.web.WebContainer)3 GlassFishRuntime (org.glassfish.embeddable.GlassFishRuntime)2 ArchiveFactory (com.sun.enterprise.deploy.shared.ArchiveFactory)1 ModulesRegistry (com.sun.enterprise.module.ModulesRegistry)1