1 package org.apache.archiva.rest.services;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
43 import org.apache.commons.lang.StringUtils;
44 import org.springframework.context.ApplicationContext;
45 import org.springframework.stereotype.Service;
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;
55 import java.util.Properties;
58 * @author Olivier Lamy
61 @Service("redbackRuntimeConfigurationService#rest")
62 public class DefaultRedbackRuntimeConfigurationService
63 extends AbstractRestService
64 implements RedbackRuntimeConfigurationService
67 private RedbackRuntimeConfigurationAdmin redbackRuntimeConfigurationAdmin;
70 @Named(value = "userManager#configurable")
71 private UserManager userManager;
74 @Named(value = "rbacManager#default")
75 private RBACManager rbacManager;
78 private RoleManager roleManager;
81 private ApplicationContext applicationContext;
84 @Named(value = "ldapConnectionFactory#configurable")
85 private LdapConnectionFactory ldapConnectionFactory;
88 @Named(value = "cache#users")
89 private Cache usersCache;
92 private LdapUserMapper ldapUserMapper;
95 public RedbackRuntimeConfiguration getRedbackRuntimeConfiguration()
96 throws ArchivaRestServiceException
100 return redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration();
102 catch ( RepositoryAdminException e )
104 throw new ArchivaRestServiceException( e.getMessage(), e );
108 public Boolean updateRedbackRuntimeConfiguration( RedbackRuntimeConfiguration redbackRuntimeConfiguration )
109 throws ArchivaRestServiceException
113 // has user manager impl changed ?
114 boolean userManagerChanged = redbackRuntimeConfiguration.getUserManagerImpls().size()
115 != redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getUserManagerImpls().size();
118 userManagerChanged || ( redbackRuntimeConfiguration.getUserManagerImpls().toString().hashCode()
119 != redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getUserManagerImpls().toString().hashCode() );
121 boolean rbacManagerChanged = redbackRuntimeConfiguration.getRbacManagerImpls().size()
122 != redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getRbacManagerImpls().size();
125 rbacManagerChanged || ( redbackRuntimeConfiguration.getRbacManagerImpls().toString().hashCode()
126 != redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getRbacManagerImpls().toString().hashCode() );
128 redbackRuntimeConfigurationAdmin.updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration );
130 if ( userManagerChanged )
132 log.info( "user managerImpls changed to {} so reload it",
133 redbackRuntimeConfiguration.getUserManagerImpls() );
134 userManager.initialize();
137 if ( rbacManagerChanged )
139 log.info( "rbac manager changed to {} so reload it",
140 redbackRuntimeConfiguration.getRbacManagerImpls() );
141 rbacManager.initialize();
142 roleManager.initialize();
145 ldapConnectionFactory.initialize();
147 Collection<PasswordRule> passwordRules = applicationContext.getBeansOfType( PasswordRule.class ).values();
149 for ( PasswordRule passwordRule : passwordRules )
151 passwordRule.initialize();
154 Collection<CookieSettings> cookieSettingsList =
155 applicationContext.getBeansOfType( CookieSettings.class ).values();
157 for ( CookieSettings cookieSettings : cookieSettingsList )
159 cookieSettings.initialize();
162 Collection<Authenticator> authenticators =
163 applicationContext.getBeansOfType( Authenticator.class ).values();
165 for ( Authenticator authenticator : authenticators )
167 authenticator.initialize();
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() );
180 ldapUserMapper.initialize();
182 //check repositories roles are here !!!
186 catch ( Exception e )
188 log.error( e.getMessage(), e );
189 throw new ArchivaRestServiceException( e.getMessage(), e );
193 public List<UserManagerImplementationInformation> getUserManagerImplementationInformations()
194 throws ArchivaRestServiceException
197 Map<String, UserManager> beans = applicationContext.getBeansOfType( UserManager.class );
199 if ( beans.isEmpty() )
201 return Collections.emptyList();
204 List<UserManagerImplementationInformation> informations =
205 new ArrayList<UserManagerImplementationInformation>( beans.size() );
207 for ( Map.Entry<String, UserManager> entry : beans.entrySet() )
209 UserManager userManager = applicationContext.getBean( entry.getKey(), UserManager.class );
210 if ( userManager.isFinalImplementation() )
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 );
223 public List<RBACManagerImplementationInformation> getRbacManagerImplementationInformations()
224 throws ArchivaRestServiceException
226 Map<String, RBACManager> beans = applicationContext.getBeansOfType( RBACManager.class );
228 if ( beans.isEmpty() )
230 return Collections.emptyList();
233 List<RBACManagerImplementationInformation> informations =
234 new ArrayList<RBACManagerImplementationInformation>( beans.size() );
236 for ( Map.Entry<String, RBACManager> entry : beans.entrySet() )
238 RBACManager rbacManager = applicationContext.getBean( entry.getKey(), RBACManager.class );
239 if ( rbacManager.isFinalImplementation() )
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 );
252 public RedbackImplementationsInformations getRedbackImplementationsInformations()
253 throws ArchivaRestServiceException
255 return new RedbackImplementationsInformations( getUserManagerImplementationInformations(),
256 getRbacManagerImplementationInformations() );
259 public Boolean checkLdapConnection()
260 throws ArchivaRestServiceException
262 LdapConnection ldapConnection = null;
265 ldapConnection = ldapConnectionFactory.getConnection();
267 catch ( LdapException e )
269 log.warn( "fail to get LdapConnection: {}", e.getMessage() );
270 throw new ArchivaRestServiceException( e.getMessage(), e );
275 if ( ldapConnection != null )
277 ldapConnection.close();
284 public Boolean checkLdapConnection( LdapConfiguration ldapConfiguration )
285 throws ArchivaRestServiceException
287 LdapConnection ldapConnection = null;
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() ) );
297 ldapConnection = ldapConnectionFactory.getConnection( ldapConnectionConfiguration );
299 ldapConnection.close();
301 // verify groups dn value too
303 ldapConnectionConfiguration =
304 new LdapConnectionConfiguration( ldapConfiguration.getHostName(), ldapConfiguration.getPort(),
305 ldapConfiguration.getBaseGroupsDn(),
306 ldapConfiguration.getContextFactory(), ldapConfiguration.getBindDn(),
307 ldapConfiguration.getPassword(),
308 ldapConfiguration.getAuthenticationMethod(),
309 toProperties( ldapConfiguration.getExtraProperties() ) );
311 ldapConnection = ldapConnectionFactory.getConnection( ldapConnectionConfiguration );
313 catch ( InvalidNameException e )
315 log.warn( "fail to get LdapConnection: {}", e.getMessage() );
316 throw new ArchivaRestServiceException( e.getMessage(), e );
318 catch ( LdapException e )
320 log.warn( "fail to get LdapConnection: {}", e.getMessage() );
321 throw new ArchivaRestServiceException( e.getMessage(), e );
326 if ( ldapConnection != null )
328 ldapConnection.close();
335 private Properties toProperties( Map<String, String> map )
337 Properties properties = new Properties();
338 if ( map == null || map.isEmpty() )
342 for ( Map.Entry<String, String> entry : map.entrySet() )
344 properties.put( entry.getKey(), entry.getValue() );