Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2004-2010 Oracle Corporation. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at Contributors: Kohsuke Kawaguchi /
 package org.kohsuke.stapler.framework;
Entry point for web applications.

Applications that use stapler can use this as the base class, and then register that as the servlet context listener.

<T> The type of the root object instance
Kohsuke Kawaguchi
 public abstract class AbstractWebAppMain<T> implements ServletContextListener {
     protected final Class<T> rootType;
     private static final String APP = "app";
     protected ServletContext context;

Once the home directory is determined, this value is set to that directory.
     protected File home;
     protected AbstractWebAppMain(Class<T> rootType) {
         this. = rootType;

Returns the application name, like "Hudson" or "Torricelli". The method should always return the same value. The name should only contain alpha-numeric character.
     protected abstract String getApplicationName();

If the root application object is loaded asynchronously, override this method to return the place holder object to serve the request in the mean time.

null to synchronously load the application object.
     protected Object createPlaceHolderForAsyncLoad() {
         return null;

Creates the root application object.
     protected abstract Object createApplication() throws Exception;
     public void contextInitialized(ServletContextEvent event) {
         try {
              = event.getServletContext();
             if (!checkEnvironment())
             Object ph = createPlaceHolderForAsyncLoad();
             if(ph!=null) {
                 // asynchronous load
                new Thread(getApplicationName()+" initialization thread") {
                    public void run() {
            } else {
        } catch (Error e) {
            .log(."Failed to initialize "+getApplicationName(),e);
            throw e;
        } catch (RuntimeException e) {
            .log(."Failed to initialize "+getApplicationName(),e);
            throw e;

Sets the root application object.
    protected void setApplicationObject() {
        try {
        } catch (Error e) {
            .log(."Failed to initialize "+getApplicationName(),e);
            throw e;
        } catch (RuntimeException e) {
            .log(."Failed to initialize "+getApplicationName(),e);
            throw e;
        } catch (Exception e) {
            .log(."Failed to initialize "+getApplicationName(),e);
            throw new Error(e);

Performs pre start-up environment check.

false if a check fails. Webapp will fail to boot in this case.
    protected boolean checkEnvironment() {
         = getHomeDir().getAbsoluteFile();
        .info(getApplicationName()+" home directory: "+);
        // check that home exists (as mkdirs could have failed silently), otherwise throw a meaningful error
        if (!.exists()) {
            .setAttribute(,new NoHomeDirError());
            return false;
        return true;

Install org.jvnet.localizer.LocaleProvider that uses the current request to determine the language.
    private void installLocaleProvider() {
        LocaleProvider.setProvider(new LocaleProvider() {
            public Locale get() {
                Locale locale=null;
                StaplerRequest req = Stapler.getCurrentRequest();
                    locale = req.getLocale();
                    locale = Locale.getDefault();
                return locale;

Determines the home directory for the application. People makes configuration mistakes, so we are trying to be nice with those by doing java.lang.String.trim().
    protected File getHomeDir() {
        // check JNDI for the home directory first
        String varName = getApplicationName().toUpperCase() + "_HOME";
        try {
            InitialContext iniCtxt = new InitialContext();
            Context env = (ContextiniCtxt.lookup("java:comp/env");
            String value = (Stringenv.lookup(varName);
            if(value!=null && value.trim().length()>0)
                return new File(value.trim());
            // look at one more place. See HUDSON-1314
            value = (StringiniCtxt.lookup(varName);
            if(value!=null && value.trim().length()>0)
                return new File(value.trim());
        } catch (NamingException e) {
            // ignore
        // finally check the system property
        String sysProp = System.getProperty(varName);
            return new File(sysProp.trim());
        // look at the env var next
        String env = System.getenv(varName);
            return new File(env.trim()).getAbsoluteFile();
        return getDefaultHomeDir();

If no home directory is configured, this method is called to determine the default location, which is "~/.appname". Override this method to change that behavior.
    protected File getDefaultHomeDir() {
        return new File(new File(System.getProperty("user.home")),'.'+getApplicationName().toLowerCase());
    public void contextDestroyed(ServletContextEvent event) {
        Object o = event.getServletContext().getAttribute();
        if(.isInstance(o)) {

Called during the destructino of the web app to perform any clean up act on the application object.
    protected void cleanUp(T app) {}
    private static final Logger LOGGER = Logger.getLogger(AbstractWebAppMain.class.getName());
New to GrepCode? Check out our FAQ X