KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > server > auth > acl > AccessControl


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.server.auth.acl;
18
19 import java.util.StringTokenizer JavaDoc;
20
21 import org.alfresco.filesys.server.SrvSession;
22 import org.alfresco.filesys.server.core.SharedDevice;
23
24 /**
25  * Access Control Base Class
26  * <p>
27  * Controls access to a shared filesystem.
28  */

29 public abstract class AccessControl
30 {
31
32     // Access control type/status
33

34     public final static int NoAccess = 0;
35     public final static int ReadOnly = 1;
36     public final static int ReadWrite = 2;
37
38     public final static int MaxLevel = 2;
39
40     // Default access status, indicates that the access conrol did not apply
41

42     public final static int Default = -1;
43
44     // Access type strings
45

46     private final static String JavaDoc[] _accessType = { "None", "Read", "Write" };
47
48     // Access control name and type
49

50     private String JavaDoc m_name;
51     private String JavaDoc m_type;
52
53     // Access type
54

55     private int m_access;
56
57     /**
58      * Class constructor
59      *
60      * @param name String
61      * @param type String
62      * @param access int
63      */

64     protected AccessControl(String JavaDoc name, String JavaDoc type, int access)
65     {
66         setName(name);
67         setType(type);
68
69         m_access = access;
70     }
71
72     /**
73      * Return the access control name
74      *
75      * @return String
76      */

77     public final String JavaDoc getName()
78     {
79         return m_name;
80     }
81
82     /**
83      * Return the access control type
84      *
85      * @return String
86      */

87     public final String JavaDoc getType()
88     {
89         return m_type;
90     }
91
92     /**
93      * Return the access control check type
94      *
95      * @return int
96      */

97     public final int getAccess()
98     {
99         return m_access;
100     }
101
102     /**
103      * Return the access control check type as a string
104      *
105      * @return String
106      */

107     public final String JavaDoc getAccessString()
108     {
109         return _accessType[m_access];
110     }
111
112     /**
113      * Check if the specified session has access to the shared device.
114      *
115      * @param sess SrvSession
116      * @param share SharedDevice
117      * @param mgr AccessControlManager
118      * @return int
119      */

120     public abstract int allowsAccess(SrvSession sess, SharedDevice share, AccessControlManager mgr);
121
122     /**
123      * Return the index of a value from a list of valid values, or 01 if not valid
124      *
125      * @param val String
126      * @param list String[]
127      * @param caseSensitive boolean
128      * @return int
129      */

130     protected final static int indexFromList(String JavaDoc val, String JavaDoc[] valid, boolean caseSensitive)
131     {
132
133         // Check if the value is valid
134

135         if (val == null || val.length() == 0)
136             return -1;
137
138         // Search for the matching value in the valid list
139

140         for (int i = 0; i < valid.length; i++)
141         {
142
143             // Check the current value in the valid list
144

145             if (caseSensitive)
146             {
147                 if (valid[i].equals(val))
148                     return i;
149             }
150             else if (valid[i].equalsIgnoreCase(val))
151                 return i;
152         }
153
154         // Value does not match any of the valid values
155

156         return -1;
157     }
158
159     /**
160      * Create a list of valid strings from a comma delimeted list
161      *
162      * @param str String
163      * @return String[]
164      */

165     protected final static String JavaDoc[] listFromString(String JavaDoc str)
166     {
167
168         // Check if the string is valid
169

170         if (str == null || str.length() == 0)
171             return null;
172
173         // Split the comma delimeted string into an array of strings
174

175         StringTokenizer JavaDoc token = new StringTokenizer JavaDoc(str, ",");
176         int numStrs = token.countTokens();
177         if (numStrs == 0)
178             return null;
179
180         String JavaDoc[] list = new String JavaDoc[numStrs];
181
182         // Parse the string into a list of strings
183

184         int i = 0;
185
186         while (token.hasMoreTokens())
187             list[i++] = token.nextToken();
188
189         // Return the string list
190

191         return list;
192     }
193
194     /**
195      * Set the access control type
196      *
197      * @param typ String
198      */

199     protected final void setType(String JavaDoc typ)
200     {
201         m_type = typ;
202     }
203
204     /**
205      * Set the access control name
206      *
207      * @param name String
208      */

209     protected final void setName(String JavaDoc name)
210     {
211         m_name = name;
212     }
213
214     /**
215      * Return the access control type as a string
216      *
217      * @param access int
218      * @return String
219      */

220     public static final String JavaDoc asAccessString(int access)
221     {
222         if (access == Default)
223             return "Default";
224         return _accessType[access];
225     }
226
227     /**
228      * Return the access control as a string
229      *
230      * @return String
231      */

232     public String JavaDoc toString()
233     {
234         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
235
236         str.append("[");
237         str.append(getType());
238         str.append(":");
239         str.append(getName());
240         str.append(",");
241         str.append(getAccessString());
242         str.append("]");
243
244         return str.toString();
245     }
246 }
247
Popular Tags