View Javadoc

1   package org.astrogrid.applications.component;
2   
3   import java.io.File;
4   import javax.servlet.ServletContext;
5   import javax.servlet.ServletException;
6   import org.apache.commons.logging.Log;
7   import org.apache.commons.logging.LogFactory;
8   import org.astrogrid.common.j2ee.environment.EnvEntry;
9   import org.astrogrid.common.j2ee.environment.Environment;
10  import org.astrogrid.common.j2ee.environment.EnvironmentServlet;
11  import org.astrogrid.config.SimpleConfig;
12  
13  /***
14   * A servlet to manage environment entries in JNDI and web.xml.
15   *
16   * This is a sub-class of the generic AstroGrid EnvironmentServlet.
17   * The change from the parent class is to initialize one specific
18   * env-entry when the servlet starts: cea.base.dir which
19   * locates the CEC's writeable directory-tree and hence the
20   * configuration files. 
21   *
22   * If the env-entry is set, then this servlet does not try
23   * to change it. However, if no value for the env-entry is
24   * found in JNDI, then this servlet sets a value based on
25   * its knowledge of the environment.
26   *
27   * If the temporary-files directory (specified by the Java
28   * system-property java.io.tmpdir) is, say, /tmp, and if the
29   * web-application context is, say, CL-CEC-1, then cea.base.dir
30   * gets set to /tmp/CL-CEC-1.
31   *
32   * @author Guy Rixon
33   */
34  public class CeaEnvironmentServlet extends EnvironmentServlet {
35    
36    static Log log = LogFactory.getLog(CeaEnvironmentServlet.class);
37    
38    /***
39     * Constructs a new CeaEnvironmentServlet.
40     */
41    public CeaEnvironmentServlet() {
42      super();
43    }
44    
45    /***
46     * Initializes the servlet.
47     * This adds actions to the initialization in the super-class.
48     */
49    public void init() throws ServletException {
50      super.init();
51      
52      log.debug("EnvironmentServlet is initialized.");
53      log.debug("Now initializing the CeaEnvironmentServlet...");
54      
55      // Get the environment object from the J2EE context; the
56      // parent class put it there during its init() method.
57      ServletContext context = this.getServletContext();
58      log.debug("Getting the environment bean from the context...");
59      Environment environment = (Environment)context.getAttribute("environment");
60      log.debug("Got the environment bean.");
61      
62      // Find the env-entry of interest.
63      log.debug("Getting the env-entries...");
64      EnvEntry[] entries = environment.getEnvEntry();
65      log.debug("Got " + entries.length + " EnvEntry beans.");
66      for (int i = 0; i < entries.length; i++) {
67        if ("cea.base.dir".equals(entries[i].getName())) {
68          log.debug("Initializing cea.base.dir...");
69          
70          // If no JNDI value, construct one. and add it as the
71          // default value. This doesn't alter anything in JNDI
72          // but does mean that a configuration constructed from the
73          // environment will get the default value.
74          if (entries[i].getOperationalValue() == null) {
75            File baseDir = new File(System.getProperty("java.io.tmpdir"),
76                                    environment.getContextPath());
77            String path = baseDir.getAbsolutePath();
78            log.debug("cea.base.dir will be set to " + path);
79            
80            // Add the new value to the environment as a replacement value.
81            // This doesn't change JNDI and isn't directly available
82            // to the consumers of the configuration (but see below). 
83            // However, it does mean that the value shows up in the 
84            // configuration editor. 
85            entries[i].setReplacementValue(path);
86            
87            // Add the value to the AstroGrid generic configuration,
88            // thus overriding the null value obtained from JNDI
89            // (the AG configuration is read from JNDI). This makes the
90            // value operational in the rest of the web-application.
91            SimpleConfig.getSingleton().setProperty("cea.base.dir", path);
92          }
93        }
94      }
95      log.info("CeaEnvironmentServlet is initialized.");
96    }
97  }