Exemplo n.º 1
0
  public static void main(String[] args) /* throws GlassFishException */ {
    logger.info(String.format("Launching %s...", APPLICATION_NAME));

    // Determine installation path
    File installationPath =
        new File(Launcher.class.getProtectionDomain().getCodeSource().getLocation().getPath());
    String installationDirectoryPath =
        (installationPath.isDirectory() ? installationPath : installationPath.getParentFile())
            .getAbsolutePath();

    logger.info(String.format("Installation directory path: %s", installationDirectoryPath));

    logger.info("Starting Java DB (Apache Derby)...");
    NetworkServerControl serverControl;
    try {
      serverControl = new NetworkServerControl(InetAddress.getByName("localhost"), 1527);
      serverControl.start(null);
    } catch (Exception e) {
      throw new RuntimeException("Cannot start Java DB (Apache Derby)!", e);
    }

    logger.info(String.format("Creating database %s...", DATABASE_NAME));
    String driver = "org.apache.derby.jdbc.ClientDriver";
    String connectionURL =
        "jdbc:derby:" + /* installationDirectoryPath + "\\" + */ DATABASE_NAME + ";create=true";
    Connection connection = null;
    try {
      Class.forName(driver);
    } catch (java.lang.ClassNotFoundException e) {
      throw new RuntimeException(e);
    }
    try {
      connection = DriverManager.getConnection(connectionURL);
    } catch (Throwable e) {
      throw new RuntimeException(e);
    } finally {
      try {
        connection.close();
      } catch (SQLException e) {
        // Ignore exception
      }
    }

    logger.info("Starting Oracle Glassfish Embedded and deploying application...");
    GlassFishProperties glassfishProperties = new GlassFishProperties();
    String configFilePath = installationDirectoryPath + "\\" + GLASSFISH_CONFIG_FILE_NAME;
    String configFileURI = new File(configFilePath).toURI().toString();
    logger.info(String.format("Configuration file URI: %s", configFileURI));
    glassfishProperties.setConfigFileURI(configFileURI);
    glassfishProperties.setConfigFileReadOnly(true);

    try {
      GlassFishRuntime glassfishRuntime = GlassFishRuntime.bootstrap();
      GlassFish glassfish = glassfishRuntime.newGlassFish(glassfishProperties);
      glassfish.start();

      // Deploy plenus Server package to GlassFish
      File applicationPackage =
          new File(installationDirectoryPath + "\\" + APPLICATION_PACKAGE_FILE_NAME);
      Deployer deployer = glassfish.getDeployer();
      deployer.deploy(
          applicationPackage); // Can be invoked instead the variant above because other parameters
                               // are optional.

      System.out.println(String.format("Press <<Enter>> to stop the %s...", APPLICATION_NAME));
      // Wait for <<Enter>>
      try {
        new BufferedReader(new java.io.InputStreamReader(System.in)).readLine();
      } catch (IOException e) {
        // Ignore exception
      }

      // Teardown GlassFish
      glassfish.dispose();
      glassfishRuntime.shutdown();
    } catch (GlassFishException e) {
      throw new RuntimeException("Glassfish error!", e);
    }

    logger.info("Stopping Java DB (Apache Derby)...");
    try {
      serverControl.shutdown();
    } catch (Exception e) {
      throw new RuntimeException("Cannot stop Java DB (Apache Derby)!", e);
    }
  }
Exemplo n.º 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());
      }
    }
  }
Exemplo n.º 3
0
  /**
   * Boots the Payara Micro Server. All parameters are checked at this point
   *
   * @return An instance of PayaraMicroRuntime that can be used to access the running server
   * @throws BootstrapException
   */
  public PayaraMicroRuntime bootStrap() throws BootstrapException {
    if (runtime != null) {
      throw new IllegalStateException(
          "Payara Micro is already running, calling bootstrap now is meaningless");
    }
    // check hazelcast cluster overrides
    MulticastConfiguration mc = new MulticastConfiguration();
    mc.setMemberName(instanceName);
    if (hzPort > Integer.MIN_VALUE) {
      mc.setMulticastPort(hzPort);
    }

    if (hzStartPort > Integer.MIN_VALUE) {
      mc.setStartPort(hzStartPort);
    }

    if (hzMulticastGroup != null) {
      mc.setMulticastGroup(hzMulticastGroup);
    }

    if (alternateHZConfigFile != null) {
      mc.setAlternateConfiguration(alternateHZConfigFile);
    }
    HazelcastCore.setMulticastOverride(mc);

    setSystemProperties();
    BootstrapProperties bprops = new BootstrapProperties();
    GlassFishRuntime gfruntime;
    PortBinder portBinder = new PortBinder();

    try {
      gfruntime =
          GlassFishRuntime.bootstrap(bprops, Thread.currentThread().getContextClassLoader());
      GlassFishProperties gfproperties = new GlassFishProperties();

      if (httpPort != Integer.MIN_VALUE) {
        // If httpAutoBind is set to true
        if (autoBindHttp == true) {
          // Search for an available port and set it as the http port
          try {
            gfproperties.setPort(
                "http-listener", portBinder.findAvailablePort(httpPort, autoBindRange));
          }

          // If no available port is found, log an exception and throw a GlassFish Exception to fail
          // the bootstrap
          catch (BindException ex) {
            Logger.getLogger(PayaraMicro.class.getName())
                .log(
                    Level.SEVERE,
                    "No available port found in range: "
                        + httpPort
                        + " - "
                        + (httpPort + autoBindRange),
                    ex);

            throw new GlassFishException("Could not bind HTTP port");
          }
        }

        // If httpAutoBind is not set to true...
        else {
          // Set the port as normal
          gfproperties.setPort("http-listener", httpPort);
        }

      }

      // If the port has not been set
      else {
        // If httpAutoBind is set to true
        if (autoBindHttp == true) {
          // Search for an available port and set it as the http port
          try {
            gfproperties.setPort(
                "http-listener", portBinder.findAvailablePort(8080, autoBindRange));
          }

          // If no available port is found, log an exception and throw a GlassFish Exception to fail
          // the bootstrap
          catch (BindException ex) {
            Logger.getLogger(PayaraMicro.class.getName())
                .log(
                    Level.SEVERE,
                    "No available port found in range: " + 8080 + " - " + (8080 + autoBindRange),
                    ex);

            throw new GlassFishException("Could not bind HTTP port");
          }
        }
      }

      if (sslPort != Integer.MIN_VALUE) {
        // If sslAutoBind is set to true
        if (autoBindSsl == true) {
          // Search for an available port and set it as the ssl port
          try {
            gfproperties.setPort(
                "https-listener", portBinder.findAvailablePort(sslPort, autoBindRange));
          } catch (BindException ex) {
            Logger.getLogger(PayaraMicro.class.getName())
                .log(
                    Level.SEVERE,
                    "No available port found in range: "
                        + sslPort
                        + " - "
                        + (sslPort + autoBindRange),
                    ex);

            throw new GlassFishException("Could not bind SSL port");
          }
        }

        // If sslAutoBind is not set to true...
        else {
          // Set the port as normal
          gfproperties.setPort("https-listener", sslPort);
        }
      }

      if (alternateDomainXML != null) {
        gfproperties.setConfigFileReadOnly(false);
        gfproperties.setConfigFileURI(
            "file:///" + alternateDomainXML.getAbsolutePath().replace('\\', '/'));
      } else {
        if (noCluster) {
          gfproperties.setConfigFileURI(
              Thread.currentThread()
                  .getContextClassLoader()
                  .getResource("microdomain-nocluster.xml")
                  .toExternalForm());

        } else {
          gfproperties.setConfigFileURI(
              Thread.currentThread()
                  .getContextClassLoader()
                  .getResource("microdomain.xml")
                  .toExternalForm());
        }
      }

      if (rootDir != null) {
        gfproperties.setInstanceRoot(rootDir.getAbsolutePath());
        File configFile =
            new File(
                rootDir.getAbsolutePath()
                    + File.separator
                    + "config"
                    + File.separator
                    + "domain.xml");
        if (!configFile.exists()) {
          installFiles(gfproperties);
        } else {
          String absolutePath = rootDir.getAbsolutePath();
          absolutePath = absolutePath.replace('\\', '/');
          gfproperties.setConfigFileURI("file:///" + absolutePath + "/config/domain.xml");
          gfproperties.setConfigFileReadOnly(false);
        }
      }

      if (this.maxHttpThreads != Integer.MIN_VALUE) {
        gfproperties.setProperty(
            "embedded-glassfish-config.server.thread-pools.thread-pool.http-thread-pool.max-thread-pool-size",
            Integer.toString(maxHttpThreads));
      }

      if (this.minHttpThreads != Integer.MIN_VALUE) {
        gfproperties.setProperty(
            "embedded-glassfish-config.server.thread-pools.thread-pool.http-thread-pool.min-thread-pool-size",
            Integer.toString(minHttpThreads));
      }

      gf = gfruntime.newGlassFish(gfproperties);

      // reset logger.

      // reset the Log Manager
      File configDir = new File(System.getProperty("com.sun.aas.instanceRoot"), "config");
      File loggingProperties = new File(configDir.getAbsolutePath(), "logging.properties");
      if (loggingProperties.exists() && loggingProperties.canRead() && loggingProperties.isFile()) {
        System.setProperty("java.util.logging.config.file", loggingProperties.getAbsolutePath());
        try {
          LogManager.getLogManager().readConfiguration();
        } catch (IOException | SecurityException ex) {
          logger.log(Level.SEVERE, null, ex);
        }
      }
      gf.start();
      this.runtime = new PayaraMicroRuntime(instanceName, gf);
      deployAll();
      return runtime;
    } catch (GlassFishException ex) {
      throw new BootstrapException(ex.getMessage(), ex);
    }
  }