KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > webapp > tiles > skin > SimpleSwitchLayoutAction


1 /*
2  * $Id: SimpleSwitchLayoutAction.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 1999-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.struts.webapp.tiles.skin;
20
21 import javax.servlet.RequestDispatcher JavaDoc;
22 import javax.servlet.ServletContext JavaDoc;
23 import javax.servlet.ServletException JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26 import javax.servlet.http.HttpSession JavaDoc;
27 import org.apache.struts.action.ActionForm;
28 import org.apache.struts.action.ActionForward;
29 import org.apache.struts.action.ActionMapping;
30 import org.apache.struts.tiles.ComponentContext;
31 import org.apache.struts.tiles.DefinitionsFactoryException;
32 import org.apache.struts.tiles.actions.TilesAction;
33
34   /**
35    * Simple Switch Layout
36    */

37 public class SimpleSwitchLayoutAction extends TilesAction
38 {
39     /** debug flag */
40   public static boolean debug = true;
41     /** Tile's attribute containing layout key */
42   public static final String JavaDoc LAYOUT_ATTRIBUTE = "layout.attribute";
43     /** Tile attribute containing name used to store user settings in session context */
44   public static String JavaDoc USER_SETTINGS_NAME_ATTRIBUTE = "userSettingsName";
45     /** Default name used to store settings in session context */
46   public static String JavaDoc DEFAULT_USER_SETTINGS_NAME = "examples.tiles.skin.SELECTED_DEFINITION";
47
48     /** Name of catalog in application context */
49   public static final String JavaDoc CATALOG_NAME = "examples.tiles.skin.CATALOG_NAME";
50
51     /** Default name used to store menu catalog in application scope */
52   public static String JavaDoc DEFAULT_CATALOG_NAME = "tiles.examples.skin.layoutCatalog";
53     /** Tile attribute containing name used to store menu catalog in application scope */
54   public static String JavaDoc CATALOG_NAME_ATTRIBUTE = "catalogName";
55     /** Tile attribute containing name of the settings definition used to initialize catalog */
56   public static final String JavaDoc CATALOG_SETTING_ATTRIBUTE = "catalogSettings";
57
58     /**
59      * Process the specified HTTP request, and create the corresponding HTTP
60      * response (or forward to another web component that will create it).
61      * Return an <code>ActionForward</code> instance describing where and how
62      * control should be forwarded, or <code>null</code> if the response has
63      * already been completed.
64      * This method should be implemented by subclasses.
65      *
66      * @param context The current Tile context, containing Tile attributes.
67      * @param mapping The ActionMapping used to select this instance.
68      * @param form The optional ActionForm bean for this request (if any).
69      * @param request The HTTP request we are processing.
70      * @param response The HTTP response we are creating.
71      *
72      * @exception Exception if the application business logic throws
73      * an exception
74      */

75     public ActionForward execute(
76         ComponentContext context,
77         ActionMapping mapping,
78         ActionForm form,
79         HttpServletRequest JavaDoc request,
80         HttpServletResponse JavaDoc response)
81         throws Exception JavaDoc
82     {
83     if(debug)
84       System.out.println( "Enter SimpleSwitchLayoutAction" );
85
86     String JavaDoc layoutDir = "/layouts/";
87     String JavaDoc userSelection = getUserSetting( context, request );
88     //String layout = "classicLayout.jsp";
89
String JavaDoc layout = (String JavaDoc)context.getAttribute( LAYOUT_ATTRIBUTE );
90     if(layout==null)
91       throw new ServletException JavaDoc( "Attribute '" + LAYOUT_ATTRIBUTE + "' is required." );
92
93     String JavaDoc layoutPath = layoutDir+userSelection+ "/" + layout;
94
95     RequestDispatcher JavaDoc rd = getServlet().getServletContext().getRequestDispatcher( layoutPath );
96     if(rd==null)
97       {
98       layoutPath = layoutDir + layout;
99       rd = getServlet().getServletContext().getRequestDispatcher( layoutPath );
100       if(rd==null)
101         throw new ServletException JavaDoc( "SwitchLayout error : Can't find layout '"
102                                   + layoutPath + "'." );
103       }
104     rd.include(request, response);
105     if(debug)
106       System.out.println( "Exit SimpleSwitchLayoutAction" );
107     return null;
108     }
109
110     /**
111      * Retrieve key associated to user.
112      * This key denote a definition in catalog.
113      * Return user selected key, or "default" if none is set.
114      */

115   public static String JavaDoc getUserSetting( ComponentContext context, HttpServletRequest JavaDoc request )
116   {
117   HttpSession JavaDoc session = request.getSession( false );
118   if( session == null )
119     return null;
120
121     // Retrieve attribute name used to store settings.
122
String JavaDoc userSettingsName = (String JavaDoc)context.getAttribute( USER_SETTINGS_NAME_ATTRIBUTE );
123   if( userSettingsName == null )
124     userSettingsName = DEFAULT_USER_SETTINGS_NAME;
125
126   return (String JavaDoc)session.getAttribute(userSettingsName);
127   }
128
129     /**
130      * Set user setting value.
131      * This key denote a definition in catalog.
132      * Return user selected key, or "default" if none is set.
133      */

134   public static void setUserSetting( ComponentContext context, HttpServletRequest JavaDoc request, String JavaDoc setting )
135   {
136   HttpSession JavaDoc session = request.getSession();
137
138     // Retrieve attribute name used to store settings.
139
String JavaDoc userSettingsName = (String JavaDoc)context.getAttribute( USER_SETTINGS_NAME_ATTRIBUTE );
140   if( userSettingsName == null )
141     userSettingsName = DEFAULT_USER_SETTINGS_NAME;
142
143   session.setAttribute(userSettingsName, setting);
144   }
145
146     /**
147      * Get catalog of available skins.
148      */

149   public static DefinitionCatalog getCatalog( ComponentContext context, HttpServletRequest JavaDoc request, ServletContext JavaDoc servletContext )
150     throws ServletException JavaDoc
151   {
152     // Retrieve name used to store catalog in application context.
153
// If not found, use default name
154
String JavaDoc catalogName = (String JavaDoc)context.getAttribute( CATALOG_NAME_ATTRIBUTE );
155   if(catalogName == null)
156     catalogName = DEFAULT_CATALOG_NAME;
157
158   if(debug)
159     System.out.println( "Catalog name=" + catalogName );
160   try
161     {
162     DefinitionCatalog catalog = (DefinitionCatalog)servletContext.getAttribute( catalogName );
163     if(catalog == null)
164       { // create catalog
165
if(debug)
166         System.out.println( "Create catalog" );
167       String JavaDoc catalogSettings = (String JavaDoc)context.getAttribute( CATALOG_SETTING_ATTRIBUTE );
168       if(catalogSettings == null)
169         throw new ServletException JavaDoc( "Error - CustomSkinAction : attribute '"
170                                   + CATALOG_SETTING_ATTRIBUTE
171                                   + "' not found in Tile's attributes. Need it to initialize catalog" );
172       catalog = new DefinitionCatalog( catalogSettings, request, servletContext );
173       if(debug)
174         System.out.println( "Catalog created" );
175       servletContext.setAttribute( catalogName, catalog );
176       } // end if
177
return catalog;
178     }
179    catch(DefinitionsFactoryException ex )
180     {
181     if(debug)
182         System.out.println( "Exception : " + ex.getMessage() );
183     throw new ServletException JavaDoc( ex.getMessage() );
184     }
185   }
186 }
187
Popular Tags