001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Portions Copyright Apache Software Foundation.
007: *
008: * The contents of this file are subject to the terms of either the GNU
009: * General Public License Version 2 only ("GPL") or the Common Development
010: * and Distribution License("CDDL") (collectively, the "License"). You
011: * may not use this file except in compliance with the License. You can obtain
012: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
013: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
014: * language governing permissions and limitations under the License.
015: *
016: * When distributing the software, include this License Header Notice in each
017: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
018: * Sun designates this particular file as subject to the "Classpath" exception
019: * as provided by Sun in the GPL Version 2 section of the License file that
020: * accompanied this code. If applicable, add the following below the License
021: * Header, with the fields enclosed by brackets [] replaced by your own
022: * identifying information: "Portions Copyrighted [year]
023: * [name of copyright owner]"
024: *
025: * Contributor(s):
026: *
027: * If you wish your version of this file to be governed by only the CDDL or
028: * only the GPL Version 2, indicate your decision by adding "[Contributor]
029: * elects to include this software in this distribution under the [CDDL or GPL
030: * Version 2] license." If you don't indicate a single choice of license, a
031: * recipient has the option to distribute your version of this file under
032: * either the CDDL, the GPL Version 2 or to extend the choice of license to
033: * its licensees as provided above. However, if you add GPL Version 2 code
034: * and therefore, elected the GPL Version 2 license, then the option applies
035: * only if the new code is made subject to such option by the copyright
036: * holder.
037: */
038:
039: package javax.servlet.jsp.jstl.core;
040:
041: import javax.servlet.ServletContext;
042: import javax.servlet.ServletRequest;
043: import javax.servlet.http.HttpSession;
044: import javax.servlet.jsp.PageContext;
045:
046: /**
047: * Class supporting access to configuration settings.
048: */
049: public class Config {
050:
051: /*
052: * I18N/Formatting actions related configuration data
053: */
054:
055: /**
056: * Name of configuration setting for application- (as opposed to browser-)
057: * based preferred locale
058: */
059: public static final String FMT_LOCALE = "javax.servlet.jsp.jstl.fmt.locale";
060:
061: /**
062: * Name of configuration setting for fallback locale
063: */
064: public static final String FMT_FALLBACK_LOCALE = "javax.servlet.jsp.jstl.fmt.fallbackLocale";
065:
066: /**
067: * Name of configuration setting for i18n localization context
068: */
069: public static final String FMT_LOCALIZATION_CONTEXT = "javax.servlet.jsp.jstl.fmt.localizationContext";
070:
071: /**
072: * Name of localization setting for time zone
073: */
074: public static final String FMT_TIME_ZONE = "javax.servlet.jsp.jstl.fmt.timeZone";
075:
076: /*
077: * SQL actions related configuration data
078: */
079:
080: /**
081: * Name of configuration setting for SQL data source
082: */
083: public static final String SQL_DATA_SOURCE = "javax.servlet.jsp.jstl.sql.dataSource";
084:
085: /**
086: * Name of configuration setting for maximum number of rows to be included
087: * in SQL query result
088: */
089: public static final String SQL_MAX_ROWS = "javax.servlet.jsp.jstl.sql.maxRows";
090:
091: /*
092: * Private constants
093: */
094: private static final String PAGE_SCOPE_SUFFIX = ".page";
095: private static final String REQUEST_SCOPE_SUFFIX = ".request";
096: private static final String SESSION_SCOPE_SUFFIX = ".session";
097: private static final String APPLICATION_SCOPE_SUFFIX = ".application";
098:
099: /**
100: * Looks up a configuration variable in the given scope.
101: *
102: * <p> The lookup of configuration variables is performed as if each scope
103: * had its own name space, that is, the same configuration variable name
104: * in one scope does not replace one stored in a different scope.
105: *
106: * @param pc Page context in which the configuration variable is to be
107: * looked up
108: * @param name Configuration variable name
109: * @param scope Scope in which the configuration variable is to be looked
110: * up
111: *
112: * @return The <tt>java.lang.Object</tt> associated with the configuration
113: * variable, or null if it is not defined.
114: */
115: public static Object get(PageContext pc, String name, int scope) {
116: switch (scope) {
117: case PageContext.PAGE_SCOPE:
118: return pc.getAttribute(name + PAGE_SCOPE_SUFFIX, scope);
119: case PageContext.REQUEST_SCOPE:
120: return pc.getAttribute(name + REQUEST_SCOPE_SUFFIX, scope);
121: case PageContext.SESSION_SCOPE:
122: return get(pc.getSession(), name);
123: case PageContext.APPLICATION_SCOPE:
124: return pc.getAttribute(name + APPLICATION_SCOPE_SUFFIX,
125: scope);
126: default:
127: throw new IllegalArgumentException("unknown scope");
128: }
129: }
130:
131: /**
132: * Looks up a configuration variable in the "request" scope.
133: *
134: * <p> The lookup of configuration variables is performed as if each scope
135: * had its own name space, that is, the same configuration variable name
136: * in one scope does not replace one stored in a different scope.
137: *
138: * @param request Request object in which the configuration variable is to
139: * be looked up
140: * @param name Configuration variable name
141: *
142: * @return The <tt>java.lang.Object</tt> associated with the configuration
143: * variable, or null if it is not defined.
144: */
145: public static Object get(ServletRequest request, String name) {
146: return request.getAttribute(name + REQUEST_SCOPE_SUFFIX);
147: }
148:
149: /**
150: * Looks up a configuration variable in the "session" scope.
151: *
152: * <p> The lookup of configuration variables is performed as if each scope
153: * had its own name space, that is, the same configuration variable name
154: * in one scope does not replace one stored in a different scope.</p>
155: *
156: * @param session Session object in which the configuration variable is to
157: * be looked up
158: * @param name Configuration variable name
159: *
160: * @return The <tt>java.lang.Object</tt> associated with the configuration
161: * variable, or null if it is not defined, if session is null, or if the session
162: * is invalidated.
163: */
164: public static Object get(HttpSession session, String name) {
165: Object ret = null;
166: if (session != null) {
167: try {
168: ret = session.getAttribute(name + SESSION_SCOPE_SUFFIX);
169: } catch (IllegalStateException ex) {
170: } // when session is invalidated
171: }
172: return ret;
173: }
174:
175: /**
176: * Looks up a configuration variable in the "application" scope.
177: *
178: * <p> The lookup of configuration variables is performed as if each scope
179: * had its own name space, that is, the same configuration variable name
180: * in one scope does not replace one stored in a different scope.
181: *
182: * @param context Servlet context in which the configuration variable is
183: * to be looked up
184: * @param name Configuration variable name
185: *
186: * @return The <tt>java.lang.Object</tt> associated with the configuration
187: * variable, or null if it is not defined.
188: */
189: public static Object get(ServletContext context, String name) {
190: return context.getAttribute(name + APPLICATION_SCOPE_SUFFIX);
191: }
192:
193: /**
194: * Sets the value of a configuration variable in the given scope.
195: *
196: * <p> Setting the value of a configuration variable is performed as if
197: * each scope had its own namespace, that is, the same configuration
198: * variable name in one scope does not replace one stored in a different
199: * scope.
200: *
201: * @param pc Page context in which the configuration variable is to be set
202: * @param name Configuration variable name
203: * @param value Configuration variable value
204: * @param scope Scope in which the configuration variable is to be set
205: */
206: public static void set(PageContext pc, String name, Object value,
207: int scope) {
208: switch (scope) {
209: case PageContext.PAGE_SCOPE:
210: pc.setAttribute(name + PAGE_SCOPE_SUFFIX, value, scope);
211: break;
212: case PageContext.REQUEST_SCOPE:
213: pc.setAttribute(name + REQUEST_SCOPE_SUFFIX, value, scope);
214: break;
215: case PageContext.SESSION_SCOPE:
216: pc.setAttribute(name + SESSION_SCOPE_SUFFIX, value, scope);
217: break;
218: case PageContext.APPLICATION_SCOPE:
219: pc.setAttribute(name + APPLICATION_SCOPE_SUFFIX, value,
220: scope);
221: break;
222: default:
223: throw new IllegalArgumentException("unknown scope");
224: }
225: }
226:
227: /**
228: * Sets the value of a configuration variable in the "request" scope.
229: *
230: * <p> Setting the value of a configuration variable is performed as if
231: * each scope had its own namespace, that is, the same configuration
232: * variable name in one scope does not replace one stored in a different
233: * scope.
234: *
235: * @param request Request object in which the configuration variable is to
236: * be set
237: * @param name Configuration variable name
238: * @param value Configuration variable value
239: */
240: public static void set(ServletRequest request, String name,
241: Object value) {
242: request.setAttribute(name + REQUEST_SCOPE_SUFFIX, value);
243: }
244:
245: /**
246: * Sets the value of a configuration variable in the "session" scope.
247: *
248: * <p> Setting the value of a configuration variable is performed as if
249: * each scope had its own namespace, that is, the same configuration
250: * variable name in one scope does not replace one stored in a different
251: * scope.
252: *
253: * @param session Session object in which the configuration variable is to
254: * be set
255: * @param name Configuration variable name
256: * @param value Configuration variable value
257: */
258: public static void set(HttpSession session, String name,
259: Object value) {
260: session.setAttribute(name + SESSION_SCOPE_SUFFIX, value);
261: }
262:
263: /**
264: * Sets the value of a configuration variable in the "application" scope.
265: *
266: * <p> Setting the value of a configuration variable is performed as if
267: * each scope had its own namespace, that is, the same configuration
268: * variable name in one scope does not replace one stored in a different
269: * scope.
270: *
271: * @param context Servlet context in which the configuration variable is to
272: * be set
273: * @param name Configuration variable name
274: * @param value Configuration variable value
275: */
276: public static void set(ServletContext context, String name,
277: Object value) {
278: context.setAttribute(name + APPLICATION_SCOPE_SUFFIX, value);
279: }
280:
281: /**
282: * Removes a configuration variable from the given scope.
283: *
284: * <p> Removing a configuration variable is performed as if each scope had
285: * its own namespace, that is, the same configuration variable name in one
286: * scope does not impact one stored in a different scope.
287: *
288: * @param pc Page context from which the configuration variable is to be
289: * removed
290: * @param name Configuration variable name
291: * @param scope Scope from which the configuration variable is to be
292: * removed
293: */
294: public static void remove(PageContext pc, String name, int scope) {
295: switch (scope) {
296: case PageContext.PAGE_SCOPE:
297: pc.removeAttribute(name + PAGE_SCOPE_SUFFIX, scope);
298: break;
299: case PageContext.REQUEST_SCOPE:
300: pc.removeAttribute(name + REQUEST_SCOPE_SUFFIX, scope);
301: break;
302: case PageContext.SESSION_SCOPE:
303: pc.removeAttribute(name + SESSION_SCOPE_SUFFIX, scope);
304: break;
305: case PageContext.APPLICATION_SCOPE:
306: pc.removeAttribute(name + APPLICATION_SCOPE_SUFFIX, scope);
307: break;
308: default:
309: throw new IllegalArgumentException("unknown scope");
310: }
311: }
312:
313: /**
314: * Removes a configuration variable from the "request" scope.
315: *
316: * <p> Removing a configuration variable is performed as if each scope had
317: * its own namespace, that is, the same configuration variable name in one
318: * scope does not impact one stored in a different scope.
319: *
320: * @param request Request object from which the configuration variable is
321: * to be removed
322: * @param name Configuration variable name
323: */
324: public static void remove(ServletRequest request, String name) {
325: request.removeAttribute(name + REQUEST_SCOPE_SUFFIX);
326: }
327:
328: /**
329: * Removes a configuration variable from the "session" scope.
330: *
331: * <p> Removing a configuration variable is performed as if each scope had
332: * its own namespace, that is, the same configuration variable name in one
333: * scope does not impact one stored in a different scope.
334: *
335: * @param session Session object from which the configuration variable is
336: * to be removed
337: * @param name Configuration variable name
338: */
339: public static void remove(HttpSession session, String name) {
340: session.removeAttribute(name + SESSION_SCOPE_SUFFIX);
341: }
342:
343: /**
344: * Removes a configuration variable from the "application" scope.
345: *
346: * <p> Removing a configuration variable is performed as if each scope had
347: * its own namespace, that is, the same configuration variable name in one
348: * scope does not impact one stored in a different scope.
349: *
350: * @param context Servlet context from which the configuration variable is
351: * to be removed
352: * @param name Configuration variable name
353: */
354: public static void remove(ServletContext context, String name) {
355: context.removeAttribute(name + APPLICATION_SCOPE_SUFFIX);
356: }
357:
358: /**
359: * Finds the value associated with a specific configuration setting
360: * identified by its context initialization parameter name.
361: *
362: * <p> For each of the JSP scopes (page, request, session, application),
363: * get the value of the configuration variable identified by <tt>name</tt>
364: * using method <tt>get()</tt>. Return as soon as a non-null value is
365: * found. If no value is found, get the value of the context initialization
366: * parameter identified by <tt>name</tt>.
367: *
368: * @param pc Page context in which the configuration setting is to be
369: * searched
370: * @param name Context initialization parameter name of the configuration
371: * setting
372: *
373: * @return The <tt>java.lang.Object</tt> associated with the configuration
374: * setting identified by <tt>name</tt>, or null if it is not defined.
375: */
376: public static Object find(PageContext pc, String name) {
377: Object ret = get(pc, name, PageContext.PAGE_SCOPE);
378: if (ret == null) {
379: ret = get(pc, name, PageContext.REQUEST_SCOPE);
380: if (ret == null) {
381: if (pc.getSession() != null) {
382: // check session only if a session is present
383: ret = get(pc, name, PageContext.SESSION_SCOPE);
384: }
385: if (ret == null) {
386: ret = get(pc, name, PageContext.APPLICATION_SCOPE);
387: if (ret == null) {
388: ret = pc.getServletContext().getInitParameter(
389: name);
390: }
391: }
392: }
393: }
394:
395: return ret;
396: }
397: }
|