KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > WebAppMain


1 package hudson;
2
3 import com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider;
4 import com.thoughtworks.xstream.core.JVM;
5 import hudson.model.Hudson;
6 import hudson.model.User;
7 import hudson.triggers.Trigger;
8 import hudson.util.IncompatibleServletVersionDetected;
9 import hudson.util.IncompatibleVMDetected;
10 import hudson.util.RingBufferLogHandler;
11
12 import javax.naming.Context JavaDoc;
13 import javax.naming.InitialContext JavaDoc;
14 import javax.naming.NamingException JavaDoc;
15 import javax.servlet.ServletContext JavaDoc;
16 import javax.servlet.ServletContextEvent JavaDoc;
17 import javax.servlet.ServletContextListener JavaDoc;
18 import javax.servlet.ServletResponse JavaDoc;
19 import javax.xml.transform.TransformerFactory JavaDoc;
20 import javax.xml.transform.TransformerFactoryConfigurationError JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.util.Properties JavaDoc;
25 import java.util.TimerTask JavaDoc;
26 import java.util.logging.Level JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28
29 /**
30  * Entry point when Hudson is used as a webapp.
31  *
32  * @author Kohsuke Kawaguchi
33  */

34 public class WebAppMain implements ServletContextListener JavaDoc {
35
36     /**
37      * Creates the sole instance of {@link Hudson} and register it to the {@link ServletContext}.
38      */

39     public void contextInitialized(ServletContextEvent JavaDoc event) {
40         installLogger();
41
42         File JavaDoc home = getHomeDir(event);
43         home.mkdirs();
44         System.out.println("hudson home directory: "+home);
45
46         ServletContext JavaDoc context = event.getServletContext();
47
48         // make sure that we are using XStream in the "enhanced" (JVM-specific) mode
49
if(new JVM().bestReflectionProvider().getClass()==PureJavaReflectionProvider.class) {
50             // nope
51
context.setAttribute("app",new IncompatibleVMDetected());
52             return;
53         }
54
55         // make sure this is servlet 2.4 container or above
56
try {
57             ServletResponse JavaDoc.class.getMethod("setCharacterEncoding",String JavaDoc.class);
58         } catch (NoSuchMethodException JavaDoc e) {
59             context.setAttribute("app,",new IncompatibleServletVersionDetected());
60             return;
61         }
62
63         // Tomcat breaks XSLT with JDK 5.0 and onward. Check if that's the case, and if so,
64
// try to correct it
65
try {
66             TransformerFactory.newInstance();
67             // if this works we are all happy
68
} catch (TransformerFactoryConfigurationError JavaDoc x) {
69             // no it didn't.
70
Logger JavaDoc logger = Logger.getLogger(WebAppMain.class.getName());
71
72             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);
73             System.setProperty(TransformerFactory JavaDoc.class.getName(),"com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");
74             try {
75                 TransformerFactory.newInstance();
76                 logger.info("XSLT is set to the JAXP RI in JRE");
77             } catch(TransformerFactoryConfigurationError JavaDoc y) {
78                 logger.log(Level.SEVERE, "Failed to correct the problem.");
79             }
80         }
81
82
83         try {
84             context.setAttribute("app",new Hudson(home,context));
85         } catch( IOException JavaDoc e ) {
86             throw new Error JavaDoc(e);
87         }
88
89         // set the version
90
Properties JavaDoc props = new Properties JavaDoc();
91         try {
92             InputStream JavaDoc is = getClass().getResourceAsStream("hudson-version.properties");
93             if(is!=null)
94                 props.load(is);
95         } catch (IOException JavaDoc e) {
96             e.printStackTrace(); // if the version properties is missing, that's OK.
97
}
98         Object JavaDoc ver = props.get("version");
99         if(ver==null) ver="?";
100         context.setAttribute("version",ver);
101
102         Trigger.init(); // start running trigger
103

104         // trigger the loading of changelogs in the background,
105
// but give the system 10 seconds so that the first page
106
// can be served quickly
107
Trigger.timer.schedule(new TimerTask JavaDoc() {
108             public void run() {
109                 User.get("nobody").getBuilds();
110             }
111         }, 1000*10);
112
113     }
114
115     /**
116      * Installs log handler to monitor all Hudson logs.
117      */

118     private void installLogger() {
119         RingBufferLogHandler handler = new RingBufferLogHandler();
120         Hudson.logRecords = handler.getView();
121         Logger.getLogger("hudson").addHandler(handler);
122     }
123
124     /**
125      * Determines the home directory for Hudson.
126      *
127      * People makes configuration mistakes, so we are trying to be nice
128      * with those by doing {@link String#trim()}.
129      */

130     private File JavaDoc getHomeDir(ServletContextEvent JavaDoc event) {
131         // check JNDI for the home directory first
132
try {
133             Context JavaDoc env = (Context JavaDoc) new InitialContext JavaDoc().lookup("java:comp/env");
134             String JavaDoc value = (String JavaDoc) env.lookup("HUDSON_HOME");
135             if(value!=null && value.trim().length()>0)
136                 return new File JavaDoc(value.trim()).getAbsoluteFile();
137         } catch (NamingException JavaDoc e) {
138             // ignore
139
}
140
141         // look at the env var next
142
String JavaDoc env = EnvVars.masterEnvVars.get("HUDSON_HOME");
143         if(env!=null)
144             return new File JavaDoc(env.trim()).getAbsoluteFile();
145
146         // finally check the system property
147
String JavaDoc sysProp = System.getProperty("HUDSON_HOME");
148         if(sysProp!=null)
149             return new File JavaDoc(sysProp.trim()).getAbsoluteFile();
150
151         // otherwise pick a place by ourselves
152

153         String JavaDoc root = event.getServletContext().getRealPath("/WEB-INF/workspace");
154         if(root!=null) {
155             File JavaDoc ws = new File JavaDoc(root.trim());
156             if(ws.exists())
157                 // Hudson <1.42 used to prefer this before ~/.hudson, so
158
// check the existence and if it's there, use it.
159
// otherwise if this is a new installation, prefer ~/.hudson
160
return ws.getAbsoluteFile();
161         }
162
163         // if for some reason we can't put it within the webapp, use home directory.
164
return new File JavaDoc(new File JavaDoc(System.getProperty("user.home")),".hudson").getAbsoluteFile();
165     }
166
167     public void contextDestroyed(ServletContextEvent JavaDoc event) {
168         Hudson instance = Hudson.getInstance();
169         if(instance!=null)
170             instance.cleanUp();
171     }
172 }
173
Popular Tags