KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > authentication > AccessControlRule


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2006 Continuent, Inc.
4  * Contact: sequoia@continuent.org
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Initial developer(s): Emmanuel Cecchet.
19  * Contributor(s): ______________________.
20  */

21
22 package org.continuent.sequoia.controller.authentication;
23
24 import java.io.Serializable JavaDoc;
25
26 import org.continuent.sequoia.common.xml.DatabasesXmlTags;
27
28 /**
29  * This class defines an AccessControlRule to accept or deny the access to the
30  * virtual database.
31  *
32  * @author <a HREF="mailto:emmanuel.cecchet@continuent.com">Emmanuel Cecchet</a>
33  * @version 1.0
34  */

35 public class AccessControlRule implements Serializable JavaDoc
36 {
37   private static final long serialVersionUID = 3819548176980894237L;
38
39   private boolean accept;
40   private String JavaDoc address;
41
42   /**
43    * Creates a new <code>AccessControlRule</code> object
44    *
45    * @param accept true if the rule is to accept the connection
46    * @param address the IP address or hostname to which the rule applies
47    */

48   public AccessControlRule(boolean accept, String JavaDoc address)
49   {
50     this.accept = accept;
51     this.address = address;
52     if (address == null)
53       throw new NullPointerException JavaDoc(
54           "Invalid null address in AccessControlRule");
55   }
56
57   /**
58    * Returns the accept value.
59    *
60    * @return Returns the accept.
61    */

62   public boolean isAccept()
63   {
64     return accept;
65   }
66
67   /**
68    * Test if the given client address matches this access control rule.
69    *
70    * @param clientAddress the client address to check
71    * @return true if the client address matches
72    */

73   public boolean addressMatchesRule(String JavaDoc clientAddress)
74   {
75     // TODO: to be refined
76
return (address.equals(clientAddress));
77   }
78
79   /**
80    * @see java.lang.Object#equals(java.lang.Object)
81    */

82   public boolean equals(Object JavaDoc obj)
83   {
84     if (obj instanceof AccessControlRule)
85     {
86       AccessControlRule rule = (AccessControlRule) obj;
87       return address.equalsIgnoreCase(rule.address);
88     }
89     return false;
90   }
91
92   /**
93    * XML representation of an ACL rule
94    *
95    * @return XML dump of the ACL rule
96    */

97   public String JavaDoc getXml()
98   {
99     StringBuffer JavaDoc xml = new StringBuffer JavaDoc("<");
100     if (accept)
101       xml.append(DatabasesXmlTags.ELT_Accept);
102     else
103       xml.append(DatabasesXmlTags.ELT_Deny);
104     xml.append(" " + DatabasesXmlTags.ATT_address + "=\"" + address + "\"/>");
105     return xml.toString();
106   }
107
108   /**
109    * @see java.lang.Object#hashCode()
110    */

111   public int hashCode()
112   {
113     return address.hashCode();
114   }
115
116   /**
117    * @see java.lang.Object#toString()
118    */

119   public String JavaDoc toString()
120   {
121     if (accept)
122       return "Accept " + address;
123     else
124       return "Deny " + address;
125   }
126
127 }
128
Popular Tags