Skip to content
Permalink
Browse files

Merge pull request #1950 from jglick/WebAppMain.contextDestroyed-logging

[JENKINS-24696] WebAppMain.contextDestroyed produces weird errors
  • Loading branch information...
jglick committed Jan 11, 2016
2 parents dda00aa + d3ebb8a commit 8cfa1e890f598edfb911d5c9591cd2109b4ad509
@@ -396,8 +396,10 @@ public void contextDestroyed(ServletContextEvent event) {
if(instance!=null)
instance.cleanUp();
Thread t = initThread;
if (t!=null)
if (t != null && t.isAlive()) {
LOGGER.log(Level.INFO, "Shutting down a Jenkins instance that was still starting up", new Throwable("reason"));
t.interrupt();
}

// Logger is in the system classloader, so if we don't do this
// the whole web app will never be undepoyed.
@@ -38,6 +38,6 @@
public class GroovyInitScript {
@Initializer(after=JOB_LOADED)
public static void init(Jenkins j) {
new GroovyHookScript("init").run();
new GroovyHookScript("init", j.servletContext, j.getRootDir(), j.getPluginManager().uberClassLoader).run();
}
}
@@ -40,7 +40,10 @@ public void publish(ServletContext context, @CheckForNull File home) {
LOGGER.log(Level.SEVERE, "Failed to initialize Jenkins",this);

WebApp.get(context).setApp(this);
new GroovyHookScript("boot-failure")
if (home == null) {
return;
}
new GroovyHookScript("boot-failure", context, home, BootFailure.class.getClassLoader())
.bind("exception",this)
.bind("home",home)
.bind("servletContext", context)
@@ -3,18 +3,18 @@
import groovy.lang.Binding;
import groovy.lang.GroovyCodeSource;
import groovy.lang.GroovyShell;
import jenkins.model.Jenkins;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Logger;

import static java.util.logging.Level.WARNING;
import java.util.logging.Logger;
import javax.annotation.Nonnull;
import javax.servlet.ServletContext;
import jenkins.model.Jenkins;

/**
* A collection of Groovy scripts that are executed as various hooks.
@@ -40,9 +40,24 @@
public class GroovyHookScript {
private final String hook;
private final Binding bindings = new Binding();
private final ServletContext servletContext;
private final File home;
private final ClassLoader loader;

@Deprecated
public GroovyHookScript(String hook) {
this(hook, Jenkins.getActiveInstance());
}

private GroovyHookScript(String hook, Jenkins j) {
this(hook, j.servletContext, j.getRootDir(), j.getPluginManager().uberClassLoader);
}

public GroovyHookScript(String hook, @Nonnull ServletContext servletContext, @Nonnull File home, @Nonnull ClassLoader loader) {
this.hook = hook;
this.servletContext = servletContext;
this.home = home;
this.loader = loader;
}

public GroovyHookScript bind(String name, Object o) {
@@ -55,34 +70,33 @@ public Binding getBindings() {
}

public void run() {
Jenkins j = Jenkins.getInstance();
final String hookGroovy = hook+".groovy";
final String hookGroovyD = hook+".groovy.d";

try {
URL bundled = j.servletContext.getResource("/WEB-INF/"+ hookGroovy);
URL bundled = servletContext.getResource("/WEB-INF/"+ hookGroovy);
execute(bundled);
} catch (IOException e) {
LOGGER.log(WARNING, "Failed to execute /WEB-INF/"+hookGroovy,e);
}

Set<String> resources = j.servletContext.getResourcePaths("/WEB-INF/"+ hookGroovyD +"/");
Set<String> resources = servletContext.getResourcePaths("/WEB-INF/"+ hookGroovyD +"/");
if (resources!=null) {
// sort to execute them in a deterministic order
for (String res : new TreeSet<String>(resources)) {
try {
URL bundled = j.servletContext.getResource(res);
URL bundled = servletContext.getResource(res);
execute(bundled);
} catch (IOException e) {
LOGGER.log(WARNING, "Failed to execute " + res, e);
}
}
}

File script = new File(j.getRootDir(), hookGroovy);
File script = new File(home, hookGroovy);
execute(script);

File scriptD = new File(j.getRootDir(), hookGroovyD);
File scriptD = new File(home, hookGroovyD);
if (scriptD.isDirectory()) {
File[] scripts = scriptD.listFiles(new FileFilter() {
public boolean accept(File f) {
@@ -129,7 +143,7 @@ protected void execute(GroovyCodeSource s) {
* Can be used to customize the environment in which the script runs.
*/
protected GroovyShell createShell() {
return new GroovyShell(Jenkins.getInstance().getPluginManager().uberClassLoader, bindings);
return new GroovyShell(loader, bindings);
}

private static final Logger LOGGER = Logger.getLogger(GroovyHookScript.class.getName());

This file was deleted.

0 comments on commit 8cfa1e8

Please sign in to comment.
You can’t perform that action at this time.