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.ConfigurationEvent;
30 import org.apache.archiva.configuration.ConfigurationListener;
31 import org.apache.archiva.configuration.IndeterminateConfigurationException;
32 import org.apache.archiva.redback.components.cache.Cache;
33 import org.apache.archiva.redback.components.registry.RegistryException;
34 import org.apache.archiva.redback.configuration.UserConfiguration;
35 import org.apache.archiva.redback.configuration.UserConfigurationException;
36 import org.apache.archiva.redback.configuration.UserConfigurationKeys;
37 import org.apache.commons.lang.StringUtils;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.stereotype.Service;
42 import javax.annotation.PostConstruct;
43 import javax.inject.Inject;
44 import javax.inject.Named;
45 import java.util.Collection;
46 import java.util.HashMap;
47 import java.util.HashSet;
48 import java.util.List;
53 * @author Olivier Lamy
56 @Service( "userConfiguration#archiva" )
57 public class DefaultRedbackRuntimeConfigurationAdmin
58 implements RedbackRuntimeConfigurationAdmin, UserConfiguration
61 protected Logger log = LoggerFactory.getLogger( getClass() );
64 private ArchivaConfiguration archivaConfiguration;
67 @Named( value = "userConfiguration#redback" )
68 UserConfiguration userConfiguration;
71 @Named( value = "cache#users" )
72 private Cache usersCache;
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, "jdo" );
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 );
223 catch ( RepositoryAdminException e )
225 throw new UserConfigurationException( e.getMessage(), e );
229 public RedbackRuntimeConfiguration getRedbackRuntimeConfiguration()
231 return build( archivaConfiguration.getConfiguration().getRedbackRuntimeConfiguration() );
234 public void updateRedbackRuntimeConfiguration( RedbackRuntimeConfiguration redbackRuntimeConfiguration )
235 throws RepositoryAdminException
237 org.apache.archiva.configuration.RedbackRuntimeConfiguration runtimeConfiguration =
238 build( redbackRuntimeConfiguration );
239 Configuration configuration = archivaConfiguration.getConfiguration();
240 configuration.setRedbackRuntimeConfiguration( runtimeConfiguration );
243 archivaConfiguration.save( configuration );
245 catch ( RegistryException e )
247 throw new RepositoryAdminException( e.getMessage(), e );
249 catch ( IndeterminateConfigurationException e )
251 throw new RepositoryAdminException( e.getMessage(), e );
255 private RedbackRuntimeConfiguration build(
256 org.apache.archiva.configuration.RedbackRuntimeConfiguration runtimeConfiguration )
258 RedbackRuntimeConfiguration redbackRuntimeConfiguration =
259 new BeanReplicator().replicateBean( runtimeConfiguration, RedbackRuntimeConfiguration.class );
261 if ( runtimeConfiguration.getLdapConfiguration() != null )
263 redbackRuntimeConfiguration.setLdapConfiguration(
264 new BeanReplicator().replicateBean( runtimeConfiguration.getLdapConfiguration(),
265 LdapConfiguration.class ) );
268 if ( runtimeConfiguration.getUsersCacheConfiguration() != null )
270 redbackRuntimeConfiguration.setUsersCacheConfiguration(
271 new BeanReplicator().replicateBean( runtimeConfiguration.getUsersCacheConfiguration(),
272 CacheConfiguration.class ) );
275 if ( redbackRuntimeConfiguration.getLdapConfiguration() == null )
278 redbackRuntimeConfiguration.setLdapConfiguration( new LdapConfiguration() );
281 if ( redbackRuntimeConfiguration.getUsersCacheConfiguration() == null )
283 redbackRuntimeConfiguration.setUsersCacheConfiguration( new CacheConfiguration() );
286 cleanupProperties( redbackRuntimeConfiguration );
288 return redbackRuntimeConfiguration;
292 * cleaning from map properties used directly in archiva configuration fields
294 * @param redbackRuntimeConfiguration
296 private void cleanupProperties( RedbackRuntimeConfiguration redbackRuntimeConfiguration )
298 Map<String, String> properties = redbackRuntimeConfiguration.getConfigurationProperties();
299 properties.remove( UserConfigurationKeys.LDAP_HOSTNAME );
300 properties.remove( UserConfigurationKeys.LDAP_PORT );
301 properties.remove( UserConfigurationKeys.LDAP_BIND_AUTHENTICATOR_ENABLED );
302 properties.remove( UserConfigurationKeys.LDAP_SSL );
303 properties.remove( UserConfigurationKeys.LDAP_BASEDN );
304 properties.remove( UserConfigurationKeys.LDAP_GROUPS_BASEDN );
305 properties.remove( UserConfigurationKeys.LDAP_CONTEX_FACTORY );
306 properties.remove( UserConfigurationKeys.LDAP_BINDDN );
307 properties.remove( UserConfigurationKeys.LDAP_PASSWORD );
308 properties.remove( UserConfigurationKeys.LDAP_AUTHENTICATION_METHOD );
309 properties.remove( UserConfigurationKeys.LDAP_WRITABLE );
310 properties.remove( UserConfigurationKeys.LDAP_GROUPS_USE_ROLENAME );
311 // cleanup groups <-> role mapping
312 /**for ( Map.Entry<String, String> entry : new HashMap<String, String>( properties ).entrySet() )
314 if ( entry.getKey().startsWith( UserConfigurationKeys.LDAP_GROUPS_ROLE_START_KEY ) )
316 properties.remove( entry.getKey() );
321 private org.apache.archiva.configuration.RedbackRuntimeConfiguration build(
322 RedbackRuntimeConfiguration archivaRuntimeConfiguration )
324 org.apache.archiva.configuration.RedbackRuntimeConfiguration redbackRuntimeConfiguration =
325 new BeanReplicator().replicateBean( archivaRuntimeConfiguration,
326 org.apache.archiva.configuration.RedbackRuntimeConfiguration.class );
328 if ( archivaRuntimeConfiguration.getLdapConfiguration() == null )
330 archivaRuntimeConfiguration.setLdapConfiguration( new LdapConfiguration() );
332 redbackRuntimeConfiguration.setLdapConfiguration(
333 new BeanReplicator().replicateBean( archivaRuntimeConfiguration.getLdapConfiguration(),
334 org.apache.archiva.configuration.LdapConfiguration.class ) );
336 if ( archivaRuntimeConfiguration.getUsersCacheConfiguration() == null )
338 archivaRuntimeConfiguration.setUsersCacheConfiguration( new CacheConfiguration() );
341 redbackRuntimeConfiguration.setUsersCacheConfiguration(
342 new BeanReplicator().replicateBean( archivaRuntimeConfiguration.getUsersCacheConfiguration(),
343 org.apache.archiva.configuration.CacheConfiguration.class ) );
345 return redbackRuntimeConfiguration;
348 // wrapper for UserConfiguration to intercept values (and store it not yet migrated
351 public String getString( String key )
353 if ( UserConfigurationKeys.USER_MANAGER_IMPL.equals( key ) )
355 // possible false for others than archiva user manager
356 return getRedbackRuntimeConfiguration().getUserManagerImpls().get( 0 );
359 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
361 if ( conf.getConfigurationProperties().containsKey( key ) )
363 return conf.getConfigurationProperties().get( key );
366 String value = userConfiguration.getString( key );
371 conf.getConfigurationProperties().put( key, value );
375 updateRedbackRuntimeConfiguration( conf );
377 catch ( RepositoryAdminException e )
379 log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
380 throw new RuntimeException( e.getMessage(), e );
386 public String getString( String key, String defaultValue )
388 if ( UserConfigurationKeys.LDAP_HOSTNAME.equals( key ) )
390 return getRedbackRuntimeConfiguration().getLdapConfiguration().getHostName();
392 if ( UserConfigurationKeys.LDAP_CONTEX_FACTORY.equals( key ) )
394 return getRedbackRuntimeConfiguration().getLdapConfiguration().getContextFactory();
396 if ( UserConfigurationKeys.LDAP_PASSWORD.equals( key ) )
398 return getRedbackRuntimeConfiguration().getLdapConfiguration().getPassword();
400 if ( UserConfigurationKeys.LDAP_AUTHENTICATION_METHOD.equals( key ) )
402 return getRedbackRuntimeConfiguration().getLdapConfiguration().getAuthenticationMethod();
405 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
407 if ( conf.getConfigurationProperties().containsKey( key ) )
409 return conf.getConfigurationProperties().get( key );
412 String value = userConfiguration.getString( key, defaultValue );
419 conf.getConfigurationProperties().put( key, value );
422 updateRedbackRuntimeConfiguration( conf );
424 catch ( RepositoryAdminException e )
426 log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
427 throw new RuntimeException( e.getMessage(), e );
433 public int getInt( String key )
435 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
437 if ( conf.getConfigurationProperties().containsKey( key ) )
439 return Integer.valueOf( conf.getConfigurationProperties().get( key ) );
442 int value = userConfiguration.getInt( key );
444 conf.getConfigurationProperties().put( key, Integer.toString( value ) );
447 updateRedbackRuntimeConfiguration( conf );
449 catch ( RepositoryAdminException e )
451 log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
452 throw new RuntimeException( e.getMessage(), e );
458 public int getInt( String key, int defaultValue )
460 if ( UserConfigurationKeys.LDAP_PORT.equals( key ) )
462 return getRedbackRuntimeConfiguration().getLdapConfiguration().getPort();
465 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
467 if ( conf.getConfigurationProperties().containsKey( key ) )
469 return Integer.valueOf( conf.getConfigurationProperties().get( key ) );
472 int value = userConfiguration.getInt( key, defaultValue );
474 conf.getConfigurationProperties().put( key, Integer.toString( value ) );
477 updateRedbackRuntimeConfiguration( conf );
479 catch ( RepositoryAdminException e )
481 log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
482 throw new RuntimeException( e.getMessage(), e );
488 public boolean getBoolean( String key )
490 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
492 if ( UserConfigurationKeys.LDAP_WRITABLE.equals( key ) )
494 return conf.getLdapConfiguration().isWritable();
497 if ( UserConfigurationKeys.LDAP_GROUPS_USE_ROLENAME.equals( key ) )
499 return conf.getLdapConfiguration().isUseRoleNameAsGroup();
502 if ( UserConfigurationKeys.LDAP_BIND_AUTHENTICATOR_ENABLED.equals( key ) )
504 return conf.getLdapConfiguration().isBindAuthenticatorEnabled();
507 if ( conf.getConfigurationProperties().containsKey( key ) )
509 return Boolean.valueOf( conf.getConfigurationProperties().get( key ) );
512 boolean value = userConfiguration.getBoolean( key );
514 conf.getConfigurationProperties().put( key, Boolean.toString( value ) );
517 updateRedbackRuntimeConfiguration( conf );
519 catch ( RepositoryAdminException e )
521 log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
522 throw new RuntimeException( e.getMessage(), e );
528 public boolean getBoolean( String key, boolean defaultValue )
530 if ( UserConfigurationKeys.LDAP_SSL.equals( key ) )
532 return getRedbackRuntimeConfiguration().getLdapConfiguration().isSsl();
535 if ( UserConfigurationKeys.LDAP_WRITABLE.equals( key ) )
537 return getRedbackRuntimeConfiguration().getLdapConfiguration().isWritable();
540 if ( UserConfigurationKeys.LDAP_GROUPS_USE_ROLENAME.equals( key ) )
542 return getRedbackRuntimeConfiguration().getLdapConfiguration().isUseRoleNameAsGroup();
545 if ( UserConfigurationKeys.LDAP_BIND_AUTHENTICATOR_ENABLED.equals( key ) )
547 return getRedbackRuntimeConfiguration().getLdapConfiguration().isBindAuthenticatorEnabled();
550 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
552 if ( conf.getConfigurationProperties().containsKey( key ) )
554 return Boolean.valueOf( conf.getConfigurationProperties().get( key ) );
557 boolean value = userConfiguration.getBoolean( key, defaultValue );
559 conf.getConfigurationProperties().put( key, Boolean.toString( value ) );
562 updateRedbackRuntimeConfiguration( conf );
564 catch ( RepositoryAdminException e )
566 log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
567 throw new RuntimeException( e.getMessage(), e );
573 public List<String> getList( String key )
575 List<String> value = userConfiguration.getList( key );
577 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
578 // TODO concat values
579 conf.getConfigurationProperties().put( key, "" );
582 updateRedbackRuntimeConfiguration( conf );
584 catch ( RepositoryAdminException e )
586 log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
587 throw new RuntimeException( e.getMessage(), e );
593 public String getConcatenatedList( String key, String defaultValue )
595 if ( UserConfigurationKeys.LDAP_BASEDN.equals( key ) )
597 return getRedbackRuntimeConfiguration().getLdapConfiguration().getBaseDn();
599 if ( UserConfigurationKeys.LDAP_BINDDN.equals( key ) )
601 return getRedbackRuntimeConfiguration().getLdapConfiguration().getBindDn();
603 if ( UserConfigurationKeys.LDAP_GROUPS_BASEDN.equals( key ) )
605 return getRedbackRuntimeConfiguration().getLdapConfiguration().getBaseGroupsDn();
607 return userConfiguration.getConcatenatedList( key, defaultValue );
610 public Collection<String> getKeys()
612 Collection<String> keys = userConfiguration.getKeys();
614 Set<String> keysSet = new HashSet<String>( keys );
616 keysSet.addAll( getRedbackRuntimeConfiguration().getConfigurationProperties().keySet() );