01: package dalma.webui;
02:
03: import dalma.container.Container;
04:
05: import javax.naming.Context;
06: import javax.naming.InitialContext;
07: import javax.naming.NamingException;
08: import javax.servlet.ServletContextEvent;
09: import javax.servlet.ServletContextListener;
10: import java.io.File;
11: import java.io.IOException;
12: import java.io.InputStream;
13: import java.util.Properties;
14: import java.util.logging.Logger;
15:
16: /**
17: * Entry point.
18: *
19: * @author Kohsuke Kawaguchi
20: */
21: public class Main implements ServletContextListener {
22:
23: private WContainer container;
24:
25: public void contextInitialized(ServletContextEvent event) {
26: File home = getHomeDir(event);
27: home.mkdirs();
28: new File(home, "apps").mkdir();
29: System.out.println("dalma home directory: " + home);
30:
31: LogRecorder logRecorder = new LogRecorder();
32: Logger.getLogger("dalma").addHandler(logRecorder);
33:
34: try {
35: container = new WContainer(Container.create(home),
36: logRecorder);
37: event.getServletContext().setAttribute("app", container);
38: } catch (IOException e) {
39: throw new Error(e);
40: }
41:
42: // set the version
43: Properties props = new Properties();
44: try {
45: InputStream is = getClass().getResourceAsStream(
46: "version.properties");
47: if (is != null)
48: props.load(is);
49: } catch (IOException e) {
50: e.printStackTrace(); // if the version properties is missing, that's OK.
51: }
52: Object ver = props.get("version");
53: if (ver == null)
54: ver = "?";
55: event.getServletContext().setAttribute("version", ver);
56: }
57:
58: public void contextDestroyed(ServletContextEvent event) {
59: if (container != null)
60: container.shutdown();
61: }
62:
63: /**
64: * Determines the home directory for Hudson.
65: */
66: private File getHomeDir(ServletContextEvent event) {
67: // check JNDI for the home directory first
68: try {
69: Context env = (Context) new InitialContext()
70: .lookup("java:comp/env");
71: String value = (String) env.lookup("DALMA_HOME");
72: if (value != null && value.trim().length() > 0)
73: return new File(value);
74: } catch (NamingException e) {
75: ; // ignore
76: }
77:
78: // look at the env var next
79: String env = EnvVars.masterEnvVars.get("DALMA_HOME");
80: if (env != null)
81: return new File(env);
82:
83: // otherwise pick a place by ourselves
84: String root = event.getServletContext().getRealPath(
85: "/WEB-INF/workspace");
86: if (root != null)
87: return new File(root);
88:
89: // if for some reason we can't put it within the webapp, use home directory.
90: return new File(new File(System.getProperty("user.home")),
91: ".dalma");
92: }
93: }
|