@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(); }
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()); } } }
/** * 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); } }