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.HashMap;
45 import java.util.HashSet;
46 import java.util.List;
51 * @author Olivier Lamy
54 @Service( "userConfiguration#archiva" )
55 public class DefaultRedbackRuntimeConfigurationAdmin
56 implements RedbackRuntimeConfigurationAdmin, UserConfiguration
59 protected Logger log = LoggerFactory.getLogger( getClass() );
62 private ArchivaConfiguration archivaConfiguration;
65 @Named( value = "userConfiguration#redback" )
66 UserConfiguration userConfiguration;
69 @Named( value = "cache#users" )
70 private Cache usersCache;
72 private RedbackRuntimeConfiguration currentRedbackRuntimeConfiguration;
75 public void initialize()
76 throws UserConfigurationException
80 RedbackRuntimeConfiguration redbackRuntimeConfiguration = getRedbackRuntimeConfiguration();
81 // migrate or not data from redback
82 if ( !redbackRuntimeConfiguration.isMigratedFromRedbackConfiguration() )
84 // not migrated so build a new fresh one
85 redbackRuntimeConfiguration = new RedbackRuntimeConfiguration();
86 // so migrate if available
87 String userManagerImpl =
88 userConfiguration.getConcatenatedList( UserConfigurationKeys.USER_MANAGER_IMPL, "jdo" );
89 if ( StringUtils.isNotEmpty( userManagerImpl ) )
91 String[] impls = StringUtils.split( userManagerImpl, ',' );
92 for ( String impl : impls )
94 redbackRuntimeConfiguration.getUserManagerImpls().add( impl );
98 String rbacManagerImpls =
99 userConfiguration.getConcatenatedList( UserConfigurationKeys.RBAC_MANAGER_IMPL, "cached" );
101 if ( StringUtils.isNotEmpty( rbacManagerImpls ) )
103 String[] impls = StringUtils.split( rbacManagerImpls, ',' );
104 for ( String impl : impls )
106 redbackRuntimeConfiguration.getRbacManagerImpls().add( impl );
112 LdapConfiguration ldapConfiguration = redbackRuntimeConfiguration.getLdapConfiguration();
113 if ( ldapConfiguration == null )
115 ldapConfiguration = new LdapConfiguration();
116 redbackRuntimeConfiguration.setLdapConfiguration( ldapConfiguration );
119 ldapConfiguration.setHostName(
120 userConfiguration.getString( UserConfigurationKeys.LDAP_HOSTNAME, null ) );
121 ldapConfiguration.setPort( userConfiguration.getInt( UserConfigurationKeys.LDAP_PORT, -1 ) );
122 ldapConfiguration.setSsl( userConfiguration.getBoolean( UserConfigurationKeys.LDAP_SSL, false ) );
123 ldapConfiguration.setBaseDn(
124 userConfiguration.getConcatenatedList( UserConfigurationKeys.LDAP_BASEDN, null ) );
126 ldapConfiguration.setBaseGroupsDn(
127 userConfiguration.getConcatenatedList( UserConfigurationKeys.LDAP_GROUPS_BASEDN,
128 ldapConfiguration.getBaseDn() ) );
130 ldapConfiguration.setContextFactory(
131 userConfiguration.getString( UserConfigurationKeys.LDAP_CONTEX_FACTORY, null ) );
132 ldapConfiguration.setBindDn(
133 userConfiguration.getConcatenatedList( UserConfigurationKeys.LDAP_BINDDN, null ) );
134 ldapConfiguration.setPassword(
135 userConfiguration.getString( UserConfigurationKeys.LDAP_PASSWORD, null ) );
136 ldapConfiguration.setAuthenticationMethod(
137 userConfiguration.getString( UserConfigurationKeys.LDAP_AUTHENTICATION_METHOD, null ) );
139 ldapConfiguration.setWritable(
140 userConfiguration.getBoolean( UserConfigurationKeys.LDAP_WRITABLE, false ) );
142 ldapConfiguration.setUseRoleNameAsGroup(
143 userConfiguration.getBoolean( UserConfigurationKeys.LDAP_GROUPS_USE_ROLENAME, false ) );
145 ldapConfiguration.setBindAuthenticatorEnabled(
146 userConfiguration.getBoolean( UserConfigurationKeys.LDAP_BIND_AUTHENTICATOR_ENABLED, false ) );
148 redbackRuntimeConfiguration.setMigratedFromRedbackConfiguration( true );
150 updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration );
153 // we must ensure userManagerImpls list is not empty if so put at least jdo one !
154 if ( redbackRuntimeConfiguration.getUserManagerImpls().isEmpty() )
157 "redbackRuntimeConfiguration with empty userManagerImpls so force at least jdo implementation !" );
158 redbackRuntimeConfiguration.getUserManagerImpls().add( "jdo" );
159 updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration );
162 // we ensure authorizerImpls is not empty if so put
163 if ( redbackRuntimeConfiguration.getRbacManagerImpls().isEmpty() )
166 "redbackRuntimeConfiguration with empty rbacManagerImpls so force at least cached implementation !" );
167 redbackRuntimeConfiguration.getRbacManagerImpls().add( "cached" );
168 updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration );
171 boolean save = false;
174 if ( redbackRuntimeConfiguration.getUsersCacheConfiguration() == null )
176 redbackRuntimeConfiguration.setUsersCacheConfiguration( new CacheConfiguration() );
178 // if -1 it means non initialized to take values from the spring bean
179 if ( redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToIdleSeconds() < 0 )
181 redbackRuntimeConfiguration.getUsersCacheConfiguration().setTimeToIdleSeconds(
182 usersCache.getTimeToIdleSeconds() );
186 usersCache.setTimeToIdleSeconds(
187 redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToIdleSeconds() );
189 if ( redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToLiveSeconds() < 0 )
191 redbackRuntimeConfiguration.getUsersCacheConfiguration().setTimeToLiveSeconds(
192 usersCache.getTimeToLiveSeconds() );
196 usersCache.setTimeToLiveSeconds(
197 redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToLiveSeconds() );
199 if ( redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsInMemory() < 0 )
201 redbackRuntimeConfiguration.getUsersCacheConfiguration().setMaxElementsInMemory(
202 usersCache.getMaxElementsInMemory() );
205 usersCache.setMaxElementsInMemory(
206 redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsInMemory() );
208 if ( redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsOnDisk() < 0 )
210 redbackRuntimeConfiguration.getUsersCacheConfiguration().setMaxElementsOnDisk(
211 usersCache.getMaxElementsOnDisk() );
214 usersCache.setMaxElementsOnDisk(
215 redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsOnDisk() );
219 updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration );
222 this.currentRedbackRuntimeConfiguration = redbackRuntimeConfiguration;
225 catch ( RepositoryAdminException e )
227 throw new UserConfigurationException( e.getMessage(), e );
231 public RedbackRuntimeConfiguration getRedbackRuntimeConfiguration()
233 return build( archivaConfiguration.getConfiguration().getRedbackRuntimeConfiguration() );
236 public void updateRedbackRuntimeConfiguration( RedbackRuntimeConfiguration redbackRuntimeConfiguration )
237 throws RepositoryAdminException
239 org.apache.archiva.configuration.RedbackRuntimeConfiguration runtimeConfiguration =
240 build( redbackRuntimeConfiguration );
241 Configuration configuration = archivaConfiguration.getConfiguration();
242 configuration.setRedbackRuntimeConfiguration( runtimeConfiguration );
245 archivaConfiguration.save( configuration );
246 this.currentRedbackRuntimeConfiguration = redbackRuntimeConfiguration;
248 catch ( RegistryException e )
250 throw new RepositoryAdminException( e.getMessage(), e );
252 catch ( IndeterminateConfigurationException e )
254 throw new RepositoryAdminException( e.getMessage(), e );
258 private RedbackRuntimeConfiguration build(
259 org.apache.archiva.configuration.RedbackRuntimeConfiguration runtimeConfiguration )
261 if ( this.currentRedbackRuntimeConfiguration != null )
263 return this.currentRedbackRuntimeConfiguration;
265 RedbackRuntimeConfiguration redbackRuntimeConfiguration =
266 new BeanReplicator().replicateBean( runtimeConfiguration, RedbackRuntimeConfiguration.class );
268 if ( runtimeConfiguration.getLdapConfiguration() != null )
270 redbackRuntimeConfiguration.setLdapConfiguration(
271 new BeanReplicator().replicateBean( runtimeConfiguration.getLdapConfiguration(),
272 LdapConfiguration.class ) );
275 if ( runtimeConfiguration.getUsersCacheConfiguration() != null )
277 redbackRuntimeConfiguration.setUsersCacheConfiguration(
278 new BeanReplicator().replicateBean( runtimeConfiguration.getUsersCacheConfiguration(),
279 CacheConfiguration.class ) );
282 if ( redbackRuntimeConfiguration.getLdapConfiguration() == null )
285 redbackRuntimeConfiguration.setLdapConfiguration( new LdapConfiguration() );
288 if ( redbackRuntimeConfiguration.getUsersCacheConfiguration() == null )
290 redbackRuntimeConfiguration.setUsersCacheConfiguration( new CacheConfiguration() );
293 cleanupProperties( redbackRuntimeConfiguration );
295 this.currentRedbackRuntimeConfiguration = redbackRuntimeConfiguration;
297 return redbackRuntimeConfiguration;
301 * cleaning from map properties used directly in archiva configuration fields
303 * @param redbackRuntimeConfiguration
305 private void cleanupProperties( RedbackRuntimeConfiguration redbackRuntimeConfiguration )
307 Map<String, String> properties = redbackRuntimeConfiguration.getConfigurationProperties();
308 properties.remove( UserConfigurationKeys.LDAP_HOSTNAME );
309 properties.remove( UserConfigurationKeys.LDAP_PORT );
310 properties.remove( UserConfigurationKeys.LDAP_BIND_AUTHENTICATOR_ENABLED );
311 properties.remove( UserConfigurationKeys.LDAP_SSL );
312 properties.remove( UserConfigurationKeys.LDAP_BASEDN );
313 properties.remove( UserConfigurationKeys.LDAP_GROUPS_BASEDN );
314 properties.remove( UserConfigurationKeys.LDAP_CONTEX_FACTORY );
315 properties.remove( UserConfigurationKeys.LDAP_BINDDN );
316 properties.remove( UserConfigurationKeys.LDAP_PASSWORD );
317 properties.remove( UserConfigurationKeys.LDAP_AUTHENTICATION_METHOD );
318 properties.remove( UserConfigurationKeys.LDAP_WRITABLE );
319 properties.remove( UserConfigurationKeys.LDAP_GROUPS_USE_ROLENAME );
320 // cleanup groups <-> role mapping
321 /**for ( Map.Entry<String, String> entry : new HashMap<String, String>( properties ).entrySet() )
323 if ( entry.getKey().startsWith( UserConfigurationKeys.LDAP_GROUPS_ROLE_START_KEY ) )
325 properties.remove( entry.getKey() );
330 private org.apache.archiva.configuration.RedbackRuntimeConfiguration build(
331 RedbackRuntimeConfiguration archivaRuntimeConfiguration )
333 org.apache.archiva.configuration.RedbackRuntimeConfiguration redbackRuntimeConfiguration =
334 new BeanReplicator().replicateBean( archivaRuntimeConfiguration,
335 org.apache.archiva.configuration.RedbackRuntimeConfiguration.class );
337 if ( archivaRuntimeConfiguration.getLdapConfiguration() == null )
339 archivaRuntimeConfiguration.setLdapConfiguration( new LdapConfiguration() );
341 redbackRuntimeConfiguration.setLdapConfiguration(
342 new BeanReplicator().replicateBean( archivaRuntimeConfiguration.getLdapConfiguration(),
343 org.apache.archiva.configuration.LdapConfiguration.class ) );
345 if ( archivaRuntimeConfiguration.getUsersCacheConfiguration() == null )
347 archivaRuntimeConfiguration.setUsersCacheConfiguration( new CacheConfiguration() );
350 redbackRuntimeConfiguration.setUsersCacheConfiguration(
351 new BeanReplicator().replicateBean( archivaRuntimeConfiguration.getUsersCacheConfiguration(),
352 org.apache.archiva.configuration.CacheConfiguration.class ) );
354 return redbackRuntimeConfiguration;
357 // wrapper for UserConfiguration to intercept values (and store it not yet migrated
360 public String getString( String key )
362 if ( UserConfigurationKeys.USER_MANAGER_IMPL.equals( key ) )
364 // possible false for others than archiva user manager
365 return getRedbackRuntimeConfiguration().getUserManagerImpls().get( 0 );
368 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
370 if ( conf.getConfigurationProperties().containsKey( key ) )
372 return conf.getConfigurationProperties().get( key );
375 String value = userConfiguration.getString( key );
380 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 String getString( String key, String defaultValue )
397 if ( UserConfigurationKeys.LDAP_HOSTNAME.equals( key ) )
399 return getRedbackRuntimeConfiguration().getLdapConfiguration().getHostName();
401 if ( UserConfigurationKeys.LDAP_CONTEX_FACTORY.equals( key ) )
403 return getRedbackRuntimeConfiguration().getLdapConfiguration().getContextFactory();
405 if ( UserConfigurationKeys.LDAP_PASSWORD.equals( key ) )
407 return getRedbackRuntimeConfiguration().getLdapConfiguration().getPassword();
409 if ( UserConfigurationKeys.LDAP_AUTHENTICATION_METHOD.equals( key ) )
411 return getRedbackRuntimeConfiguration().getLdapConfiguration().getAuthenticationMethod();
414 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
416 if ( conf.getConfigurationProperties().containsKey( key ) )
418 return conf.getConfigurationProperties().get( key );
421 String value = userConfiguration.getString( key, defaultValue );
428 conf.getConfigurationProperties().put( key, value );
431 updateRedbackRuntimeConfiguration( conf );
433 catch ( RepositoryAdminException e )
435 log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
436 throw new RuntimeException( e.getMessage(), e );
442 public int getInt( String key )
444 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
446 if ( conf.getConfigurationProperties().containsKey( key ) )
448 return Integer.valueOf( conf.getConfigurationProperties().get( key ) );
451 int value = userConfiguration.getInt( key );
453 conf.getConfigurationProperties().put( key, Integer.toString( value ) );
456 updateRedbackRuntimeConfiguration( conf );
458 catch ( RepositoryAdminException e )
460 log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
461 throw new RuntimeException( e.getMessage(), e );
467 public int getInt( String key, int defaultValue )
469 if ( UserConfigurationKeys.LDAP_PORT.equals( key ) )
471 return getRedbackRuntimeConfiguration().getLdapConfiguration().getPort();
474 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
476 if ( conf.getConfigurationProperties().containsKey( key ) )
478 return Integer.valueOf( conf.getConfigurationProperties().get( key ) );
481 int value = userConfiguration.getInt( key, defaultValue );
483 conf.getConfigurationProperties().put( key, Integer.toString( value ) );
486 updateRedbackRuntimeConfiguration( conf );
488 catch ( RepositoryAdminException e )
490 log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
491 throw new RuntimeException( e.getMessage(), e );
497 public boolean getBoolean( String key )
499 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
501 if ( UserConfigurationKeys.LDAP_WRITABLE.equals( key ) )
503 return conf.getLdapConfiguration().isWritable();
506 if ( UserConfigurationKeys.LDAP_GROUPS_USE_ROLENAME.equals( key ) )
508 return conf.getLdapConfiguration().isUseRoleNameAsGroup();
511 if ( UserConfigurationKeys.LDAP_BIND_AUTHENTICATOR_ENABLED.equals( key ) )
513 return conf.getLdapConfiguration().isBindAuthenticatorEnabled();
516 if ( conf.getConfigurationProperties().containsKey( key ) )
518 return Boolean.valueOf( conf.getConfigurationProperties().get( key ) );
521 boolean value = userConfiguration.getBoolean( key );
523 conf.getConfigurationProperties().put( key, Boolean.toString( value ) );
526 updateRedbackRuntimeConfiguration( conf );
528 catch ( RepositoryAdminException e )
530 log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
531 throw new RuntimeException( e.getMessage(), e );
537 public boolean getBoolean( String key, boolean defaultValue )
539 if ( UserConfigurationKeys.LDAP_SSL.equals( key ) )
541 return getRedbackRuntimeConfiguration().getLdapConfiguration().isSsl();
544 if ( UserConfigurationKeys.LDAP_WRITABLE.equals( key ) )
546 return getRedbackRuntimeConfiguration().getLdapConfiguration().isWritable();
549 if ( UserConfigurationKeys.LDAP_GROUPS_USE_ROLENAME.equals( key ) )
551 return getRedbackRuntimeConfiguration().getLdapConfiguration().isUseRoleNameAsGroup();
554 if ( UserConfigurationKeys.LDAP_BIND_AUTHENTICATOR_ENABLED.equals( key ) )
556 return getRedbackRuntimeConfiguration().getLdapConfiguration().isBindAuthenticatorEnabled();
559 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
561 if ( conf.getConfigurationProperties().containsKey( key ) )
563 return Boolean.valueOf( conf.getConfigurationProperties().get( key ) );
566 boolean value = userConfiguration.getBoolean( key, defaultValue );
568 conf.getConfigurationProperties().put( key, Boolean.toString( value ) );
571 updateRedbackRuntimeConfiguration( conf );
573 catch ( RepositoryAdminException e )
575 log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
576 throw new RuntimeException( e.getMessage(), e );
582 public List<String> getList( String key )
584 List<String> value = userConfiguration.getList( key );
586 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
587 // TODO concat values
588 conf.getConfigurationProperties().put( key, "" );
591 updateRedbackRuntimeConfiguration( conf );
593 catch ( RepositoryAdminException e )
595 log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
596 throw new RuntimeException( e.getMessage(), e );
602 public String getConcatenatedList( String key, String defaultValue )
604 if ( UserConfigurationKeys.LDAP_BASEDN.equals( key ) )
606 return getRedbackRuntimeConfiguration().getLdapConfiguration().getBaseDn();
608 if ( UserConfigurationKeys.LDAP_BINDDN.equals( key ) )
610 return getRedbackRuntimeConfiguration().getLdapConfiguration().getBindDn();
612 if ( UserConfigurationKeys.LDAP_GROUPS_BASEDN.equals( key ) )
614 return getRedbackRuntimeConfiguration().getLdapConfiguration().getBaseGroupsDn();
616 return userConfiguration.getConcatenatedList( key, defaultValue );
619 public Collection<String> getKeys()
621 Collection<String> keys = userConfiguration.getKeys();
623 Set<String> keysSet = new HashSet<String>( keys );
625 keysSet.addAll( getRedbackRuntimeConfiguration().getConfigurationProperties().keySet() );