View Javadoc

1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  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,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  
21  
22  package org.apache.james.userrepository;
23  
24  import org.apache.avalon.cornerstone.services.store.ObjectRepository;
25  import org.apache.avalon.cornerstone.services.store.Store;
26  import org.apache.avalon.framework.activity.Initializable;
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.service.ServiceException;
32  import org.apache.avalon.framework.service.ServiceManager;
33  import org.apache.avalon.framework.service.Serviceable;
34  import org.apache.james.api.user.User;
35  import org.apache.james.impl.jamesuser.AbstractUsersRepository;
36  import org.apache.james.impl.user.DefaultJamesUser;
37  
38  
39  import java.util.Iterator;
40  
41  /**
42   * Implementation of a Repository to store users on the File System.
43   *
44   * Requires a configuration element in the .conf.xml file of the form:
45   *  <repository destinationURL="file://path-to-root-dir-for-repository"
46   *              type="USERS"
47   *              model="SYNCHRONOUS"/>
48   * Requires a logger called UsersRepository.
49   *
50   *
51   * @version CVS $Revision: 521427 $
52   *
53   */
54  public class UsersFileRepository
55      extends AbstractUsersRepository
56      implements Configurable, Serviceable, Initializable {
57   
58      /**
59       * Whether 'deep debugging' is turned on.
60       */
61      protected static boolean DEEP_DEBUG = false;
62  
63      private Store store;
64      private ObjectRepository objectRepository;
65      private static String urlSeparator = "/"; 
66  
67      /**
68       * The destination URL used to define the repository.
69       */
70      private String destination;
71  
72      /**
73       * Set the Store
74       * 
75       * @param store the Store
76       */
77      public void setStore(Store store) {
78          this.store = store;
79      }
80  
81      /**
82       * @see org.apache.avalon.framework.service.Serviceable#service(ServiceManager)
83       */
84      public void service( final ServiceManager componentManager )
85          throws ServiceException {
86  
87          try {
88              setStore((Store)componentManager.lookup( Store.ROLE ));
89          } catch (Exception e) {
90              final String message = "Failed to retrieve Store component:" + e.getMessage();
91              getLogger().error( message, e );
92              throw new ServiceException ("", message, e );
93          }
94      }
95  
96      /**
97       * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
98       */
99      public void configure( final Configuration configuration )
100         throws ConfigurationException {
101         super.configure(configuration);
102         destination = configuration.getChild( "destination" ).getAttribute( "URL" );
103 
104         if (!destination.endsWith(urlSeparator)) {
105             destination += urlSeparator;
106         }
107     }
108 
109     /**
110      * @see org.apache.avalon.framework.activity.Initializable#initialize()
111      */
112     public void initialize()
113         throws Exception {
114 
115         try {
116             //prepare Configurations for object and stream repositories
117             final DefaultConfiguration objectConfiguration
118                 = new DefaultConfiguration( "repository",
119                                             "generated:UsersFileRepository.compose()" );
120 
121             objectConfiguration.setAttribute( "destinationURL", destination );
122             objectConfiguration.setAttribute( "type", "OBJECT" );
123             objectConfiguration.setAttribute( "model", "SYNCHRONOUS" );
124 
125             objectRepository = (ObjectRepository)store.select( objectConfiguration );
126             if (getLogger().isDebugEnabled()) {
127                 StringBuffer logBuffer =
128                     new StringBuffer(192)
129                             .append(this.getClass().getName())
130                             .append(" created in ")
131                             .append(destination);
132                 getLogger().debug(logBuffer.toString());
133             }
134         } catch (Exception e) {
135             if (getLogger().isErrorEnabled()) {
136                 getLogger().error("Failed to initialize repository:" + e.getMessage(), e );
137             }
138             throw e;
139         }
140     }
141 
142     /**
143      * @see org.apache.james.api.user.UsersRepository#list()
144      */
145     public Iterator list() {
146         return objectRepository.list();
147     }
148 
149     /**
150      * @see org.apache.james.impl.jamesuser.AbstractUsersRepository#doAddUser(org.apache.james.api.user.User)
151      */
152     protected void doAddUser(User user) {
153         try {
154             objectRepository.put(user.getUserName(), user);
155         } catch (Exception e) {
156             throw new RuntimeException("Exception caught while storing user: " + e );
157         }
158     }
159 
160     /**
161      * @see org.apache.james.api.user.UsersRepository#addUser(java.lang.String, java.lang.String)
162      */
163     public boolean addUser(String username, String password) {
164         User newbie = new DefaultJamesUser(username, "SHA");
165         newbie.setPassword(password);
166         return addUser(newbie);
167     }
168 
169     /**
170      * @see org.apache.james.api.user.UsersRepository#getUserByName(java.lang.String)
171      */
172     public synchronized User getUserByName(String name) {
173         if (ignoreCase) {
174             name = getRealName(name);
175             if (name == null ) {
176                 return null;
177             }
178         }
179         if (contains(name)) {
180             try {
181                 return (User)objectRepository.get(name);
182             } catch (Exception e) {
183                 throw new RuntimeException("Exception while retrieving user: "
184                                            + e.getMessage());
185             }
186         } else {
187             return null;
188         }
189     }
190 
191     /**
192      * @see org.apache.james.api.user.UsersRepository#getUserByNameCaseInsensitive(java.lang.String)
193      */
194     public User getUserByNameCaseInsensitive(String name) {
195         String realName = getRealName(name, true);
196         if (realName == null ) {
197             return null;
198         }
199         return getUserByName(realName);
200     }
201 
202     /**
203      * Return the real name, given the ignoreCase boolean parameter
204      */
205     public String getRealName(String name, boolean ignoreCase) {
206         if (ignoreCase) {
207             Iterator it = list();
208             while (it.hasNext()) {
209                 String temp = (String) it.next();
210                 if (name.equalsIgnoreCase(temp)) {
211                     return temp;
212                 }
213             }
214             return null;
215         } else {
216             return objectRepository.containsKey(name) ? name : null;
217         }
218     }
219 
220     /**
221      * @see org.apache.james.api.user.UsersRepository#getRealName(java.lang.String)
222      */
223     public String getRealName(String name) {
224         return getRealName(name, ignoreCase);
225     }
226     
227     /**
228      * @see org.apache.james.impl.jamesuser.AbstractUsersRepository#doUpdateUser(org.apache.james.api.user.User)
229      */
230     public void doUpdateUser(User user) {
231         try {
232             objectRepository.put(user.getUserName(), user);
233         } catch (Exception e) {
234             throw new RuntimeException("Exception caught while storing user: "
235                     + e);
236         }
237     }
238 
239     /**
240      * @see org.apache.james.api.user.UsersRepository#removeUser(java.lang.String)
241      */
242     public synchronized void removeUser(String name) {
243         objectRepository.remove(name);
244     }
245 
246     /**
247      * @see org.apache.james.api.user.UsersRepository#contains(java.lang.String)
248      */
249     public boolean contains(String name) {
250         if (ignoreCase) {
251             return containsCaseInsensitive(name);
252         } else {
253             return objectRepository.containsKey(name);
254         }
255     }
256 
257     /**
258      * @see org.apache.james.api.user.UsersRepository#containsCaseInsensitive(java.lang.String)
259      */
260     public boolean containsCaseInsensitive(String name) {
261         Iterator it = list();
262         while (it.hasNext()) {
263             if (name.equalsIgnoreCase((String)it.next())) {
264                 return true;
265             }
266         }
267         return false;
268     }
269 
270     /**
271      * @see org.apache.james.api.user.UsersRepository#test(java.lang.String, java.lang.String)
272      */
273     public boolean test(String name, String password) {
274         User user;
275         try {
276             user = getUserByName(name);
277             if (user == null) return false;
278         } catch (Exception e) {
279             throw new RuntimeException("Exception retrieving User" + e);
280         }
281         return user.verifyPassword(password);
282     }
283 
284     /**
285      * @see org.apache.james.api.user.UsersRepository#countUsers()
286      */
287     public int countUsers() {
288         int count = 0;
289         for (Iterator it = list(); it.hasNext(); it.next()) {
290             count++;
291         }
292         return count;
293     }
294 
295 }