1 package org.apache.archiva.admin.repository.runtime;
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 net.sf.beanlib.provider.replicator.BeanReplicator;
22 import org.apache.archiva.admin.model.RepositoryAdminException;
23 import org.apache.archiva.admin.model.beans.CacheConfiguration;
24 import org.apache.archiva.admin.model.beans.LdapConfiguration;
25 import org.apache.archiva.admin.model.beans.RedbackRuntimeConfiguration;
26 import org.apache.archiva.admin.model.runtime.RedbackRuntimeConfigurationAdmin;
27 import org.apache.archiva.configuration.ArchivaConfiguration;
28 import org.apache.archiva.configuration.Configuration;
29 import org.apache.archiva.configuration.IndeterminateConfigurationException;
30 import org.apache.archiva.redback.components.cache.Cache;
31 import org.apache.archiva.redback.components.registry.RegistryException;
32 import org.apache.archiva.redback.configuration.UserConfiguration;
33 import org.apache.archiva.redback.configuration.UserConfigurationException;
34 import org.apache.archiva.redback.configuration.UserConfigurationKeys;
35 import org.apache.commons.lang.StringUtils;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.stereotype.Service;
40 import javax.annotation.PostConstruct;
41 import javax.inject.Inject;
42 import javax.inject.Named;
43 import java.util.List;
46 * @author Olivier Lamy
49 @Service( "userConfiguration#archiva" )
50 public class DefaultRedbackRuntimeConfigurationAdmin
51 implements RedbackRuntimeConfigurationAdmin, UserConfiguration
54 protected Logger log = LoggerFactory.getLogger( getClass() );
57 private ArchivaConfiguration archivaConfiguration;
60 @Named( value = "userConfiguration#redback" )
61 UserConfiguration userConfiguration;
64 @Named( value = "cache#users" )
65 private Cache usersCache;
68 public void initialize()
69 throws UserConfigurationException
73 RedbackRuntimeConfiguration redbackRuntimeConfiguration = getRedbackRuntimeConfiguration();
74 // migrate or not data from redback
75 if ( !redbackRuntimeConfiguration.isMigratedFromRedbackConfiguration() )
77 // so migrate if available
78 String userManagerImpl = userConfiguration.getString( UserConfigurationKeys.USER_MANAGER_IMPL );
79 if ( StringUtils.isNotEmpty( userManagerImpl ) )
81 redbackRuntimeConfiguration.getUserManagerImpls().add( userManagerImpl );
86 LdapConfiguration ldapConfiguration = redbackRuntimeConfiguration.getLdapConfiguration();
87 if ( ldapConfiguration == null )
89 ldapConfiguration = new LdapConfiguration();
90 redbackRuntimeConfiguration.setLdapConfiguration( ldapConfiguration );
93 ldapConfiguration.setHostName(
94 userConfiguration.getString( UserConfigurationKeys.LDAP_HOSTNAME, null ) );
95 ldapConfiguration.setPort( userConfiguration.getInt( UserConfigurationKeys.LDAP_PORT, -1 ) );
96 ldapConfiguration.setSsl( userConfiguration.getBoolean( UserConfigurationKeys.LDAP_SSL, false ) );
97 ldapConfiguration.setBaseDn(
98 userConfiguration.getConcatenatedList( UserConfigurationKeys.LDAP_BASEDN, null ) );
99 ldapConfiguration.setContextFactory(
100 userConfiguration.getString( UserConfigurationKeys.LDAP_CONTEX_FACTORY, null ) );
101 ldapConfiguration.setBindDn(
102 userConfiguration.getConcatenatedList( UserConfigurationKeys.LDAP_BINDDN, null ) );
103 ldapConfiguration.setPassword(
104 userConfiguration.getString( UserConfigurationKeys.LDAP_PASSWORD, null ) );
105 ldapConfiguration.setAuthenticationMethod(
106 userConfiguration.getString( UserConfigurationKeys.LDAP_AUTHENTICATION_METHOD, null ) );
108 redbackRuntimeConfiguration.setMigratedFromRedbackConfiguration( true );
110 updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration );
113 // we must ensure userManagerImpls list is not empty if so put at least jdo one !
114 if ( redbackRuntimeConfiguration.getUserManagerImpls().isEmpty() )
117 "redbackRuntimeConfiguration with empty userManagerImpls so force at least jdo implementation !" );
118 redbackRuntimeConfiguration.getUserManagerImpls().add( "jdo" );
119 updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration );
122 boolean save = false;
125 if ( redbackRuntimeConfiguration.getUsersCacheConfiguration() == null )
127 redbackRuntimeConfiguration.setUsersCacheConfiguration( new CacheConfiguration() );
129 // if -1 it means non initialized to take values from the spring bean
130 if ( redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToIdleSeconds() < 0 )
132 redbackRuntimeConfiguration.getUsersCacheConfiguration().setTimeToIdleSeconds(
133 usersCache.getTimeToIdleSeconds() );
137 usersCache.setTimeToIdleSeconds(
138 redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToIdleSeconds() );
140 if ( redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToLiveSeconds() < 0 )
142 redbackRuntimeConfiguration.getUsersCacheConfiguration().setTimeToLiveSeconds(
143 usersCache.getTimeToLiveSeconds() );
147 usersCache.setTimeToLiveSeconds(
148 redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToLiveSeconds() );
150 if ( redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsInMemory() < 0 )
152 redbackRuntimeConfiguration.getUsersCacheConfiguration().setMaxElementsInMemory(
153 usersCache.getMaxElementsInMemory() );
156 usersCache.setMaxElementsInMemory(
157 redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsInMemory() );
159 if ( redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsOnDisk() < 0 )
161 redbackRuntimeConfiguration.getUsersCacheConfiguration().setMaxElementsOnDisk(
162 usersCache.getMaxElementsOnDisk() );
165 usersCache.setMaxElementsOnDisk(
166 redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsOnDisk() );
170 updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration );
174 catch ( RepositoryAdminException e )
176 throw new UserConfigurationException( e.getMessage(), e );
180 public RedbackRuntimeConfiguration getRedbackRuntimeConfiguration()
182 return build( archivaConfiguration.getConfiguration().getRedbackRuntimeConfiguration() );
185 public void updateRedbackRuntimeConfiguration( RedbackRuntimeConfiguration redbackRuntimeConfiguration )
186 throws RepositoryAdminException
188 org.apache.archiva.configuration.RedbackRuntimeConfiguration runtimeConfiguration =
189 build( redbackRuntimeConfiguration );
190 Configuration configuration = archivaConfiguration.getConfiguration();
191 configuration.setRedbackRuntimeConfiguration( runtimeConfiguration );
194 archivaConfiguration.save( configuration );
196 catch ( RegistryException e )
198 throw new RepositoryAdminException( e.getMessage(), e );
200 catch ( IndeterminateConfigurationException e )
202 throw new RepositoryAdminException( e.getMessage(), e );
206 private RedbackRuntimeConfiguration build(
207 org.apache.archiva.configuration.RedbackRuntimeConfiguration runtimeConfiguration )
209 RedbackRuntimeConfiguration redbackRuntimeConfiguration =
210 new BeanReplicator().replicateBean( runtimeConfiguration, RedbackRuntimeConfiguration.class );
212 if ( runtimeConfiguration.getLdapConfiguration() != null )
214 redbackRuntimeConfiguration.setLdapConfiguration(
215 new BeanReplicator().replicateBean( runtimeConfiguration.getLdapConfiguration(),
216 LdapConfiguration.class ) );
219 if ( runtimeConfiguration.getUsersCacheConfiguration() != null )
221 redbackRuntimeConfiguration.setUsersCacheConfiguration(
222 new BeanReplicator().replicateBean( runtimeConfiguration.getUsersCacheConfiguration(),
223 CacheConfiguration.class ) );
226 if ( redbackRuntimeConfiguration.getLdapConfiguration() == null )
229 redbackRuntimeConfiguration.setLdapConfiguration( new LdapConfiguration() );
232 if ( redbackRuntimeConfiguration.getUsersCacheConfiguration() == null )
234 redbackRuntimeConfiguration.setUsersCacheConfiguration( new CacheConfiguration() );
237 return redbackRuntimeConfiguration;
240 private org.apache.archiva.configuration.RedbackRuntimeConfiguration build(
241 RedbackRuntimeConfiguration archivaRuntimeConfiguration )
243 org.apache.archiva.configuration.RedbackRuntimeConfiguration redbackRuntimeConfiguration =
244 new BeanReplicator().replicateBean( archivaRuntimeConfiguration,
245 org.apache.archiva.configuration.RedbackRuntimeConfiguration.class );
247 if ( archivaRuntimeConfiguration.getLdapConfiguration() == null )
249 archivaRuntimeConfiguration.setLdapConfiguration( new LdapConfiguration() );
251 redbackRuntimeConfiguration.setLdapConfiguration(
252 new BeanReplicator().replicateBean( archivaRuntimeConfiguration.getLdapConfiguration(),
253 org.apache.archiva.configuration.LdapConfiguration.class ) );
255 if ( archivaRuntimeConfiguration.getUsersCacheConfiguration() == null )
257 archivaRuntimeConfiguration.setUsersCacheConfiguration( new CacheConfiguration() );
260 redbackRuntimeConfiguration.setUsersCacheConfiguration(
261 new BeanReplicator().replicateBean( archivaRuntimeConfiguration.getUsersCacheConfiguration(),
262 org.apache.archiva.configuration.CacheConfiguration.class ) );
264 return redbackRuntimeConfiguration;
267 // wrapper for UserConfiguration to intercept values (and store it not yet migrated
270 public String getString( String key )
272 if ( UserConfigurationKeys.USER_MANAGER_IMPL.equals( key ) )
274 // possible false for others than archiva user manager
275 return getRedbackRuntimeConfiguration().getUserManagerImpls().get( 0 );
278 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
280 if ( conf.getConfigurationProperties().containsKey( key ) )
282 return conf.getConfigurationProperties().get( key );
285 String value = userConfiguration.getString( key );
290 conf.getConfigurationProperties().put( key, value );
294 updateRedbackRuntimeConfiguration( conf );
296 catch ( RepositoryAdminException e )
298 log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
299 throw new RuntimeException( e.getMessage(), e );
305 public String getString( String key, String defaultValue )
307 if ( UserConfigurationKeys.LDAP_HOSTNAME.equals( key ) )
309 return getRedbackRuntimeConfiguration().getLdapConfiguration().getHostName();
311 if ( UserConfigurationKeys.LDAP_CONTEX_FACTORY.equals( key ) )
313 return getRedbackRuntimeConfiguration().getLdapConfiguration().getContextFactory();
315 if ( UserConfigurationKeys.LDAP_PASSWORD.equals( key ) )
317 return getRedbackRuntimeConfiguration().getLdapConfiguration().getPassword();
319 if ( UserConfigurationKeys.LDAP_AUTHENTICATION_METHOD.equals( key ) )
321 return getRedbackRuntimeConfiguration().getLdapConfiguration().getAuthenticationMethod();
324 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
326 if ( conf.getConfigurationProperties().containsKey( key ) )
328 return conf.getConfigurationProperties().get( key );
331 String value = userConfiguration.getString( key, defaultValue );
338 conf.getConfigurationProperties().put( key, value );
341 updateRedbackRuntimeConfiguration( conf );
343 catch ( RepositoryAdminException e )
345 log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
346 throw new RuntimeException( e.getMessage(), e );
352 public int getInt( String key )
354 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
356 if ( conf.getConfigurationProperties().containsKey( key ) )
358 return Integer.valueOf( conf.getConfigurationProperties().get( key ) );
361 int value = userConfiguration.getInt( key );
363 conf.getConfigurationProperties().put( key, Integer.toString( value ) );
366 updateRedbackRuntimeConfiguration( conf );
368 catch ( RepositoryAdminException e )
370 log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
371 throw new RuntimeException( e.getMessage(), e );
377 public int getInt( String key, int defaultValue )
379 if ( UserConfigurationKeys.LDAP_PORT.equals( key ) )
381 return getRedbackRuntimeConfiguration().getLdapConfiguration().getPort();
384 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
386 if ( conf.getConfigurationProperties().containsKey( key ) )
388 return Integer.valueOf( conf.getConfigurationProperties().get( key ) );
391 int value = userConfiguration.getInt( key, defaultValue );
393 conf.getConfigurationProperties().put( key, Integer.toString( value ) );
396 updateRedbackRuntimeConfiguration( conf );
398 catch ( RepositoryAdminException e )
400 log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
401 throw new RuntimeException( e.getMessage(), e );
407 public boolean getBoolean( String key )
409 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
411 if ( conf.getConfigurationProperties().containsKey( key ) )
413 return Boolean.valueOf( conf.getConfigurationProperties().get( key ) );
416 boolean value = userConfiguration.getBoolean( key );
418 conf.getConfigurationProperties().put( key, Boolean.toString( value ) );
421 updateRedbackRuntimeConfiguration( conf );
423 catch ( RepositoryAdminException e )
425 log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
426 throw new RuntimeException( e.getMessage(), e );
432 public boolean getBoolean( String key, boolean defaultValue )
434 if ( UserConfigurationKeys.LDAP_SSL.equals( key ) )
436 return getRedbackRuntimeConfiguration().getLdapConfiguration().isSsl();
439 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
441 if ( conf.getConfigurationProperties().containsKey( key ) )
443 return Boolean.valueOf( conf.getConfigurationProperties().get( key ) );
446 boolean value = userConfiguration.getBoolean( key, defaultValue );
448 conf.getConfigurationProperties().put( key, Boolean.toString( value ) );
451 updateRedbackRuntimeConfiguration( conf );
453 catch ( RepositoryAdminException e )
455 log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
456 throw new RuntimeException( e.getMessage(), e );
462 public List<String> getList( String key )
464 List<String> value = userConfiguration.getList( key );
466 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
467 // TODO concat values
468 conf.getConfigurationProperties().put( key, "" );
471 updateRedbackRuntimeConfiguration( conf );
473 catch ( RepositoryAdminException e )
475 log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
476 throw new RuntimeException( e.getMessage(), e );
482 public String getConcatenatedList( String key, String defaultValue )
484 if ( UserConfigurationKeys.LDAP_BASEDN.equals( key ) )
486 return getRedbackRuntimeConfiguration().getLdapConfiguration().getBaseDn();
488 if ( UserConfigurationKeys.LDAP_BINDDN.equals( key ) )
490 return getRedbackRuntimeConfiguration().getLdapConfiguration().getBindDn();
492 return userConfiguration.getConcatenatedList( key, defaultValue );