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 if ( StringUtils.contains( userManagerImpl, ',' ) )
83 String[] impls = StringUtils.split( userManagerImpl, ',' );
84 for ( String impl : impls )
86 redbackRuntimeConfiguration.getUserManagerImpls().add( impl );
91 redbackRuntimeConfiguration.getUserManagerImpls().add( userManagerImpl );
97 LdapConfiguration ldapConfiguration = redbackRuntimeConfiguration.getLdapConfiguration();
98 if ( ldapConfiguration == null )
100 ldapConfiguration = new LdapConfiguration();
101 redbackRuntimeConfiguration.setLdapConfiguration( ldapConfiguration );
104 ldapConfiguration.setHostName(
105 userConfiguration.getString( UserConfigurationKeys.LDAP_HOSTNAME, null ) );
106 ldapConfiguration.setPort( userConfiguration.getInt( UserConfigurationKeys.LDAP_PORT, -1 ) );
107 ldapConfiguration.setSsl( userConfiguration.getBoolean( UserConfigurationKeys.LDAP_SSL, false ) );
108 ldapConfiguration.setBaseDn(
109 userConfiguration.getConcatenatedList( UserConfigurationKeys.LDAP_BASEDN, null ) );
110 ldapConfiguration.setContextFactory(
111 userConfiguration.getString( UserConfigurationKeys.LDAP_CONTEX_FACTORY, null ) );
112 ldapConfiguration.setBindDn(
113 userConfiguration.getConcatenatedList( UserConfigurationKeys.LDAP_BINDDN, null ) );
114 ldapConfiguration.setPassword(
115 userConfiguration.getString( UserConfigurationKeys.LDAP_PASSWORD, null ) );
116 ldapConfiguration.setAuthenticationMethod(
117 userConfiguration.getString( UserConfigurationKeys.LDAP_AUTHENTICATION_METHOD, null ) );
119 redbackRuntimeConfiguration.setMigratedFromRedbackConfiguration( true );
121 updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration );
124 // we must ensure userManagerImpls list is not empty if so put at least jdo one !
125 if ( redbackRuntimeConfiguration.getUserManagerImpls().isEmpty() )
128 "redbackRuntimeConfiguration with empty userManagerImpls so force at least jdo implementation !" );
129 redbackRuntimeConfiguration.getUserManagerImpls().add( "jdo" );
130 updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration );
133 boolean save = false;
136 if ( redbackRuntimeConfiguration.getUsersCacheConfiguration() == null )
138 redbackRuntimeConfiguration.setUsersCacheConfiguration( new CacheConfiguration() );
140 // if -1 it means non initialized to take values from the spring bean
141 if ( redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToIdleSeconds() < 0 )
143 redbackRuntimeConfiguration.getUsersCacheConfiguration().setTimeToIdleSeconds(
144 usersCache.getTimeToIdleSeconds() );
148 usersCache.setTimeToIdleSeconds(
149 redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToIdleSeconds() );
151 if ( redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToLiveSeconds() < 0 )
153 redbackRuntimeConfiguration.getUsersCacheConfiguration().setTimeToLiveSeconds(
154 usersCache.getTimeToLiveSeconds() );
158 usersCache.setTimeToLiveSeconds(
159 redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToLiveSeconds() );
161 if ( redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsInMemory() < 0 )
163 redbackRuntimeConfiguration.getUsersCacheConfiguration().setMaxElementsInMemory(
164 usersCache.getMaxElementsInMemory() );
167 usersCache.setMaxElementsInMemory(
168 redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsInMemory() );
170 if ( redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsOnDisk() < 0 )
172 redbackRuntimeConfiguration.getUsersCacheConfiguration().setMaxElementsOnDisk(
173 usersCache.getMaxElementsOnDisk() );
176 usersCache.setMaxElementsOnDisk(
177 redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsOnDisk() );
181 updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration );
185 catch ( RepositoryAdminException e )
187 throw new UserConfigurationException( e.getMessage(), e );
191 public RedbackRuntimeConfiguration getRedbackRuntimeConfiguration()
193 return build( archivaConfiguration.getConfiguration().getRedbackRuntimeConfiguration() );
196 public void updateRedbackRuntimeConfiguration( RedbackRuntimeConfiguration redbackRuntimeConfiguration )
197 throws RepositoryAdminException
199 org.apache.archiva.configuration.RedbackRuntimeConfiguration runtimeConfiguration =
200 build( redbackRuntimeConfiguration );
201 Configuration configuration = archivaConfiguration.getConfiguration();
202 configuration.setRedbackRuntimeConfiguration( runtimeConfiguration );
205 archivaConfiguration.save( configuration );
207 catch ( RegistryException e )
209 throw new RepositoryAdminException( e.getMessage(), e );
211 catch ( IndeterminateConfigurationException e )
213 throw new RepositoryAdminException( e.getMessage(), e );
217 private RedbackRuntimeConfiguration build(
218 org.apache.archiva.configuration.RedbackRuntimeConfiguration runtimeConfiguration )
220 RedbackRuntimeConfiguration redbackRuntimeConfiguration =
221 new BeanReplicator().replicateBean( runtimeConfiguration, RedbackRuntimeConfiguration.class );
223 if ( runtimeConfiguration.getLdapConfiguration() != null )
225 redbackRuntimeConfiguration.setLdapConfiguration(
226 new BeanReplicator().replicateBean( runtimeConfiguration.getLdapConfiguration(),
227 LdapConfiguration.class ) );
230 if ( runtimeConfiguration.getUsersCacheConfiguration() != null )
232 redbackRuntimeConfiguration.setUsersCacheConfiguration(
233 new BeanReplicator().replicateBean( runtimeConfiguration.getUsersCacheConfiguration(),
234 CacheConfiguration.class ) );
237 if ( redbackRuntimeConfiguration.getLdapConfiguration() == null )
240 redbackRuntimeConfiguration.setLdapConfiguration( new LdapConfiguration() );
243 if ( redbackRuntimeConfiguration.getUsersCacheConfiguration() == null )
245 redbackRuntimeConfiguration.setUsersCacheConfiguration( new CacheConfiguration() );
248 return redbackRuntimeConfiguration;
251 private org.apache.archiva.configuration.RedbackRuntimeConfiguration build(
252 RedbackRuntimeConfiguration archivaRuntimeConfiguration )
254 org.apache.archiva.configuration.RedbackRuntimeConfiguration redbackRuntimeConfiguration =
255 new BeanReplicator().replicateBean( archivaRuntimeConfiguration,
256 org.apache.archiva.configuration.RedbackRuntimeConfiguration.class );
258 if ( archivaRuntimeConfiguration.getLdapConfiguration() == null )
260 archivaRuntimeConfiguration.setLdapConfiguration( new LdapConfiguration() );
262 redbackRuntimeConfiguration.setLdapConfiguration(
263 new BeanReplicator().replicateBean( archivaRuntimeConfiguration.getLdapConfiguration(),
264 org.apache.archiva.configuration.LdapConfiguration.class ) );
266 if ( archivaRuntimeConfiguration.getUsersCacheConfiguration() == null )
268 archivaRuntimeConfiguration.setUsersCacheConfiguration( new CacheConfiguration() );
271 redbackRuntimeConfiguration.setUsersCacheConfiguration(
272 new BeanReplicator().replicateBean( archivaRuntimeConfiguration.getUsersCacheConfiguration(),
273 org.apache.archiva.configuration.CacheConfiguration.class ) );
275 return redbackRuntimeConfiguration;
278 // wrapper for UserConfiguration to intercept values (and store it not yet migrated
281 public String getString( String key )
283 if ( UserConfigurationKeys.USER_MANAGER_IMPL.equals( key ) )
285 // possible false for others than archiva user manager
286 return getRedbackRuntimeConfiguration().getUserManagerImpls().get( 0 );
289 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
291 if ( conf.getConfigurationProperties().containsKey( key ) )
293 return conf.getConfigurationProperties().get( key );
296 String value = userConfiguration.getString( key );
301 conf.getConfigurationProperties().put( key, value );
305 updateRedbackRuntimeConfiguration( conf );
307 catch ( RepositoryAdminException e )
309 log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
310 throw new RuntimeException( e.getMessage(), e );
316 public String getString( String key, String defaultValue )
318 if ( UserConfigurationKeys.LDAP_HOSTNAME.equals( key ) )
320 return getRedbackRuntimeConfiguration().getLdapConfiguration().getHostName();
322 if ( UserConfigurationKeys.LDAP_CONTEX_FACTORY.equals( key ) )
324 return getRedbackRuntimeConfiguration().getLdapConfiguration().getContextFactory();
326 if ( UserConfigurationKeys.LDAP_PASSWORD.equals( key ) )
328 return getRedbackRuntimeConfiguration().getLdapConfiguration().getPassword();
330 if ( UserConfigurationKeys.LDAP_AUTHENTICATION_METHOD.equals( key ) )
332 return getRedbackRuntimeConfiguration().getLdapConfiguration().getAuthenticationMethod();
335 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
337 if ( conf.getConfigurationProperties().containsKey( key ) )
339 return conf.getConfigurationProperties().get( key );
342 String value = userConfiguration.getString( key, defaultValue );
349 conf.getConfigurationProperties().put( key, value );
352 updateRedbackRuntimeConfiguration( conf );
354 catch ( RepositoryAdminException e )
356 log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
357 throw new RuntimeException( e.getMessage(), e );
363 public int getInt( String key )
365 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
367 if ( conf.getConfigurationProperties().containsKey( key ) )
369 return Integer.valueOf( conf.getConfigurationProperties().get( key ) );
372 int value = userConfiguration.getInt( key );
374 conf.getConfigurationProperties().put( key, Integer.toString( value ) );
377 updateRedbackRuntimeConfiguration( conf );
379 catch ( RepositoryAdminException e )
381 log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
382 throw new RuntimeException( e.getMessage(), e );
388 public int getInt( String key, int defaultValue )
390 if ( UserConfigurationKeys.LDAP_PORT.equals( key ) )
392 return getRedbackRuntimeConfiguration().getLdapConfiguration().getPort();
395 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
397 if ( conf.getConfigurationProperties().containsKey( key ) )
399 return Integer.valueOf( conf.getConfigurationProperties().get( key ) );
402 int value = userConfiguration.getInt( key, defaultValue );
404 conf.getConfigurationProperties().put( key, Integer.toString( value ) );
407 updateRedbackRuntimeConfiguration( conf );
409 catch ( RepositoryAdminException e )
411 log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
412 throw new RuntimeException( e.getMessage(), e );
418 public boolean getBoolean( String key )
420 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
422 if ( conf.getConfigurationProperties().containsKey( key ) )
424 return Boolean.valueOf( conf.getConfigurationProperties().get( key ) );
427 boolean value = userConfiguration.getBoolean( key );
429 conf.getConfigurationProperties().put( key, Boolean.toString( value ) );
432 updateRedbackRuntimeConfiguration( conf );
434 catch ( RepositoryAdminException e )
436 log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
437 throw new RuntimeException( e.getMessage(), e );
443 public boolean getBoolean( String key, boolean defaultValue )
445 if ( UserConfigurationKeys.LDAP_SSL.equals( key ) )
447 return getRedbackRuntimeConfiguration().getLdapConfiguration().isSsl();
450 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
452 if ( conf.getConfigurationProperties().containsKey( key ) )
454 return Boolean.valueOf( conf.getConfigurationProperties().get( key ) );
457 boolean value = userConfiguration.getBoolean( key, defaultValue );
459 conf.getConfigurationProperties().put( key, Boolean.toString( value ) );
462 updateRedbackRuntimeConfiguration( conf );
464 catch ( RepositoryAdminException e )
466 log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
467 throw new RuntimeException( e.getMessage(), e );
473 public List<String> getList( String key )
475 List<String> value = userConfiguration.getList( key );
477 RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
478 // TODO concat values
479 conf.getConfigurationProperties().put( key, "" );
482 updateRedbackRuntimeConfiguration( conf );
484 catch ( RepositoryAdminException e )
486 log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
487 throw new RuntimeException( e.getMessage(), e );
493 public String getConcatenatedList( String key, String defaultValue )
495 if ( UserConfigurationKeys.LDAP_BASEDN.equals( key ) )
497 return getRedbackRuntimeConfiguration().getLdapConfiguration().getBaseDn();
499 if ( UserConfigurationKeys.LDAP_BINDDN.equals( key ) )
501 return getRedbackRuntimeConfiguration().getLdapConfiguration().getBindDn();
503 return userConfiguration.getConcatenatedList( key, defaultValue );