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.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.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 import org.apache.commons.lang.StringUtils;
41 import org.springframework.context.ApplicationContext;
42 import org.springframework.stereotype.Service;
44 import javax.annotation.PostConstruct;
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.List;
53 import java.util.Properties;
56 * @author Olivier Lamy
59 @Service("redbackRuntimeConfigurationService#rest")
60 public class DefaultRedbackRuntimeConfigurationService
61 extends AbstractRestService
62 implements RedbackRuntimeConfigurationService
65 private RedbackRuntimeConfigurationAdmin redbackRuntimeConfigurationAdmin;
68 @Named(value = "userManager#configurable")
69 private UserManager userManager;
72 private ApplicationContext applicationContext;
75 @Named(value = "ldapConnectionFactory#configurable")
76 private LdapConnectionFactory ldapConnectionFactory;
79 @Named(value = "cache#users")
80 private Cache usersCache;
83 private LdapUserMapper ldapUserMapper;
86 public RedbackRuntimeConfiguration getRedbackRuntimeConfiguration()
87 throws ArchivaRestServiceException
91 return redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration();
93 catch ( RepositoryAdminException e )
95 throw new ArchivaRestServiceException( e.getMessage(), e );
99 public Boolean updateRedbackRuntimeConfiguration( RedbackRuntimeConfiguration redbackRuntimeConfiguration )
100 throws ArchivaRestServiceException
104 // has user manager impl changed ?
105 boolean userManagerChanged = redbackRuntimeConfiguration.getUserManagerImpls().size()
106 != redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getUserManagerImpls().size();
109 userManagerChanged || ( redbackRuntimeConfiguration.getUserManagerImpls().toString().hashCode()
110 != redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getUserManagerImpls().toString().hashCode() );
112 redbackRuntimeConfigurationAdmin.updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration );
114 if ( userManagerChanged )
116 log.info( "user managerImpls changed to {} so reload it",
117 redbackRuntimeConfiguration.getUserManagerImpls() );
118 userManager.initialize();
121 ldapConnectionFactory.initialize();
123 Collection<PasswordRule> passwordRules = applicationContext.getBeansOfType( PasswordRule.class ).values();
125 for ( PasswordRule passwordRule : passwordRules )
127 passwordRule.initialize();
130 Collection<CookieSettings> cookieSettingsList =
131 applicationContext.getBeansOfType( CookieSettings.class ).values();
133 for ( CookieSettings cookieSettings : cookieSettingsList )
135 cookieSettings.initialize();
138 Collection<Authenticator> authenticators =
139 applicationContext.getBeansOfType( Authenticator.class ).values();
141 for ( Authenticator authenticator : authenticators )
143 authenticator.initialize();
147 usersCache.setTimeToIdleSeconds(
148 redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToIdleSeconds() );
149 usersCache.setTimeToLiveSeconds(
150 redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToLiveSeconds() );
151 usersCache.setMaxElementsInMemory(
152 redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsInMemory() );
153 usersCache.setMaxElementsOnDisk(
154 redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsOnDisk() );
157 ldapUserMapper.initialize();
161 catch ( AuthenticationException e )
163 throw new ArchivaRestServiceException( e.getMessage(), e );
165 catch ( RepositoryAdminException e )
167 throw new ArchivaRestServiceException( e.getMessage(), e );
172 public List<UserManagerImplementationInformation> getUserManagerImplementationInformations()
173 throws ArchivaRestServiceException
176 Map<String, UserManager> beans = applicationContext.getBeansOfType( UserManager.class );
178 if ( beans.isEmpty() )
180 return Collections.emptyList();
183 List<UserManagerImplementationInformation> informations =
184 new ArrayList<UserManagerImplementationInformation>( beans.size() );
186 for ( Map.Entry<String, UserManager> entry : beans.entrySet() )
188 UserManager userManager = applicationContext.getBean( entry.getKey(), UserManager.class );
189 if ( userManager.isFinalImplementation() )
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 );
203 public Boolean checkLdapConnection()
204 throws ArchivaRestServiceException
206 LdapConnection ldapConnection = null;
209 ldapConnection = ldapConnectionFactory.getConnection();
211 catch ( LdapException e )
213 log.warn( "fail to get LdapConnection: {}", e.getMessage() );
214 throw new ArchivaRestServiceException( e.getMessage(), e );
219 if ( ldapConnection != null )
221 ldapConnection.close();
228 public Boolean checkLdapConnection( LdapConfiguration ldapConfiguration )
229 throws ArchivaRestServiceException
231 LdapConnection ldapConnection = null;
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() ) );
241 ldapConnection = ldapConnectionFactory.getConnection( ldapConnectionConfiguration );
243 catch ( InvalidNameException e )
245 log.warn( "fail to get LdapConnection: {}", e.getMessage() );
246 throw new ArchivaRestServiceException( e.getMessage(), e );
248 catch ( LdapException e )
250 log.warn( "fail to get LdapConnection: {}", e.getMessage() );
251 throw new ArchivaRestServiceException( e.getMessage(), e );
256 if ( ldapConnection != null )
258 ldapConnection.close();
265 private Properties toProperties( Map<String, String> map )
267 Properties properties = new Properties();
268 if ( map == null || map.isEmpty() )
272 for ( Map.Entry<String, String> entry : map.entrySet() )
274 properties.put( entry.getKey(), entry.getValue() );