Search in sources :

Example 1 with WebAppController

use of hudson.stapler.WebAppController in project hudson-2.x by hudson.

the class WebAppMain method contextInitialized.

/**
 * Creates the sole instance of {@link Hudson} and register it to the {@link ServletContext}.
 */
public void contextInitialized(ServletContextEvent event) {
    try {
        final ServletContext context = event.getServletContext();
        // Install the current servlet context, unless its already been set
        final WebAppController controller = WebAppController.get();
        try {
            // Attempt to set the context
            controller.setContext(context);
        } catch (IllegalStateException e) {
        // context already set ignore
        }
        // Setup the default install strategy if not already configured
        try {
            controller.setInstallStrategy(new DefaultInstallStrategy());
        } catch (IllegalStateException e) {
        // strategy already set ignore
        }
        // use the current request to determine the language
        LocaleProvider.setProvider(new LocaleProvider() {

            public Locale get() {
                Locale locale = null;
                StaplerRequest req = Stapler.getCurrentRequest();
                if (req != null)
                    locale = req.getLocale();
                if (locale == null)
                    locale = Locale.getDefault();
                return locale;
            }
        });
        // quick check to see if we (seem to) have enough permissions to run. (see #719)
        JVM jvm;
        try {
            jvm = new JVM();
            new URLClassLoader(new URL[0], getClass().getClassLoader());
        } catch (SecurityException e) {
            controller.install(new InsufficientPermissionDetected(e));
            return;
        }
        try {
            // remove Sun PKCS11 provider if present. See http://wiki.hudson-ci.org/display/HUDSON/Solaris+Issue+6276483
            Security.removeProvider("SunPKCS11-Solaris");
        } catch (SecurityException e) {
        // ignore this error.
        }
        installLogger();
        File dir = getHomeDir(event);
        try {
            dir = dir.getCanonicalFile();
        } catch (IOException e) {
            dir = dir.getAbsoluteFile();
        }
        final File home = dir;
        home.mkdirs();
        LOGGER.info("Home directory: " + home);
        // check that home exists (as mkdirs could have failed silently), otherwise throw a meaningful error
        if (!home.exists()) {
            controller.install(new NoHomeDir(home));
            return;
        }
        // make sure that we are using XStream in the "enhanced" (JVM-specific) mode
        if (jvm.bestReflectionProvider().getClass() == PureJavaReflectionProvider.class) {
            // nope
            controller.install(new IncompatibleVMDetected());
            return;
        }
        // make sure this is servlet 2.4 container or above
        try {
            ServletResponse.class.getMethod("setCharacterEncoding", String.class);
        } catch (NoSuchMethodException e) {
            controller.install(new IncompatibleServletVersionDetected(ServletResponse.class));
            return;
        }
        // make sure that we see Ant 1.7
        try {
            FileSet.class.getMethod("getDirectoryScanner");
        } catch (NoSuchMethodException e) {
            controller.install(new IncompatibleAntVersionDetected(FileSet.class));
            return;
        }
        // make sure AWT is functioning, or else JFreeChart won't even load.
        if (ChartUtil.awtProblemCause != null) {
            controller.install(new AWTProblem(ChartUtil.awtProblemCause));
            return;
        }
        // check that and report an error
        try {
            File f = File.createTempFile("test", "test");
            f.delete();
        } catch (IOException e) {
            controller.install(new NoTempDir(e));
            return;
        }
        // try to correct it
        try {
            TransformerFactory.newInstance();
        // if this works we are all happy
        } catch (TransformerFactoryConfigurationError x) {
            // no it didn't.
            LOGGER.log(Level.WARNING, "XSLT not configured correctly. Hudson will try to fix this. See http://issues.apache.org/bugzilla/show_bug.cgi?id=40895 for more details", x);
            System.setProperty(TransformerFactory.class.getName(), "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");
            try {
                TransformerFactory.newInstance();
                LOGGER.info("XSLT is set to the JAXP RI in JRE");
            } catch (TransformerFactoryConfigurationError y) {
                LOGGER.log(Level.SEVERE, "Failed to correct the problem.");
            }
        }
        installExpressionFactory(event);
        controller.install(new HudsonIsLoading());
        new Thread("hudson initialization thread") {

            @Override
            public void run() {
                try {
                    // Creating of the god object performs most of the booting muck
                    Hudson hudson = new Hudson(home, context);
                    // once its done, hook up to stapler and things should be ready to go
                    controller.install(hudson);
                    // trigger the loading of changelogs in the background,
                    // but give the system 10 seconds so that the first page
                    // can be served quickly
                    Trigger.timer.schedule(new SafeTimerTask() {

                        public void doRun() {
                            User.getUnknown().getBuilds();
                        }
                    }, 1000 * 10);
                } catch (Error e) {
                    LOGGER.log(Level.SEVERE, "Failed to initialize Hudson", e);
                    controller.install(new HudsonFailedToLoad(e));
                    throw e;
                } catch (Exception e) {
                    LOGGER.log(Level.SEVERE, "Failed to initialize Hudson", e);
                    controller.install(new HudsonFailedToLoad(e));
                }
            }
        }.start();
    } catch (Error e) {
        LOGGER.log(Level.SEVERE, "Failed to initialize Hudson", e);
        throw e;
    } catch (RuntimeException e) {
        LOGGER.log(Level.SEVERE, "Failed to initialize Hudson", e);
        throw e;
    }
}
Also used : Locale(java.util.Locale) JVM(com.thoughtworks.xstream.core.JVM) IncompatibleAntVersionDetected(hudson.util.IncompatibleAntVersionDetected) StaplerRequest(org.kohsuke.stapler.StaplerRequest) NoHomeDir(hudson.util.NoHomeDir) ServletContext(javax.servlet.ServletContext) HudsonFailedToLoad(hudson.util.HudsonFailedToLoad) TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) IncompatibleVMDetected(hudson.util.IncompatibleVMDetected) Hudson(hudson.model.Hudson) InsufficientPermissionDetected(hudson.util.InsufficientPermissionDetected) TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) IOException(java.io.IOException) WebAppController(hudson.stapler.WebAppController) HudsonIsLoading(hudson.util.HudsonIsLoading) NoTempDir(hudson.util.NoTempDir) AWTProblem(hudson.util.AWTProblem) NamingException(javax.naming.NamingException) IOException(java.io.IOException) DefaultInstallStrategy(hudson.stapler.WebAppController.DefaultInstallStrategy) LocaleProvider(org.jvnet.localizer.LocaleProvider) URLClassLoader(java.net.URLClassLoader) IncompatibleServletVersionDetected(hudson.util.IncompatibleServletVersionDetected) File(java.io.File) SafeTimerTask(hudson.triggers.SafeTimerTask)

Example 2 with WebAppController

use of hudson.stapler.WebAppController in project hudson-2.x by hudson.

the class HudsonTestCase method setUp.

@Override
protected void setUp() throws Exception {
    // System.setProperty("hudson.PluginStrategy", "hudson.ClassicPluginStrategy");
    env.pin();
    recipe();
    AbstractProject.WORKSPACE.toString();
    User.clear();
    // Bootstrap the container with details about our testing classes, so it can figure out what to scan/include
    // force-reset, some tests may not properly hit the tear-down to reset so do it again here
    SmoothieUtil.reset();
    new SmoothieContainerBootstrap().bootstrap(getClass().getClassLoader(), Hudson.class, Smoothie.class, HudsonTestCase.class, getClass());
    try {
        hudson = newHudson();
    } catch (Exception e) {
        // if Hudson instance fails to initialize, it leaves the instance field non-empty and break all the rest of the tests, so clean that up.
        Field f = Hudson.class.getDeclaredField("theInstance");
        f.setAccessible(true);
        f.set(null, null);
        throw e;
    }
    // collecting usage stats from tests are pointless.
    hudson.setNoUsageStatistics(true);
    hudson.setCrumbIssuer(new TestCrumbIssuer());
    final WebAppController controller = WebAppController.get();
    try {
        controller.setContext(hudson.servletContext);
    } catch (IllegalStateException e) {
        // handle tests which run several times inside the same JVM
        Field f = WebAppController.class.getDeclaredField("context");
        f.setAccessible(true);
        f.set(controller, hudson.servletContext);
    }
    try {
        controller.setInstallStrategy(new DefaultInstallStrategy());
    } catch (IllegalStateException e) {
    // strategy already set ignore
    }
    controller.install(hudson);
    hudson.servletContext.setAttribute("version", "?");
    WebAppMain.installExpressionFactory(new ServletContextEvent(hudson.servletContext));
    // set a default JDK to be the one that the harness is using.
    hudson.getJDKs().add(new JDK("default", System.getProperty("java.home")));
    configureUpdateCenter();
    // expose the test instance as a part of URL tree.
    // this allows tests to use a part of the URL space for itself.
    hudson.getActions().add(this);
    // cause all the descriptors to reload.
    // ideally we'd like to reset them to properly emulate the behavior, but that's not possible.
    Mailer.descriptor().setHudsonUrl(null);
    for (Descriptor d : hudson.getExtensionList(Descriptor.class)) d.load();
}
Also used : Field(java.lang.reflect.Field) DefaultInstallStrategy(hudson.stapler.WebAppController.DefaultInstallStrategy) ClassDescriptor(org.kohsuke.stapler.ClassDescriptor) PropertyDescriptor(java.beans.PropertyDescriptor) SmoothieContainerBootstrap(org.hudsonci.inject.internal.SmoothieContainerBootstrap) WebAppController(hudson.stapler.WebAppController) FailingHttpStatusCodeException(com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException) BadCredentialsException(org.acegisecurity.BadCredentialsException) UsernameNotFoundException(org.acegisecurity.userdetails.UsernameNotFoundException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) CSSParseException(org.w3c.css.sac.CSSParseException) AbstractArtifactResolutionException(org.apache.maven.artifact.resolver.AbstractArtifactResolutionException) AuthenticationException(org.acegisecurity.AuthenticationException) SAXException(org.xml.sax.SAXException) DataAccessException(org.springframework.dao.DataAccessException) CSSException(org.w3c.css.sac.CSSException) MalformedURLException(java.net.MalformedURLException) ServletContextEvent(javax.servlet.ServletContextEvent)

Aggregations

WebAppController (hudson.stapler.WebAppController)2 DefaultInstallStrategy (hudson.stapler.WebAppController.DefaultInstallStrategy)2 IOException (java.io.IOException)2 FailingHttpStatusCodeException (com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException)1 JVM (com.thoughtworks.xstream.core.JVM)1 Hudson (hudson.model.Hudson)1 SafeTimerTask (hudson.triggers.SafeTimerTask)1 AWTProblem (hudson.util.AWTProblem)1 HudsonFailedToLoad (hudson.util.HudsonFailedToLoad)1 HudsonIsLoading (hudson.util.HudsonIsLoading)1 IncompatibleAntVersionDetected (hudson.util.IncompatibleAntVersionDetected)1 IncompatibleServletVersionDetected (hudson.util.IncompatibleServletVersionDetected)1 IncompatibleVMDetected (hudson.util.IncompatibleVMDetected)1 InsufficientPermissionDetected (hudson.util.InsufficientPermissionDetected)1 NoHomeDir (hudson.util.NoHomeDir)1 NoTempDir (hudson.util.NoTempDir)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 File (java.io.File)1 Field (java.lang.reflect.Field)1 MalformedURLException (java.net.MalformedURLException)1