Search in sources :

Example 16 with Deployer

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

the class BookStoreTest 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-bookstore application.
    File archive = new File(System.getProperty("basedir") + // TODO :: use mvn apis to get the
    "/target/bookstore.war");
    // 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-bookstore 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 + "/bookstore/BookStoreServlet", "Please wait while accessing the bookstore database.....");
        get("http://" + instanceIP + ":" + HTTP_PORT + "/bookstore/BookStoreServlet?title=Advanced+guide+for+developing+PaaS+components&authors=Shalini+M&price=100%24", "Here are the list of books available in our store:");
        get("http://" + instanceIP + ":" + HTTP_PORT + "/bookstore/BookStoreServlet", "Advanced guide for developing PaaS components");
    // 4. Undeploy the Bookstore application .
    } 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) 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 17 with Deployer

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

the class PayaraMicroImpl method deployAll.

private void deployAll() throws GlassFishException {
    // Deploy from within the jar first.
    int deploymentCount = 0;
    Deployer deployer = gf.getDeployer();
    // search and deploy from MICRO-INF/deploy directory.
    // if there is a deployment called ROOT deploy to the root context /
    URL url = this.getClass().getClassLoader().getResource("MICRO-INF/deploy");
    if (url != null) {
        String entryName = "";
        try {
            List<String> microInfEntries = new LinkedList<>();
            JarURLConnection urlcon = (JarURLConnection) url.openConnection();
            JarFile jFile = urlcon.getJarFile();
            Enumeration<JarEntry> entries = jFile.entries();
            while (entries.hasMoreElements()) {
                JarEntry entry = entries.nextElement();
                entryName = entry.getName();
                if (!entry.isDirectory() && !entryName.endsWith(".properties") && !entryName.endsWith(".xml") && !entryName.endsWith(".gitkeep") && entryName.startsWith("MICRO-INF/deploy")) {
                    microInfEntries.add(entryName);
                }
            }
            for (String entry : microInfEntries) {
                File file = new File(entry);
                String contextRoot = file.getName();
                String name = contextRoot.substring(0, contextRoot.length() - 4);
                if (contextRoot.endsWith(".ear") || contextRoot.endsWith(".war") || contextRoot.endsWith(".jar") || contextRoot.endsWith(".rar")) {
                    contextRoot = name;
                }
                if (contextRoot.equals("ROOT")) {
                    contextRoot = "/";
                }
                deployer.deploy(this.getClass().getClassLoader().getResourceAsStream(entry), "--availabilityenabled", "true", "--contextroot", contextRoot, "--name", name, "--force", "true", "--loadOnly", "true");
                deploymentCount++;
            }
        } catch (IOException ioe) {
            LOGGER.log(Level.WARNING, "Could not deploy jar entry {0}", entryName);
        }
    } else {
        LOGGER.info("No META-INF/deploy directory");
    }
    // Deploy command line provided files
    if (deployments != null) {
        for (File war : deployments) {
            if (war.exists() && war.canRead()) {
                if (war.getName().startsWith("ROOT.")) {
                    deployer.deploy(war, "--availabilityenabled=true", "--force=true", "--contextroot=/", "--loadOnly", "true");
                } else {
                    deployer.deploy(war, "--availabilityenabled=true", "--force=true", "--loadOnly", "true");
                }
                deploymentCount++;
            } else {
                LOGGER.log(Level.WARNING, "{0} is not a valid deployment", war.getAbsolutePath());
            }
        }
    }
    // Deploy from deployment directory
    if (deploymentRoot != null) {
        // Get all files in the directory, and sort them by file type
        List<File> deploymentDirEntries = Arrays.asList(deploymentRoot.listFiles());
        deploymentDirEntries.sort(new DeploymentComparator());
        for (File entry : deploymentDirEntries) {
            String entryPath = entry.getAbsolutePath();
            if (entry.isFile() && entry.canRead() && (entryPath.endsWith(".war") || entryPath.endsWith(".ear") || entryPath.endsWith(".jar") || entryPath.endsWith(".rar"))) {
                if (entry.getName().startsWith("ROOT.")) {
                    deployer.deploy(entry, "--availabilityenabled=true", "--force=true", "--contextroot=/", "--loadOnly", "true");
                } else {
                    deployer.deploy(entry, "--availabilityenabled=true", "--force=true", "--loadOnly", "true");
                }
                deploymentCount++;
            }
        }
    }
    // Deploy from URI only called if GAVs provided
    if (GAVs != null) {
        // Convert the provided GAV Strings into target URLs
        getGAVURLs();
        if (!deploymentURLsMap.isEmpty()) {
            for (Map.Entry<String, URL> deploymentMapEntry : deploymentURLsMap.entrySet()) {
                try {
                    // Convert the URL to a URI for use with the deploy method
                    URI artefactURI = deploymentMapEntry.getValue().toURI();
                    deployer.deploy(artefactURI, "--availabilityenabled", "true", "--contextroot", deploymentMapEntry.getKey(), "--force=true", "--loadOnly", "true");
                    deploymentCount++;
                } catch (URISyntaxException ex) {
                    LOGGER.log(Level.WARNING, "{0} could not be converted to a URI," + " artefact will be skipped", deploymentMapEntry.getValue().toString());
                }
            }
        }
    }
    LOGGER.log(Level.INFO, "Deployed {0} archive(s)", deploymentCount);
}
Also used : JarURLConnection(java.net.JarURLConnection) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) URI(java.net.URI) URL(java.net.URL) LinkedList(java.util.LinkedList) JarFile(java.util.jar.JarFile) File(java.io.File) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) Deployer(org.glassfish.embeddable.Deployer)

Example 18 with Deployer

use of org.glassfish.embeddable.Deployer 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/appscoped_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 {
        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 + "/appscoped_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");
        }
    }
}
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 19 with Deployer

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

the class BasicbookstoreDnsPaasTest method runTests.

private void runTests(GlassFish glassfish) throws Exception {
    // 2. Deploy the PaaS application.
    File basicArchive = new File(System.getProperty("basedir") + // TODO :: use mvn apis to
    "/basic_paas_sample.war");
    File bookArchive = new File(System.getProperty("basedir") + "/bookstore.war");
    Assert.assertTrue(basicArchive.exists());
    Assert.assertTrue(bookArchive.exists());
    Deployer deployer = null;
    String firstappName = null;
    String secondappName = null;
    BaseTest firstBaseTest = new BaseTest(glassfish);
    BaseTest secondBaseTest = new BaseTest(glassfish);
    try {
        CreateDNSExternalService();
        CreateLbSharedService(glassfish);
        firstappName = firstBaseTest.deploy(basicArchive, "basic_paas_sample", null);
        System.err.println("Deployed [" + firstappName + "]");
        Assert.assertNotNull(firstappName);
        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.
        firstappName = firstappName.replaceAll("_", "-");
        String HTTP_PORT = (System.getProperty("http.port") != null) ? System.getProperty("http.port") : "28080";
        get("http://" + firstappName + ".hudson.com:" + HTTP_PORT + "/BasicPaaSServlet", "Request headers from the request:");
        secondappName = secondBaseTest.deploy(bookArchive, "bookstore", null);
        System.err.println("Deployed [" + secondappName + "]");
        Assert.assertNotNull(secondappName);
        secondappName = secondappName.replaceAll("_", "-");
        get("http://" + secondappName + ".hudson.com:" + HTTP_PORT + "/BookStoreServlet", "Please wait while accessing the bookstore database.....");
        get("http://" + secondappName + ".hudson.com:" + HTTP_PORT + "/BookStoreServlet?title=Advanced+guide+for+developing+PaaS+components&authors=Shalini+M&price=100%24", "Here are the list of books available in our store:");
        get("http://" + secondappName + ".hudson.com:" + HTTP_PORT + "/BookStoreServlet", "Advanced guide for developing PaaS components");
        get("http://" + firstappName + ".hudson.com:" + HTTP_PORT + "/BasicPaaSServlet", "Request headers from the request:");
        if (secondappName != null) {
            secondBaseTest.undeploy();
            System.err.println("Undeployed [" + secondappName + "]");
        }
        get("http://" + firstappName + ".hudson.com:" + HTTP_PORT + "/BasicPaaSServlet", "Request headers from the request:");
    // 4. Undeploy the PaaS application . TODO :: use cloud-undeploy??
    } finally {
        if (firstappName != null) {
            firstappName = firstappName.replaceAll("-", "_");
            firstBaseTest.undeploy();
            System.err.println("Undeployed [" + firstappName + "]");
            deleteSharedLbandDNSexternalSerivce();
            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 : File(java.io.File) BaseTest(org.glassfish.tests.paas.basetest.BaseTest) CommandRunner(org.glassfish.embeddable.CommandRunner) Deployer(org.glassfish.embeddable.Deployer) CommandResult(org.glassfish.embeddable.CommandResult)

Example 20 with Deployer

use of org.glassfish.embeddable.Deployer 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_teardown_sql.war");
    // TODO :: use mvn apis to get the archive location.
    Assert.assertTrue(archive.exists());
    Deployer deployer = null;
    String appName = null;
    List dbConnectionDetails = 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_teardown_sql/CoffeeServlet", "Coffee ID");
        dbConnectionDetails = getDbConnectionDetails("http://" + instanceIP + ":" + HTTP_PORT + "/basic_db_teardown_sql/DbConnectionDetailsServlet");
    // 4. Undeploy the PaaS application .
    } finally {
        if (appName != null) {
            deployer.undeploy(appName);
            System.err.println("Undeployed [" + appName + "]");
            testTearDownSql(dbConnectionDetails);
            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) ArrayList(java.util.ArrayList) List(java.util.List) GlassFish(org.glassfish.embeddable.GlassFish) 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)

Aggregations

Deployer (org.glassfish.embeddable.Deployer)33 File (java.io.File)32 GlassFish (org.glassfish.embeddable.GlassFish)24 Test (org.junit.Test)24 CommandRunner (org.glassfish.embeddable.CommandRunner)22 CommandResult (org.glassfish.embeddable.CommandResult)21 GlassFishProperties (org.glassfish.embeddable.GlassFishProperties)21 PrintStream (java.io.PrintStream)16 JarFile (java.util.jar.JarFile)13 URI (java.net.URI)6 IOException (java.io.IOException)4 InitialContext (javax.naming.InitialContext)4 GlassFishException (org.glassfish.embeddable.GlassFishException)4 URL (java.net.URL)3 ScatteredArchive (org.glassfish.embeddable.archive.ScatteredArchive)3 HttpListener (org.glassfish.embeddable.web.HttpListener)3 WebContainer (org.glassfish.embeddable.web.WebContainer)3 NamingException (javax.naming.NamingException)2 SampleEjb (org.glassfish.distributions.test.ejb.SampleEjb)2 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)2