Skip to content

Commit

Permalink
Uberjar compilable again
Browse files Browse the repository at this point in the history
- Updated to use the current api
- Some constants have to be public
- If we agree to kill Uber, they can be marked as private again, or even better,
  they should be moved outside - note that some are used as System.properties,
  some don't. So it may need more work.

Signed-off-by: David Matějček <david.matejcek@omnifish.ee>
  • Loading branch information
dmatej committed Jan 24, 2023
1 parent 41ad5fb commit d659e92
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 166 deletions.
Empty file.
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -16,24 +17,25 @@

package org.glassfish.uberjar;

import org.glassfish.embeddable.BootstrapProperties;
import org.glassfish.embeddable.GlassFish;
import org.glassfish.embeddable.GlassFishRuntime;
import com.sun.enterprise.glassfish.bootstrap.Constants;
import com.sun.enterprise.glassfish.bootstrap.Constants.Platform;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Properties;
import java.util.logging.Logger;

import org.glassfish.embeddable.BootstrapProperties;
import org.glassfish.embeddable.GlassFish;
import org.glassfish.embeddable.GlassFishProperties;
import org.glassfish.embeddable.GlassFishRuntime;

/**
*
* This is a main class for 'java -jar glassfish-uber.jar'
*
* @author bhavanishankar@dev.java.net
*/

public class UberJarMain {

private static Logger logger = Logger.getLogger("embedded-glassfish");
Expand Down Expand Up @@ -73,11 +75,12 @@ private void start() throws Exception {

private void privilegedStart() throws Exception {
AccessController.doPrivileged(new PrivilegedAction<Void>() {
@Override
public Void run() {
try {
Properties props = new Properties();
props.setProperty(BootstrapProperties.PLATFORM_PROPERTY_KEY,
System.getProperty(BootstrapProperties.PLATFORM_PROPERTY_KEY, BootstrapProperties.Platform.Felix.toString()));
props.setProperty(Constants.PLATFORM_PROPERTY_KEY,
System.getProperty(Constants.PLATFORM_PROPERTY_KEY, Platform.Felix.name()));

long startTime = System.currentTimeMillis();

Expand Down
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -16,6 +17,14 @@

package org.glassfish.uberjar.activator;

import com.sun.enterprise.glassfish.bootstrap.Constants;
import com.sun.enterprise.glassfish.bootstrap.Constants.Platform;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Properties;
import java.util.logging.Logger;

import org.glassfish.embeddable.BootstrapProperties;
import org.glassfish.embeddable.GlassFish;
import org.glassfish.embeddable.GlassFishProperties;
Expand All @@ -24,11 +33,6 @@
import org.osgi.framework.BundleContext;
import org.osgi.framework.launch.Framework;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Properties;
import java.util.logging.Logger;

/**
* This is an activator to allow just dropping the uber jar
* into a running OSGi environment.
Expand All @@ -46,6 +50,7 @@ public class UberJarGlassFishActivator implements BundleActivator {
"org.glassfish.embedded.osgimain.autostartBundles";


@Override
public void start(BundleContext bundleContext) throws Exception {
privilegedStart(bundleContext);

Expand Down Expand Up @@ -86,10 +91,11 @@ public void start(BundleContext bundleContext) throws Exception {

private void privilegedStart(final BundleContext bundleContext) throws Exception {
AccessController.doPrivileged(new PrivilegedAction<Void>() {
@Override
public Void run() {
try {
Properties props = new Properties();
props.setProperty(BootstrapProperties.PLATFORM_PROPERTY_KEY, BootstrapProperties.Platform.Felix.toString());
props.setProperty(Constants.PLATFORM_PROPERTY_KEY, Platform.Felix.name());

logger.info("ThreadContextClassLoader = " + Thread.currentThread().getContextClassLoader() +
", classloader = " + getClass().getClassLoader());
Expand Down Expand Up @@ -128,6 +134,7 @@ public Void run() {
});
}

@Override
public void stop(BundleContext bundleContext) throws Exception {
logger.info("EmbeddedGlassFishActivator is stopped");
}
Expand Down
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2022, 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2010, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -16,62 +17,70 @@

package org.glassfish.uberjar.bootstrap;

import com.sun.enterprise.glassfish.bootstrap.OSGiFrameworkLauncher;
import com.sun.enterprise.glassfish.bootstrap.Constants;
import com.sun.enterprise.glassfish.bootstrap.Constants.Platform;
import com.sun.enterprise.glassfish.bootstrap.osgi.OSGiFrameworkLauncher;
import com.sun.enterprise.util.io.FileUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.glassfish.embeddable.BootstrapProperties;
import org.glassfish.embeddable.GlassFishException;
import org.glassfish.embeddable.GlassFishProperties;
import org.glassfish.embeddable.GlassFishRuntime;
import org.glassfish.embeddable.spi.RuntimeBuilder;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.BundleReference;
import org.osgi.framework.launch.Framework;
import org.osgi.util.tracker.ServiceTracker;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URI;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.glassfish.embeddable.GlassFishException;
import org.glassfish.embeddable.spi.RuntimeBuilder;
import static com.sun.enterprise.glassfish.bootstrap.Constants.PLATFORM_PROPERTY_KEY;
import static com.sun.enterprise.util.io.FileUtils.USER_HOME;

/**
* @author bhavanishankar@dev.java.net
*/

public class UberJarOSGiGlassFishRuntimeBuilder implements RuntimeBuilder {

private static Logger logger = Logger.getLogger("embedded-glassfish");

private static final String AUTO_START_BUNDLES_PROP = "org.glassfish.embedded.osgimain.autostartBundles";

private static final String UBER_JAR_URI = "org.glassfish.embedded.osgimain.jarURI";

private Framework framework;

@Override
public boolean handles(BootstrapProperties bsOptions) {
// default is Felix
/* Constants.Platform platform =
Constants.Platform.valueOf(props.getProperty(
Constants.PLATFORM_PROPERTY_KEY, Constants.Platform.Felix.name())); */
BootstrapProperties.Platform platform = BootstrapProperties.Platform.valueOf(bsOptions.getPlatform());
if(platform == null) {
platform = BootstrapProperties.Platform.valueOf(BootstrapProperties.Platform.Felix.name());
Platform platform = Platform.valueOf(bsOptions.getProperty(Constants.PLATFORM_PROPERTY_KEY));
if (platform == null) {
platform = Platform.Felix;
}
logger.finer("platform = " + platform);
logger.log(Level.FINER, "platform = {0}", platform);
// TODO(Sahoo): Add support for generic OSGi platform
switch (platform) {
case Felix:
case Equinox:
case Knopflerfish:
return true;
default:
return false;
}
return false;
}

public void destroy() throws GlassFishException {

if (framework != null) {
if (framework == null) {
logger.finer("EmbeddedOSGIRuntimeBuilder.destroy called, but framework is null.");
} else {
try {
framework.stop();
framework.waitForStop(0);
Expand All @@ -81,26 +90,13 @@ public void destroy() throws GlassFishException {
} catch (BundleException ex) {
throw new GlassFishException(ex);
}
} else {
logger.finer("EmbeddedOSGIRuntimeBuilder.destroy called");
}
}

private static Logger logger = Logger.getLogger("embedded-glassfish");

public static final String AUTO_START_BUNDLES_PROP =
"org.glassfish.embedded.osgimain.autostartBundles";


private static final String UBER_JAR_URI = "org.glassfish.embedded.osgimain.jarURI";

@Override
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);
String uberJarURI = bsOptions.getProperty(UBER_JAR_URI);
logger.log(Level.FINER, "EmbeddedOSGIRuntimeBuilder.build, uberJarUri={0}", uberJarURI);

URI jar = null;
try {
Expand All @@ -115,39 +111,38 @@ public GlassFishRuntime build(BootstrapProperties bsOptions) throws GlassFishExc

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());
bsOptions.setInstallRoot(installRoot);
bsOptions.setProperty(Constants.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);
String instanceRoot = bsOptions.getProperty(Constants.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());
bsOptions.setProperty(Constants.INSTANCE_ROOT_PROP_NAME, instanceRoot);
bsOptions.setProperty(Constants.INSTANCE_ROOT_URI_PROP_NAME, new File(instanceRoot).toURI().toString());
}
FileUtils.ensureWritableDir(new File(instanceRoot));
try {
copyConfigFile(props.getProperty(GlassFishProperties.CONFIG_FILE_URI_PROP_NAME), instanceRoot);
copyConfigFile(bsOptions.getProperty(GlassFishProperties.CONFIG_FILE_URI_PROP_NAME), instanceRoot);
} catch (Exception ex) {
throw new GlassFishException(ex);
}

String platform = props.getProperty(BootstrapProperties.PLATFORM_PROPERTY_KEY);
String platform = bsOptions.getProperty(PLATFORM_PROPERTY_KEY);
if (platform == null) {
platform = BootstrapProperties.Platform.Felix.toString();
props.setProperty(BootstrapProperties.PLATFORM_PROPERTY_KEY, platform);
platform = Platform.Felix.toString();
bsOptions.setProperty(PLATFORM_PROPERTY_KEY, platform);
}

// readConfigProperties(installRoot, props);

System.setProperty(UBER_JAR_URI, jar.toString()); // embedded-osgi-main module will need this to extract the modules.

String osgiMainModule = "jar:" + jar.toString() + "!/uber-osgi-main.jar";
props.setProperty("glassfish.auto.start", osgiMainModule);
bsOptions.setProperty("glassfish.auto.start", osgiMainModule);

String autoStartBundleLocation = "jar:" + jar.toString() + "!/modules/installroot-builder_jar/," +
"jar:" + jar.toString() + "!/modules/instanceroot-builder_jar/," +
Expand All @@ -158,13 +153,13 @@ public GlassFishRuntime build(BootstrapProperties bsOptions) throws GlassFishExc
",jar:" + jar.toString() + "!/modules/osgi-modules-uninstaller_jar/";
}

props.setProperty(AUTO_START_BUNDLES_PROP, autoStartBundleLocation);
bsOptions.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);
System.setProperty(Constants.INSTALL_ROOT_PROP_NAME, installRoot);
System.setProperty(Constants.INSTANCE_ROOT_PROP_NAME, instanceRoot);

props.setProperty("org.osgi.framework.system.packages.extra",
bsOptions.setProperty("org.osgi.framework.system.packages.extra",
"org.glassfish.simpleglassfishapi; version=3.1");

// props.setProperty(org.osgi.framework.Constants.FRAMEWORK_BUNDLE_PARENT,
Expand All @@ -175,41 +170,34 @@ public GlassFishRuntime build(BootstrapProperties bsOptions) throws GlassFishExc
// "org.jvnet.hk2.annotations.*");
// props.setProperty("org.osgi.framework.bootdelegation", "*");

props.setProperty("org.osgi.framework.storage", instanceRoot + "/osgi-cache/Felix");
bsOptions.setProperty("org.osgi.framework.storage", instanceRoot + "/osgi-cache/Felix");
// }

logger.logp(Level.FINER, "EmbeddedOSGIRuntimeBuilder", "build",
"Building file system {0}", props);
logger.logp(Level.FINER, "EmbeddedOSGIRuntimeBuilder", "build", "Building file system {0}", bsOptions);

try {
if (!isOSGiEnv()) {
final OSGiFrameworkLauncher fwLauncher = new OSGiFrameworkLauncher(props);
final OSGiFrameworkLauncher fwLauncher = new OSGiFrameworkLauncher(bsOptions.getProperties());
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);
}
BundleContext context = ((BundleReference) getClass().getClassLoader()).getBundle().getBundleContext();
Bundle autostartBundle = context.installBundle(bsOptions.getProperty("glassfish.auto.start"));
autostartBundle.start(Bundle.START_TRANSIENT);
logger.log(Level.FINER, "Started autostartBundle {0}", autostartBundle);
return getService(GlassFishRuntime.class, context);
} catch (Throwable t) {
t.printStackTrace();
throw new GlassFishException(new Exception(t));
// return null;
}
}

private String getDefaultInstallRoot() {
String userDir = System.getProperty("user.home");
return new File(userDir, ".glassfish7-embedded").getAbsolutePath();
return new File(USER_HOME, ".glassfish7-embedded").getAbsolutePath();
}

private String getDefaultInstanceRoot() {
String userDir = System.getProperty("user.home");
String fs = File.separator;
return new File(userDir, ".glassfish7-embedded" + fs + "domains" + fs + "domain1").getAbsolutePath();
return USER_HOME.toPath().resolve(Path.of(".glassfish7-embedded", "domains", "domain1")).toFile()
.getAbsolutePath();
}

private boolean isOSGiEnv() {
Expand Down
8 changes: 7 additions & 1 deletion appserver/extras/embedded/common/installroot-builder/pom.xml
Expand Up @@ -66,6 +66,12 @@
<artifactId>osgi.core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.main.distributions</groupId>
<artifactId>glassfish</artifactId>
<type>zip</type>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
7 changes: 7 additions & 0 deletions appserver/extras/embedded/common/instanceroot-builder/pom.xml
Expand Up @@ -66,6 +66,13 @@
<artifactId>osgi.core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.main.distributions</groupId>
<artifactId>glassfish</artifactId>
<type>zip</type>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
1 change: 1 addition & 0 deletions appserver/extras/embedded/pom.xml
Expand Up @@ -32,6 +32,7 @@
<name>GlassFish Embedded modules</name>

<modules>
<module>common</module>
<module>all</module>
<module>nucleus</module>
<module>web</module>
Expand Down

0 comments on commit d659e92

Please sign in to comment.