KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > web > cache > mapping > Field


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.cache.mapping;
25
26 import java.text.MessageFormat JavaDoc;
27
28 import java.util.logging.Logger JavaDoc;
29 import java.util.ResourceBundle JavaDoc;
30
31 import javax.servlet.ServletContext JavaDoc;
32
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpSession JavaDoc;
35 import javax.servlet.http.Cookie JavaDoc;
36
37 import com.sun.enterprise.web.logging.pwc.LogDomains;
38
39 public class Field {
40
41     // PWC_LOGGER
42
private static Logger JavaDoc _logger;
43     /**
44      * The resource bundle containing the localized message strings.
45      */

46     private static ResourceBundle JavaDoc _rb = null;
47
48     // field name and scope
49
protected String JavaDoc name;
50
51     // scope defs in Constants
52
protected int scope;
53
54     /**
55      * create a new cache field, given a string representation of the scope
56      * @param name name of this field
57      * @param scope scope of this field
58      */

59     public Field (String JavaDoc name, String JavaDoc scope) throws IllegalArgumentException JavaDoc {
60         // web container logger
61
_logger = LogDomains.getLogger(LogDomains.PWC_LOGGER);
62         _rb = _logger.getResourceBundle();
63
64         this.name = name;
65         this.scope = parseScope(scope);
66     }
67
68     /**
69      * set the associated name
70      * @param name name of this field
71      */

72     public void setName(String JavaDoc name) {
73         this.name = name;
74     }
75
76     /**
77      * set the associated scope
78      * @param scope scope of this field
79      */

80     public void setScope(int scope) {
81         this.scope = scope;
82     }
83
84     /**
85      * set the associated scope
86      * @param scope scope of this field
87      */

88     private int parseScope(String JavaDoc value) throws IllegalArgumentException JavaDoc {
89         int scope;
90         if ("context.attribute".equals(value))
91             scope = Constants.SCOPE_CONTEXT_ATTRIBUTE;
92         else if ("request.header".equals(value))
93             scope = Constants.SCOPE_REQUEST_HEADER;
94         else if ("request.parameter".equals(value))
95             scope = Constants.SCOPE_REQUEST_PARAMETER;
96         else if ("request.cookie".equals(value))
97             scope = Constants.SCOPE_REQUEST_COOKIE;
98         else if ("request.attribute".equals(value))
99             scope = Constants.SCOPE_REQUEST_ATTRIBUTE;
100         else if ("session.attribute".equals(value))
101             scope = Constants.SCOPE_SESSION_ATTRIBUTE;
102         else if ("session.id".equals(value))
103             scope = Constants.SCOPE_SESSION_ID;
104         else {
105             String JavaDoc msg = _rb.getString("cache.mapping.incorrectScope");
106             Object JavaDoc[] params = { value, name };
107             msg = MessageFormat.format(msg, params);
108
109             throw new IllegalArgumentException JavaDoc(msg);
110         }
111         return scope;
112     }
113
114     /**
115      * get the associated name
116      * @return the name of this field
117      */

118     public String JavaDoc getName() {
119         return name;
120     }
121
122     /**
123      * get the associated scope
124      * @return the scope of this field
125      */

126     public int getScope() {
127         return scope;
128     }
129
130     /** get the field value by looking up in the given scope
131      * @param context <code>ServletContext</code> underlying web app context
132      * @param request <code>HttpServletRequest</code>
133      * @return field value in the scope
134      */

135     public Object JavaDoc getValue(ServletContext JavaDoc context,
136                            HttpServletRequest JavaDoc request) {
137
138         Object JavaDoc value = null;
139         switch (scope) {
140             case Constants.SCOPE_CONTEXT_ATTRIBUTE:
141                     value = context.getAttribute(name);
142                     break;
143             case Constants.SCOPE_REQUEST_HEADER:
144                     value = request.getHeader(name);
145                     break;
146             case Constants.SCOPE_REQUEST_PARAMETER:
147                     value = request.getParameter(name);
148                     break;
149             case Constants.SCOPE_REQUEST_COOKIE:
150                     Cookie JavaDoc cookies[] = request.getCookies();
151                     for (int i = 0; i < cookies.length; i++) {
152                         if (name.equals(cookies[i].getName())) {
153                             value = cookies[i].getValue();
154                             break;
155                         }
156                     }
157                     break;
158             case Constants.SCOPE_REQUEST_ATTRIBUTE:
159                     value = request.getAttribute(name);
160                     break;
161             case Constants.SCOPE_SESSION_ID:
162                     {
163                         HttpSession JavaDoc session = request.getSession(false);
164                         if (session != null) {
165                             value = session.getId();
166                         }
167                     }
168                     break;
169             case Constants.SCOPE_SESSION_ATTRIBUTE:
170                     {
171                         HttpSession JavaDoc session = request.getSession(false);
172                         if (session != null) {
173                             value = session.getAttribute(name);
174                         }
175                     }
176                     break;
177         }
178         return value;
179     }
180 }
181
Popular Tags