KickJava   Java API By Example, From Geeks To Geeks.

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


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
20
21 /**
22  * The remote access rule
23  * redirects if the request came
24  * from specified remote address.
25  *
26  * @author Yoav Shapira
27  */

28 public class RemoteAddressRule extends BaseRule {
29     /**
30      * The target remote address.
31      */

32     private String JavaDoc remoteAddress;
33
34     /**
35      * Sets the target remote address.
36      *
37      * @param theAddress The address
38      */

39     public void setRemoteAddress(String JavaDoc theAddress) {
40         if (theAddress == null) {
41             throw new IllegalArgumentException JavaDoc("The address cannot be null.");
42         } else {
43             remoteAddress = theAddress;
44         }
45     }
46
47     /**
48      * Returns the target remote address.
49      *
50      * @return String
51      */

52     protected String JavaDoc getRemoteAddress() {
53         return remoteAddress;
54     }
55
56     /**
57      * @see org.apache.webapp.balancer.Rule#matches(HttpServletRequest)
58      *
59      * Looks for the request's remote address.
60      */

61     public boolean matches(HttpServletRequest JavaDoc request) {
62         String JavaDoc requestAddr = request.getRemoteAddr();
63
64         return (requestAddr.compareTo(getRemoteAddress()) == 0);
65     }
66
67     /**
68      * Returns a String representation of this object.
69      *
70      * @return String
71      */

72     public String JavaDoc toString() {
73         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
74
75         buffer.append("[");
76         buffer.append(getClass().getName());
77         buffer.append(": ");
78
79         buffer.append("Target remote address: ");
80         buffer.append(getRemoteAddress());
81         buffer.append(" / ");
82
83         buffer.append("Redirect URL: ");
84         buffer.append(getRedirectUrl());
85
86         buffer.append("]");
87
88         return buffer.toString();
89     }
90 }
91
92
93 // End of file: RemoteAddressRule.java
94
Popular Tags