View Javadoc

1   package net.sf.navigator.menu;
2   
3   import net.sf.navigator.util.LoadableResourceException;
4   import org.apache.commons.logging.Log;
5   import org.apache.commons.logging.LogFactory;
6   
7   import javax.servlet.ServletContext;
8   import javax.servlet.ServletContextEvent;
9   import javax.servlet.ServletContextListener;
10  
11  
12  /**
13   * This class is designed for use in applications that don't use Struts
14   * but do want to use Struts Menu.  You simply need to configure this
15   * listener in your web.xml file with the following syntax:</p>
16   * <pre>
17   *   &lt;!--
18   *   - Loads the menu-config.xml for struts-menu at startup,
19   *   - by default from "/WEB-INF/menu-config.xml".
20   *   - To override this, add a context-param named "menuConfigLocation"
21   *   - web.xml file.
22   *  --&gt;
23   * &lt;listener&gt;
24   *  &lt;listener-class&gt;net.sf.navigator.menu.MenuContextListener&lt;/listener-class&gt;
25   * &lt;/listener&gt;
26   * </pre>
27   * 
28   * @author Matt Raible
29   */
30  public class MenuContextListener implements ServletContextListener {
31      private static Log log = LogFactory.getLog(MenuContextListener.class);
32      private ServletContext ctx;
33  
34      /** Configuration file for menus */
35      private String menuConfig = "/WEB-INF/menu-config.xml";
36  
37      /**
38       * Initialization of the Menu Repository.
39       */
40      public void contextInitialized(ServletContextEvent sce) {
41          ctx = sce.getServletContext();
42  
43          if (log.isDebugEnabled()) {
44              log.debug("Starting struts-menu initialization");
45          }
46          
47          // check for menuConfigLocation context-param
48          String override = 
49              sce.getServletContext().getInitParameter("menuConfigLocation");
50          if (override != null) {
51              if (log.isDebugEnabled()) {
52                  log.debug("using menuConfigLocation: " + override);
53              }
54              this.menuConfig = override;
55          }
56          
57          MenuRepository repository = new MenuRepository();
58          repository.setLoadParam(menuConfig);
59          repository.setServletContext(ctx);
60  
61          try {
62              repository.load();
63              ctx.setAttribute(MenuRepository.MENU_REPOSITORY_KEY, repository);
64  
65              if (log.isDebugEnabled()) {
66                  log.debug("struts-menu initialization successfull");
67              }
68          } catch (LoadableResourceException lre) {
69              log.fatal("Failure initializing struts-menu: " + lre.getMessage());
70          }
71      }
72  
73      public void contextDestroyed(ServletContextEvent sce) {
74          if (log.isDebugEnabled()) {
75              log.debug("destroying struts-menu...");
76          }
77  
78          sce.getServletContext().removeAttribute(MenuRepository.MENU_REPOSITORY_KEY);
79          menuConfig = null;
80          ctx = null;
81      }
82  }