Source Code Cross Referenced for ModuleUtils.java in  » Web-Framework » struts-1.3.8 » org » apache » struts » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » struts 1.3.8 » org.apache.struts.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: ModuleUtils.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:        package org.apache.struts.util;
022:
023:        import org.apache.commons.logging.Log;
024:        import org.apache.commons.logging.LogFactory;
025:        import org.apache.struts.Globals;
026:        import org.apache.struts.action.RequestProcessor;
027:        import org.apache.struts.config.MessageResourcesConfig;
028:        import org.apache.struts.config.ModuleConfig;
029:
030:        import javax.servlet.ServletContext;
031:        import javax.servlet.http.HttpServletRequest;
032:
033:        /**
034:         * General purpose utility methods related to module processing.
035:         *
036:         * @version $Rev: 471754 $
037:         * @since Struts 1.2
038:         */
039:        public class ModuleUtils {
040:            /**
041:             * The Singleton instance.
042:             */
043:            private static final ModuleUtils instance = new ModuleUtils();
044:
045:            /**
046:             * Commons logging instance.
047:             */
048:            private static final Log log = LogFactory.getLog(ModuleUtils.class);
049:
050:            /**
051:             * Constructor for ModuleUtils.
052:             */
053:            protected ModuleUtils() {
054:                super ();
055:            }
056:
057:            /**
058:             * Returns the Singleton instance of TagUtils.
059:             */
060:            public static ModuleUtils getInstance() {
061:                return instance;
062:            }
063:
064:            /**
065:             * Return the current ModuleConfig object stored in request, if it exists,
066:             * null otherwise. This method can be used by plugin to retrieve the
067:             * current module config object. If no moduleConfig is found, this means
068:             * that the request haven't hit the server throught the struts servlet.
069:             * The appropriate module config can be set and found with <code>{@link
070:             * ModuleUtils#selectModule(HttpServletRequest, ServletContext)} </code>.
071:             *
072:             * @param request The servlet request we are processing
073:             * @return the ModuleConfig object from request, or null if none is set in
074:             *         the request.
075:             */
076:            public ModuleConfig getModuleConfig(HttpServletRequest request) {
077:                return (ModuleConfig) request.getAttribute(Globals.MODULE_KEY);
078:            }
079:
080:            /**
081:             * Return the desired ModuleConfig object stored in context, if it exists,
082:             * null otherwise.
083:             *
084:             * @param prefix  The module prefix of the desired module
085:             * @param context The ServletContext for this web application
086:             * @return the ModuleConfig object specified, or null if not found in the
087:             *         context.
088:             */
089:            public ModuleConfig getModuleConfig(String prefix,
090:                    ServletContext context) {
091:                if ((prefix == null) || "/".equals(prefix)) {
092:                    return (ModuleConfig) context
093:                            .getAttribute(Globals.MODULE_KEY);
094:                } else {
095:                    return (ModuleConfig) context
096:                            .getAttribute(Globals.MODULE_KEY + prefix);
097:                }
098:            }
099:
100:            /**
101:             * Return the desired ModuleConfig object stored in context, if it exists,
102:             * otherwise return the current ModuleConfig
103:             *
104:             * @param prefix  The module prefix of the desired module
105:             * @param request The servlet request we are processing
106:             * @param context The ServletContext for this web application
107:             * @return the ModuleConfig object specified, or null if not found in the
108:             *         context.
109:             */
110:            public ModuleConfig getModuleConfig(String prefix,
111:                    HttpServletRequest request, ServletContext context) {
112:                ModuleConfig moduleConfig = null;
113:
114:                if (prefix != null) {
115:                    //lookup module stored with the given prefix.
116:                    moduleConfig = this .getModuleConfig(prefix, context);
117:                } else {
118:                    //return the current module if no prefix was supplied.
119:                    moduleConfig = this .getModuleConfig(request, context);
120:                }
121:
122:                return moduleConfig;
123:            }
124:
125:            /**
126:             * Return the ModuleConfig object is it exists, null otherwise.
127:             *
128:             * @param request The servlet request we are processing
129:             * @param context The ServletContext for this web application
130:             * @return the ModuleConfig object
131:             */
132:            public ModuleConfig getModuleConfig(HttpServletRequest request,
133:                    ServletContext context) {
134:                ModuleConfig moduleConfig = this .getModuleConfig(request);
135:
136:                if (moduleConfig == null) {
137:                    moduleConfig = this .getModuleConfig("", context);
138:                    request.setAttribute(Globals.MODULE_KEY, moduleConfig);
139:                }
140:
141:                return moduleConfig;
142:            }
143:
144:            /**
145:             * Get the module name to which the specified request belong.
146:             *
147:             * @param request The servlet request we are processing
148:             * @param context The ServletContext for this web application
149:             * @return The module prefix or ""
150:             */
151:            public String getModuleName(HttpServletRequest request,
152:                    ServletContext context) {
153:                // Acquire the path used to compute the module
154:                String matchPath = (String) request
155:                        .getAttribute(RequestProcessor.INCLUDE_SERVLET_PATH);
156:
157:                if (matchPath == null) {
158:                    matchPath = request.getServletPath();
159:                }
160:
161:                return this .getModuleName(matchPath, context);
162:            }
163:
164:            /**
165:             * Get the module name to which the specified uri belong.
166:             *
167:             * @param matchPath The uri from which we want the module name.
168:             * @param context   The ServletContext for this web application
169:             * @return The module prefix or ""
170:             */
171:            public String getModuleName(String matchPath, ServletContext context) {
172:                if (log.isDebugEnabled()) {
173:                    log.debug("Get module name for path " + matchPath);
174:                }
175:
176:                String prefix = ""; // Initialize prefix before we try lookup
177:                String[] prefixes = getModulePrefixes(context);
178:
179:                // Get all other possible prefixes
180:                int lastSlash = 0; // Initialize before loop
181:
182:                while (prefix.equals("")
183:                        && ((lastSlash = matchPath.lastIndexOf("/")) > 0)) {
184:                    // We may be in a non-default module.  Try to get it's prefix.
185:                    matchPath = matchPath.substring(0, lastSlash);
186:
187:                    // Match against the list of module prefixes
188:                    for (int i = 0; i < prefixes.length; i++) {
189:                        if (matchPath.equals(prefixes[i])) {
190:                            prefix = prefixes[i];
191:
192:                            break;
193:                        }
194:                    }
195:                }
196:
197:                if (log.isDebugEnabled()) {
198:                    log.debug("Module name found: "
199:                            + (prefix.equals("") ? "default" : prefix));
200:                }
201:
202:                return prefix;
203:            }
204:
205:            /**
206:             * Return the list of module prefixes that are defined for this web
207:             * application.  <strong>NOTE</strong> - the "" prefix for the default
208:             * module is not included in this list.
209:             *
210:             * @param context The ServletContext for this web application.
211:             * @return An array of module prefixes.
212:             */
213:            public String[] getModulePrefixes(ServletContext context) {
214:                return (String[]) context
215:                        .getAttribute(Globals.MODULE_PREFIXES_KEY);
216:            }
217:
218:            /**
219:             * Select the module to which the specified request belongs, and add
220:             * corresponding request attributes to this request.
221:             *
222:             * @param request The servlet request we are processing
223:             * @param context The ServletContext for this web application
224:             */
225:            public void selectModule(HttpServletRequest request,
226:                    ServletContext context) {
227:                // Compute module name
228:                String prefix = getModuleName(request, context);
229:
230:                // Expose the resources for this module
231:                this .selectModule(prefix, request, context);
232:            }
233:
234:            /**
235:             * Select the module to which the specified request belongs, and add
236:             * corresponding request attributes to this request.
237:             *
238:             * @param prefix  The module prefix of the desired module
239:             * @param request The servlet request we are processing
240:             * @param context The ServletContext for this web application
241:             */
242:            public void selectModule(String prefix, HttpServletRequest request,
243:                    ServletContext context) {
244:                // Expose the resources for this module
245:                ModuleConfig config = getModuleConfig(prefix, context);
246:
247:                if (config != null) {
248:                    request.setAttribute(Globals.MODULE_KEY, config);
249:
250:                    MessageResourcesConfig[] mrConfig = config
251:                            .findMessageResourcesConfigs();
252:
253:                    for (int i = 0; i < mrConfig.length; i++) {
254:                        String key = mrConfig[i].getKey();
255:                        MessageResources resources = (MessageResources) context
256:                                .getAttribute(key + prefix);
257:
258:                        if (resources != null) {
259:                            request.setAttribute(key, resources);
260:                        } else {
261:                            request.removeAttribute(key);
262:                        }
263:                    }
264:                } else {
265:                    request.removeAttribute(Globals.MODULE_KEY);
266:                }
267:            }
268:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.