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 ; 13 import javax.naming.InitialContext ; 14 import javax.naming.NamingException ; 15 import javax.servlet.ServletContext ; 16 import javax.servlet.ServletContextEvent ; 17 import javax.servlet.ServletContextListener ; 18 import javax.servlet.ServletResponse ; 19 import javax.xml.transform.TransformerFactory ; 20 import javax.xml.transform.TransformerFactoryConfigurationError ; 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.util.Properties ; 25 import java.util.TimerTask ; 26 import java.util.logging.Level ; 27 import java.util.logging.Logger ; 28 29 34 public class WebAppMain implements ServletContextListener { 35 36 39 public void contextInitialized(ServletContextEvent event) { 40 installLogger(); 41 42 File home = getHomeDir(event); 43 home.mkdirs(); 44 System.out.println("hudson home directory: "+home); 45 46 ServletContext context = event.getServletContext(); 47 48 if(new JVM().bestReflectionProvider().getClass()==PureJavaReflectionProvider.class) { 50 context.setAttribute("app",new IncompatibleVMDetected()); 52 return; 53 } 54 55 try { 57 ServletResponse .class.getMethod("setCharacterEncoding",String .class); 58 } catch (NoSuchMethodException e) { 59 context.setAttribute("app,",new IncompatibleServletVersionDetected()); 60 return; 61 } 62 63 try { 66 TransformerFactory.newInstance(); 67 } catch (TransformerFactoryConfigurationError x) { 69 Logger 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 .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 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 e ) { 86 throw new Error (e); 87 } 88 89 Properties props = new Properties (); 91 try { 92 InputStream is = getClass().getResourceAsStream("hudson-version.properties"); 93 if(is!=null) 94 props.load(is); 95 } catch (IOException e) { 96 e.printStackTrace(); } 98 Object ver = props.get("version"); 99 if(ver==null) ver="?"; 100 context.setAttribute("version",ver); 101 102 Trigger.init(); 104 Trigger.timer.schedule(new TimerTask () { 108 public void run() { 109 User.get("nobody").getBuilds(); 110 } 111 }, 1000*10); 112 113 } 114 115 118 private void installLogger() { 119 RingBufferLogHandler handler = new RingBufferLogHandler(); 120 Hudson.logRecords = handler.getView(); 121 Logger.getLogger("hudson").addHandler(handler); 122 } 123 124 130 private File getHomeDir(ServletContextEvent event) { 131 try { 133 Context env = (Context ) new InitialContext ().lookup("java:comp/env"); 134 String value = (String ) env.lookup("HUDSON_HOME"); 135 if(value!=null && value.trim().length()>0) 136 return new File (value.trim()).getAbsoluteFile(); 137 } catch (NamingException e) { 138 } 140 141 String env = EnvVars.masterEnvVars.get("HUDSON_HOME"); 143 if(env!=null) 144 return new File (env.trim()).getAbsoluteFile(); 145 146 String sysProp = System.getProperty("HUDSON_HOME"); 148 if(sysProp!=null) 149 return new File (sysProp.trim()).getAbsoluteFile(); 150 151 153 String root = event.getServletContext().getRealPath("/WEB-INF/workspace"); 154 if(root!=null) { 155 File ws = new File (root.trim()); 156 if(ws.exists()) 157 return ws.getAbsoluteFile(); 161 } 162 163 return new File (new File (System.getProperty("user.home")),".hudson").getAbsoluteFile(); 165 } 166 167 public void contextDestroyed(ServletContextEvent event) { 168 Hudson instance = Hudson.getInstance(); 169 if(instance!=null) 170 instance.cleanUp(); 171 } 172 } 173 | Popular Tags |