]> source.dussan.org Git - archiva.git/blob
2ed44243d4ac6d3f53d1cd6157ef1443e9ff9e12
[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             //check repositories roles are here !!!
183
184             return Boolean.TRUE;
185         }
186         catch ( Exception e )
187         {
188             log.error( e.getMessage(), e );
189             throw new ArchivaRestServiceException( e.getMessage(), e );
190         }
191     }
192
193     public List<UserManagerImplementationInformation> getUserManagerImplementationInformations()
194         throws ArchivaRestServiceException
195     {
196
197         Map<String, UserManager> beans = applicationContext.getBeansOfType( UserManager.class );
198
199         if ( beans.isEmpty() )
200         {
201             return Collections.emptyList();
202         }
203
204         List<UserManagerImplementationInformation> informations =
205             new ArrayList<UserManagerImplementationInformation>( beans.size() );
206
207         for ( Map.Entry<String, UserManager> entry : beans.entrySet() )
208         {
209             UserManager userManager = applicationContext.getBean( entry.getKey(), UserManager.class );
210             if ( userManager.isFinalImplementation() )
211             {
212                 UserManagerImplementationInformation information = new UserManagerImplementationInformation();
213                 information.setBeanId( StringUtils.substringAfter( entry.getKey(), "#" ) );
214                 information.setDescriptionKey( userManager.getDescriptionKey() );
215                 information.setReadOnly( userManager.isReadOnly() );
216                 informations.add( information );
217             }
218         }
219
220         return informations;
221     }
222
223     public List<RBACManagerImplementationInformation> getRbacManagerImplementationInformations()
224         throws ArchivaRestServiceException
225     {
226         Map<String, RBACManager> beans = applicationContext.getBeansOfType( RBACManager.class );
227
228         if ( beans.isEmpty() )
229         {
230             return Collections.emptyList();
231         }
232
233         List<RBACManagerImplementationInformation> informations =
234             new ArrayList<RBACManagerImplementationInformation>( beans.size() );
235
236         for ( Map.Entry<String, RBACManager> entry : beans.entrySet() )
237         {
238             RBACManager rbacManager = applicationContext.getBean( entry.getKey(), RBACManager.class );
239             if ( rbacManager.isFinalImplementation() )
240             {
241                 RBACManagerImplementationInformation information = new RBACManagerImplementationInformation();
242                 information.setBeanId( StringUtils.substringAfter( entry.getKey(), "#" ) );
243                 information.setDescriptionKey( rbacManager.getDescriptionKey() );
244                 information.setReadOnly( rbacManager.isReadOnly() );
245                 informations.add( information );
246             }
247         }
248
249         return informations;
250     }
251
252     public RedbackImplementationsInformations getRedbackImplementationsInformations()
253         throws ArchivaRestServiceException
254     {
255         return new RedbackImplementationsInformations( getUserManagerImplementationInformations(),
256                                                        getRbacManagerImplementationInformations() );
257     }
258
259     public Boolean checkLdapConnection()
260         throws ArchivaRestServiceException
261     {
262         LdapConnection ldapConnection = null;
263         try
264         {
265             ldapConnection = ldapConnectionFactory.getConnection();
266         }
267         catch ( LdapException e )
268         {
269             log.warn( "fail to get LdapConnection: {}", e.getMessage(), e );
270             throw new ArchivaRestServiceException( e.getMessage(), e );
271         }
272         finally
273         {
274
275             if ( ldapConnection != null )
276             {
277                 ldapConnection.close();
278             }
279         }
280
281         return Boolean.TRUE;
282     }
283
284     public Boolean checkLdapConnection( LdapConfiguration ldapConfiguration )
285         throws ArchivaRestServiceException
286     {
287         LdapConnection ldapConnection = null;
288         try
289         {
290             LdapConnectionConfiguration ldapConnectionConfiguration =
291                 new LdapConnectionConfiguration( ldapConfiguration.getHostName(), ldapConfiguration.getPort(),
292                                                  ldapConfiguration.getBaseDn(), ldapConfiguration.getContextFactory(),
293                                                  ldapConfiguration.getBindDn(), ldapConfiguration.getPassword(),
294                                                  ldapConfiguration.getAuthenticationMethod(),
295                                                  toProperties( ldapConfiguration.getExtraProperties() ) );
296             ldapConnectionConfiguration.setSsl( ldapConfiguration.isSsl() );
297
298             ldapConnection = ldapConnectionFactory.getConnection( ldapConnectionConfiguration );
299
300             ldapConnection.close();
301
302             // verify groups dn value too
303
304             ldapConnectionConfiguration =
305                 new LdapConnectionConfiguration( ldapConfiguration.getHostName(), ldapConfiguration.getPort(),
306                                                  ldapConfiguration.getBaseGroupsDn(),
307                                                  ldapConfiguration.getContextFactory(), ldapConfiguration.getBindDn(),
308                                                  ldapConfiguration.getPassword(),
309                                                  ldapConfiguration.getAuthenticationMethod(),
310                                                  toProperties( ldapConfiguration.getExtraProperties() ) );
311
312             ldapConnectionConfiguration.setSsl( ldapConfiguration.isSsl() );
313
314             ldapConnection = ldapConnectionFactory.getConnection( ldapConnectionConfiguration );
315         }
316         catch ( InvalidNameException e )
317         {
318             log.warn( "fail to get LdapConnection: {}", e.getMessage(), e);
319             throw new ArchivaRestServiceException( e.getMessage(), e );
320         }
321         catch ( LdapException e )
322         {
323             log.warn( "fail to get LdapConnection: {}", e.getMessage(), e);
324             throw new ArchivaRestServiceException( e.getMessage(), e );
325         }
326         finally
327         {
328
329             if ( ldapConnection != null )
330             {
331                 ldapConnection.close();
332             }
333         }
334
335         return Boolean.TRUE;
336     }
337
338     private Properties toProperties( Map<String, String> map )
339     {
340         Properties properties = new Properties();
341         if ( map == null || map.isEmpty() )
342         {
343             return properties;
344         }
345         for ( Map.Entry<String, String> entry : map.entrySet() )
346         {
347             properties.put( entry.getKey(), entry.getValue() );
348         }
349         return properties;
350     }
351
352 }
353
354