View Javadoc

1   package com.panogenesis.webapp.listener;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   
6   import javax.servlet.ServletContext;
7   import javax.servlet.ServletContextEvent;
8   import javax.servlet.ServletContextListener;
9   
10  import org.apache.commons.logging.Log;
11  import org.apache.commons.logging.LogFactory;
12  import com.panogenesis.Constants;
13  import com.panogenesis.service.LookupManager;
14  import org.springframework.context.ApplicationContext;
15  import org.springframework.web.context.ContextLoaderListener;
16  import org.springframework.web.context.support.WebApplicationContextUtils;
17  
18  /***
19   * StartupListener class used to initialize and database settings
20   * and populate any application-wide drop-downs.
21   *
22   * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
23   *
24   * @web.listener
25   */
26  public class StartupListener extends ContextLoaderListener
27      implements ServletContextListener {
28      
29      private static final Log log = LogFactory.getLog(StartupListener.class);
30  
31      public void contextInitialized(ServletContextEvent event) {
32          if (log.isDebugEnabled()) {
33              log.debug("initializing context...");
34          }
35  
36          // call Spring's context ContextLoaderListener to initialize
37          // all the context files specified in web.xml
38          super.contextInitialized(event);
39  
40          ServletContext context = event.getServletContext();
41          String daoType = context.getInitParameter(Constants.DAO_TYPE);
42  
43          // if daoType is not specified, use DAO as default
44          if (daoType == null) {
45              log.warn("No 'daoType' context carameter, using hibernate");
46              daoType = Constants.DAO_TYPE_HIBERNATE;
47          }
48  
49          // Orion starts Servlets before Listeners, so check if the config
50          // object already exists
51          Map config = (HashMap) context.getAttribute(Constants.CONFIG);
52  
53          if (config == null) {
54              config = new HashMap();
55          }
56  
57          // Create a config object to hold all the app config values
58          config.put(Constants.DAO_TYPE, daoType);
59          context.setAttribute(Constants.CONFIG, config);
60  
61          // output the retrieved values for the Init and Context Parameters
62          if (log.isDebugEnabled()) {
63              log.debug("daoType: " + daoType);
64              log.debug("populating drop-downs...");
65          }
66  
67          setupContext(context);
68      }
69  
70      public static void setupContext(ServletContext context) {
71          ApplicationContext ctx = 
72              WebApplicationContextUtils.getRequiredWebApplicationContext(context);
73  
74          LookupManager mgr = (LookupManager) ctx.getBean("lookupManager");
75  
76          // get list of possible roles
77          context.setAttribute(Constants.AVAILABLE_ROLES, mgr.getAllRoles());
78  
79          // get list of possible project statuses
80          context.setAttribute(Constants.AVAILABLE_PROJECT_STATUSES, mgr.getAllProjectStatuses());
81  
82          // get list of possible issue statuses
83          context.setAttribute(Constants.AVAILABLE_ISSUE_STATUSES, mgr.getAllIssueStatuses());
84  
85          // get list of possible issue severities
86          context.setAttribute(Constants.AVAILABLE_ISSUE_SEVERITIES, mgr.getAllIssueSeverities());
87  
88          // get list of possible issue resolutions
89          context.setAttribute(Constants.AVAILABLE_ISSUE_RESOLUTIONS, mgr.getAllIssueResolutions());
90          
91          // get list of possible issue resolutions
92          context.setAttribute(Constants.AVAILABLE_USERS, mgr.getAllUsers());
93          
94          if (log.isDebugEnabled()) {
95              log.debug("drop-down initialization complete [OK]");
96          }
97      }
98  }