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