Search in sources :

Example 1 with BootstrapProperties

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

the class Util method startGlassFish.

public static synchronized GlassFish startGlassFish(String serverID, String installRoot, String instanceRoot, String configFileURI, boolean configFileReadOnly, int httpPort) throws GlassFishException {
    GlassFish glassfish = gfMap.get(serverID);
    if (glassfish != null) {
        return glassfish;
    }
    if (glassfishRuntime == null) {
        BootstrapProperties bootstrapProperties = new BootstrapProperties();
        if (installRoot != null) {
            bootstrapProperties.setInstallRoot(installRoot);
        }
        glassfishRuntime = GlassFishRuntime.bootstrap(bootstrapProperties);
    }
    GlassFishProperties glassfishProperties = new GlassFishProperties();
    if (instanceRoot != null) {
        glassfishProperties.setInstanceRoot(instanceRoot);
    }
    if (configFileURI != null) {
        glassfishProperties.setConfigFileURI(configFileURI);
        glassfishProperties.setConfigFileReadOnly(configFileReadOnly);
    }
    if (instanceRoot == null && configFileURI == null) {
        // only set port if embedded domain.xml is used
        if (httpPort != -1) {
            glassfishProperties.setPort("http-listener", httpPort);
        }
    }
    glassfish = glassfishRuntime.newGlassFish(glassfishProperties);
    glassfish.start();
    gfMap.put(serverID, glassfish);
    System.out.println("Started GlassFish [" + serverID + "]");
    return glassfish;
}
Also used : BootstrapProperties(org.glassfish.embeddable.BootstrapProperties) GlassFish(org.glassfish.embeddable.GlassFish) GlassFishProperties(org.glassfish.embeddable.GlassFishProperties)

Example 2 with BootstrapProperties

use of org.glassfish.embeddable.BootstrapProperties 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 3 with BootstrapProperties

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

the class PayaraMicroImpl method bootStrap.

/**
 * 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
 */
@Override
public PayaraMicroRuntime bootStrap() throws BootstrapException {
    // First check whether we are already running
    if (isRunning()) {
        throw new IllegalStateException("Payara Micro is already running, calling bootstrap now is meaningless");
    }
    long start = System.currentTimeMillis();
    // Build the runtime directory
    try {
        unPackRuntime();
    } catch (IOException | URISyntaxException ex) {
        throw new BootstrapException("Problem unpacking the Runtime", ex);
    }
    resetLogging();
    // build the runtime
    BootstrapProperties bprops = new BootstrapProperties();
    bprops.setInstallRoot(runtimeDir.getDirectory().getAbsolutePath());
    bprops.setProperty(Constants.PLATFORM_PROPERTY_KEY, Constants.Platform.PayaraMicro.toString());
    GlassFishRuntime gfruntime;
    try {
        gfruntime = GlassFishRuntime.bootstrap(bprops, Thread.currentThread().getContextClassLoader());
        GlassFishProperties gfproperties = new GlassFishProperties();
        gfproperties.setProperty("-type", "MICRO");
        gfproperties.setInstanceRoot(runtimeDir.getDirectory().getAbsolutePath());
        gfproperties.setConfigFileReadOnly(false);
        gfproperties.setConfigFileURI(runtimeDir.getDomainXML().toURI().toString());
        try {
            configureCommandFiles();
        } catch (IOException ex) {
            LOGGER.log(Level.SEVERE, "Unable to load command file", ex);
        }
        gf = gfruntime.newGlassFish(gfproperties);
        configurePorts();
        configureThreads();
        configureAccessLogging();
        configureHazelcast();
        configurePhoneHome();
        configureNotificationService();
        configureHealthCheck();
        configureRequestTracingService();
        configureSecrets();
        // Add additional libraries
        addLibraries();
        // boot the server
        preBootCommands.executeCommands(gf.getCommandRunner());
        HazelcastCore.setThreadLocalDisabled(true);
        try {
            gf.start();
            // Attempt paranoid unbinding of all reserved ports.
            MicroNetworkListener.clearReservedSockets();
            // Execute post boot commands
            postBootCommands.executeCommands(gf.getCommandRunner());
            this.runtime = new PayaraMicroRuntimeImpl(gf, gfruntime);
            // load all applications, but do not start them until Hazelcast gets a chance to initialize
            deployAll();
        } catch (IOException ex) {
            LOGGER.log(Level.SEVERE, "Error unbinding reserved port.", ex);
        } finally {
            HazelcastCore.setThreadLocalDisabled(false);
        }
        if (!noCluster) {
            gf.getCommandRunner().run("set-hazelcast-configuration", "--enabled", "true", "--dynamic", "true", "--target", "server-config", "--hostawarepartitioning", Boolean.toString(hostAware), "--lite", Boolean.toString(liteMember));
        }
        gf.getCommandRunner().run("initialize-all-applications");
        postDeployCommands.executeCommands(gf.getCommandRunner());
        long end = System.currentTimeMillis();
        dumpFinalStatus(end - start);
        return runtime;
    } catch (Exception ex) {
        try {
            gf.dispose();
        } catch (GlassFishException ex1) {
            LOGGER.log(Level.SEVERE, null, ex1);
        }
        throw new BootstrapException(ex.getMessage(), ex);
    }
}
Also used : GlassFishException(org.glassfish.embeddable.GlassFishException) BootstrapProperties(org.glassfish.embeddable.BootstrapProperties) GlassFishRuntime(org.glassfish.embeddable.GlassFishRuntime) BootstrapException(fish.payara.micro.BootstrapException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) GlassFishProperties(org.glassfish.embeddable.GlassFishProperties) URISyntaxException(java.net.URISyntaxException) FileNotFoundException(java.io.FileNotFoundException) BootstrapException(fish.payara.micro.BootstrapException) BindException(java.net.BindException) GlassFishException(org.glassfish.embeddable.GlassFishException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ValidationException(fish.payara.micro.cmd.options.ValidationException)

Example 4 with BootstrapProperties

use of org.glassfish.embeddable.BootstrapProperties 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 BootstrapProperties

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

the class UberJarOSGiGlassFishRuntimeBuilder method build.

public GlassFishRuntime build(BootstrapProperties bsOptions) throws GlassFishException {
    // Get all the properties in the Bootstrap options and then manipulate the Properties object.
    Properties props = bsOptions.getProperties();
    String uberJarURI = bsOptions.getProperties().getProperty(UBER_JAR_URI);
    logger.finer("EmbeddedOSGIRuntimeBuilder.build, uberJarUri = " + uberJarURI);
    URI jar = null;
    try {
        jar = uberJarURI != null ? new URI(uberJarURI) : Util.whichJar(GlassFishRuntime.class);
    } catch (URISyntaxException ex) {
        logger.log(Level.SEVERE, null, ex);
    }
    // XXX : Commented out by Prasad , we are again looking for instance root here. Why ?
    // String instanceRoot = props.getProperty(Constants.INSTALL_ROOT_PROP_NAME);
    String installRoot = bsOptions.getInstallRoot();
    if (installRoot == null) {
        installRoot = getDefaultInstallRoot();
        props.setProperty(BootstrapProperties.INSTALL_ROOT_PROP_NAME, installRoot);
        props.setProperty(BootstrapProperties.INSTALL_ROOT_URI_PROP_NAME, new File(installRoot).toURI().toString());
    }
    // XXX : Assuming that this property will be set along with Bootstrap options.
    // This is a temporary hack, we need to separate the properties out between bootstrap and newGlassfish methods clearly
    // and not mix them in the code.
    String instanceRoot = props.getProperty(GlassFishProperties.INSTANCE_ROOT_PROP_NAME);
    if (instanceRoot == null) {
        instanceRoot = getDefaultInstanceRoot();
        props.setProperty(GlassFishProperties.INSTANCE_ROOT_PROP_NAME, instanceRoot);
        props.setProperty(GlassFishProperties.INSTANCE_ROOT_URI_PROP_NAME, new File(instanceRoot).toURI().toString());
    }
    try {
        copyConfigFile(props.getProperty(GlassFishProperties.CONFIG_FILE_URI_PROP_NAME), instanceRoot);
    } catch (Exception ex) {
        throw new GlassFishException(ex);
    }
    String platform = props.getProperty(BootstrapProperties.PLATFORM_PROPERTY_KEY);
    if (platform == null) {
        platform = BootstrapProperties.Platform.Felix.toString();
        props.setProperty(BootstrapProperties.PLATFORM_PROPERTY_KEY, platform);
    }
    // readConfigProperties(installRoot, props);
    // embedded-osgi-main module will need this to extract the modules.
    System.setProperty(UBER_JAR_URI, jar.toString());
    String osgiMainModule = "jar:" + jar.toString() + "!/uber-osgi-main.jar";
    props.setProperty("glassfish.auto.start", osgiMainModule);
    String autoStartBundleLocation = "jar:" + jar.toString() + "!/modules/installroot-builder_jar/," + "jar:" + jar.toString() + "!/modules/instanceroot-builder_jar/," + "jar:" + jar.toString() + // TODO :: was modules/glassfish_jar
    "!/modules/kernel_jar/";
    if (isOSGiEnv()) {
        autoStartBundleLocation = autoStartBundleLocation + ",jar:" + jar.toString() + "!/modules/osgi-modules-uninstaller_jar/";
    }
    props.setProperty(AUTO_START_BUNDLES_PROP, autoStartBundleLocation);
    System.setProperty(AUTO_START_BUNDLES_PROP, autoStartBundleLocation);
    System.setProperty(BootstrapProperties.INSTALL_ROOT_PROP_NAME, installRoot);
    System.setProperty(GlassFishProperties.INSTANCE_ROOT_PROP_NAME, instanceRoot);
    props.setProperty("org.osgi.framework.system.packages.extra", "org.glassfish.simpleglassfishapi; version=3.1");
    // props.setProperty(org.osgi.framework.Constants.FRAMEWORK_BUNDLE_PARENT,
    // org.osgi.framework.Constants.FRAMEWORK_BUNDLE_PARENT_FRAMEWORK);
    // props.setProperty("org.osgi.framework.bootdelegation", "org.jvnet.hk2.component, " +
    // "org.jvnet.hk2.component.*," +
    // "org.jvnet.hk2.annotations," +
    // "org.jvnet.hk2.annotations.*");
    // props.setProperty("org.osgi.framework.bootdelegation", "*");
    props.setProperty("org.osgi.framework.storage", instanceRoot + "/osgi-cache/Felix");
    // }
    logger.logp(Level.FINER, "EmbeddedOSGIRuntimeBuilder", "build", "Building file system {0}", props);
    try {
        if (!isOSGiEnv()) {
            final OSGiFrameworkLauncher fwLauncher = new OSGiFrameworkLauncher(props);
            framework = fwLauncher.launchOSGiFrameWork();
            return fwLauncher.getService(GlassFishRuntime.class);
        } else {
            BundleContext context = ((BundleReference) (getClass().getClassLoader())).getBundle().getBundleContext();
            Bundle autostartBundle = context.installBundle(props.getProperty("glassfish.auto.start"));
            autostartBundle.start(Bundle.START_TRANSIENT);
            logger.finer("Started autostartBundle " + autostartBundle);
            return getService(GlassFishRuntime.class, context);
        }
    } catch (Throwable t) {
        t.printStackTrace();
        throw new GlassFishException(new Exception(t));
    // return null;
    }
}
Also used : GlassFishException(org.glassfish.embeddable.GlassFishException) Bundle(org.osgi.framework.Bundle) URISyntaxException(java.net.URISyntaxException) OSGiFrameworkLauncher(com.sun.enterprise.glassfish.bootstrap.OSGiFrameworkLauncher) Properties(java.util.Properties) BootstrapProperties(org.glassfish.embeddable.BootstrapProperties) GlassFishProperties(org.glassfish.embeddable.GlassFishProperties) URI(java.net.URI) File(java.io.File) GlassFishException(org.glassfish.embeddable.GlassFishException) URISyntaxException(java.net.URISyntaxException) BundleException(org.osgi.framework.BundleException) BundleContext(org.osgi.framework.BundleContext)

Aggregations

BootstrapProperties (org.glassfish.embeddable.BootstrapProperties)6 GlassFishProperties (org.glassfish.embeddable.GlassFishProperties)5 GlassFishException (org.glassfish.embeddable.GlassFishException)4 File (java.io.File)3 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 Properties (java.util.Properties)2 GlassFish (org.glassfish.embeddable.GlassFish)2 BundleException (org.osgi.framework.BundleException)2 OSGiFrameworkLauncher (com.sun.enterprise.glassfish.bootstrap.OSGiFrameworkLauncher)1 BootstrapException (fish.payara.micro.BootstrapException)1 ValidationException (fish.payara.micro.cmd.options.ValidationException)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 BindException (java.net.BindException)1 MalformedURLException (java.net.MalformedURLException)1 JarFile (java.util.jar.JarFile)1 NamingException (javax.naming.NamingException)1 Deployer (org.glassfish.embeddable.Deployer)1 GlassFishRuntime (org.glassfish.embeddable.GlassFishRuntime)1