Skip to content

Commit

Permalink
Merge pull request OpenLiberty#2 from tjwatson/addComments
Browse files Browse the repository at this point in the history
Add comments to code
  • Loading branch information
tjwatson authored Jan 25, 2018
2 parents 774db65 + cd09e9d commit e024ebc
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ public final class SpringConstants {
public static final String SPRING_START_CLASS_HEADER = "Start-Class";
public static final String SPRING_BOOT_CLASSES_HEADER = "Spring-Boot-Classes";
public static final String SPRING_BOOT_LIB_HEADER = "Spring-Boot-Lib";
public static final String SPRING_BOOT_INVOKE_MAIN = "com.ibm.ws.app.manager.spring.invoke.main";
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
package com.ibm.ws.app.manager.springboot.internal;

import static com.ibm.ws.app.manager.springboot.internal.SpringConstants.SPRING_BOOT_CLASSES_HEADER;
import static com.ibm.ws.app.manager.springboot.internal.SpringConstants.SPRING_BOOT_INVOKE_MAIN;
import static com.ibm.ws.app.manager.springboot.internal.SpringConstants.SPRING_BOOT_LIB_HEADER;
import static com.ibm.ws.app.manager.springboot.internal.SpringConstants.SPRING_START_CLASS_HEADER;

Expand Down Expand Up @@ -83,13 +82,11 @@ class SpringDeployedAppInfo extends DeployedAppInfoBase implements SpringContain
private final ExecutorService executor;
private final FutureMonitor futureMonitor;
private final SpringModuleContainerInfo springContainerModuleInfo;
private final boolean invokeSpringAppMain;
private final CountDownLatch waitToDeploy = new CountDownLatch(1);

SpringDeployedAppInfo(ApplicationInformation<DeployedAppInfo> applicationInformation,
SpringDeployedAppInfoFactoryImpl factory) throws UnableToAdaptException {
super(applicationInformation, factory);
invokeSpringAppMain = Boolean.parseBoolean(factory.getBundleContext().getProperty(SPRING_BOOT_INVOKE_MAIN));
springModuleHandler = factory.getSpringModuleHandler();
executor = factory.getExecutor();
futureMonitor = factory.getFutureMonitor();
Expand Down Expand Up @@ -207,16 +204,22 @@ public boolean preDeployApp(Future<Boolean> result) {
futureMonitor.onCompletion(mainInvokeResult, new CompletionListener<Boolean>() {
@Override
public void successfulCompletion(Future<Boolean> future, Boolean result) {
// doing a countDown here just to be safe incase invoking main did not do it.
// TODO this would really be unexpected, should we log some error if the count is not zero first?
waitToDeploy.countDown();
}

@Override
public void failedCompletion(Future<Boolean> future, Throwable t) {
// Something bad happened invoking main record failure
success.set(false);
waitToDeploy.countDown();
// set the error BEFORE countDown to ensure the future is set before continuing
futureMonitor.setResult(result, t);
// unblock any potential awaits still going on
waitToDeploy.countDown();
}
});

invokeSpringMain(mainInvokeResult);

try {
Expand All @@ -233,6 +236,10 @@ public void failedCompletion(Future<Boolean> future, Throwable t) {
*
*/
private void registerSpringContainerService() {
// Register the SpringContainer service with the context
// of the gateway bundle for the application.
// Find the gateway bunlde by searching the hierarchy of the
// the application classloader until a BundleReference is found.
ClassLoader cl = springContainerModuleInfo.getClassLoader();
while (cl != null && !(cl instanceof BundleReference)) {
cl = cl.getParent();
Expand All @@ -242,24 +249,26 @@ private void registerSpringContainerService() {
}
Bundle b = ((BundleReference) cl).getBundle();
BundleContext context = b.getBundleContext();
// We don't save the registration here, the service implementation track the reg.
context.registerService(SpringContainer.class, this, null);
}

// TODO should @FFDCIgnore on InvocationTragetException -- but that doesn't seem to work for lambdas
//@FFDCIgnore(InvocationTargetException.class)
private void invokeSpringMain(Future<Boolean> mainInvokeResult) {
if (!invokeSpringAppMain) {
futureMonitor.setResult(mainInvokeResult, true);
return;
}
final Method main;
try {
Class<?> springAppClass = springContainerModuleInfo.getClassLoader().loadClass(springContainerModuleInfo.springStartClass);
main = springAppClass.getMethod("main", String[].class);
// TODO not sure Spring Boot supports non-private main methods
main.setAccessible(true);
} catch (ClassNotFoundException | NoSuchMethodException e) {
futureMonitor.setResult(mainInvokeResult, e);
return;
}

// Execute the main method asynchronously.
// The mainInvokeResult is tracked to monitor completion
executor.execute(() -> {
try {
// TODO figure out how to pass arguments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ private void start0() throws EmbeddedServletContainerException {
Thread.currentThread().interrupt();
throw new EmbeddedServletContainerException("Initialization of ServletContext got interrupted.", e);
}
if (exception.get() != null) {
throw new EmbeddedServletContainerException("Error occured initializing the ServletContext.", exception.get());
}
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@
*/
public final class WebContainerConfiguration {
public static WebContainerConfiguration getWebContainerConfiguration(Object consumer) {
// A Spring Container is registered with the gateway bundle for the application
// here we find the gateway bundle by looking for a BundleReference in the class
// loader hierarchy.
ClassLoader cl = consumer.getClass().getClassLoader();
while (cl != null && (!(cl instanceof BundleReference))) {
cl = cl.getParent();
}
if (cl == null) {
throw new IllegalStateException("Did not find a BundleReference class loader.");
}
return new WebContainerConfiguration(getSpringContainer(cl), cl);
return new WebContainerConfiguration(getSpringContainer(cl));
}

private static SpringContainer getSpringContainer(ClassLoader cl) {
Expand All @@ -61,19 +64,20 @@ private static SpringContainer getSpringContainer(ClassLoader cl) {
}

private final SpringContainer springContainer;
private final ClassLoader cl;

/**
* @param springContainer
* @param cl
*/
private WebContainerConfiguration(SpringContainer springContainer, ClassLoader cl) {
private WebContainerConfiguration(SpringContainer springContainer) {
this.springContainer = springContainer;
this.cl = cl;
}

public void deploy(UnaryOperator<ServletContext> contextListener) {
// register an extension factory for the context BEFORE deploying
registerExtensionFactory(contextListener);
// Deploy the application will make the application known to the
// web container. This will cause the extension factory to be called
springContainer.deploy();
}

Expand All @@ -96,6 +100,7 @@ public ExtensionProcessor createExtensionProcessor(IServletContext webapp) throw
// TODO figure out how to make sure this is the correct context
if (true /* webapp.getClassLoader() == cl */) {
contextListener.apply(webapp);
// this registration is no longer needed
reg.unregister();
}
return null;
Expand Down

0 comments on commit e024ebc

Please sign in to comment.