示例#1
0
  @Test
  public void test() throws Exception {

    GlassFishProperties props = new GlassFishProperties();
    props.setPort("http-listener", 8080);
    GlassFish glassfish = GlassFishRuntime.bootstrap().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();
  }
示例#2
0
  public void execute(String... args) throws GlassFishException, IOException {
    CmdLineParser parser = new CmdLineParser(this);
    parser.setUsageWidth(80);
    try {
      parser.parseArgument(args);
    } catch (CmdLineException e) {
      // ignore
    }

    // show help, if requested
    if (help) {
      System.err.println("java -jar [jarfile] [options...] arguments...");
      parser.printUsage(System.err);
      System.err.println();
      parser.printUsage(System.err);
    } else {
      logger.info("Initiliazing BITalino server..");
      // set-up embedded Glassfish instance
      GlassFishProperties gfProperties = new GlassFishProperties();
      final String domainXmlPath = new File("server/target/classes/domain.xml").toURI().toString();
      logger.info("domain.xml -> " + domainXmlPath);
      gfProperties.setConfigFileURI(domainXmlPath);
      gfProperties.setPort("admin-listener", portAdmin);
      gfProperties.setPort("http-listener-1", port);
      final GlassFish server = GlassFishRuntime.bootstrap().newGlassFish(gfProperties);

      // register shutdown hook
      Runtime.getRuntime()
          .addShutdownHook(
              new Thread(
                  new Runnable() {
                    @Override
                    public void run() {
                      logger.info("Stopping server..");
                      try {
                        server.stop();
                      } catch (GlassFishException e) {
                        logger.severe(e.getMessage());
                      }
                    }
                  },
                  "shutdownHook"));

      // run
      try {
        server.start();

        // for some unknown reason, this doesn't work when in domain.xml
        server
            .getCommandRunner()
            .run("create-jdbc-resource", "--connectionpoolid", "PgPool", "jdbc/DS");

        // create WAR
        ScatteredArchive archive = new ScatteredArchive("bitalino", ScatteredArchive.Type.WAR);
        archive.addClassPath(new File("model/target", "classes"));
        archive.addMetadata(new File("model/src/main/resources/META-INF", "persistence.xml"));
        archive.addClassPath(new File("rest/target", "classes"));
        archive.addMetadata(new File("rest/src/main/webapp/WEB-INF", "web.xml"));

        // deploy the scattered web archive.
        server.getDeployer().deploy(archive.toURI(), "--contextroot=/bitalino");

        logger.info("Press CTRL^C to exit..");
        Thread.currentThread().join();
      } catch (Exception e) {
        logger.severe(e.getMessage());
      }
    }
  }