View Javadoc

1   package uk.ac.cam.spectra.spectrasub.servlet;
2   
3   import java.io.File;
4   import java.net.MalformedURLException;
5   
6   import javax.servlet.ServletContext;
7   
8   import org.apache.log4j.Logger;
9   import org.apache.tapestry.web.WebContext;
10  import org.springframework.context.support.AbstractApplicationContext;
11  import org.springframework.web.context.ConfigurableWebApplicationContext;
12  import org.springframework.web.context.WebApplicationContext;
13  import org.springframework.web.context.support.WebApplicationContextUtils;
14  import org.springframework.web.context.support.XmlWebApplicationContext;
15  
16  import uk.ac.cam.spectra.spectrasub.ConfigurationException;
17  import uk.ac.cam.spectra.spectrasub.Constants;
18  import uk.ac.cam.spectra.spectrasub.Profile2BeanConfig;
19  
20  /**
21   * If a valid workspace has been detected and set as a servlet context attribute
22   * this component adds it to the config path for the spring application context,
23   * otherwise just creates the default context. Also contains methods for
24   * triggering the generation of dynamic spring config from profile.xml and
25   * restarting the spring container.
26   * 
27   * @author ojd20
28   * 
29   */
30  public class SpectraContextLoader {
31  
32      private static final Logger LOG = Logger
33              .getLogger(SpectraContextLoader.class);
34  
35      /**
36       * Adds the custom spring-beans file to the Spring config if it exists
37       * 
38       * @param servletContext
39       * @param wac
40       */
41      public void addCustomSpringConfig(String workspacePath,
42              ConfigurableWebApplicationContext wac) {
43          if (workspacePath != null) {
44              File ws = new File(workspacePath);
45              if (ws.exists()) {
46                  File customBeans = new File(ws,
47                          Constants.SPRING_CONFIG_FILENAME);
48                  if (customBeans.exists()) {
49                      String[] configLocations;
50                      try {
51                          configLocations = new String[] {
52                                  XmlWebApplicationContext.DEFAULT_CONFIG_LOCATION,
53                                  customBeans.toURL().toExternalForm() };
54                          wac.setConfigLocations(configLocations);
55                      } catch (MalformedURLException e) {
56                          throw new RuntimeException(
57                                  "JVM error, file object returned bad URL");
58                      }
59                  }
60              }
61          }
62      }
63  
64      public void checkAndReconfigure(ServletContext servletContext) {
65          String wsPath = (String) servletContext
66                  .getAttribute(Constants.WORKSPACE_ATTRIBUTE);
67          AbstractApplicationContext applicationContext = (AbstractApplicationContext) WebApplicationContextUtils
68                  .getRequiredWebApplicationContext(servletContext);
69          checkAndReconfigure(wsPath, applicationContext);
70      }
71  
72      public void checkAndReconfigure(String wsPath,
73              AbstractApplicationContext applicationContext) {
74          LOG.debug("Found workspace path: " + wsPath);
75          if (wsPath != null) {
76              File workspace = new File(wsPath);
77              if (workspace.exists()) {
78                  File profile = new File(workspace, Constants.PROFILE_FILENAME);
79                  File springConfig = new File(workspace,
80                          Constants.SPRING_CONFIG_FILENAME);
81                  if (profile.exists()) {
82                      if (!springConfig.exists()
83                              || profile.lastModified() > springConfig
84                                      .lastModified()) {
85                          LOG.info("Refreshing Spring configuration...");
86  
87                          Profile2BeanConfig configGen = new Profile2BeanConfig();
88                          configGen.defaultConfiguration(workspace);
89                          try {
90                              configGen.init();
91                              LOG.info("Creating new spring config file...");
92                              configGen.generateConfiguration();
93                              configGen.writeConfiguration();
94                          } catch (ConfigurationException e) {
95                              throw new RuntimeException(e);
96                          }
97                      }
98                  }
99                  addCustomSpringConfig(wsPath,
100                         (ConfigurableWebApplicationContext) applicationContext);
101                 LOG.info("Restarting spring context...");
102                 applicationContext.close();
103                 applicationContext.refresh();
104                 LOG.info("Done reconfiguring Spring.");
105             }
106         }
107     }
108 
109     public void checkAndReconfigure(WebContext webContext) {
110         AbstractApplicationContext applicationContext = (AbstractApplicationContext) webContext
111                 .getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
112         String wsPath = (String) webContext
113                 .getAttribute(Constants.WORKSPACE_ATTRIBUTE);
114         checkAndReconfigure(wsPath, applicationContext);
115 
116     }
117 
118     public void bounceContext(WebContext webContext) {
119         AbstractApplicationContext applicationContext = (AbstractApplicationContext) webContext
120                 .getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
121         LOG.info("Restarting spring context...");
122         applicationContext.close();
123         applicationContext.refresh();
124         LOG.info("Done reconfiguring Spring.");
125     }
126 
127 }