dgraham 2003/08/16 11:20:08 Modified: src/tiles-documentation/org/apache/struts/webapp/tiles/portal UserMenuAction.java Log: Formatted and replaced hardcoded debugging with commons logging. Revision Changes Path 1.4 +204 -161 jakarta-struts/src/tiles-documentation/org/apache/struts/webapp/tiles/portal/UserMenuAction.java Index: UserMenuAction.java =================================================================== RCS file: /home/cvs/jakarta-struts/src/tiles-documentation/org/apache/struts/webapp/tiles/portal/UserMenuAction.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- UserMenuAction.java 21 Jul 2003 15:18:46 -0000 1.3 +++ UserMenuAction.java 16 Aug 2003 18:20:08 -0000 1.4 @@ -7,7 +7,7 @@ * * The Apache Software License, Version 1.1 * - * Copyright (c) 1999-2002 The Apache Software Foundation. All rights + * Copyright (c) 1999-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -72,6 +72,8 @@ import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; @@ -82,7 +84,6 @@ import org.apache.struts.tiles.actions.TilesAction; import org.apache.struts.tiles.beans.MenuItem; - /** * This controller load user menu settings and put them in tile context. * If menu settings are not defined for user, defined them based on tiles @@ -110,25 +111,46 @@ * @author Cedric Dumoulin * @version $Revision$ $Date$ */ - public final class UserMenuAction extends TilesAction implements Controller { - /** debug flag */ - public static boolean debug = false; + /** + * Commons Logging instance. + */ + private static Log log = LogFactory.getLog(UserMenuAction.class); - /** Tile attribute containing name used to store user settings in session context */ + /** + * Tile attribute containing name used to store user settings in session + * context. + */ public static String USER_SETTINGS_NAME_ATTRIBUTE = "userSettingsName"; - /** Default name used to store settings in session context */ - public static String DEFAULT_USER_SETTINGS_NAME = "tiles.examples.portal.USER_MENU_SETTINGS"; - /** Default name used to store menu catalog in application scope */ - public static String DEFAULT_MENU_CATALOG_NAME = "tiles.examples.portal.MenuCatalog"; - /** Tile attribute containing name used to store menu catalog in application scope */ + /** + * Default name used to store settings in session context. + */ + public static String DEFAULT_USER_SETTINGS_NAME = + "tiles.examples.portal.USER_MENU_SETTINGS"; + + /** + * Default name used to store menu catalog in application scope. + */ + public static String DEFAULT_MENU_CATALOG_NAME = + "tiles.examples.portal.MenuCatalog"; + + /** + * Tile attribute containing name used to store menu catalog in application + * scope. + */ public static String MENU_CATALOG_NAME_ATTRIBUTE = "catalogName"; - /** Tile attribute containing name of the settings definition used to initialize catalog */ + + /** + * Tile attribute containing name of the settings definition used to + * initialize catalog. + */ public static final String CATALOG_SETTING_ATTRIBUTE = "catalogSettings"; - /** Tile attribute containing items to render */ + /** + * Tile attribute containing items to render. + */ public static String USER_ITEMS_ATTRIBUTE = "items"; /** @@ -155,37 +177,37 @@ ActionForm form, HttpServletRequest request, HttpServletResponse response) - throws Exception - { - perform( context, request, response, getServlet().getServletContext() ); - return null; //(mapping.findForward("success")); + throws Exception { + + perform(context, request, response, getServlet().getServletContext()); + return null; //(mapping.findForward("success")); } - /** - * Method associated to a tile and called immediately before tile is included. - * @param tileContext Current tile context. - * @param request Current request - * @param response Current response - * @param servletContext Current servlet context - */ - public void perform(ComponentContext context, - HttpServletRequest request, HttpServletResponse response, - ServletContext servletContext) - throws ServletException, IOException - { - if(debug) - System.out.println("Enter action UserMenuAction"); - - // Load user settings from user context - MenuSettings settings = getUserSettings( request, context); - // Set parameters for rendering page - context.putAttribute( USER_ITEMS_ATTRIBUTE, settings.getItems() ); - - if(debug) - { - System.out.println( "settings=" + settings ); - System.out.println("Exit action UserMenuAction"); - } + /** + * Method associated to a tile and called immediately before tile is included. + * @param tileContext Current tile context. + * @param request Current request + * @param response Current response + * @param servletContext Current servlet context + */ + public void perform( + ComponentContext context, + HttpServletRequest request, + HttpServletResponse response, + ServletContext servletContext) + throws ServletException, IOException { + + log.debug("Enter action UserMenuAction"); + + // Load user settings from user context + MenuSettings settings = getUserSettings(request, context); + + // Set parameters for rendering page + context.putAttribute(USER_ITEMS_ATTRIBUTE, settings.getItems()); + + log.debug("settings=" + settings); + log.debug("Exit action UserMenuAction"); + } /** @@ -195,130 +217,151 @@ * in Tile's context. * If settings are not found, initialized them. */ - public static MenuSettings getUserSettings( HttpServletRequest request, ComponentContext context ) - throws ServletException - { - // Get current session. - HttpSession session = request.getSession(); - - // Retrieve attribute name used to store settings. - String userSettingsName = (String)context.getAttribute( USER_SETTINGS_NAME_ATTRIBUTE ); - if( userSettingsName == null ) - userSettingsName = DEFAULT_USER_SETTINGS_NAME; - - // Get user list from user context - MenuSettings settings = (MenuSettings)session.getAttribute( userSettingsName ); - - // If settings don't exist, create and initialize them - // Initialization is done from context attribute denoted by ITEMS - if( settings == null ) - { // List doesn't exist, create it and initialize it from Tiles parameters - settings = new MenuSettings(); - try { - settings.addItems( (List)context.getAttribute( USER_ITEMS_ATTRIBUTE ) ); + public static MenuSettings getUserSettings( + HttpServletRequest request, + ComponentContext context) + throws ServletException { + + // Get current session. + HttpSession session = request.getSession(); + + // Retrieve attribute name used to store settings. + String userSettingsName = + (String) context.getAttribute(USER_SETTINGS_NAME_ATTRIBUTE); + + if (userSettingsName == null) { + userSettingsName = DEFAULT_USER_SETTINGS_NAME; } - catch( ClassCastException ex ) - { - throw new ServletException( "Can't initialize user menu : default items must be a list of items" ); + + // Get user list from user context + MenuSettings settings = + (MenuSettings) session.getAttribute(userSettingsName); + + // If settings don't exist, create and initialize them + // Initialization is done from context attribute denoted by ITEMS + if (settings == null) { + // List doesn't exist, create it and initialize it from Tiles parameters + settings = new MenuSettings(); + try { + settings.addItems((List) context.getAttribute(USER_ITEMS_ATTRIBUTE)); + } catch (ClassCastException ex) { + throw new ServletException("Can't initialize user menu : default items must be a list of items"); + } + // Save user settings in session + session.setAttribute(userSettingsName, settings); } - // Save user settings in session - session.setAttribute( userSettingsName, settings); - } // end if - - return settings; - } - - /** - * Get catalog of available menu entries. - * This implementation create catalog list from the provided menu bar entries. - * - * as Tiles attribute. - * Create it from default values if needed. - */ - static public List getCatalog( ComponentContext context, HttpServletRequest request, ServletContext servletContext) - throws ServletException - { + + return settings; + } + + /** + * Get catalog of available menu entries. + * This implementation create catalog list from the provided menu bar entries. + * + * as Tiles attribute. + * Create it from default values if needed. + */ + public static List getCatalog( + ComponentContext context, + HttpServletRequest request, + ServletContext servletContext) + throws ServletException { + // Retrieve name used to store catalog in application context. // If not found, use default name - String catalogName = (String)context.getAttribute( MENU_CATALOG_NAME_ATTRIBUTE ); - if(catalogName == null) - catalogName = DEFAULT_MENU_CATALOG_NAME; + String catalogName = + (String) context.getAttribute(MENU_CATALOG_NAME_ATTRIBUTE); + + if (catalogName == null) { + catalogName = DEFAULT_MENU_CATALOG_NAME; + } // Get catalog from context - List catalog = (List)servletContext.getAttribute( catalogName ); + List catalog = (List) servletContext.getAttribute(catalogName); + // If not found, initialize it from provided default menu - if( catalog == null ) - { - Object menuBar = context.getAttribute( CATALOG_SETTING_ATTRIBUTE ); - if( menuBar == null ) - throw new ServletException( "Attribute '" + CATALOG_SETTING_ATTRIBUTE + "' must be set. It define entries used in catalog" ); - catalog = new ArrayList(); - extractItems(catalog, menuBar, request, servletContext); - if( catalog.size() == 0 ) - throw new ServletException( "Can't initialize menu items catalog" ); - - // save it for future use - servletContext.setAttribute( catalogName, catalog ); - } // end if - return catalog; - } - /** - * Extract menu items from passed object. Items are stored in parameter 'result'. - * This method allows to create a list of menu entries from existing menus. - * Check object type class : - * <li> - * <ul>MenuItems : add it</ul> - * <ul>ComponentDefinition : get attribute items, or list if not found. - * Call ExtractItems with resulting attribute. - * </ul> - * <ul>List : iterate on list, and call ExtractItems for each element. - * </li> - * @param result result list (should be initialized) - * @param object object to add (MenuItems, Definition, ...) - * @param request current request - * @param servletContext current servlet context. - */ - static private void extractItems( List result, Object object, HttpServletRequest request, ServletContext servletContext) - { - if(debug) - System.out.println( "Extract menu item from '" + object + "'" ); - if( object instanceof String ) - { // definition name - try { - ComponentDefinition def = DefinitionsUtil.getDefinition( (String)object, request, servletContext ); - extractItems( result, def, request, servletContext ); - } - catch( Exception ex ) - { // silently fail - } + if (catalog == null) { + Object menuBar = context.getAttribute(CATALOG_SETTING_ATTRIBUTE); + if (menuBar == null) { + throw new ServletException( + "Attribute '" + + CATALOG_SETTING_ATTRIBUTE + + "' must be set. It define entries used in catalog"); + } + + catalog = new ArrayList(); + extractItems(catalog, menuBar, request, servletContext); + if (catalog.size() == 0) { + throw new ServletException("Can't initialize menu items catalog"); + } + // save it for future use + servletContext.setAttribute(catalogName, catalog); } - else if( object instanceof List ) - { - List list = (List)object; - Iterator iter = list.iterator(); - while( iter.hasNext() ) - { - extractItems( result, iter.next(), request, servletContext ); - } // end loop - } - else if( object instanceof ComponentDefinition ) - { - ComponentDefinition definition = (ComponentDefinition)object; - Object attribute = definition.getAttribute( "items" ); - if( attribute == null ) - attribute = definition.getAttribute( "list" ); - if( attribute == null ) - { - return; - } - extractItems( result, attribute, request, servletContext ); - } - else if( object instanceof MenuItem ) - { - result.add( object ); + + return catalog; + } + + /** + * Extract menu items from passed object. Items are stored in parameter 'result'. + * This method allows to create a list of menu entries from existing menus. + * Check object type class : + * <li> + * <ul>MenuItems : add it</ul> + * <ul>ComponentDefinition : get attribute items, or list if not found. + * Call ExtractItems with resulting attribute. + * </ul> + * <ul>List : iterate on list, and call ExtractItems for each element. + * </li> + * @param result result list (should be initialized) + * @param object object to add (MenuItems, Definition, ...) + * @param request current request + * @param servletContext current servlet context. + */ + private static void extractItems( + List result, + Object object, + HttpServletRequest request, + ServletContext servletContext) { + + log.debug("Extract menu item from '" + object + "'"); + + if (object instanceof String) { // definition name + try { + ComponentDefinition def = + DefinitionsUtil.getDefinition( + (String) object, + request, + servletContext); + + extractItems(result, def, request, servletContext); + + } catch (Exception ex) { // silently fail + } + + } else if (object instanceof List) { + List list = (List) object; + Iterator iter = list.iterator(); + while (iter.hasNext()) { + extractItems(result, iter.next(), request, servletContext); + } + + } else if (object instanceof ComponentDefinition) { + ComponentDefinition definition = (ComponentDefinition) object; + Object attribute = definition.getAttribute("items"); + if (attribute == null) { + attribute = definition.getAttribute("list"); + } + + if (attribute == null) { + return; + } + + extractItems(result, attribute, request, servletContext); + + } else if (object instanceof MenuItem) { + result.add(object); } - } + } } -
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]