KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.ArrayList JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.continuent.sequoia.common.xml.DatabasesXmlTags;
30
31 /**
32  * This class defines an AccessControl policy.
33  *
34  * @author <a HREF="mailto:emmanuel.cecchet@continuent.com">Emmanuel Cecchet</a>
35  * @version 1.0
36  */

37 public class AccessControl implements Serializable JavaDoc
38 {
39   private static final long serialVersionUID = -5819003895772344875L;
40
41   private boolean defaultPolicyAcceptAll;
42   private List JavaDoc accessList;
43
44   /**
45    * Creates a new <code>AccessControl</code> object
46    *
47    * @param defaultPolicyIsAcceptAll true if the default policy is acceptAll,
48    * false if default policy is denyAll
49    */

50   public AccessControl(boolean defaultPolicyIsAcceptAll)
51   {
52     defaultPolicyAcceptAll = defaultPolicyIsAcceptAll;
53     accessList = new ArrayList JavaDoc();
54   }
55
56   /**
57    * Adds an AccessControlRule to the rule list.
58    *
59    * @param rule the rule to add
60    */

61   public void addAccessControlRule(AccessControlRule rule)
62   {
63     synchronized (accessList)
64     {
65       accessList.add(rule);
66     }
67   }
68
69   /**
70    * Returns true if the connection can be accepted from the given address. The
71    * first rule matching the address applies, else the default policy applies if
72    * no rule has matched.
73    *
74    * @param address the address to look for
75    * @return true if the connection can be accepted
76    */

77   public boolean acceptConnectionFrom(String JavaDoc address)
78   {
79     for (Iterator JavaDoc iter = accessList.iterator(); iter.hasNext();)
80     {
81       AccessControlRule rule = (AccessControlRule) iter.next();
82       if (rule.addressMatchesRule(address))
83         return rule.isAccept();
84     }
85     return defaultPolicyAcceptAll;
86   }
87
88   /**
89    * XML representation of an ACL
90    *
91    * @return XML dump of the ACL
92    */

93   public String JavaDoc getXml()
94   {
95     StringBuffer JavaDoc xml = new StringBuffer JavaDoc("<"
96         + DatabasesXmlTags.ELT_AccessControl
97         + " "
98         + DatabasesXmlTags.ATT_defaultPolicy
99         + "=\""
100         + (defaultPolicyAcceptAll
101             ? DatabasesXmlTags.VAL_acceptAll
102             : DatabasesXmlTags.VAL_denyAll) + "\">");
103     for (Iterator JavaDoc iter = accessList.iterator(); iter.hasNext();)
104     {
105       AccessControlRule rule = (AccessControlRule) iter.next();
106       xml.append(rule.getXml());
107     }
108     xml.append("</" + DatabasesXmlTags.ELT_AccessControl + ">");
109     return xml.toString();
110   }
111
112   /**
113    * @see java.lang.Object#toString()
114    */

115   public String JavaDoc toString()
116   {
117     return "Default policy: " + (defaultPolicyAcceptAll ? "accept" : "deny")
118         + "\n" + accessList;
119   }
120
121 }
122
Popular Tags