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();
184 catch ( Exception e )
186 log.error( e.getMessage(), e );
187 throw new ArchivaRestServiceException( e.getMessage(), e );
191 public List<UserManagerImplementationInformation> getUserManagerImplementationInformations()
192 throws ArchivaRestServiceException
195 Map<String, UserManager> beans = applicationContext.getBeansOfType( UserManager.class );
197 if ( beans.isEmpty() )
199 return Collections.emptyList();
202 List<UserManagerImplementationInformation> informations =
203 new ArrayList<UserManagerImplementationInformation>( beans.size() );
205 for ( Map.Entry<String, UserManager> entry : beans.entrySet() )
207 UserManager userManager = applicationContext.getBean( entry.getKey(), UserManager.class );
208 if ( userManager.isFinalImplementation() )
210 UserManagerImplementationInformation information = new UserManagerImplementationInformation();
211 information.setBeanId( StringUtils.substringAfter( entry.getKey(), "#" ) );
212 information.setDescriptionKey( userManager.getDescriptionKey() );
213 information.setReadOnly( userManager.isReadOnly() );
214 informations.add( information );
221 public List<RBACManagerImplementationInformation> getRbacManagerImplementationInformations()
222 throws ArchivaRestServiceException
224 Map<String, RBACManager> beans = applicationContext.getBeansOfType( RBACManager.class );
226 if ( beans.isEmpty() )
228 return Collections.emptyList();
231 List<RBACManagerImplementationInformation> informations =
232 new ArrayList<RBACManagerImplementationInformation>( beans.size() );
234 for ( Map.Entry<String, RBACManager> entry : beans.entrySet() )
236 RBACManager rbacManager = applicationContext.getBean( entry.getKey(), RBACManager.class );
237 if ( rbacManager.isFinalImplementation() )
239 RBACManagerImplementationInformation information = new RBACManagerImplementationInformation();
240 information.setBeanId( StringUtils.substringAfter( entry.getKey(), "#" ) );
241 information.setDescriptionKey( rbacManager.getDescriptionKey() );
242 information.setReadOnly( rbacManager.isReadOnly() );
243 informations.add( information );
250 public RedbackImplementationsInformations getRedbackImplementationsInformations()
251 throws ArchivaRestServiceException
253 return new RedbackImplementationsInformations( getUserManagerImplementationInformations(),
254 getRbacManagerImplementationInformations() );
257 public Boolean checkLdapConnection()
258 throws ArchivaRestServiceException
260 LdapConnection ldapConnection = null;
263 ldapConnection = ldapConnectionFactory.getConnection();
265 catch ( LdapException e )
267 log.warn( "fail to get LdapConnection: {}", e.getMessage() );
268 throw new ArchivaRestServiceException( e.getMessage(), e );
273 if ( ldapConnection != null )
275 ldapConnection.close();
282 public Boolean checkLdapConnection( LdapConfiguration ldapConfiguration )
283 throws ArchivaRestServiceException
285 LdapConnection ldapConnection = null;
288 LdapConnectionConfiguration ldapConnectionConfiguration =
289 new LdapConnectionConfiguration( ldapConfiguration.getHostName(), ldapConfiguration.getPort(),
290 ldapConfiguration.getBaseDn(), ldapConfiguration.getContextFactory(),
291 ldapConfiguration.getBindDn(), ldapConfiguration.getPassword(),
292 ldapConfiguration.getAuthenticationMethod(),
293 toProperties( ldapConfiguration.getExtraProperties() ) );
295 ldapConnection = ldapConnectionFactory.getConnection( ldapConnectionConfiguration );
297 ldapConnection.close();
299 // verify groups dn value too
301 ldapConnectionConfiguration =
302 new LdapConnectionConfiguration( ldapConfiguration.getHostName(), ldapConfiguration.getPort(),
303 ldapConfiguration.getBaseGroupsDn(),
304 ldapConfiguration.getContextFactory(), ldapConfiguration.getBindDn(),
305 ldapConfiguration.getPassword(),
306 ldapConfiguration.getAuthenticationMethod(),
307 toProperties( ldapConfiguration.getExtraProperties() ) );
309 ldapConnection = ldapConnectionFactory.getConnection( ldapConnectionConfiguration );
311 catch ( InvalidNameException e )
313 log.warn( "fail to get LdapConnection: {}", e.getMessage() );
314 throw new ArchivaRestServiceException( e.getMessage(), e );
316 catch ( LdapException e )
318 log.warn( "fail to get LdapConnection: {}", e.getMessage() );
319 throw new ArchivaRestServiceException( e.getMessage(), e );
324 if ( ldapConnection != null )
326 ldapConnection.close();
333 private Properties toProperties( Map<String, String> map )
335 Properties properties = new Properties();
336 if ( map == null || map.isEmpty() )
340 for ( Map.Entry<String, String> entry : map.entrySet() )
342 properties.put( entry.getKey(), entry.getValue() );