1
2
3
4
5
6 package net.sf.navigator.menu;
7
8
9 import net.sf.navigator.util.LoadableResourceException;
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.apache.struts.action.ActionServlet;
13 import org.apache.struts.action.PlugIn;
14 import org.apache.struts.config.ModuleConfig;
15
16 import javax.servlet.ServletException;
17 import javax.servlet.http.HttpServlet;
18
19
20 /**
21 * Struts plug-in adapter for the menuing module.
22 *
23 * @author ssayles
24 */
25 public class MenuPlugIn implements PlugIn {
26
27
28 /**
29 * The <code>Log</code> instance for this class.
30 */
31 private static Log log = LogFactory.getLog(MenuPlugIn.class);
32 private MenuRepository repository;
33 private String menuConfig = "/WEB-INF/menu-config.xml";
34 private HttpServlet servlet;
35
36
37
38 public String getMenuConfig() {
39 return menuConfig;
40 }
41
42 public void setMenuConfig(String menuConfig) {
43 this.menuConfig = menuConfig;
44 }
45
46 public void init(ActionServlet servlet, ModuleConfig config)
47 throws ServletException {
48 if (log.isDebugEnabled()) {
49 log.debug("Starting struts-menu initialization");
50 }
51
52 this.servlet = servlet;
53 repository = new MenuRepository();
54 repository.setLoadParam(menuConfig);
55 repository.setServletContext(servlet.getServletContext());
56
57 try {
58 repository.load();
59 servlet.getServletContext().setAttribute(MenuRepository.MENU_REPOSITORY_KEY, repository);
60
61 if (log.isDebugEnabled()) {
62 log.debug("struts-menu initialization successful");
63 }
64 } catch (LoadableResourceException lre) {
65 throw new ServletException("Failure initializing struts-menu: " +
66 lre.getMessage());
67 }
68 }
69
70 public void destroy() {
71 repository = null;
72 servlet.getServletContext().removeAttribute(MenuRepository.MENU_REPOSITORY_KEY);
73 menuConfig = null;
74 servlet = null;
75 }
76 }