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