001: /*
002: * $Id: TilesUtilStrutsModulesImpl.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: package org.apache.struts.tiles;
023:
024: import java.io.IOException;
025:
026: import javax.servlet.ServletContext;
027: import javax.servlet.ServletException;
028: import javax.servlet.ServletRequest;
029: import javax.servlet.http.HttpServletRequest;
030: import javax.servlet.http.HttpServletResponse;
031:
032: import org.apache.struts.Globals;
033: import org.apache.struts.config.ModuleConfig;
034: import org.apache.struts.util.ModuleUtils;
035:
036: /**
037: * Implementation of TilesUtil for Struts multi modules.
038: * Methods in this implementation are aware of the Struts module context.
039: * <br>
040: * <ul>
041: * <li>The method getFactory(...) returns the factory for the current Struts
042: * module.</li>
043: * <li>Methods doForward() and doInclude() use their counterparts in the
044: * current RequestProcessor (todo).</li>
045: * <li>The method createFactory(...) creates a factory for the current module and
046: * stores it under the appropriate property name.</li>
047: * </ul>
048: */
049: public class TilesUtilStrutsModulesImpl extends TilesUtilStrutsImpl {
050:
051: /**
052: * Do a forward using request dispatcher.
053: *
054: * This method is used by the Tiles package anytime a forward is required.
055: * @param uri Uri or Definition name to forward.
056: * @param request Current page request.
057: * @param response Current page response.
058: * @param servletContext Current servlet context.
059: */
060: public void doForward(String uri, HttpServletRequest request,
061: HttpServletResponse response, ServletContext servletContext)
062: throws IOException, ServletException {
063:
064: request.getRequestDispatcher(uri).forward(request, response);
065: }
066:
067: /**
068: * Do an include using request dispatcher.
069: *
070: * This method is used by the Tiles package anytime an include is required.
071: * @param uri Uri or Definition name to forward.
072: * @param request Current page request.
073: * @param response Current page response.
074: * @param servletContext Current servlet context.
075: */
076: public void doInclude(String uri, HttpServletRequest request,
077: HttpServletResponse response, ServletContext servletContext)
078: throws IOException, ServletException {
079:
080: request.getRequestDispatcher(uri).include(request, response);
081: }
082:
083: /**
084: * Get the definition factory from appropriate servlet context.
085: * @param request Current request.
086: * @param servletContext Current servlet context.
087: * @return Definitions factory or null if not found.
088: */
089: public DefinitionsFactory getDefinitionsFactory(
090: ServletRequest request, ServletContext servletContext) {
091:
092: return getDefinitionsFactory(servletContext, getModuleConfig(
093: (HttpServletRequest) request, servletContext));
094: }
095:
096: /**
097: * Get definition factory for the module attached to specified moduleConfig.
098: * @param servletContext Current servlet context.
099: * @param moduleConfig Module config of the module for which the factory is requested.
100: * @return Definitions factory or null if not found.
101: */
102: public DefinitionsFactory getDefinitionsFactory(
103: ServletContext servletContext, ModuleConfig moduleConfig) {
104:
105: return (DefinitionsFactory) servletContext
106: .getAttribute(DEFINITIONS_FACTORY
107: + moduleConfig.getPrefix());
108: }
109:
110: /**
111: * Make definition factory accessible to tags.
112: * Factory is stored in servlet context.
113: * @param factory Factory to be made accessible.
114: * @param servletContext Current servlet context.
115: */
116: protected void makeDefinitionsFactoryAccessible(
117: DefinitionsFactory factory, ServletContext servletContext) {
118:
119: String prefix = factory.getConfig().getFactoryName();
120: servletContext.setAttribute(DEFINITIONS_FACTORY + prefix,
121: factory);
122: }
123:
124: /**
125: * Get Tiles RequestProcessor associated to the current module.
126: * @param request Current request.
127: * @param servletContext Current servlet context.
128: * @return The {@link TilesRequestProcessor} for the current request.
129: */
130: protected TilesRequestProcessor getRequestProcessor(
131: HttpServletRequest request, ServletContext servletContext) {
132:
133: ModuleConfig moduleConfig = getModuleConfig(request,
134: servletContext);
135:
136: return (TilesRequestProcessor) servletContext
137: .getAttribute(Globals.REQUEST_PROCESSOR_KEY
138: + moduleConfig.getPrefix());
139: }
140:
141: /**
142: * Get the current ModuleConfig.
143: * <br>
144: * Lookup in the request and do selectModule if not found. The side effect
145: * is, that the ModuleConfig object is set in the request if it was not present.
146: * @param request Current request.
147: * @param servletContext Current servlet context*.
148: * @return The ModuleConfig for current request.
149: */
150: protected ModuleConfig getModuleConfig(HttpServletRequest request,
151: ServletContext servletContext) {
152:
153: ModuleConfig moduleConfig = ModuleUtils.getInstance()
154: .getModuleConfig(request);
155:
156: if (moduleConfig == null) {
157: // ModuleConfig not found in current request. Select it.
158: ModuleUtils.getInstance().selectModule(request,
159: servletContext);
160: moduleConfig = ModuleUtils.getInstance().getModuleConfig(
161: request);
162: }
163:
164: return moduleConfig;
165: }
166:
167: }
|