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.Collection;
44 import java.util.HashSet;
45 import java.util.List;
49 * @author Olivier Lamy
52 @Service("userConfiguration#archiva")
53 public class DefaultRedbackRuntimeConfigurationAdmin
54 implements RedbackRuntimeConfigurationAdmin, UserConfiguration
57 protected Logger log = LoggerFactory.getLogger( getClass() );
60 private ArchivaConfiguration archivaConfiguration;
63 @Named(value = "userConfiguration#redback")
64 UserConfiguration userConfiguration;
67 @Named(value = "cache#users")
68 private Cache usersCache;
71 public void initialize()
72 throws UserConfigurationException
76 RedbackRuntimeConfiguration redbackRuntimeConfiguration = getRedbackRuntimeConfiguration();
77 // migrate or not data from redback
78 if ( !redbackRuntimeConfiguration.isMigratedFromRedbackConfiguration() )
80 // so migrate if available
81 String userManagerImpl =
82 userConfiguration.getConcatenatedList( UserConfigurationKeys.USER_MANAGER_IMPL, "jdo" );
83 if ( StringUtils.isNotEmpty( userManagerImpl ) )
85 if ( StringUtils.contains( userManagerImpl, ',' ) )
87 String[] impls = StringUtils.split( userManagerImpl, ',' );
88 for ( String impl : impls )
90 redbackRuntimeConfiguration.getUserManagerImpls().add( impl );
95 redbackRuntimeConfiguration.getUserManagerImpls().add( userManagerImpl );
99 String authorizerImpls =
100 userConfiguration.getConcatenatedList( UserConfigurationKeys.AUTHORIZER_IMPL, "rbac" );
102 if ( StringUtils.isNotEmpty( authorizerImpls ) )
104 if ( StringUtils.contains( authorizerImpls, ',' ) )
106 String[] impls = StringUtils.split( authorizerImpls, ',' );
107 for ( String impl : impls )
109 redbackRuntimeConfiguration.getAuthorizerImpls().add( impl );
114 redbackRuntimeConfiguration.getAuthorizerImpls().add( userManagerImpl );
120 LdapConfiguration ldapConfiguration = redbackRuntimeConfiguration.getLdapConfiguration();
121 if ( ldapConfiguration == null )
123 ldapConfiguration = new LdapConfiguration();
124 redbackRuntimeConfiguration.setLdapConfiguration( ldapConfiguration );
127 ldapConfiguration.setHostName(
128 userConfiguration.getString( UserConfigurationKeys.LDAP_HOSTNAME, null ) );
129 ldapConfiguration.setPort( userConfiguration.getInt( UserConfigurationKeys.LDAP_PORT, -1 ) );
130 ldapConfiguration.setSsl( userConfiguration.getBoolean( UserConfigurationKeys.LDAP_SSL, false ) );
131 ldapConfiguration.setBaseDn(
132 userConfiguration.getConcatenatedList( UserConfigurationKeys.LDAP_BASEDN, null ) );
133 ldapConfiguration.setContextFactory(
134 userConfiguration.getString( UserConfigurationKeys.LDAP_CONTEX_FACTORY, null ) );
135 ldapConfiguration.setBindDn(
136 userConfiguration.getConcatenatedList( UserConfigurationKeys.LDAP_BINDDN, null ) );
137 ldapConfiguration.setPassword(
138 userConfiguration.getString( UserConfigurationKeys.LDAP_PASSWORD, null ) );
139 ldapConfiguration.setAuthenticationMethod(
140 userConfiguration.getString( UserConfigurationKeys.LDAP_AUTHENTICATION_METHOD, null ) );
142 redbackRuntimeConfiguration.setMigratedFromRedbackConfiguration( true );
144 updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration );
147 // we must ensure userManagerImpls list is not empty if so put at least jdo one !
148 if ( redbackRuntimeConfiguration.getUserManagerImpls().isEmpty() )
151 "redbackRuntimeConfiguration with empty userManagerImpls so force at least jdo implementation !" );
152 redbackRuntimeConfiguration.getUserManagerImpls().add( "jdo" );
153 updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration );
156 // we ensure authorizerImpls is not empty if so put
157 if ( redbackRuntimeConfiguration.getAuthorizerImpls().isEmpty() )
160 "redbackRuntimeConfiguration with empty authorizerImpls so force at least rbac implementation !" );
161 redbackRuntimeConfiguration.getAuthorizerImpls().add( "rbac" );
162 updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration );
165 boolean save = false;
168 if ( redbackRuntimeConfiguration.getUsersCacheConfiguration() == null )
170 redbackRuntimeConfiguration.setUsersCacheConfiguration( new CacheConfiguration() );
172 // if -1 it means non initialized to take values from the spring bean
173 if ( redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToIdleSeconds() < 0 )
175 redbackRuntimeConfiguration.getUsersCacheConfiguration().setTimeToIdleSeconds(
176 usersCache.getTimeToIdleSeconds() );
180 usersCache.setTimeToIdleSeconds(
181 redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToIdleSeconds() );
183 if ( redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToLiveSeconds() < 0 )
185 redbackRuntimeConfiguration.getUsersCacheConfiguration().setTimeToLiveSeconds(
186 usersCache.getTimeToLiveSeconds() );
190 usersCache.setTimeToLiveSeconds(
191 redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToLiveSeconds() );
193 if ( redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsInMemory() < 0 )
195 redbackRuntimeConfiguration.getUsersCacheConfiguration().setMaxElementsInMemory(
196 usersCache.getMaxElementsInMemory() );
199 usersCache.setMaxElementsInMemory(
200 redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsInMemory() );
202 if ( redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsOnDisk() < 0 )
204 redbackRuntimeConfiguration.getUsersCacheConfiguration().setMaxElementsOnDisk(
205 usersCache.getMaxElementsOnDisk() );
208 usersCache.setMaxElementsOnDisk(
209 redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsOnDisk() );
213 updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration );
217 catch ( RepositoryAdminException e )
219 throw new UserConfigurationException( e.getMessage(), e );
223 public RedbackRuntimeConfiguration getRedbackRuntimeConfiguration()
225 return build( archivaConfiguration.getConfiguration().getRedbackRuntimeConfiguration() );
228 public void updateRedbackRuntimeConfiguration( RedbackRuntimeConfiguration redbackRuntimeConfiguration )
229 throws RepositoryAdminException
231 org.apache.archiva.configuration.RedbackRuntimeConfiguration runtimeConfiguration =
232 build( redbackRuntimeConfiguration );
233 Configuration configuration = archivaConfiguration.getConfiguration();
234 configuration.setRedbackRuntimeConfiguration( runtimeConfiguration );
237 archivaConfiguration.save( configuration );
239 catch ( RegistryException e )
241 throw new RepositoryAdminException( e.getMessage(), e );
243 catch ( IndeterminateConfigurationException e )
245 throw new RepositoryAdminException( e.getMessage(), e );
249 private RedbackRuntimeConfiguration build(
250 org.apache.archiva.configuration.RedbackRuntimeConfiguration runtimeConfiguration )
252 RedbackRuntimeConfiguration redbackRuntimeConfiguration =
253 new BeanReplicator().replicateBean( runtimeConfiguration, RedbackRuntimeConfiguration.class );
255 if ( runtimeConfiguration.getLdapConfiguration() != null )
257 redbackRuntimeConfiguration.setLdapConfiguration(
258 new BeanReplicator().replicateBean( runtimeConfiguration.getLdapConfiguration(),
259 LdapConfiguration.class ) );
262 if ( runtimeConfiguration.getUsersCacheConfiguration() != null )
264 redbackRuntimeConfiguration.setUsersCacheConfiguration(
265 new BeanReplicator().replicateBean( runtimeConfiguration.getUsersCacheConfiguration(),
266 CacheConfiguration.class ) );
269 if ( redbackRuntimeConfiguration.getLdapConfiguration() == null )
272 redbackRuntimeConfiguration.setLdapConfiguration( new LdapConfiguration() );
275 if ( redbackRuntimeConfiguration.getUsersCacheConfiguration() == null )
277 redbackRuntimeConfiguration.setUsersCacheConfiguration( new CacheConfiguration() );
280 return redbackRuntimeConfiguration;
283 private org.apache.archiva.configuration.RedbackRuntimeConfiguration build(
284 RedbackRuntimeConfiguration archivaRuntimeConfiguration )
286 org.apache.archiva.configuration.RedbackRuntimeConfiguration redbackRuntimeConfiguration =
287 new BeanReplicator().replicateBean( archivaRuntimeConfiguration,
288 org.apache.archiva.configuration.RedbackRuntimeConfiguration.class );
290 if ( archivaRuntimeConfiguration.getLdapConfiguration() == null )
292 archivaRuntimeConfiguration.setLdapConfiguration( new LdapConfiguration() );
294 redbackRuntimeConfiguration.setLdapConfiguration(
295 new BeanReplicator().replicateBean( archivaRuntimeConfiguration.getLdapConfiguration(),
296 org.apache.archiva.configuration.LdapConfiguration.class ) );
298 if ( archivaRuntimeConfiguration.getUsersCacheConfiguration() == null )
300 archivaRuntimeConfiguration.setUsersCacheConfiguration( new CacheConfiguration() );
303 redbackRuntimeConfiguration.setUsersCacheConfiguration(
304 new BeanReplicator().replicateBean( archivaRuntimeConfiguration.getUsersCacheConfiguration(),
305 org.apache.archiva.configuration.CacheConfiguration.class ) );
307 return redbackRuntimeConfiguration;
310 // wrapper for UserConfiguration to intercept values (and store it not yet migrated
313 public String getString( String key )
315 if ( UserConfigurationKeys.USER_MANAGER_IMPL.equals( key ) )
317 // possible false for others than archiva user manager
318 return getRedbackRuntimeConfiguration().getUserManagerImpls().get( 0 );
321 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
323 if ( conf.getConfigurationProperties().containsKey( key ) )
325 return conf.getConfigurationProperties().get( key );
328 String value = userConfiguration.getString( key );
333 conf.getConfigurationProperties().put( key, value );
337 updateRedbackRuntimeConfiguration( conf );
339 catch ( RepositoryAdminException e )
341 log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
342 throw new RuntimeException( e.getMessage(), e );
348 public String getString( String key, String defaultValue )
350 if ( UserConfigurationKeys.LDAP_HOSTNAME.equals( key ) )
352 return getRedbackRuntimeConfiguration().getLdapConfiguration().getHostName();
354 if ( UserConfigurationKeys.LDAP_CONTEX_FACTORY.equals( key ) )
356 return getRedbackRuntimeConfiguration().getLdapConfiguration().getContextFactory();
358 if ( UserConfigurationKeys.LDAP_PASSWORD.equals( key ) )
360 return getRedbackRuntimeConfiguration().getLdapConfiguration().getPassword();
362 if ( UserConfigurationKeys.LDAP_AUTHENTICATION_METHOD.equals( key ) )
364 return getRedbackRuntimeConfiguration().getLdapConfiguration().getAuthenticationMethod();
367 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
369 if ( conf.getConfigurationProperties().containsKey( key ) )
371 return conf.getConfigurationProperties().get( key );
374 String value = userConfiguration.getString( key, defaultValue );
381 conf.getConfigurationProperties().put( key, value );
384 updateRedbackRuntimeConfiguration( conf );
386 catch ( RepositoryAdminException e )
388 log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
389 throw new RuntimeException( e.getMessage(), e );
395 public int getInt( String key )
397 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
399 if ( conf.getConfigurationProperties().containsKey( key ) )
401 return Integer.valueOf( conf.getConfigurationProperties().get( key ) );
404 int value = userConfiguration.getInt( key );
406 conf.getConfigurationProperties().put( key, Integer.toString( value ) );
409 updateRedbackRuntimeConfiguration( conf );
411 catch ( RepositoryAdminException e )
413 log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
414 throw new RuntimeException( e.getMessage(), e );
420 public int getInt( String key, int defaultValue )
422 if ( UserConfigurationKeys.LDAP_PORT.equals( key ) )
424 return getRedbackRuntimeConfiguration().getLdapConfiguration().getPort();
427 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
429 if ( conf.getConfigurationProperties().containsKey( key ) )
431 return Integer.valueOf( conf.getConfigurationProperties().get( key ) );
434 int value = userConfiguration.getInt( key, defaultValue );
436 conf.getConfigurationProperties().put( key, Integer.toString( value ) );
439 updateRedbackRuntimeConfiguration( conf );
441 catch ( RepositoryAdminException e )
443 log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
444 throw new RuntimeException( e.getMessage(), e );
450 public boolean getBoolean( String key )
452 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
454 if ( conf.getConfigurationProperties().containsKey( key ) )
456 return Boolean.valueOf( conf.getConfigurationProperties().get( key ) );
459 boolean value = userConfiguration.getBoolean( key );
461 conf.getConfigurationProperties().put( key, Boolean.toString( value ) );
464 updateRedbackRuntimeConfiguration( conf );
466 catch ( RepositoryAdminException e )
468 log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
469 throw new RuntimeException( e.getMessage(), e );
475 public boolean getBoolean( String key, boolean defaultValue )
477 if ( UserConfigurationKeys.LDAP_SSL.equals( key ) )
479 return getRedbackRuntimeConfiguration().getLdapConfiguration().isSsl();
482 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
484 if ( conf.getConfigurationProperties().containsKey( key ) )
486 return Boolean.valueOf( conf.getConfigurationProperties().get( key ) );
489 boolean value = userConfiguration.getBoolean( key, defaultValue );
491 conf.getConfigurationProperties().put( key, Boolean.toString( value ) );
494 updateRedbackRuntimeConfiguration( conf );
496 catch ( RepositoryAdminException e )
498 log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
499 throw new RuntimeException( e.getMessage(), e );
505 public List<String> getList( String key )
507 List<String> value = userConfiguration.getList( key );
509 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
510 // TODO concat values
511 conf.getConfigurationProperties().put( key, "" );
514 updateRedbackRuntimeConfiguration( conf );
516 catch ( RepositoryAdminException e )
518 log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
519 throw new RuntimeException( e.getMessage(), e );
525 public String getConcatenatedList( String key, String defaultValue )
527 if ( UserConfigurationKeys.LDAP_BASEDN.equals( key ) )
529 return getRedbackRuntimeConfiguration().getLdapConfiguration().getBaseDn();
531 if ( UserConfigurationKeys.LDAP_BINDDN.equals( key ) )
533 return getRedbackRuntimeConfiguration().getLdapConfiguration().getBindDn();
535 return userConfiguration.getConcatenatedList( key, defaultValue );
538 public Collection<String> getKeys()
540 Collection<String> keys = userConfiguration.getKeys();
542 Set<String> keysSet = new HashSet<String>( keys );
544 keysSet.addAll( getRedbackRuntimeConfiguration().getConfigurationProperties().keySet() );