KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webapp > balancer > rules > SessionAttributeRule


1 /*
2  * Copyright 2000,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 package org.apache.webapp.balancer.rules;
17
18 import javax.servlet.http.HttpServletRequest JavaDoc;
19 import javax.servlet.http.HttpSession JavaDoc;
20
21
22 /**
23  * This rule accepts or rejects requests
24  * based on the presence of a attribute
25  * in the request.
26  *
27  * @author Yoav Shapira
28  */

29 public class SessionAttributeRule extends BaseRule {
30     /**
31      * The target attribute name (attribute
32      * must be present for match to succeed).
33      */

34     private String JavaDoc attributeName;
35
36     /**
37      * The target attribute value. This
38      * is optional: null means any attribute
39      * value is OK for a match. A non-null
40      * value will be matches exactly.
41      */

42     private Object JavaDoc attributeValue;
43
44     /**
45      * Sets the target attribute name.
46      *
47      * @param theAttributeName The attribute name
48      */

49     public void setAttributeName(String JavaDoc theAttributeName) {
50         if (theAttributeName == null) {
51             throw new IllegalArgumentException JavaDoc(
52                 "attributeName cannot be null.");
53         } else {
54             attributeName = theAttributeName;
55         }
56     }
57
58     /**
59      * Returns the target attribute name.
60      *
61      * @return String The target attribute name.
62      */

63     protected String JavaDoc getAttributeName() {
64         return attributeName;
65     }
66
67     /**
68      * Sets the attribute value, which may be null.
69      *
70      * @param theAttributeValue The attribute value
71      */

72     public void setAttributeValue(Object JavaDoc theAttributeValue) {
73         attributeValue = theAttributeValue;
74     }
75
76     /**
77      * Returns the target attribute value,
78      * which may be null.
79      *
80      * @return Object The target attribute value
81      */

82     protected Object JavaDoc getAttributeValue() {
83         return attributeValue;
84     }
85
86     /**
87      * @see org.apache.webapp.balancer.Rule#matches(HttpServletRequest)
88      */

89     public boolean matches(HttpServletRequest JavaDoc request) {
90         HttpSession JavaDoc session = request.getSession();
91         Object JavaDoc actualAttributeValue = session.getAttribute(getAttributeName());
92
93         if (actualAttributeValue == null) {
94             return (getAttributeValue() == null);
95         } else {
96             return (actualAttributeValue.equals(getAttributeValue()));
97         }
98     }
99
100     /**
101      * Returns a String representation of this object.
102      *
103      * @return String
104      */

105     public String JavaDoc toString() {
106         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
107
108         buffer.append("[");
109         buffer.append(getClass().getName());
110         buffer.append(": ");
111
112         buffer.append("Target attribute name: ");
113         buffer.append(getAttributeName());
114         buffer.append(" / ");
115
116         buffer.append("Target attribute value: ");
117         buffer.append(getAttributeValue());
118         buffer.append(" / ");
119
120         buffer.append("Redirect URL: ");
121         buffer.append(getRedirectUrl());
122
123         buffer.append("]");
124
125         return buffer.toString();
126     }
127 }
128
129
130 // End of file: SessionAttributeRule.java
131
Popular Tags