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.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.rbac.RBACManager;
37 import org.apache.archiva.redback.users.UserManager;
38 import org.apache.archiva.rest.api.model.RBACManagerImplementationInformation;
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.Comparator;
54 import java.util.List;
56 import java.util.Properties;
59 * @author Olivier Lamy
62 @Service("redbackRuntimeConfigurationService#rest")
63 public class DefaultRedbackRuntimeConfigurationService
64 extends AbstractRestService
65 implements RedbackRuntimeConfigurationService
68 private RedbackRuntimeConfigurationAdmin redbackRuntimeConfigurationAdmin;
71 @Named(value = "userManager#configurable")
72 private UserManager userManager;
75 private ApplicationContext applicationContext;
78 @Named(value = "ldapConnectionFactory#configurable")
79 private LdapConnectionFactory ldapConnectionFactory;
82 @Named(value = "cache#users")
83 private Cache usersCache;
86 private LdapUserMapper ldapUserMapper;
89 public RedbackRuntimeConfiguration getRedbackRuntimeConfiguration()
90 throws ArchivaRestServiceException
94 return redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration();
96 catch ( RepositoryAdminException e )
98 throw new ArchivaRestServiceException( e.getMessage(), e );
102 public Boolean updateRedbackRuntimeConfiguration( RedbackRuntimeConfiguration redbackRuntimeConfiguration )
103 throws ArchivaRestServiceException
107 // has user manager impl changed ?
108 boolean userManagerChanged = redbackRuntimeConfiguration.getUserManagerImpls().size()
109 != redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getUserManagerImpls().size();
112 userManagerChanged || ( redbackRuntimeConfiguration.getUserManagerImpls().toString().hashCode()
113 != redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getUserManagerImpls().toString().hashCode() );
115 redbackRuntimeConfigurationAdmin.updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration );
117 if ( userManagerChanged )
119 log.info( "user managerImpls changed to {} so reload it",
120 redbackRuntimeConfiguration.getUserManagerImpls() );
121 userManager.initialize();
124 ldapConnectionFactory.initialize();
126 Collection<PasswordRule> passwordRules = applicationContext.getBeansOfType( PasswordRule.class ).values();
128 for ( PasswordRule passwordRule : passwordRules )
130 passwordRule.initialize();
133 Collection<CookieSettings> cookieSettingsList =
134 applicationContext.getBeansOfType( CookieSettings.class ).values();
136 for ( CookieSettings cookieSettings : cookieSettingsList )
138 cookieSettings.initialize();
141 Collection<Authenticator> authenticators =
142 applicationContext.getBeansOfType( Authenticator.class ).values();
144 for ( Authenticator authenticator : authenticators )
146 authenticator.initialize();
150 usersCache.setTimeToIdleSeconds(
151 redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToIdleSeconds() );
152 usersCache.setTimeToLiveSeconds(
153 redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToLiveSeconds() );
154 usersCache.setMaxElementsInMemory(
155 redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsInMemory() );
156 usersCache.setMaxElementsOnDisk(
157 redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsOnDisk() );
159 ldapUserMapper.initialize();
163 catch ( AuthenticationException e )
165 throw new ArchivaRestServiceException( e.getMessage(), e );
167 catch ( RepositoryAdminException e )
169 throw new ArchivaRestServiceException( e.getMessage(), e );
174 public List<UserManagerImplementationInformation> getUserManagerImplementationInformations()
175 throws ArchivaRestServiceException
178 Map<String, UserManager> beans = applicationContext.getBeansOfType( UserManager.class );
180 if ( beans.isEmpty() )
182 return Collections.emptyList();
185 List<UserManagerImplementationInformation> informations =
186 new ArrayList<UserManagerImplementationInformation>( beans.size() );
188 for ( Map.Entry<String, UserManager> entry : beans.entrySet() )
190 UserManager userManager = applicationContext.getBean( entry.getKey(), UserManager.class );
191 if ( userManager.isFinalImplementation() )
193 UserManagerImplementationInformation information = new UserManagerImplementationInformation();
194 information.setBeanId( StringUtils.substringAfter( entry.getKey(), "#" ) );
195 information.setDescriptionKey( userManager.getDescriptionKey() );
196 information.setReadOnly( userManager.isReadOnly() );
197 informations.add( information );
204 public List<RBACManagerImplementationInformation> getRbacManagerImplementationInformations()
205 throws ArchivaRestServiceException
207 Map<String, RBACManager> beans = applicationContext.getBeansOfType( RBACManager.class );
209 if ( beans.isEmpty() )
211 return Collections.emptyList();
214 List<RBACManagerImplementationInformation> informations =
215 new ArrayList<RBACManagerImplementationInformation>( beans.size() );
217 for ( Map.Entry<String, RBACManager> entry : beans.entrySet() )
219 UserManager userManager = applicationContext.getBean( entry.getKey(), UserManager.class );
220 if ( userManager.isFinalImplementation() )
222 RBACManagerImplementationInformation information = new RBACManagerImplementationInformation();
223 information.setBeanId( StringUtils.substringAfter( entry.getKey(), "#" ) );
224 information.setDescriptionKey( userManager.getDescriptionKey() );
225 information.setReadOnly( userManager.isReadOnly() );
226 informations.add( information );
233 public Boolean checkLdapConnection()
234 throws ArchivaRestServiceException
236 LdapConnection ldapConnection = null;
239 ldapConnection = ldapConnectionFactory.getConnection();
241 catch ( LdapException e )
243 log.warn( "fail to get LdapConnection: {}", e.getMessage() );
244 throw new ArchivaRestServiceException( e.getMessage(), e );
249 if ( ldapConnection != null )
251 ldapConnection.close();
258 public Boolean checkLdapConnection( LdapConfiguration ldapConfiguration )
259 throws ArchivaRestServiceException
261 LdapConnection ldapConnection = null;
264 LdapConnectionConfiguration ldapConnectionConfiguration =
265 new LdapConnectionConfiguration( ldapConfiguration.getHostName(), ldapConfiguration.getPort(),
266 ldapConfiguration.getBaseDn(), ldapConfiguration.getContextFactory(),
267 ldapConfiguration.getBindDn(), ldapConfiguration.getPassword(),
268 ldapConfiguration.getAuthenticationMethod(),
269 toProperties( ldapConfiguration.getExtraProperties() ) );
271 ldapConnection = ldapConnectionFactory.getConnection( ldapConnectionConfiguration );
273 ldapConnection.close();
275 // verify groups dn value too
277 ldapConnectionConfiguration =
278 new LdapConnectionConfiguration( ldapConfiguration.getHostName(), ldapConfiguration.getPort(),
279 ldapConfiguration.getBaseGroupsDn(),
280 ldapConfiguration.getContextFactory(), ldapConfiguration.getBindDn(),
281 ldapConfiguration.getPassword(),
282 ldapConfiguration.getAuthenticationMethod(),
283 toProperties( ldapConfiguration.getExtraProperties() ) );
285 ldapConnection = ldapConnectionFactory.getConnection( ldapConnectionConfiguration );
287 catch ( InvalidNameException e )
289 log.warn( "fail to get LdapConnection: {}", e.getMessage() );
290 throw new ArchivaRestServiceException( e.getMessage(), e );
292 catch ( LdapException e )
294 log.warn( "fail to get LdapConnection: {}", e.getMessage() );
295 throw new ArchivaRestServiceException( e.getMessage(), e );
300 if ( ldapConnection != null )
302 ldapConnection.close();
309 private Properties toProperties( Map<String, String> map )
311 Properties properties = new Properties();
312 if ( map == null || map.isEmpty() )
316 for ( Map.Entry<String, String> entry : map.entrySet() )
318 properties.put( entry.getKey(), entry.getValue() );