]> source.dussan.org Git - archiva.git/blob
a4e9fd304fa577f37a5eb2f1be84b9fdafdea406
[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.PropertyEntry;
23 import org.apache.archiva.admin.model.beans.RedbackRuntimeConfiguration;
24 import org.apache.archiva.admin.model.beans.LdapConfiguration;
25 import org.apache.archiva.admin.model.runtime.RedbackRuntimeConfigurationAdmin;
26 import org.apache.archiva.redback.authentication.AuthenticationException;
27 import org.apache.archiva.redback.authentication.Authenticator;
28 import org.apache.archiva.redback.common.ldap.user.LdapUserMapper;
29 import org.apache.archiva.redback.common.ldap.connection.LdapConnection;
30 import org.apache.archiva.redback.common.ldap.connection.LdapConnectionConfiguration;
31 import org.apache.archiva.redback.common.ldap.connection.LdapConnectionFactory;
32 import org.apache.archiva.redback.common.ldap.connection.LdapException;
33 import org.apache.archiva.redback.components.cache.Cache;
34 import org.apache.archiva.redback.policy.CookieSettings;
35 import org.apache.archiva.redback.policy.PasswordRule;
36 import org.apache.archiva.redback.users.UserManager;
37 import org.apache.archiva.rest.api.model.UserManagerImplementationInformation;
38 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
39 import org.apache.archiva.rest.api.services.RedbackRuntimeConfigurationService;
40 import org.apache.commons.lang.StringUtils;
41 import org.springframework.context.ApplicationContext;
42 import org.springframework.stereotype.Service;
43
44 import javax.inject.Inject;
45 import javax.inject.Named;
46 import javax.naming.InvalidNameException;
47 import java.util.ArrayList;
48 import java.util.Collection;
49 import java.util.Collections;
50 import java.util.Comparator;
51 import java.util.List;
52 import java.util.Map;
53 import java.util.Properties;
54
55 /**
56  * @author Olivier Lamy
57  * @since 1.4-M4
58  */
59 @Service("redbackRuntimeConfigurationService#rest")
60 public class DefaultRedbackRuntimeConfigurationService
61     extends AbstractRestService
62     implements RedbackRuntimeConfigurationService
63 {
64     @Inject
65     private RedbackRuntimeConfigurationAdmin redbackRuntimeConfigurationAdmin;
66
67     @Inject
68     @Named(value = "userManager#configurable")
69     private UserManager userManager;
70
71     @Inject
72     private ApplicationContext applicationContext;
73
74     @Inject
75     @Named(value = "ldapConnectionFactory#configurable")
76     private LdapConnectionFactory ldapConnectionFactory;
77
78     @Inject
79     @Named(value = "cache#users")
80     private Cache usersCache;
81
82     @Inject
83     private LdapUserMapper ldapUserMapper;
84
85
86     public RedbackRuntimeConfiguration getRedbackRuntimeConfiguration()
87         throws ArchivaRestServiceException
88     {
89         try
90         {
91             return redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration();
92         }
93         catch ( RepositoryAdminException e )
94         {
95             throw new ArchivaRestServiceException( e.getMessage(), e );
96         }
97     }
98
99     public Boolean updateRedbackRuntimeConfiguration( RedbackRuntimeConfiguration redbackRuntimeConfiguration )
100         throws ArchivaRestServiceException
101     {
102         try
103         {
104             // has user manager impl changed ?
105             boolean userManagerChanged = redbackRuntimeConfiguration.getUserManagerImpls().size()
106                 != redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getUserManagerImpls().size();
107
108             userManagerChanged =
109                 userManagerChanged || ( redbackRuntimeConfiguration.getUserManagerImpls().toString().hashCode()
110                     != redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getUserManagerImpls().toString().hashCode() );
111
112             redbackRuntimeConfigurationAdmin.updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration );
113
114             if ( userManagerChanged )
115             {
116                 log.info( "user managerImpls changed to {} so reload it",
117                           redbackRuntimeConfiguration.getUserManagerImpls() );
118                 userManager.initialize();
119             }
120
121             ldapConnectionFactory.initialize();
122
123             Collection<PasswordRule> passwordRules = applicationContext.getBeansOfType( PasswordRule.class ).values();
124
125             for ( PasswordRule passwordRule : passwordRules )
126             {
127                 passwordRule.initialize();
128             }
129
130             Collection<CookieSettings> cookieSettingsList =
131                 applicationContext.getBeansOfType( CookieSettings.class ).values();
132
133             for ( CookieSettings cookieSettings : cookieSettingsList )
134             {
135                 cookieSettings.initialize();
136             }
137
138             Collection<Authenticator> authenticators =
139                 applicationContext.getBeansOfType( Authenticator.class ).values();
140
141             for ( Authenticator authenticator : authenticators )
142             {
143                 authenticator.initialize();
144             }
145
146             // users cache
147             usersCache.setTimeToIdleSeconds(
148                 redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToIdleSeconds() );
149             usersCache.setTimeToLiveSeconds(
150                 redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToLiveSeconds() );
151             usersCache.setMaxElementsInMemory(
152                 redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsInMemory() );
153             usersCache.setMaxElementsOnDisk(
154                 redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsOnDisk() );
155
156             ldapUserMapper.initialize();
157
158             return Boolean.TRUE;
159         }
160         catch ( AuthenticationException e )
161         {
162             throw new ArchivaRestServiceException( e.getMessage(), e );
163         }
164         catch ( RepositoryAdminException e )
165         {
166             throw new ArchivaRestServiceException( e.getMessage(), e );
167         }
168
169     }
170
171     public List<UserManagerImplementationInformation> getUserManagerImplementationInformations()
172         throws ArchivaRestServiceException
173     {
174
175         Map<String, UserManager> beans = applicationContext.getBeansOfType( UserManager.class );
176
177         if ( beans.isEmpty() )
178         {
179             return Collections.emptyList();
180         }
181
182         List<UserManagerImplementationInformation> informations =
183             new ArrayList<UserManagerImplementationInformation>( beans.size() );
184
185         for ( Map.Entry<String, UserManager> entry : beans.entrySet() )
186         {
187             UserManager userManager = applicationContext.getBean( entry.getKey(), UserManager.class );
188             if ( userManager.isFinalImplementation() )
189             {
190                 UserManagerImplementationInformation information = new UserManagerImplementationInformation();
191                 information.setBeanId( StringUtils.substringAfter( entry.getKey(), "#" ) );
192                 information.setDescriptionKey( userManager.getDescriptionKey() );
193                 information.setReadOnly( userManager.isReadOnly() );
194                 informations.add( information );
195             }
196         }
197
198         return informations;
199     }
200
201
202     public Boolean checkLdapConnection()
203         throws ArchivaRestServiceException
204     {
205         LdapConnection ldapConnection = null;
206         try
207         {
208             ldapConnection = ldapConnectionFactory.getConnection();
209         }
210         catch ( LdapException e )
211         {
212             log.warn( "fail to get LdapConnection: {}", e.getMessage() );
213             throw new ArchivaRestServiceException( e.getMessage(), e );
214         }
215         finally
216         {
217
218             if ( ldapConnection != null )
219             {
220                 ldapConnection.close();
221             }
222         }
223
224         return Boolean.TRUE;
225     }
226
227     public Boolean checkLdapConnection( LdapConfiguration ldapConfiguration )
228         throws ArchivaRestServiceException
229     {
230         LdapConnection ldapConnection = null;
231         try
232         {
233             LdapConnectionConfiguration ldapConnectionConfiguration =
234                 new LdapConnectionConfiguration( ldapConfiguration.getHostName(), ldapConfiguration.getPort(),
235                                                  ldapConfiguration.getBaseDn(), ldapConfiguration.getContextFactory(),
236                                                  ldapConfiguration.getBindDn(), ldapConfiguration.getPassword(),
237                                                  ldapConfiguration.getAuthenticationMethod(),
238                                                  toProperties( ldapConfiguration.getExtraProperties() ) );
239
240             ldapConnection = ldapConnectionFactory.getConnection( ldapConnectionConfiguration );
241
242             ldapConnection.close();
243
244             // verify groups dn value too
245
246             ldapConnectionConfiguration =
247                 new LdapConnectionConfiguration( ldapConfiguration.getHostName(), ldapConfiguration.getPort(),
248                                                  ldapConfiguration.getBaseGroupsDn(),
249                                                  ldapConfiguration.getContextFactory(), ldapConfiguration.getBindDn(),
250                                                  ldapConfiguration.getPassword(),
251                                                  ldapConfiguration.getAuthenticationMethod(),
252                                                  toProperties( ldapConfiguration.getExtraProperties() ) );
253
254             ldapConnection = ldapConnectionFactory.getConnection( ldapConnectionConfiguration );
255         }
256         catch ( InvalidNameException e )
257         {
258             log.warn( "fail to get LdapConnection: {}", e.getMessage() );
259             throw new ArchivaRestServiceException( e.getMessage(), e );
260         }
261         catch ( LdapException e )
262         {
263             log.warn( "fail to get LdapConnection: {}", e.getMessage() );
264             throw new ArchivaRestServiceException( e.getMessage(), e );
265         }
266         finally
267         {
268
269             if ( ldapConnection != null )
270             {
271                 ldapConnection.close();
272             }
273         }
274
275         return Boolean.TRUE;
276     }
277
278     private Properties toProperties( Map<String, String> map )
279     {
280         Properties properties = new Properties();
281         if ( map == null || map.isEmpty() )
282         {
283             return properties;
284         }
285         for ( Map.Entry<String, String> entry : map.entrySet() )
286         {
287             properties.put( entry.getKey(), entry.getValue() );
288         }
289         return properties;
290     }
291 }
292
293