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.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;
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;
52 import java.util.Properties;
55 * @author Olivier Lamy
58 @Service("redbackRuntimeConfigurationService#rest")
59 public class DefaultRedbackRuntimeConfigurationService
60 extends AbstractRestService
61 implements RedbackRuntimeConfigurationService
64 private RedbackRuntimeConfigurationAdmin redbackRuntimeConfigurationAdmin;
67 @Named(value = "userManager#configurable")
68 private UserManager userManager;
71 private ApplicationContext applicationContext;
74 @Named(value = "ldapConnectionFactory#configurable")
75 private LdapConnectionFactory ldapConnectionFactory;
78 @Named(value = "cache#users")
79 private Cache usersCache;
82 public RedbackRuntimeConfiguration getRedbackRuntimeConfiguration()
83 throws ArchivaRestServiceException
87 return redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration();
89 catch ( RepositoryAdminException e )
91 throw new ArchivaRestServiceException( e.getMessage(), e );
95 public Boolean updateRedbackRuntimeConfiguration( RedbackRuntimeConfiguration redbackRuntimeConfiguration )
96 throws ArchivaRestServiceException
100 // has user manager impl changed ?
101 boolean userManagerChanged = redbackRuntimeConfiguration.getUserManagerImpls().size()
102 != redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getUserManagerImpls().size();
105 userManagerChanged || ( redbackRuntimeConfiguration.getUserManagerImpls().toString().hashCode()
106 != redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getUserManagerImpls().toString().hashCode() );
108 redbackRuntimeConfigurationAdmin.updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration );
110 if ( userManagerChanged )
112 log.info( "user managerImpls changed to {} so reload it",
113 redbackRuntimeConfiguration.getUserManagerImpls() );
114 userManager.initialize();
117 ldapConnectionFactory.initialize();
119 Collection<PasswordRule> passwordRules = applicationContext.getBeansOfType( PasswordRule.class ).values();
121 for ( PasswordRule passwordRule : passwordRules )
123 passwordRule.initialize();
126 Collection<CookieSettings> cookieSettingsList =
127 applicationContext.getBeansOfType( CookieSettings.class ).values();
129 for ( CookieSettings cookieSettings : cookieSettingsList )
131 cookieSettings.initialize();
134 Collection<Authenticator> authenticators =
135 applicationContext.getBeansOfType( Authenticator.class ).values();
137 for ( Authenticator authenticator : authenticators )
139 authenticator.initialize();
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() );
154 catch ( AuthenticationException e )
156 throw new ArchivaRestServiceException( e.getMessage(), e );
158 catch ( RepositoryAdminException e )
160 throw new ArchivaRestServiceException( e.getMessage(), e );
165 public List<UserManagerImplementationInformation> getUserManagerImplementationInformations()
166 throws ArchivaRestServiceException
169 Map<String, UserManager> beans = applicationContext.getBeansOfType( UserManager.class );
171 if ( beans.isEmpty() )
173 return Collections.emptyList();
176 List<UserManagerImplementationInformation> informations =
177 new ArrayList<UserManagerImplementationInformation>( beans.size() );
179 for ( Map.Entry<String, UserManager> entry : beans.entrySet() )
181 UserManager userManager = applicationContext.getBean( entry.getKey(), UserManager.class );
182 if ( userManager.isFinalImplementation() )
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 );
196 public Boolean checkLdapConnection()
197 throws ArchivaRestServiceException
199 LdapConnection ldapConnection = null;
202 ldapConnection = ldapConnectionFactory.getConnection();
204 catch ( LdapException e )
206 log.warn( "fail to get LdapConnection: {}", e.getMessage() );
207 throw new ArchivaRestServiceException( e.getMessage(), e );
212 if ( ldapConnection != null )
214 ldapConnection.close();
221 public Boolean checkLdapConnection( LdapConfiguration ldapConfiguration )
222 throws ArchivaRestServiceException
224 LdapConnection ldapConnection = null;
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() ) );
234 ldapConnection = ldapConnectionFactory.getConnection( ldapConnectionConfiguration );
236 catch ( InvalidNameException e )
238 log.warn( "fail to get LdapConnection: {}", e.getMessage() );
239 throw new ArchivaRestServiceException( e.getMessage(), e );
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 private Properties toProperties( Map<String, String> map )
260 Properties properties = new Properties();
261 if ( map == null || map.isEmpty() )
265 for ( Map.Entry<String, String> entry : map.entrySet() )
267 properties.put( entry.getKey(), entry.getValue() );