KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > userrepository > UsersFileRepository


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.userrepository;
19
20 import org.apache.avalon.cornerstone.services.store.ObjectRepository;
21 import org.apache.avalon.cornerstone.services.store.Store;
22 import org.apache.avalon.framework.activity.Initializable;
23 import org.apache.avalon.framework.component.Component;
24 import org.apache.avalon.framework.component.ComponentException;
25 import org.apache.avalon.framework.component.ComponentManager;
26 import org.apache.avalon.framework.component.Composable;
27 import org.apache.avalon.framework.configuration.Configurable;
28 import org.apache.avalon.framework.configuration.Configuration;
29 import org.apache.avalon.framework.configuration.ConfigurationException;
30 import org.apache.avalon.framework.configuration.DefaultConfiguration;
31 import org.apache.avalon.framework.logger.AbstractLogEnabled;
32 import org.apache.james.services.User;
33 import org.apache.james.services.UsersRepository;
34
35 import java.io.File JavaDoc;
36 import java.util.Iterator JavaDoc;
37
38 /**
39  * Implementation of a Repository to store users on the File System.
40  *
41  * Requires a configuration element in the .conf.xml file of the form:
42  * <repository destinationURL="file://path-to-root-dir-for-repository"
43  * type="USERS"
44  * model="SYNCHRONOUS"/>
45  * Requires a logger called UsersRepository.
46  *
47  *
48  * @version CVS $Revision: 1.10.4.3 $
49  *
50  */

51 public class UsersFileRepository
52     extends AbstractLogEnabled
53     implements UsersRepository, Component, Configurable, Composable, Initializable {
54  
55     /**
56      * Whether 'deep debugging' is turned on.
57      */

58     protected static boolean DEEP_DEBUG = false;
59
60     /** @deprecated what was this for? */
61     private static final String JavaDoc TYPE = "USERS";
62
63     private Store store;
64     private ObjectRepository or;
65
66     /**
67      * The destination URL used to define the repository.
68      */

69     private String JavaDoc destination;
70
71     /**
72      * @see org.apache.avalon.framework.component.Composable#compose(ComponentManager)
73      */

74     public void compose( final ComponentManager componentManager )
75         throws ComponentException {
76
77         try {
78             store = (Store)componentManager.
79                 lookup( "org.apache.avalon.cornerstone.services.store.Store" );
80         } catch (Exception JavaDoc e) {
81             final String JavaDoc message = "Failed to retrieve Store component:" + e.getMessage();
82             getLogger().error( message, e );
83             throw new ComponentException( message, e );
84         }
85     }
86
87     /**
88      * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
89      */

90     public void configure( final Configuration configuration )
91         throws ConfigurationException {
92
93         destination = configuration.getChild( "destination" ).getAttribute( "URL" );
94
95         if (!destination.endsWith(File.separator)) {
96             destination += File.separator;
97         }
98     }
99
100     /**
101      * @see org.apache.avalon.framework.activity.Initializable#initialize()
102      */

103     public void initialize()
104         throws Exception JavaDoc {
105
106         try {
107             //prepare Configurations for object and stream repositories
108
final DefaultConfiguration objectConfiguration
109                 = new DefaultConfiguration( "repository",
110                                             "generated:UsersFileRepository.compose()" );
111
112             objectConfiguration.setAttribute( "destinationURL", destination );
113             objectConfiguration.setAttribute( "type", "OBJECT" );
114             objectConfiguration.setAttribute( "model", "SYNCHRONOUS" );
115
116             or = (ObjectRepository)store.select( objectConfiguration );
117             if (getLogger().isDebugEnabled()) {
118                 StringBuffer JavaDoc logBuffer =
119                     new StringBuffer JavaDoc(192)
120                             .append(this.getClass().getName())
121                             .append(" created in ")
122                             .append(destination);
123                 getLogger().debug(logBuffer.toString());
124             }
125         } catch (Exception JavaDoc e) {
126             if (getLogger().isErrorEnabled()) {
127                 getLogger().error("Failed to initialize repository:" + e.getMessage(), e );
128             }
129             throw e;
130         }
131     }
132
133     /**
134      * List users in repository.
135      *
136      * @return Iterator over a collection of Strings, each being one user in the repository.
137      */

138     public Iterator JavaDoc list() {
139         return or.list();
140     }
141
142     /**
143      * Update the repository with the specified user object. A user object
144      * with this username must already exist.
145      *
146      * @param user the user to be added.
147      *
148      * @return true if successful.
149      */

150     public synchronized boolean addUser(User user) {
151         String JavaDoc username = user.getUserName();
152         if (contains(username)) {
153             return false;
154         }
155         try {
156             or.put(username, user);
157         } catch (Exception JavaDoc e) {
158             throw new RuntimeException JavaDoc("Exception caught while storing user: " + e );
159         }
160         return true;
161     }
162
163     public void addUser(String JavaDoc name, Object JavaDoc attributes) {
164         if (attributes instanceof String JavaDoc) {
165             User newbie = new DefaultUser(name, "SHA");
166             newbie.setPassword( (String JavaDoc) attributes);
167             addUser(newbie);
168         }
169         else {
170             throw new RuntimeException JavaDoc("Improper use of deprecated method"
171                                        + " - use addUser(User user)");
172         }
173     }
174
175     public synchronized User getUserByName(String JavaDoc name) {
176         if (contains(name)) {
177             try {
178                 return (User)or.get(name);
179             } catch (Exception JavaDoc e) {
180                 throw new RuntimeException JavaDoc("Exception while retrieving user: "
181                                            + e.getMessage());
182             }
183         } else {
184             return null;
185         }
186     }
187
188     public User getUserByNameCaseInsensitive(String JavaDoc name) {
189         String JavaDoc realName = getRealName(name);
190         if (realName == null ) {
191             return null;
192         }
193         return getUserByName(realName);
194     }
195
196     public String JavaDoc getRealName(String JavaDoc name) {
197         Iterator JavaDoc it = list();
198         while (it.hasNext()) {
199             String JavaDoc temp = (String JavaDoc) it.next();
200             if (name.equalsIgnoreCase(temp)) {
201                 return temp;
202             }
203         }
204         return null;
205     }
206
207     public Object JavaDoc getAttributes(String JavaDoc name) {
208         throw new UnsupportedOperationException JavaDoc("Improper use of deprecated method - read javadocs");
209     }
210
211     public boolean updateUser(User user) {
212         String JavaDoc username = user.getUserName();
213         if (!contains(username)) {
214             return false;
215         }
216         try {
217             or.put(username, user);
218         } catch (Exception JavaDoc e) {
219             throw new RuntimeException JavaDoc("Exception caught while storing user: " + e );
220         }
221         return true;
222     }
223
224     public synchronized void removeUser(String JavaDoc name) {
225         or.remove(name);
226     }
227
228     public boolean contains(String JavaDoc name) {
229         return or.containsKey(name);
230     }
231
232     public boolean containsCaseInsensitive(String JavaDoc name) {
233         Iterator JavaDoc it = list();
234         while (it.hasNext()) {
235             if (name.equalsIgnoreCase((String JavaDoc)it.next())) {
236                 return true;
237             }
238         }
239         return false;
240     }
241
242     public boolean test(String JavaDoc name, Object JavaDoc attributes) {
243         try {
244             return attributes.equals(or.get(name));
245         } catch (Exception JavaDoc e) {
246             return false;
247         }
248     }
249
250     public boolean test(String JavaDoc name, String JavaDoc password) {
251         User user;
252         try {
253             if (contains(name)) {
254                 user = (User) or.get(name);
255             } else {
256                return false;
257             }
258         } catch (Exception JavaDoc e) {
259             throw new RuntimeException JavaDoc("Exception retrieving User" + e);
260         }
261         return user.verifyPassword(password);
262     }
263
264     public int countUsers() {
265         int count = 0;
266         for (Iterator JavaDoc it = list(); it.hasNext(); it.next()) {
267             count++;
268         }
269         return count;
270     }
271
272 }
273
Popular Tags