KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > servlet > jsp > jstl > core > Config


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

16
17 package javax.servlet.jsp.jstl.core;
18
19 import javax.servlet.ServletContext JavaDoc;
20 import javax.servlet.ServletRequest JavaDoc;
21 import javax.servlet.http.HttpSession JavaDoc;
22 import javax.servlet.jsp.PageContext JavaDoc;
23
24 /**
25  * Class supporting access to configuration settings.
26  */

27 public class Config {
28
29     /*
30      * I18N/Formatting actions related configuration data
31      */

32     
33     /**
34      * Name of configuration setting for application- (as opposed to browser-)
35      * based preferred locale
36      */

37     public static final String JavaDoc FMT_LOCALE
38     = "javax.servlet.jsp.jstl.fmt.locale";
39
40     /**
41      * Name of configuration setting for fallback locale
42      */

43     public static final String JavaDoc FMT_FALLBACK_LOCALE
44     = "javax.servlet.jsp.jstl.fmt.fallbackLocale";
45
46     /**
47      * Name of configuration setting for i18n localization context
48      */

49     public static final String JavaDoc FMT_LOCALIZATION_CONTEXT
50     = "javax.servlet.jsp.jstl.fmt.localizationContext";
51
52     /**
53      * Name of localization setting for time zone
54      */

55     public static final String JavaDoc FMT_TIME_ZONE
56     = "javax.servlet.jsp.jstl.fmt.timeZone";
57
58     /*
59      * SQL actions related configuration data
60      */

61
62     /**
63      * Name of configuration setting for SQL data source
64      */

65     public static final String JavaDoc SQL_DATA_SOURCE
66     = "javax.servlet.jsp.jstl.sql.dataSource";
67
68     /**
69      * Name of configuration setting for maximum number of rows to be included
70      * in SQL query result
71      */

72     public static final String JavaDoc SQL_MAX_ROWS
73     = "javax.servlet.jsp.jstl.sql.maxRows";
74     
75     /*
76      * Private constants
77      */

78     private static final String JavaDoc PAGE_SCOPE_SUFFIX = ".page";
79     private static final String JavaDoc REQUEST_SCOPE_SUFFIX = ".request";
80     private static final String JavaDoc SESSION_SCOPE_SUFFIX = ".session";
81     private static final String JavaDoc APPLICATION_SCOPE_SUFFIX = ".application";
82
83     /**
84      * Looks up a configuration variable in the given scope.
85      *
86      * <p> The lookup of configuration variables is performed as if each scope
87      * had its own name space, that is, the same configuration variable name
88      * in one scope does not replace one stored in a different scope.
89      *
90      * @param pc Page context in which the configuration variable is to be
91      * looked up
92      * @param name Configuration variable name
93      * @param scope Scope in which the configuration variable is to be looked
94      * up
95      *
96      * @return The <tt>java.lang.Object</tt> associated with the configuration
97      * variable, or null if it is not defined.
98      */

99     public static Object JavaDoc get(PageContext JavaDoc pc, String JavaDoc name, int scope) {
100     switch (scope) {
101     case PageContext.PAGE_SCOPE:
102         return pc.getAttribute(name + PAGE_SCOPE_SUFFIX, scope);
103     case PageContext.REQUEST_SCOPE:
104         return pc.getAttribute(name + REQUEST_SCOPE_SUFFIX, scope);
105     case PageContext.SESSION_SCOPE:
106         return get(pc.getSession(), name);
107     case PageContext.APPLICATION_SCOPE:
108         return pc.getAttribute(name + APPLICATION_SCOPE_SUFFIX, scope);
109     default:
110         throw new IllegalArgumentException JavaDoc("unknown scope");
111     }
112     }
113
114     /**
115      * Looks up a configuration variable in the "request" scope.
116      *
117      * <p> The lookup of configuration variables is performed as if each scope
118      * had its own name space, that is, the same configuration variable name
119      * in one scope does not replace one stored in a different scope.
120      *
121      * @param request Request object in which the configuration variable is to
122      * be looked up
123      * @param name Configuration variable name
124      *
125      * @return The <tt>java.lang.Object</tt> associated with the configuration
126      * variable, or null if it is not defined.
127      */

128     public static Object JavaDoc get(ServletRequest JavaDoc request, String JavaDoc name) {
129     return request.getAttribute(name + REQUEST_SCOPE_SUFFIX);
130     }
131
132     /**
133      * Looks up a configuration variable in the "session" scope.
134      *
135      * <p> The lookup of configuration variables is performed as if each scope
136      * had its own name space, that is, the same configuration variable name
137      * in one scope does not replace one stored in a different scope.</p>
138      *
139      * @param session Session object in which the configuration variable is to
140      * be looked up
141      * @param name Configuration variable name
142      *
143      * @return The <tt>java.lang.Object</tt> associated with the configuration
144      * variable, or null if it is not defined, if session is null, or if the session
145      * is invalidated.
146      */

147     public static Object JavaDoc get(HttpSession JavaDoc session, String JavaDoc name) {
148         Object JavaDoc ret = null;
149         if (session != null) {
150             try {
151                 ret = session.getAttribute(name + SESSION_SCOPE_SUFFIX);
152             } catch (IllegalStateException JavaDoc ex) {} // when session is invalidated
153
}
154         return ret;
155     }
156
157     /**
158      * Looks up a configuration variable in the "application" scope.
159      *
160      * <p> The lookup of configuration variables is performed as if each scope
161      * had its own name space, that is, the same configuration variable name
162      * in one scope does not replace one stored in a different scope.
163      *
164      * @param context Servlet context in which the configuration variable is
165      * to be looked up
166      * @param name Configuration variable name
167      *
168      * @return The <tt>java.lang.Object</tt> associated with the configuration
169      * variable, or null if it is not defined.
170      */

171     public static Object JavaDoc get(ServletContext JavaDoc context, String JavaDoc name) {
172     return context.getAttribute(name + APPLICATION_SCOPE_SUFFIX);
173     }
174
175     /**
176      * Sets the value of a configuration variable in the given scope.
177      *
178      * <p> Setting the value of a configuration variable is performed as if
179      * each scope had its own namespace, that is, the same configuration
180      * variable name in one scope does not replace one stored in a different
181      * scope.
182      *
183      * @param pc Page context in which the configuration variable is to be set
184      * @param name Configuration variable name
185      * @param value Configuration variable value
186      * @param scope Scope in which the configuration variable is to be set
187      */

188     public static void set(PageContext JavaDoc pc, String JavaDoc name, Object JavaDoc value,
189                int scope) {
190     switch (scope) {
191     case PageContext.PAGE_SCOPE:
192         pc.setAttribute(name + PAGE_SCOPE_SUFFIX, value, scope);
193         break;
194     case PageContext.REQUEST_SCOPE:
195         pc.setAttribute(name + REQUEST_SCOPE_SUFFIX, value, scope);
196         break;
197     case PageContext.SESSION_SCOPE:
198         pc.setAttribute(name + SESSION_SCOPE_SUFFIX, value, scope);
199         break;
200     case PageContext.APPLICATION_SCOPE:
201         pc.setAttribute(name + APPLICATION_SCOPE_SUFFIX, value, scope);
202         break;
203     default:
204         throw new IllegalArgumentException JavaDoc("unknown scope");
205     }
206     }
207
208     /**
209      * Sets the value of a configuration variable in the "request" scope.
210      *
211      * <p> Setting the value of a configuration variable is performed as if
212      * each scope had its own namespace, that is, the same configuration
213      * variable name in one scope does not replace one stored in a different
214      * scope.
215      *
216      * @param request Request object in which the configuration variable is to
217      * be set
218      * @param name Configuration variable name
219      * @param value Configuration variable value
220      */

221     public static void set(ServletRequest JavaDoc request, String JavaDoc name, Object JavaDoc value) {
222     request.setAttribute(name + REQUEST_SCOPE_SUFFIX, value);
223     }
224
225     /**
226      * Sets the value of a configuration variable in the "session" scope.
227      *
228      * <p> Setting the value of a configuration variable is performed as if
229      * each scope had its own namespace, that is, the same configuration
230      * variable name in one scope does not replace one stored in a different
231      * scope.
232      *
233      * @param session Session object in which the configuration variable is to
234      * be set
235      * @param name Configuration variable name
236      * @param value Configuration variable value
237      */

238     public static void set(HttpSession JavaDoc session, String JavaDoc name, Object JavaDoc value) {
239     session.setAttribute(name + SESSION_SCOPE_SUFFIX, value);
240     }
241
242     /**
243      * Sets the value of a configuration variable in the "application" scope.
244      *
245      * <p> Setting the value of a configuration variable is performed as if
246      * each scope had its own namespace, that is, the same configuration
247      * variable name in one scope does not replace one stored in a different
248      * scope.
249      *
250      * @param context Servlet context in which the configuration variable is to
251      * be set
252      * @param name Configuration variable name
253      * @param value Configuration variable value
254      */

255     public static void set(ServletContext JavaDoc context, String JavaDoc name, Object JavaDoc value) {
256     context.setAttribute(name + APPLICATION_SCOPE_SUFFIX, value);
257     }
258  
259     /**
260      * Removes a configuration variable from the given scope.
261      *
262      * <p> Removing a configuration variable is performed as if each scope had
263      * its own namespace, that is, the same configuration variable name in one
264      * scope does not impact one stored in a different scope.
265      *
266      * @param pc Page context from which the configuration variable is to be
267      * removed
268      * @param name Configuration variable name
269      * @param scope Scope from which the configuration variable is to be
270      * removed
271      */

272     public static void remove(PageContext JavaDoc pc, String JavaDoc name, int scope) {
273     switch (scope) {
274     case PageContext.PAGE_SCOPE:
275         pc.removeAttribute(name + PAGE_SCOPE_SUFFIX, scope);
276         break;
277     case PageContext.REQUEST_SCOPE:
278         pc.removeAttribute(name + REQUEST_SCOPE_SUFFIX, scope);
279         break;
280     case PageContext.SESSION_SCOPE:
281         pc.removeAttribute(name + SESSION_SCOPE_SUFFIX, scope);
282         break;
283     case PageContext.APPLICATION_SCOPE:
284         pc.removeAttribute(name + APPLICATION_SCOPE_SUFFIX, scope);
285         break;
286     default:
287         throw new IllegalArgumentException JavaDoc("unknown scope");
288     }
289     }
290
291     /**
292      * Removes a configuration variable from the "request" scope.
293      *
294      * <p> Removing a configuration variable is performed as if each scope had
295      * its own namespace, that is, the same configuration variable name in one
296      * scope does not impact one stored in a different scope.
297      *
298      * @param request Request object from which the configuration variable is
299      * to be removed
300      * @param name Configuration variable name
301      */

302     public static void remove(ServletRequest JavaDoc request, String JavaDoc name) {
303     request.removeAttribute(name + REQUEST_SCOPE_SUFFIX);
304     }
305
306     /**
307      * Removes a configuration variable from the "session" scope.
308      *
309      * <p> Removing a configuration variable is performed as if each scope had
310      * its own namespace, that is, the same configuration variable name in one
311      * scope does not impact one stored in a different scope.
312      *
313      * @param session Session object from which the configuration variable is
314      * to be removed
315      * @param name Configuration variable name
316      */

317     public static void remove(HttpSession JavaDoc session, String JavaDoc name) {
318     session.removeAttribute(name + SESSION_SCOPE_SUFFIX);
319     }
320
321     /**
322      * Removes a configuration variable from the "application" scope.
323      *
324      * <p> Removing a configuration variable is performed as if each scope had
325      * its own namespace, that is, the same configuration variable name in one
326      * scope does not impact one stored in a different scope.
327      *
328      * @param context Servlet context from which the configuration variable is
329      * to be removed
330      * @param name Configuration variable name
331      */

332     public static void remove(ServletContext JavaDoc context, String JavaDoc name) {
333     context.removeAttribute(name + APPLICATION_SCOPE_SUFFIX);
334     }
335  
336     /**
337      * Finds the value associated with a specific configuration setting
338      * identified by its context initialization parameter name.
339      *
340      * <p> For each of the JSP scopes (page, request, session, application),
341      * get the value of the configuration variable identified by <tt>name</tt>
342      * using method <tt>get()</tt>. Return as soon as a non-null value is
343      * found. If no value is found, get the value of the context initialization
344      * parameter identified by <tt>name</tt>.
345      *
346      * @param pc Page context in which the configuration setting is to be
347      * searched
348      * @param name Context initialization parameter name of the configuration
349      * setting
350      *
351      * @return The <tt>java.lang.Object</tt> associated with the configuration
352      * setting identified by <tt>name</tt>, or null if it is not defined.
353      */

354     public static Object JavaDoc find(PageContext JavaDoc pc, String JavaDoc name) {
355     Object JavaDoc ret = get(pc, name, PageContext.PAGE_SCOPE);
356     if (ret == null) {
357         ret = get(pc, name, PageContext.REQUEST_SCOPE);
358         if (ret == null) {
359         if (pc.getSession() != null) {
360             // check session only if a session is present
361
ret = get(pc, name, PageContext.SESSION_SCOPE);
362         }
363         if (ret == null) {
364             ret = get(pc, name, PageContext.APPLICATION_SCOPE);
365             if (ret == null) {
366             ret = pc.getServletContext().getInitParameter(name);
367             }
368         }
369         }
370     }
371
372     return ret;
373     }
374 }
375
Popular Tags