use of org.glassfish.embeddable.GlassFishException in project Payara by payara.
the class WebContainerImpl method addVirtualServer.
/**
* Adds the given <tt>VirtualServer</tt> to this
* <tt>WebContainer</tt>.
*
* <p>If this <tt>WebContainer</tt> has already been started,
* the given <tt>virtualServer</tt> will be started as well.
*
* @param virtualServer the <tt>VirtualServer</tt> to add
*
* @throws ConfigException if a <tt>VirtualServer</tt> with the
* same id has already been registered with this
* <tt>WebContainer</tt>
* @throws GlassFishException if the given <tt>virtualServer</tt> fails
* to be started
*/
public void addVirtualServer(VirtualServer virtualServer) throws ConfigException, GlassFishException {
if (!initialized) {
init();
}
if (log.isLoggable(Level.INFO)) {
log.info("Adding virtual server " + virtualServer.getID());
}
com.sun.enterprise.web.VirtualServer vs = (com.sun.enterprise.web.VirtualServer) engine.findChild(virtualServer.getID());
if (vs != null) {
throw new ConfigException("VirtualServer with id " + virtualServer.getID() + " is already registered");
}
Collection<WebListener> webListeners = virtualServer.getWebListeners();
List<String> names = new ArrayList<String>();
if ((webListeners != null) && (!webListeners.isEmpty())) {
for (WebListener listener : webListeners) {
names.add(listener.getId());
}
} else {
for (NetworkListener networkListener : networkConfig.getNetworkListeners().getNetworkListener()) {
names.add(networkListener.getName());
}
webListeners = listeners;
}
StringBuffer networkListeners = new StringBuffer("");
if (names.size() > 0) {
networkListeners.append(names.get(0));
}
for (int i = 1; i < names.size(); i++) {
networkListeners.append(",");
networkListeners.append(names.get(i));
}
String docRoot = null;
if (virtualServer.getDocRoot() != null) {
docRoot = virtualServer.getDocRoot().getAbsolutePath();
}
String hostName = null;
if (virtualServer.getConfig() != null) {
hostName = virtualServer.getConfig().getHostNames();
}
final String root = docRoot;
final String nl = networkListeners.toString();
final String id = virtualServer.getID();
final String hosts = hostName;
try {
ConfigSupport.apply(new SingleConfigCode<HttpService>() {
public Object run(HttpService param) throws PropertyVetoException, TransactionFailure {
com.sun.enterprise.config.serverbeans.VirtualServer newVirtualServer = param.createChild(com.sun.enterprise.config.serverbeans.VirtualServer.class);
newVirtualServer.setId(id);
newVirtualServer.setNetworkListeners(nl);
if (hosts != null) {
newVirtualServer.setHosts(hosts);
}
Property property = newVirtualServer.createChild(Property.class);
property.setName("docroot");
property.setValue(root);
newVirtualServer.getProperty().add(property);
param.getVirtualServer().add(newVirtualServer);
return newVirtualServer;
}
}, httpService);
} catch (Exception ex) {
throw new GlassFishException(ex);
}
if ((webListeners != null) && (!webListeners.isEmpty())) {
for (WebListener listener : webListeners) {
if (getWebListener(listener.getId()) == null) {
addWebListener(listener, virtualServer.getID());
}
}
}
vs = (com.sun.enterprise.web.VirtualServer) engine.findChild(id);
if (vs != null) {
if (log.isLoggable(Level.INFO)) {
log.info("Added virtual server " + id + " docroot " + docRoot + " networklisteners " + nl);
}
if (virtualServer instanceof VirtualServerFacade) {
((VirtualServerFacade) virtualServer).setVirtualServer(vs);
}
vs.setNetworkListenerNames(names.toArray(new String[names.size()]));
} else {
log.severe("Could not add virtual server " + id);
throw new GlassFishException(new Exception("Cannot add virtual server " + id));
}
}
use of org.glassfish.embeddable.GlassFishException in project Payara by payara.
the class StaticGlassFishRuntime method newGlassFish.
/**
* Creates a new GlassFish instance and add it to a Map of instances
* created by this runtime.
*
* @param glassFishProperties
* @return
* @throws Exception
*/
@Override
public synchronized GlassFish newGlassFish(GlassFishProperties glassFishProperties) throws GlassFishException {
// some code to be executed which may be depending on the environment variable values.
try {
// Don't set temporarily created instanceroot in the user supplied
// GlassFishProperties, hence clone it.
Properties cloned = new Properties();
cloned.putAll(glassFishProperties.getProperties());
final GlassFishProperties gfProps = new GlassFishProperties(cloned);
setEnv(gfProps);
final StartupContext startupContext = new StartupContext(gfProps.getProperties());
ModulesRegistry modulesRegistry = SingleHK2Factory.getInstance().createModulesRegistry();
ServiceLocator serviceLocator = main.createServiceLocator(modulesRegistry, startupContext, Arrays.asList((PopulatorPostProcessor) new EmbeddedInhabitantsParser(), new ContextDuplicatePostProcessor()), null);
final ModuleStartup gfKernel = main.findStartupService(modulesRegistry, serviceLocator, null, startupContext);
// create a new GlassFish instance
GlassFishImpl gfImpl = new GlassFishImpl(gfKernel, serviceLocator, gfProps.getProperties()) {
@Override
public void dispose() throws GlassFishException {
try {
super.dispose();
} finally {
gfMap.remove(gfProps.getInstanceRoot());
if ("true".equalsIgnoreCase(gfProps.getProperties().getProperty(autoDelete)) && gfProps.getInstanceRoot() != null) {
File instanceRoot = new File(gfProps.getInstanceRoot());
if (instanceRoot.exists()) {
// might have been deleted already.
Util.deleteRecursive(instanceRoot);
}
}
}
}
};
// Add this newly created instance to a Map
gfMap.put(gfProps.getInstanceRoot(), gfImpl);
return gfImpl;
} catch (GlassFishException e) {
throw e;
} catch (Exception e) {
throw new GlassFishException(e);
}
}
use of org.glassfish.embeddable.GlassFishException in project Payara by payara.
the class AppServerStartup method doStart.
private void doStart() throws GlassFishException {
if (!run()) {
// startup failed
logger.log(Level.SEVERE, "Failed to start, exiting");
throw new GlassFishException("Server failed to start");
}
// if (appInstanceListener.)
final CountDownLatch latch = new CountDownLatch(1);
// spwan a non-daemon thread that waits indefinitely for shutdown request.
// This stops the VM process from exiting.
serverThread = new Thread("GlassFish Kernel Main Thread") {
@Override
public void run() {
logger.logp(level, "AppServerStartup", "run", "[{0}] started", new Object[] { this });
// notify the other thread to continue now that a non-daemon
// thread has started.
latch.countDown();
synchronized (this) {
while (!shutdownSignal) {
try {
// Wait indefinitely until shutdown is requested
wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
logger.logp(level, "AppServerStartup", "run", "[{0}] exiting", new Object[] { this });
}
};
// by default a thread inherits daemon status of parent thread.
// Since this method can be called by non-daemon threads (e.g.,
// PackageAdmin service in case of an update of bundles), we
// have to explicitly set the daemon status to false.
serverThread.setDaemon(false);
serverThread.start();
// wait until we have spawned a non-daemon thread
try {
latch.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
use of org.glassfish.embeddable.GlassFishException in project Payara by payara.
the class EmbeddedOSGiGlassFishRuntime method newGlassFish.
@Override
public synchronized GlassFish newGlassFish(GlassFishProperties gfProps) throws GlassFishException {
try {
// set env props before updating config, because configuration update may actually trigger
// some code to be executed which may be depending on the environment variable values.
setEnv(gfProps.getProperties());
final StartupContext startupContext = new StartupContext(gfProps.getProperties());
final ServiceTracker hk2Tracker = new ServiceTracker(getBundleContext(), Main.class.getName(), null);
hk2Tracker.open();
final Main main = (Main) hk2Tracker.waitForService(0);
hk2Tracker.close();
final ModulesRegistry mr = ModulesRegistry.class.cast(getBundleContext().getService(getBundleContext().getServiceReference(ModulesRegistry.class.getName())));
ServiceLocator serviceLocator = main.createServiceLocator(mr, startupContext, null, null);
final ModuleStartup gfKernel = main.findStartupService(mr, serviceLocator, null, startupContext);
GlassFish glassFish = createGlassFish(gfKernel, serviceLocator, gfProps.getProperties());
gfs.add(glassFish);
return glassFish;
} catch (BootException ex) {
throw new GlassFishException(ex);
} catch (InterruptedException ex) {
throw new GlassFishException(ex);
}
}
use of org.glassfish.embeddable.GlassFishException 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;
}
}
Aggregations