]> source.dussan.org Git - archiva.git/blob
bd856338a6ee68731692e6c7c05011d7fd114a7d
[archiva.git] /
1 package org.apache.archiva.rest.services;
2 /*
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  */
20
21 import org.apache.archiva.admin.model.RepositoryAdminException;
22 import org.apache.archiva.admin.model.beans.RedbackRuntimeConfiguration;
23 import org.apache.archiva.admin.model.beans.LdapConfiguration;
24 import org.apache.archiva.admin.model.runtime.RedbackRuntimeConfigurationAdmin;
25 import org.apache.archiva.redback.authentication.Authenticator;
26 import org.apache.archiva.redback.common.ldap.user.LdapUserMapper;
27 import org.apache.archiva.redback.common.ldap.connection.LdapConnection;
28 import org.apache.archiva.redback.common.ldap.connection.LdapConnectionConfiguration;
29 import org.apache.archiva.redback.common.ldap.connection.LdapConnectionFactory;
30 import org.apache.archiva.redback.common.ldap.connection.LdapException;
31 import org.apache.archiva.redback.components.cache.Cache;
32 import org.apache.archiva.redback.policy.CookieSettings;
33 import org.apache.archiva.redback.policy.PasswordRule;
34 import org.apache.archiva.redback.rbac.RBACManager;
35 import org.apache.archiva.redback.role.RoleManager;
36 import org.apache.archiva.redback.users.UserManager;
37 import org.apache.archiva.rest.api.model.RBACManagerImplementationInformation;
38 import org.apache.archiva.rest.api.model.RedbackImplementationsInformations;
39 import org.apache.archiva.rest.api.model.UserManagerImplementationInformation;
40 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
41 import org.apache.archiva.rest.api.services.RedbackRuntimeConfigurationService;
42
43 import org.apache.commons.lang.StringUtils;
44 import org.springframework.context.ApplicationContext;
45 import org.springframework.stereotype.Service;
46
47 import javax.inject.Inject;
48 import javax.inject.Named;
49 import javax.naming.InvalidNameException;
50 import java.util.ArrayList;
51 import java.util.Collection;
52 import java.util.Collections;
53 import java.util.List;
54 import java.util.Map;
55 import java.util.Properties;
56
57 /**
58  * @author Olivier Lamy
59  * @since 1.4-M4
60  */
61 @Service("redbackRuntimeConfigurationService#rest")
62 public class DefaultRedbackRuntimeConfigurationService
63     extends AbstractRestService
64     implements RedbackRuntimeConfigurationService
65 {
66     @Inject
67     private RedbackRuntimeConfigurationAdmin redbackRuntimeConfigurationAdmin;
68
69     @Inject
70     @Named(value = "userManager#configurable")
71     private UserManager userManager;
72
73     @Inject
74     @Named(value = "rbacManager#default")
75     private RBACManager rbacManager;
76
77     @Inject
78     private RoleManager roleManager;
79
80     @Inject
81     private ApplicationContext applicationContext;
82
83     @Inject
84     @Named(value = "ldapConnectionFactory#configurable")
85     private LdapConnectionFactory ldapConnectionFactory;
86
87     @Inject
88     @Named(value = "cache#users")
89     private Cache usersCache;
90
91     @Inject
92     private LdapUserMapper ldapUserMapper;
93
94
95     public RedbackRuntimeConfiguration getRedbackRuntimeConfiguration()
96         throws ArchivaRestServiceException
97     {
98         try
99         {
100             return redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration();
101         }
102         catch ( RepositoryAdminException e )
103         {
104             throw new ArchivaRestServiceException( e.getMessage(), e );
105         }
106     }
107
108     public Boolean updateRedbackRuntimeConfiguration( RedbackRuntimeConfiguration redbackRuntimeConfiguration )
109         throws ArchivaRestServiceException
110     {
111         try
112         {
113             // has user manager impl changed ?
114             boolean userManagerChanged = redbackRuntimeConfiguration.getUserManagerImpls().size()
115                 != redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getUserManagerImpls().size();
116
117             userManagerChanged =
118                 userManagerChanged || ( redbackRuntimeConfiguration.getUserManagerImpls().toString().hashCode()
119                     != redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getUserManagerImpls().toString().hashCode() );
120
121             boolean rbacManagerChanged = redbackRuntimeConfiguration.getRbacManagerImpls().size()
122                 != redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getRbacManagerImpls().size();
123
124             rbacManagerChanged =
125                 rbacManagerChanged || ( redbackRuntimeConfiguration.getRbacManagerImpls().toString().hashCode()
126                     != redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getRbacManagerImpls().toString().hashCode() );
127
128             redbackRuntimeConfigurationAdmin.updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration );
129
130             if ( userManagerChanged )
131             {
132                 log.info( "user managerImpls changed to {} so reload it",
133                           redbackRuntimeConfiguration.getUserManagerImpls() );
134                 userManager.initialize();
135             }
136
137             if ( rbacManagerChanged )
138             {
139                 log.info( "rbac manager changed to {} so reload it",
140                           redbackRuntimeConfiguration.getRbacManagerImpls() );
141                 rbacManager.initialize();
142                 roleManager.initialize();
143             }
144
145             ldapConnectionFactory.initialize();
146
147             Collection<PasswordRule> passwordRules = applicationContext.getBeansOfType( PasswordRule.class ).values();
148
149             for ( PasswordRule passwordRule : passwordRules )
150             {
151                 passwordRule.initialize();
152             }
153
154             Collection<CookieSettings> cookieSettingsList =
155                 applicationContext.getBeansOfType( CookieSettings.class ).values();
156
157             for ( CookieSettings cookieSettings : cookieSettingsList )
158             {
159                 cookieSettings.initialize();
160             }
161
162             Collection<Authenticator> authenticators =
163                 applicationContext.getBeansOfType( Authenticator.class ).values();
164
165             for ( Authenticator authenticator : authenticators )
166             {
167                 authenticator.initialize();
168             }
169
170             // users cache
171             usersCache.setTimeToIdleSeconds(
172                 redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToIdleSeconds() );
173             usersCache.setTimeToLiveSeconds(
174                 redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToLiveSeconds() );
175             usersCache.setMaxElementsInMemory(
176                 redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsInMemory() );
177             usersCache.setMaxElementsOnDisk(
178                 redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsOnDisk() );
179
180             ldapUserMapper.initialize();
181
182             return Boolean.TRUE;
183         }
184         catch ( Exception e )
185         {
186             log.error( e.getMessage(), e );
187             throw new ArchivaRestServiceException( e.getMessage(), e );
188         }
189     }
190
191     public List<UserManagerImplementationInformation> getUserManagerImplementationInformations()
192         throws ArchivaRestServiceException
193     {
194
195         Map<String, UserManager> beans = applicationContext.getBeansOfType( UserManager.class );
196
197         if ( beans.isEmpty() )
198         {
199             return Collections.emptyList();
200         }
201
202         List<UserManagerImplementationInformation> informations =
203             new ArrayList<UserManagerImplementationInformation>( beans.size() );
204
205         for ( Map.Entry<String, UserManager> entry : beans.entrySet() )
206         {
207             UserManager userManager = applicationContext.getBean( entry.getKey(), UserManager.class );
208             if ( userManager.isFinalImplementation() )
209             {
210                 UserManagerImplementationInformation information = new UserManagerImplementationInformation();
211                 information.setBeanId( StringUtils.substringAfter( entry.getKey(), "#" ) );
212                 information.setDescriptionKey( userManager.getDescriptionKey() );
213                 information.setReadOnly( userManager.isReadOnly() );
214                 informations.add( information );
215             }
216         }
217
218         return informations;
219     }
220
221     public List<RBACManagerImplementationInformation> getRbacManagerImplementationInformations()
222         throws ArchivaRestServiceException
223     {
224         Map<String, RBACManager> beans = applicationContext.getBeansOfType( RBACManager.class );
225
226         if ( beans.isEmpty() )
227         {
228             return Collections.emptyList();
229         }
230
231         List<RBACManagerImplementationInformation> informations =
232             new ArrayList<RBACManagerImplementationInformation>( beans.size() );
233
234         for ( Map.Entry<String, RBACManager> entry : beans.entrySet() )
235         {
236             RBACManager rbacManager = applicationContext.getBean( entry.getKey(), RBACManager.class );
237             if ( rbacManager.isFinalImplementation() )
238             {
239                 RBACManagerImplementationInformation information = new RBACManagerImplementationInformation();
240                 information.setBeanId( StringUtils.substringAfter( entry.getKey(), "#" ) );
241                 information.setDescriptionKey( rbacManager.getDescriptionKey() );
242                 information.setReadOnly( rbacManager.isReadOnly() );
243                 informations.add( information );
244             }
245         }
246
247         return informations;
248     }
249
250     public RedbackImplementationsInformations getRedbackImplementationsInformations()
251         throws ArchivaRestServiceException
252     {
253         return new RedbackImplementationsInformations( getUserManagerImplementationInformations(),
254                                                        getRbacManagerImplementationInformations() );
255     }
256
257     public Boolean checkLdapConnection()
258         throws ArchivaRestServiceException
259     {
260         LdapConnection ldapConnection = null;
261         try
262         {
263             ldapConnection = ldapConnectionFactory.getConnection();
264         }
265         catch ( LdapException e )
266         {
267             log.warn( "fail to get LdapConnection: {}", e.getMessage() );
268             throw new ArchivaRestServiceException( e.getMessage(), e );
269         }
270         finally
271         {
272
273             if ( ldapConnection != null )
274             {
275                 ldapConnection.close();
276             }
277         }
278
279         return Boolean.TRUE;
280     }
281
282     public Boolean checkLdapConnection( LdapConfiguration ldapConfiguration )
283         throws ArchivaRestServiceException
284     {
285         LdapConnection ldapConnection = null;
286         try
287         {
288             LdapConnectionConfiguration ldapConnectionConfiguration =
289                 new LdapConnectionConfiguration( ldapConfiguration.getHostName(), ldapConfiguration.getPort(),
290                                                  ldapConfiguration.getBaseDn(), ldapConfiguration.getContextFactory(),
291                                                  ldapConfiguration.getBindDn(), ldapConfiguration.getPassword(),
292                                                  ldapConfiguration.getAuthenticationMethod(),
293                                                  toProperties( ldapConfiguration.getExtraProperties() ) );
294
295             ldapConnection = ldapConnectionFactory.getConnection( ldapConnectionConfiguration );
296
297             ldapConnection.close();
298
299             // verify groups dn value too
300
301             ldapConnectionConfiguration =
302                 new LdapConnectionConfiguration( ldapConfiguration.getHostName(), ldapConfiguration.getPort(),
303                                                  ldapConfiguration.getBaseGroupsDn(),
304                                                  ldapConfiguration.getContextFactory(), ldapConfiguration.getBindDn(),
305                                                  ldapConfiguration.getPassword(),
306                                                  ldapConfiguration.getAuthenticationMethod(),
307                                                  toProperties( ldapConfiguration.getExtraProperties() ) );
308
309             ldapConnection = ldapConnectionFactory.getConnection( ldapConnectionConfiguration );
310         }
311         catch ( InvalidNameException e )
312         {
313             log.warn( "fail to get LdapConnection: {}", e.getMessage() );
314             throw new ArchivaRestServiceException( e.getMessage(), e );
315         }
316         catch ( LdapException e )
317         {
318             log.warn( "fail to get LdapConnection: {}", e.getMessage() );
319             throw new ArchivaRestServiceException( e.getMessage(), e );
320         }
321         finally
322         {
323
324             if ( ldapConnection != null )
325             {
326                 ldapConnection.close();
327             }
328         }
329
330         return Boolean.TRUE;
331     }
332
333     private Properties toProperties( Map<String, String> map )
334     {
335         Properties properties = new Properties();
336         if ( map == null || map.isEmpty() )
337         {
338             return properties;
339         }
340         for ( Map.Entry<String, String> entry : map.entrySet() )
341         {
342             properties.put( entry.getKey(), entry.getValue() );
343         }
344         return properties;
345     }
346
347 }
348
349