KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > ac > file > FileUserManager


1 /*
2  * Copyright 1999-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  */

17
18 package org.apache.lenya.ac.file;
19
20 import java.io.File JavaDoc;
21 import java.util.Arrays JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import org.apache.lenya.ac.AccessControlException;
28 import org.apache.lenya.ac.Item;
29 import org.apache.lenya.ac.User;
30 import org.apache.lenya.ac.UserManager;
31 import org.apache.lenya.ac.UserType;
32
33 /**
34  * File-based user manager implementation.
35  * @version $Id: FileUserManager.java 43669 2004-09-10 14:27:02Z andreas $
36  */

37 public class FileUserManager extends FileItemManager implements UserManager {
38
39     private static Map JavaDoc instances = new HashMap JavaDoc();
40     private Set JavaDoc userTypes;
41
42     /**
43      * Create a UserManager
44      *
45      * @param configurationDirectory for which the UserManager should be instanciated.
46      * @param userTypes The supported user types.
47      * @throws AccessControlException if the UserManager could not be instantiated.
48      */

49     protected FileUserManager(File JavaDoc configurationDirectory, UserType[] userTypes)
50             throws AccessControlException {
51         super(configurationDirectory);
52         this.userTypes = new HashSet JavaDoc(Arrays.asList(userTypes));
53     }
54
55     /**
56      * Describe <code>instance</code> method here.
57      *
58      * @param configurationDirectory a directory
59      * @param userTypes The supported user types.
60      * @return an <code>UserManager</code> value
61      * @exception AccessControlException if an error occurs
62      */

63     public static FileUserManager instance(File JavaDoc configurationDirectory, UserType[] userTypes)
64             throws AccessControlException {
65
66         assert configurationDirectory != null;
67         if (!configurationDirectory.isDirectory()) {
68             throw new AccessControlException("Configuration directory [" + configurationDirectory
69                     + "] does not exist!");
70         }
71
72         if (!instances.containsKey(configurationDirectory)) {
73             instances.put(configurationDirectory, new FileUserManager(configurationDirectory,
74                     userTypes));
75         }
76
77         return (FileUserManager) instances.get(configurationDirectory);
78     }
79
80     /**
81      * Get all users.
82      *
83      * @return an Iterator to iterate over all users
84      */

85     public User[] getUsers() {
86         Item[] items = super.getItems();
87         User[] users = new User[items.length];
88         for (int i = 0; i < users.length; i++) {
89             users[i] = (User) items[i];
90         }
91         return users;
92     }
93
94     /**
95      * @see org.apache.lenya.ac.UserManager#add(org.apache.lenya.ac.User)
96      */

97     public void add(User user) throws AccessControlException {
98         super.add(user);
99     }
100
101     /**
102      * @see org.apache.lenya.ac.UserManager#remove(org.apache.lenya.ac.User)
103      */

104     public void remove(User user) throws AccessControlException {
105         super.remove(user);
106     }
107
108     /**
109      * Get the user with the given user id.
110      *
111      * @param userId user id of requested user
112      * @return the requested user or null if there is no user with the given user id
113      */

114     public User getUser(String JavaDoc userId) {
115         return (User) getItem(userId);
116     }
117
118     /**
119      * @see org.apache.lenya.ac.UserManager#getUserTypes()
120      */

121     public UserType[] getUserTypes() {
122         return (UserType[]) userTypes.toArray(new UserType[userTypes.size()]);
123     }
124
125     protected static final String JavaDoc SUFFIX = ".iml";
126
127     /**
128      * @see org.apache.lenya.ac.file.FileItemManager#getSuffix()
129      */

130     protected String JavaDoc getSuffix() {
131         return SUFFIX;
132     }
133
134 }
Popular Tags