? VelocityMenuDisplayer.patch Index: src/java/net/sf/navigator/displayer/VelocityMenuDisplayer.java =================================================================== RCS file: /cvsroot/struts-menu/navigator/src/java/net/sf/navigator/displayer/VelocityMenuDisplayer.java,v retrieving revision 1.11 diff -b -u -r1.11 VelocityMenuDisplayer.java --- src/java/net/sf/navigator/displayer/VelocityMenuDisplayer.java 12 May 2006 19:52:25 -0000 1.11 +++ src/java/net/sf/navigator/displayer/VelocityMenuDisplayer.java 2 Jun 2006 10:07:14 -0000 @@ -12,6 +12,7 @@ import org.apache.velocity.Template; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; +import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.app.tools.VelocityFormatter; import org.apache.velocity.runtime.RuntimeConstants; import org.apache.velocity.tools.view.servlet.ServletLogger; @@ -31,19 +32,20 @@ */ public class VelocityMenuDisplayer extends MessageResourcesMenuDisplayer { protected static final Log log = LogFactory.getLog(VelocityMenuDisplayer.class); + private static VelocityEngine velocityEngine = new VelocityEngine(); private PageContext pageContext = null; public static void initialize(ServletContext context) { // MR: Copied from VelocityViewServlet to initialize WebappLoader - Velocity.setApplicationAttribute(ServletContext.class.getName(), context); + velocityEngine.setApplicationAttribute(ServletContext.class.getName(), context); // default to servletlogger, which logs to the servlet engines log - Velocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, + velocityEngine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, ServletLogger.class.getName()); // by default, load resources with webapp resource loader - Velocity.setProperty(RuntimeConstants.RESOURCE_LOADER, "webapp"); - Velocity.setProperty("webapp.resource.loader.class", WebappLoader.class.getName()); + velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "webapp"); + velocityEngine.setProperty("webapp.resource.loader.class", WebappLoader.class.getName()); // now all is ready - init Velocity try { @@ -71,7 +73,7 @@ // only initialized the first time it's called, from: // http://jakarta.apache.org/velocity/developer-guide.html // it's ignored for subsequent calls - Velocity.init(props); + velocityEngine.init(props); } catch (Exception e) { log.error("Error initializing Velocity: " + e.getMessage()); e.printStackTrace(); @@ -110,7 +112,7 @@ log.debug("using template: " + template); } - t = Velocity.getTemplate(template); + t = velocityEngine.getTemplate(template); } catch (Exception e) { String msg = "Error initializing Velocity: " + e.toString(); log.error(msg, e); @@ -150,10 +152,12 @@ context.put("request", request); context.put("session", request.getSession(false)); - updateAllowedComponents(menu); + // clone menu so menu in servlet context isn't affected + MenuComponent copyOfMenu = (MenuComponent) menu.clone(); + updateAllowedComponents(copyOfMenu); context.put("menuId", pageContext.getAttribute("menuId")); - context.put("menu", menu); + context.put("menu", copyOfMenu); context.put("displayer", this); try { Index: src/java/net/sf/navigator/menu/MenuComponent.java =================================================================== RCS file: /cvsroot/struts-menu/navigator/src/java/net/sf/navigator/menu/MenuComponent.java,v retrieving revision 1.15 diff -b -u -r1.15 MenuComponent.java --- src/java/net/sf/navigator/menu/MenuComponent.java 12 May 2006 19:52:25 -0000 1.15 +++ src/java/net/sf/navigator/menu/MenuComponent.java 2 Jun 2006 10:07:14 -0000 @@ -6,12 +6,14 @@ package net.sf.navigator.menu; import org.apache.commons.lang.StringUtils; +import org.apache.commons.beanutils.BeanUtils; import java.io.Serializable; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Collections; +import java.lang.reflect.InvocationTargetException; /** @@ -21,7 +23,7 @@ * @author Scott Sayles, Matt Raible * @version $Revision: 1.15 $ $Date: 2006/05/12 19:52:25 $ */ -public class MenuComponent extends MenuBase implements Serializable, Component { +public class MenuComponent extends MenuBase implements Serializable, Component, Cloneable { //~ Static fields/initializers ============================================= protected static MenuComponent[] _menuComponent = new MenuComponent[0]; @@ -49,6 +51,13 @@ return (MenuComponent[]) menuComponents.toArray(_menuComponent); } + public void setMenuComponents(MenuComponent[] menuComponents) { + for (int i=0; i < menuComponents.length; i++) { + MenuComponent component = menuComponents[i]; + this.menuComponents.add(component); + } + } + public void setParent(MenuComponent parentMenu) { if (parentMenu != null) { // look up the parent and make sure that it has this menu as a child @@ -184,4 +193,16 @@ public String toString() { return "name: " + this.name; } + + public Object clone() { + try { + MenuComponent c = new MenuComponent(); + BeanUtils.copyProperties(c, this); + return c; + } catch (Exception e) { + e.printStackTrace(); + System.err.println(e.getMessage()); + return this; + } + } }