KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.logging.Logger JavaDoc;
27 import java.util.logging.Level JavaDoc;
28
29 import javax.servlet.ServletContext JavaDoc;
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31
32 import com.sun.enterprise.web.logging.pwc.LogDomains;
33
34 /** ConstraintField class represents a single Field and constraints on its
35  * values; Field name and its scope are inherited from the Field class.
36  */

37 public class ConstraintField extends Field {
38
39     private static Logger JavaDoc _logger = null;
40     private static boolean _isTraceEnabled = false;
41
42     // whether to cache if there was a match
43
boolean cacheOnMatch = true;
44     // whether to cache if there was a failure to match
45
boolean cacheOnMatchFailure = false;
46
47     // field value constraints
48
ValueConstraint constraints[] = new ValueConstraint[0];
49
50     /**
51      * create a new cache field, given a string representation of the scope
52      * @param name name of this field
53      * @param scope scope of this field
54      */

55     public ConstraintField (String JavaDoc name, String JavaDoc scope)
56                                 throws IllegalArgumentException JavaDoc {
57         super(name, scope);
58         if (_logger == null) {
59             _logger = LogDomains.getLogger(LogDomains.PWC_LOGGER);
60             _isTraceEnabled = _logger.isLoggable(Level.FINE);
61         }
62     }
63
64     /** set whether to cache should the constraints check pass
65      * @param cacheOnMatch should the constraint check pass, should we cache?
66      */

67     public void setCacheOnMatch(boolean cacheOnMatch) {
68         this.cacheOnMatch = cacheOnMatch;
69     }
70
71     /**
72      * @return cache-on-match setting
73      */

74     public boolean getCacheOnMatch() {
75         return cacheOnMatch;
76     }
77
78     /** set whether to cache should there be a failure forcing the constraint
79      * @param cacheOnMatchFailure should there be a constraint check failure,
80      * enable cache?
81      */

82     public void setCacheOnMatchFailure(boolean cacheOnMatchFailure) {
83         this.cacheOnMatchFailure = cacheOnMatchFailure;
84     }
85
86     /**
87      * @return cache-on-match-failure setting
88      */

89     public boolean getCacheOnMatchFailure() {
90         return cacheOnMatchFailure;
91     }
92
93     /**
94      * add a constraint for this field
95      * @param constraint one constraint associated with this field
96      */

97     public void addConstraint(ValueConstraint constraint) {
98         if (constraint == null)
99             return;
100
101         ValueConstraint results[] =
102             new ValueConstraint[constraints.length + 1];
103         for (int i = 0; i < constraints.length; i++)
104             results[i] = constraints[i];
105
106         results[constraints.length] = constraint;
107         constraints = results;
108     }
109
110     /**
111      * add an array of constraints for this field
112      * @param vcs constraints associated with this field
113      */

114     public void setValueConstraints(ValueConstraint[] vcs) {
115         if (vcs == null)
116             return;
117
118         constraints = vcs;
119     }
120
121     /** apply the constraints on the value of the field in the given request.
122      * return a true if all the constraints pass; false when the
123      * field is not found or the field value doesn't pass the caching
124      * constraints.
125      */

126     public boolean applyConstraints(ServletContext JavaDoc context,
127                                     HttpServletRequest JavaDoc request) {
128
129         Object JavaDoc value = getValue(context, request);
130         if (value == null) {
131             // the field is not present in the request
132
if (_isTraceEnabled) {
133                 _logger.fine("The constraint field " + name + " is not found in the scope " + Constants.SCOPE_NAMES[scope] + "; returning cache-on-match-failure: " + cacheOnMatchFailure);
134             }
135             return cacheOnMatchFailure;
136         } else if (constraints.length == 0) {
137             // the field is present but has no value constraints
138
if (_isTraceEnabled) {
139                 _logger.fine("The constraint field " + name + " value = " + value.toString() + " is found in the scope " + Constants.SCOPE_NAMES[scope] + "; returning cache-on-match: " + cacheOnMatch);
140             }
141             return cacheOnMatch;
142         }
143
144         // apply all the value constraints
145
for (int i = 0; i < constraints.length; i++) {
146             ValueConstraint c = constraints[i];
147
148             // one of the values matched
149
if (c.matches(value)) {
150                 if (_isTraceEnabled) {
151                     _logger.fine("The constraint field " + name + " value = " + value.toString() + " is found in the scope " + Constants.SCOPE_NAMES[scope] + "; and matches with a value " + c.toString() +"; returning cache-on-match: " + cacheOnMatch);
152             }
153                 return cacheOnMatch;
154             }
155         }
156
157         // none of the values matched; should we cache?
158
if (_isTraceEnabled) {
159             _logger.fine("The constraint field " + name + " value = " + value.toString() + " is found in the scope " + Constants.SCOPE_NAMES[scope] + "; but didn't match with any of the value constraints; returning cache-on-match-failure = " + cacheOnMatchFailure);
160         }
161         return cacheOnMatchFailure;
162     }
163 }
164
Popular Tags