This project has retired. For details please refer to its Attic page.
MemoryUserManager xref
View Javadoc

1   package org.apache.archiva.redback.users.memory;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.archiva.redback.users.User;
23  import org.apache.archiva.redback.users.UserManager;
24  import org.apache.archiva.redback.policy.UserSecurityPolicy;
25  import org.apache.archiva.redback.users.AbstractUserManager;
26  import org.apache.archiva.redback.users.PermanentUserException;
27  import org.apache.archiva.redback.users.UserManagerException;
28  import org.apache.archiva.redback.users.UserQuery;
29  import org.apache.archiva.redback.users.UserNotFoundException;
30  import org.apache.archiva.redback.users.memory.util.UserSorter;
31  import org.apache.commons.lang.StringUtils;
32  import org.springframework.stereotype.Service;
33  
34  import java.io.IOException;
35  import java.net.URL;
36  import java.util.ArrayList;
37  import java.util.Collections;
38  import java.util.HashMap;
39  import java.util.Iterator;
40  import java.util.List;
41  import java.util.Map;
42  import java.util.Properties;
43  
44  import javax.annotation.Resource;
45  
46  /**
47   *
48   */
49  @Service("userManager#memory")
50  public class MemoryUserManager
51      extends AbstractUserManager
52      implements UserManager
53  {
54      @Resource
55      private UserSecurityPolicy userSecurityPolicy;
56  
57      public String getId()
58      {
59          return "memory";
60      }
61      
62      public boolean isReadOnly()
63      {
64          return false;
65      }
66  
67      public UserQuery createUserQuery()
68      {
69          return new SimpleUserQuery();
70      }
71  
72      public List<User> findUsersByQuery( UserQuery query )
73      {
74          SimpleUserQuery uq = (SimpleUserQuery) query;
75  
76          List<User> list = new ArrayList<User>();
77  
78          for ( Iterator<User> i = users.values().iterator(); i.hasNext(); )
79          {
80              SimpleUser user = (SimpleUser) i.next();
81              boolean matches = uq.matches( user );
82              if ( matches )
83              {
84                  list.add( user );
85              }
86          }
87  
88          Collections.sort( list, uq.getComparator() );
89  
90          List<User> cutList = new ArrayList<User>();
91  
92          for ( long i = query.getFirstResult();
93                i < list.size() && ( query.getMaxResults() == -1 || i < query.getFirstResult() + uq.getMaxResults() );
94                i++ )
95          {
96              cutList.add( list.get( (int) i ) );
97          }
98          return cutList;
99      }
100 
101     private Map<Object, User> users = new HashMap<Object, User>();
102 
103     public User addUser( User user )
104     {
105         saveUser( user );
106         fireUserManagerUserAdded( user );
107 
108         // If there exists no encoded password, then this is a new user setup 
109         if ( StringUtils.isEmpty( user.getEncodedPassword() ) )
110         {
111             userSecurityPolicy.extensionChangePassword( user );
112         }
113 
114         return user;
115     }
116 
117     private void saveUser( User user )
118     {
119         triggerInit();
120         users.put( user.getUsername(), user );
121     }
122 
123     public User updateUser( User user )
124     {
125         return updateUser( user, false );
126     }
127 
128     public User updateUser( User user, boolean passwordChangeRequired )
129     {
130         if ( StringUtils.isNotEmpty( user.getPassword() ) )
131         {
132             userSecurityPolicy.extensionChangePassword( user, passwordChangeRequired );
133         }
134 
135         saveUser( user );
136 
137         fireUserManagerUserUpdated( user );
138 
139         return user;
140     }
141 
142     public boolean userExists( String principal )
143     {
144         try
145         {
146             findUser( principal );
147             return true;
148         }
149         catch ( UserNotFoundException ne )
150         {
151             return false;
152         }
153     }
154 
155 
156     public User createUser( String username, String fullName, String emailAddress )
157     {
158         User user = new SimpleUser();
159         user.setUsername( username );
160         user.setFullName( fullName );
161         user.setEmail( emailAddress );
162 
163         return user;
164     }
165 
166     public void deleteUser( String username )
167         throws UserNotFoundException, UserManagerException
168     {
169         User user = findUser( username );
170 
171         if ( user.isPermanent() )
172         {
173             throw new PermanentUserException( "Cannot delete permanent user." );
174         }
175 
176         users.remove( user.getUsername() );
177 
178         fireUserManagerUserRemoved( user );
179     }
180 
181     public void addUserUnchecked( User user )
182     {
183         addUser( user );
184     }
185 
186     public void eraseDatabase()
187     {
188         users.clear();
189     }
190 
191     public User findUser( String username )
192         throws UserNotFoundException
193     {
194         triggerInit();
195         User user = null;
196 
197         Iterator<User> it = users.values().iterator();
198         while ( it.hasNext() )
199         {
200             User u = it.next();
201             if ( u.getUsername().equals( username ) )
202             {
203                 user = u;
204             }
205         }
206 
207         if ( user == null )
208         {
209             throw new UserNotFoundException( "Unable to find user '" + username + "'" );
210         }
211 
212         return user;
213     }
214 
215     public List<User> findUsersByUsernameKey( String usernameKey, boolean orderAscending )
216     {
217         triggerInit();
218 
219         List<User> userList = new ArrayList<User>();
220 
221         Iterator<User> it = users.values().iterator();
222         while ( it.hasNext() )
223         {
224             User u = it.next();
225             if ( u.getUsername().indexOf( usernameKey ) > -1 )
226             {
227                 userList.add( u );
228             }
229         }
230 
231         Collections.sort( userList, new UserSorter( orderAscending ) );
232 
233         return userList;
234     }
235 
236     public List<User> findUsersByFullNameKey( String fullNameKey, boolean orderAscending )
237     {
238         triggerInit();
239 
240         List<User> userList = new ArrayList<User>();
241 
242         Iterator<User> it = users.values().iterator();
243         while ( it.hasNext() )
244         {
245             User u = it.next();
246             if ( u.getFullName().indexOf( fullNameKey ) > -1 )
247             {
248                 userList.add( u );
249             }
250         }
251 
252         Collections.sort( userList, new UserSorter( orderAscending ) );
253 
254         return userList;
255     }
256 
257     public List<User> findUsersByEmailKey( String emailKey, boolean orderAscending )
258     {
259         triggerInit();
260 
261         List<User> userList = new ArrayList<User>();
262 
263         Iterator<User> it = users.values().iterator();
264         while ( it.hasNext() )
265         {
266             User u = it.next();
267             if ( u.getEmail().indexOf( emailKey ) > -1 )
268             {
269                 userList.add( u );
270             }
271         }
272 
273         Collections.sort( userList, new UserSorter( orderAscending ) );
274 
275         return userList;
276     }
277 
278     public List<User> getUsers()
279     {
280         triggerInit();
281         return new ArrayList<User>( users.values() );
282     }
283 
284     public List<User> getUsers( boolean ascendingUsername )
285     {
286         return getUsers();
287     }
288 
289     private boolean hasTriggeredInit = false;
290 
291     public void triggerInit()
292     {
293         if ( !hasTriggeredInit )
294         {
295             fireUserManagerInit( users.isEmpty() );
296             hasTriggeredInit = true;
297         }
298     }
299 
300     public String getDescriptionKey()
301     {
302         return "archiva.redback.usermanager.memory";
303     }
304 }