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 import org.springframework.context.ApplicationContextException; 7 import org.springframework.web.context.support.WebApplicationObjectSupport; 8 9 import javax.servlet.ServletException; 10 11 /** 12 * This loader is available for those that use the Spring Framework. To 13 * use it, simply configure it as follows in your applicationContext.xml file. 14 * </p> 15 * <pre> 16 * <bean id="menu" class="net.sf.navigator.menu.MenuLoader"> 17 * <property name="menuConfig"> 18 * <value>/WEB-INF/menu-config.xml</value> 19 * </property> 20 * </bean> 21 * </pre> 22 * <p>The menuConfig property is an optional attribute. It is set to 23 * /WEB-INF/menu-config.xml by default.</p> 24 * 25 * @author Matt Raible 26 */ 27 public class MenuLoader extends WebApplicationObjectSupport { 28 private static Log log = LogFactory.getLog(MenuLoader.class); 29 30 /** Configuration file for menus */ 31 private String menuConfig = "/WEB-INF/menu-config.xml"; 32 33 /** 34 * Set the Menu configuration file 35 * @param menuConfig the file containing the Menus/Items 36 */ 37 public void setMenuConfig(String menuConfig) { 38 this.menuConfig = menuConfig; 39 } 40 41 /** 42 * Initialization of the Menu Repository. 43 * @throws org.springframework.context.ApplicationContextException if an error occurs 44 */ 45 protected void initApplicationContext() throws ApplicationContextException { 46 try { 47 if (log.isDebugEnabled()) { 48 log.debug("Starting struts-menu initialization"); 49 } 50 51 MenuRepository repository = new MenuRepository(); 52 repository.setLoadParam(menuConfig); 53 repository.setServletContext(getServletContext()); 54 55 try { 56 repository.load(); 57 getServletContext().setAttribute(MenuRepository.MENU_REPOSITORY_KEY, repository); 58 59 if (log.isDebugEnabled()) { 60 log.debug("struts-menu initialization successful"); 61 } 62 } catch (LoadableResourceException lre) { 63 throw new ServletException("Failure initializing struts-menu: " + 64 lre.getMessage()); 65 } 66 } catch (Exception ex) { 67 throw new ApplicationContextException("Failed to initialize Struts Menu repository", 68 ex); 69 } 70 } 71 }