KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > web > taglibs > cache > CacheUtil


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.appserv.web.taglibs.cache;
25
26 import java.util.ResourceBundle JavaDoc;
27 import java.text.MessageFormat JavaDoc;
28
29 import javax.servlet.jsp.PageContext JavaDoc;
30 import javax.servlet.ServletContext JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32
33 import com.sun.appserv.util.cache.Cache;
34 import com.sun.enterprise.web.logging.pwc.LogDomains;
35
36 /**
37  * CacheUtil has utility methods used by the cache tag library.
38  */

39 public class CacheUtil {
40
41     /**
42      * The resource bundle containing the localized message strings.
43      */

44     private static ResourceBundle JavaDoc _rb =
45         LogDomains.getLogger(LogDomains.PWC_LOGGER).getResourceBundle();
46
47     private static final String JavaDoc PAGE_SCOPE = "page";
48     private static final String JavaDoc REQUEST_SCOPE = "request";
49     private static final String JavaDoc SESSION_SCOPE = "session";
50     private static final String JavaDoc APPLICATION_SCOPE = "application";
51
52     /**
53      * This is used to get the cache itself. The cache is stored as an
54      * attribute in the specified scope.
55      * @return the cache object
56      */

57     public static Cache getCache(PageContext JavaDoc pc, int scope)
58     {
59         return (Cache)pc.getAttribute(Constants.JSPTAG_CACHE_KEY, scope);
60     }
61
62     /**
63      * This function generates the key to the cache. It creates the key
64      * by suffixing the servlet path with either the user-specified key or
65      * by keeping a counter in the request attribute which it will
66      * increment each time so that multiple cache tags in a page each get
67      * a unique key.
68      * @return the generated key
69      */

70     public static String JavaDoc generateKey(String JavaDoc key, PageContext JavaDoc pc)
71     {
72         HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc)pc.getRequest();
73
74         // use the key as the suffix by default
75
String JavaDoc suffix = key;
76         if (suffix == null) {
77             String JavaDoc saved = (String JavaDoc)req.getAttribute(Constants.JSPTAG_COUNTER_KEY);
78
79             if (saved == null)
80                 suffix = "1";
81             else
82                 suffix = Integer.toString(Integer.parseInt(saved) + 1);
83
84             req.setAttribute(Constants.JSPTAG_COUNTER_KEY, suffix);
85         }
86         
87         // concatenate the servlet path and the suffix to generate key
88
return req.getServletPath() + '_' + suffix;
89     }
90
91
92     /*
93      * Converts the string representation of the given scope into an int.
94      *
95      * @param scope The string representation of the scope
96      *
97      * @return The corresponding int constant
98      *
99      * @throws IllegalArgumentException if the specified scope is different
100      * from request, session, and application
101      */

102     public static int convertScope(String JavaDoc scope) {
103
104         int ret;
105
106         if (REQUEST_SCOPE.equalsIgnoreCase(scope)) {
107             ret = PageContext.REQUEST_SCOPE;
108     } else if (SESSION_SCOPE.equalsIgnoreCase(scope)) {
109             ret = PageContext.SESSION_SCOPE;
110         } else if (APPLICATION_SCOPE.equalsIgnoreCase(scope)) {
111             ret = PageContext.APPLICATION_SCOPE;
112         } else {
113             String JavaDoc msg = _rb.getString("taglibs.cache.illegalScope");
114             msg = MessageFormat.format(msg, new Object JavaDoc[] { scope });
115             throw new IllegalArgumentException JavaDoc(msg);
116         }
117
118         return ret;
119     }
120 }
121
Popular Tags