]> source.dussan.org Git - archiva.git/blob
d15990189db858aacf141c950ea192bc0e16eb35
[archiva.git] /
1 package org.apache.archiva.admin.repository.runtime;
2 /*
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
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
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
18  * under the License.
19  */
20
21 import org.apache.archiva.admin.model.RepositoryAdminException;
22 import org.apache.archiva.admin.model.beans.CacheConfiguration;
23 import org.apache.archiva.admin.model.beans.LdapConfiguration;
24 import org.apache.archiva.admin.model.beans.LdapGroupMapping;
25 import org.apache.archiva.admin.model.beans.RedbackRuntimeConfiguration;
26 import org.apache.archiva.admin.model.runtime.RedbackRuntimeConfigurationAdmin;
27 import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
28 import org.apache.archiva.configuration.ArchivaConfiguration;
29 import org.apache.archiva.configuration.Configuration;
30 import org.apache.archiva.configuration.IndeterminateConfigurationException;
31 import org.apache.archiva.components.cache.Cache;
32 import org.apache.archiva.components.registry.RegistryException;
33 import org.apache.archiva.configuration.util.ConfigMapper;
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.archiva.redback.users.User;
38 import org.apache.commons.lang3.StringUtils;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.stereotype.Service;
42
43 import javax.annotation.PostConstruct;
44 import javax.inject.Inject;
45 import javax.inject.Named;
46 import java.util.ArrayList;
47 import java.util.Arrays;
48 import java.util.Collection;
49 import java.util.HashMap;
50 import java.util.HashSet;
51 import java.util.List;
52 import java.util.Map;
53 import java.util.Set;
54 import java.util.function.Function;
55
56 import static org.apache.archiva.redback.configuration.UserConfigurationKeys.*;
57
58 /**
59  * @author Olivier Lamy
60  * @since 1.4-M4
61  */
62 @Service( "redbackRuntimeConfigurationAdmin#default" )
63 public class DefaultRedbackRuntimeConfigurationAdmin
64     extends AbstractRepositoryAdmin
65     implements RedbackRuntimeConfigurationAdmin, UserConfiguration
66 {
67
68     protected Logger log = LoggerFactory.getLogger( getClass() );
69
70     private ArchivaConfiguration archivaConfiguration;
71
72     private UserConfiguration userConfiguration;
73
74     private static final Map<String, List<String>> LDAP_PROPERTIES = new HashMap<>( );
75
76     private static final ConfigMapper<LdapConfiguration, List<LdapGroupMapping>> LDAP_MAPPER = new ConfigMapper( );
77
78     static {
79         LDAP_MAPPER.addStringMapping( LDAP_HOSTNAME, LdapConfiguration::getHostName );
80         LDAP_MAPPER.addStringMapping( LDAP_CONTEX_FACTORY, LdapConfiguration::getContextFactory );
81         LDAP_MAPPER.addStringMapping( LDAP_PASSWORD, LdapConfiguration::getPassword );
82         LDAP_MAPPER.addStringMapping( LDAP_AUTHENTICATION_METHOD, LdapConfiguration::getAuthenticationMethod );
83         LDAP_MAPPER.addStringMapping( LDAP_BASEDN, LdapConfiguration::getBaseDn );
84         LDAP_MAPPER.addStringMapping( LDAP_GROUPS_BASEDN, LdapConfiguration::getBaseGroupsDn );
85         LDAP_MAPPER.addStringMapping( LDAP_BINDDN, LdapConfiguration::getBindDn );
86         LDAP_MAPPER.addPrefixStringMapping( LDAP_GROUPS_ROLE_START_KEY, (String attributeName, List<LdapGroupMapping> mapping)-> {
87             int index = mapping.indexOf( new LdapGroupMapping(
88                 StringUtils.substringAfter( attributeName, LDAP_GROUPS_ROLE_START_KEY ) ) );
89             if ( index > -1 )
90             {
91                 return StringUtils.join( mapping.get( index ).getRoleNames(),
92                     ',' );
93             } else {
94                 return "";
95             }
96         } );
97         LDAP_MAPPER.addIntMapping( LDAP_PORT, LdapConfiguration::getPort );
98         LDAP_MAPPER.addBooleanMapping( LDAP_SSL, LdapConfiguration::isSsl );
99         LDAP_MAPPER.addBooleanMapping( LDAP_WRITABLE, LdapConfiguration::isWritable );
100         LDAP_MAPPER.addBooleanMapping( LDAP_GROUPS_USE_ROLENAME, LdapConfiguration::isUseRoleNameAsGroup );
101         LDAP_MAPPER.addBooleanMapping( LDAP_BIND_AUTHENTICATOR_ENABLED, LdapConfiguration::isBindAuthenticatorEnabled );
102     }
103
104     private Cache<String, User> usersCache;
105
106     @Inject
107     public DefaultRedbackRuntimeConfigurationAdmin( ArchivaConfiguration archivaConfiguration,//
108                                                     @Named( value = "userConfiguration#redback" ) //
109                                                         UserConfiguration userConfiguration,
110                                                     @Named( value = "cache#users" ) Cache<String, User> usersCache )
111     {
112         this.archivaConfiguration = archivaConfiguration;
113         this.userConfiguration = userConfiguration;
114         this.usersCache = usersCache;
115     }
116
117     @PostConstruct
118     @Override
119     public void initialize()
120         throws UserConfigurationException
121     {
122         try
123         {
124             RedbackRuntimeConfiguration redbackRuntimeConfiguration = getRedbackRuntimeConfiguration();
125             Function<RedbackRuntimeConfiguration, LdapConfiguration> kk = RedbackRuntimeConfiguration::getLdapConfiguration;
126             // migrate or not data from redback
127             if ( !redbackRuntimeConfiguration.isMigratedFromRedbackConfiguration() )
128             {
129                 // not migrated so build a new fresh one
130                 redbackRuntimeConfiguration = new RedbackRuntimeConfiguration();
131                 // so migrate if available
132                 String userManagerImpl =
133                     userConfiguration.getConcatenatedList( USER_MANAGER_IMPL, //
134                                                            DEFAULT_USER_MANAGER_IMPL );
135                 if ( StringUtils.isNotEmpty( userManagerImpl ) )
136                 {
137                     String[] impls = StringUtils.split( userManagerImpl, ',' );
138                     for ( String impl : impls )
139                     {
140                         if (StringUtils.equalsIgnoreCase( "jdo", impl ))
141                         {
142                             impl = DEFAULT_USER_MANAGER_IMPL;
143                         }
144                         redbackRuntimeConfiguration.getUserManagerImpls().add( impl );
145                     }
146                 }
147                 else
148                 {
149                     redbackRuntimeConfiguration.getUserManagerImpls().add( DEFAULT_USER_MANAGER_IMPL );
150                 }
151
152                 String rbacManagerImpls =
153                     userConfiguration.getConcatenatedList( RBAC_MANAGER_IMPL, //
154                                                            DEFAULT_RBAC_MANAGER_IMPL );
155
156                 if ( StringUtils.isNotEmpty( rbacManagerImpls ) )
157                 {
158                     String[] impls = StringUtils.split( rbacManagerImpls, ',' );
159                     for ( String impl : impls )
160                     {
161                         if (StringUtils.equalsIgnoreCase( "jdo", impl ))
162                         {
163                             impl = DEFAULT_RBAC_MANAGER_IMPL;
164                         }
165                         redbackRuntimeConfiguration.getRbacManagerImpls().add( impl );
166                     }
167                 }
168                 else
169                 {
170                     redbackRuntimeConfiguration.getRbacManagerImpls().add( DEFAULT_RBAC_MANAGER_IMPL );
171                 }
172
173                 // now ldap
174
175                 LdapConfiguration ldapConfiguration = redbackRuntimeConfiguration.getLdapConfiguration();
176                 if ( ldapConfiguration == null )
177                 {
178                     ldapConfiguration = new LdapConfiguration();
179                     redbackRuntimeConfiguration.setLdapConfiguration( ldapConfiguration );
180                 }
181
182                 ldapConfiguration.setHostName(
183                     userConfiguration.getString( LDAP_HOSTNAME, null ) );
184                 ldapConfiguration.setPort( userConfiguration.getInt( LDAP_PORT, -1 ) );
185                 ldapConfiguration.setSsl( userConfiguration.getBoolean( LDAP_SSL, false ) );
186                 ldapConfiguration.setBaseDn(
187                     userConfiguration.getConcatenatedList( LDAP_BASEDN, null ) );
188
189                 ldapConfiguration.setBaseGroupsDn(
190                     userConfiguration.getConcatenatedList( LDAP_GROUPS_BASEDN,
191                                                            ldapConfiguration.getBaseDn() ) );
192
193                 ldapConfiguration.setContextFactory(
194                     userConfiguration.getString( LDAP_CONTEX_FACTORY,
195                                                  isSunContextFactoryAvailable()
196                                                      ? "com.sun.jndi.ldap.LdapCtxFactory"
197                                                      : "" ) );
198                 ldapConfiguration.setBindDn(
199                     userConfiguration.getConcatenatedList( LDAP_BINDDN, null ) );
200                 ldapConfiguration.setPassword(
201                     userConfiguration.getString( LDAP_PASSWORD, null ) );
202                 ldapConfiguration.setAuthenticationMethod(
203                     userConfiguration.getString( LDAP_AUTHENTICATION_METHOD, null ) );
204
205                 ldapConfiguration.setWritable(
206                     userConfiguration.getBoolean( LDAP_WRITABLE, false ) );
207
208                 ldapConfiguration.setUseRoleNameAsGroup(
209                     userConfiguration.getBoolean( LDAP_GROUPS_USE_ROLENAME, false ) );
210
211                 boolean ldapBindAuthenticatorEnabled =
212                     userConfiguration.getBoolean( LDAP_BIND_AUTHENTICATOR_ENABLED, false );
213                 ldapConfiguration.setBindAuthenticatorEnabled( ldapBindAuthenticatorEnabled );
214
215                 // LDAP groups mapping reading !!
216                 // UserConfigurationKeys.LDAP_GROUPS_ROLE_START_KEY
217                 // userConfiguration.getKeys()
218
219                 Collection<String> keys = userConfiguration.getKeys();
220
221                 List<LdapGroupMapping> ldapGroupMappings = new ArrayList<>();
222
223                 for ( String key : keys )
224                 {
225                     if ( key.startsWith( LDAP_GROUPS_ROLE_START_KEY ) )
226                     {
227                         String group =
228                             StringUtils.substringAfter( key, LDAP_GROUPS_ROLE_START_KEY );
229                         String val = userConfiguration.getConcatenatedList( key, "" );
230                         if ( !StringUtils.isEmpty( val ) )
231                         {
232                             String[] roles = StringUtils.split( val, ',' );
233                             ldapGroupMappings.add( new LdapGroupMapping( group, roles ) );
234                         }
235                     }
236                 }
237
238                 redbackRuntimeConfiguration.setLdapGroupMappings( ldapGroupMappings );
239
240                 redbackRuntimeConfiguration.setMigratedFromRedbackConfiguration( true );
241
242                 updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration );
243
244             }
245             // we must ensure userManagerImpls list is not empty if so put at least jdo one !
246             if ( redbackRuntimeConfiguration.getUserManagerImpls().isEmpty() )
247             {
248                 log.info(
249                     "redbackRuntimeConfiguration with empty userManagerImpls so force at least jdo implementation !" );
250                 redbackRuntimeConfiguration.getUserManagerImpls().add( "jdo" );
251                 updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration );
252             }
253             else
254             {
255                 log.info( "using userManagerImpls: {}", redbackRuntimeConfiguration.getUserManagerImpls() );
256             }
257
258             // we ensure rbacManagerImpls is not empty if so put at least cached
259             if ( redbackRuntimeConfiguration.getRbacManagerImpls().isEmpty() )
260             {
261                 log.info(
262                     "redbackRuntimeConfiguration with empty rbacManagerImpls so force at least cached implementation !" );
263                 redbackRuntimeConfiguration.getRbacManagerImpls().add( "cached" );
264                 updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration );
265             }
266             else
267             {
268                 log.info( "using rbacManagerImpls: {}", redbackRuntimeConfiguration.getRbacManagerImpls() );
269             }
270
271             boolean save = false;
272
273             // NPE free
274             if ( redbackRuntimeConfiguration.getUsersCacheConfiguration() == null )
275             {
276                 redbackRuntimeConfiguration.setUsersCacheConfiguration( new CacheConfiguration() );
277             }
278             // if -1 it means non initialized to take values from the spring bean
279             if ( redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToIdleSeconds() < 0 )
280             {
281                 redbackRuntimeConfiguration.getUsersCacheConfiguration().setTimeToIdleSeconds(
282                     usersCache.getTimeToIdleSeconds() );
283                 save = true;
284
285             }
286             usersCache.setTimeToIdleSeconds(
287                 redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToIdleSeconds() );
288
289             if ( redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToLiveSeconds() < 0 )
290             {
291                 redbackRuntimeConfiguration.getUsersCacheConfiguration().setTimeToLiveSeconds(
292                     usersCache.getTimeToLiveSeconds() );
293                 save = true;
294
295             }
296             usersCache.setTimeToLiveSeconds(
297                 redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToLiveSeconds() );
298
299             if ( redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsInMemory() < 0 )
300             {
301                 redbackRuntimeConfiguration.getUsersCacheConfiguration().setMaxElementsInMemory(
302                     usersCache.getMaxElementsInMemory() );
303                 save = true;
304             }
305             usersCache.setMaxElementsInMemory(
306                 redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsInMemory() );
307
308             if ( redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsOnDisk() < 0 )
309             {
310                 redbackRuntimeConfiguration.getUsersCacheConfiguration().setMaxElementsOnDisk(
311                     usersCache.getMaxElementsOnDisk() );
312                 save = true;
313             }
314             usersCache.setMaxElementsOnDisk(
315                 redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsOnDisk() );
316
317             if ( save )
318             {
319                 updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration );
320             }
321
322         }
323         catch ( RepositoryAdminException e )
324         {
325             throw new UserConfigurationException( e.getMessage(), e );
326         }
327     }
328
329     private boolean isSunContextFactoryAvailable()
330     {
331         try
332         {
333             return Thread.currentThread().getContextClassLoader().loadClass( "com.sun.jndi.ldap.LdapCtxFactory" )
334                 != null;
335         }
336         catch ( ClassNotFoundException e )
337         {
338             return false;
339         }
340     }
341
342     @Override
343     public RedbackRuntimeConfiguration getRedbackRuntimeConfiguration()
344     {
345         return build( archivaConfiguration.getConfiguration().getRedbackRuntimeConfiguration() );
346     }
347
348     @Override
349     public void updateRedbackRuntimeConfiguration( RedbackRuntimeConfiguration redbackRuntimeConfiguration )
350         throws RepositoryAdminException
351     {
352         org.apache.archiva.configuration.RedbackRuntimeConfiguration runtimeConfiguration =
353             build( redbackRuntimeConfiguration );
354         Configuration configuration = archivaConfiguration.getConfiguration();
355         configuration.setRedbackRuntimeConfiguration( runtimeConfiguration );
356         try
357         {
358             archivaConfiguration.save( configuration );
359         }
360         catch ( RegistryException e )
361         {
362             throw new RepositoryAdminException( e.getMessage(), e );
363         }
364         catch ( IndeterminateConfigurationException e )
365         {
366             throw new RepositoryAdminException( e.getMessage(), e );
367         }
368     }
369
370     private RedbackRuntimeConfiguration build(
371         org.apache.archiva.configuration.RedbackRuntimeConfiguration runtimeConfiguration )
372     {
373         RedbackRuntimeConfiguration redbackRuntimeConfiguration =
374             getModelMapper().map( runtimeConfiguration, RedbackRuntimeConfiguration.class );
375
376         if ( runtimeConfiguration.getLdapConfiguration() != null )
377         {
378             redbackRuntimeConfiguration.setLdapConfiguration(
379                 getModelMapper().map( runtimeConfiguration.getLdapConfiguration(), LdapConfiguration.class ) );
380         }
381
382         if ( runtimeConfiguration.getUsersCacheConfiguration() != null )
383         {
384             redbackRuntimeConfiguration.setUsersCacheConfiguration(
385                 getModelMapper().map( runtimeConfiguration.getUsersCacheConfiguration(), CacheConfiguration.class ) );
386         }
387
388         if ( redbackRuntimeConfiguration.getLdapConfiguration() == null )
389         {
390             // prevent NPE
391             redbackRuntimeConfiguration.setLdapConfiguration( new LdapConfiguration() );
392         }
393
394         if ( redbackRuntimeConfiguration.getUsersCacheConfiguration() == null )
395         {
396             redbackRuntimeConfiguration.setUsersCacheConfiguration( new CacheConfiguration() );
397         }
398
399         List<org.apache.archiva.configuration.LdapGroupMapping> mappings = runtimeConfiguration.getLdapGroupMappings();
400
401         if ( mappings != null && mappings.size() > 0 )
402         {
403             List<LdapGroupMapping> ldapGroupMappings = new ArrayList<>( mappings.size() );
404
405             for ( org.apache.archiva.configuration.LdapGroupMapping mapping : mappings )
406             {
407                 ldapGroupMappings.add( new LdapGroupMapping( mapping.getGroup(), mapping.getRoleNames() ) );
408             }
409
410             redbackRuntimeConfiguration.setLdapGroupMappings( ldapGroupMappings );
411         }
412
413         cleanupProperties( redbackRuntimeConfiguration );
414
415         return redbackRuntimeConfiguration;
416     }
417
418     /**
419      * cleaning from map properties used directly in archiva configuration fields
420      *
421      * @param redbackRuntimeConfiguration
422      */
423     private void cleanupProperties( RedbackRuntimeConfiguration redbackRuntimeConfiguration )
424     {
425         Map<String, String> properties = redbackRuntimeConfiguration.getConfigurationProperties();
426         LDAP_MAPPER.getAllAttributes( ).stream( ).forEach( att -> properties.remove( att ) );
427     }
428
429     private org.apache.archiva.configuration.RedbackRuntimeConfiguration build(
430         RedbackRuntimeConfiguration redbackRuntimeConfiguration )
431     {
432         org.apache.archiva.configuration.RedbackRuntimeConfiguration res =
433             getModelMapper().map( redbackRuntimeConfiguration,
434                                   org.apache.archiva.configuration.RedbackRuntimeConfiguration.class );
435
436         if ( redbackRuntimeConfiguration.getLdapConfiguration() == null )
437         {
438             redbackRuntimeConfiguration.setLdapConfiguration( new LdapConfiguration() );
439         }
440         res.setLdapConfiguration( getModelMapper().map( redbackRuntimeConfiguration.getLdapConfiguration(),
441                                                         org.apache.archiva.configuration.LdapConfiguration.class ) );
442
443         if ( redbackRuntimeConfiguration.getUsersCacheConfiguration() == null )
444         {
445             redbackRuntimeConfiguration.setUsersCacheConfiguration( new CacheConfiguration() );
446         }
447
448         res.setUsersCacheConfiguration( getModelMapper().map( redbackRuntimeConfiguration.getUsersCacheConfiguration(),
449                                                               org.apache.archiva.configuration.CacheConfiguration.class ) );
450
451         List<LdapGroupMapping> ldapGroupMappings = redbackRuntimeConfiguration.getLdapGroupMappings();
452
453         if ( ldapGroupMappings != null && ldapGroupMappings.size() > 0 )
454         {
455
456             List<org.apache.archiva.configuration.LdapGroupMapping> mappings =
457                 new ArrayList<>( ldapGroupMappings.size() );
458
459             for ( LdapGroupMapping ldapGroupMapping : ldapGroupMappings )
460             {
461
462                 org.apache.archiva.configuration.LdapGroupMapping mapping =
463                     new org.apache.archiva.configuration.LdapGroupMapping();
464                 mapping.setGroup( ldapGroupMapping.getGroup() );
465                 mapping.setRoleNames( new ArrayList<>( ldapGroupMapping.getRoleNames() ) );
466                 mappings.add( mapping );
467
468             }
469             res.setLdapGroupMappings( mappings );
470         }
471         return res;
472     }
473
474     // wrapper for UserConfiguration to intercept values (and store it not yet migrated)
475
476
477     @Override
478     public String getString( String key )
479     {
480         final RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
481         if (LDAP_MAPPER.isStringMapping( key )) {
482             final LdapConfiguration ldapConf = conf.getLdapConfiguration( );
483             return LDAP_MAPPER.getString( key, ldapConf );
484         }
485         if (LDAP_MAPPER.isPrefixMapping( key )) {
486             return LDAP_MAPPER.getPrefixString( key, conf.getLdapGroupMappings( ) );
487         }
488         if (conf.getLdapConfiguration().getExtraProperties().containsKey( key )) {
489             return conf.getLdapConfiguration( ).getExtraProperties( ).get( key );
490         }
491
492         if ( USER_MANAGER_IMPL.equals( key ) )
493         {
494             // possible false for others than archiva user manager
495             return conf.getUserManagerImpls().get( 0 );
496         }
497
498         if ( conf.getConfigurationProperties().containsKey( key ) )
499         {
500             return conf.getConfigurationProperties().get( key );
501         }
502
503         String value = userConfiguration.getString( key );
504         if ( value == null )
505         {
506             return null;
507         }
508         conf.getConfigurationProperties().put( key, value );
509
510         try
511         {
512             updateRedbackRuntimeConfiguration( conf );
513         }
514         catch ( RepositoryAdminException e )
515         {
516             log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
517             throw new RuntimeException( e.getMessage(), e );
518         }
519
520         return value;
521     }
522
523     @Override
524     public String getString( String key, String defaultValue )
525     {
526         final RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
527         if (LDAP_MAPPER.isStringMapping( key )) {
528             final LdapConfiguration ldapConf = conf.getLdapConfiguration( );
529             return LDAP_MAPPER.getString( key, ldapConf );
530         }
531         if (LDAP_MAPPER.isPrefixMapping( key )) {
532             return LDAP_MAPPER.getPrefixString( key, conf.getLdapGroupMappings( ) );
533         }
534         if (conf.getLdapConfiguration().getExtraProperties().containsKey( key )) {
535             return conf.getLdapConfiguration( ).getExtraProperties( ).get( key );
536         }
537
538         if ( conf.getConfigurationProperties().containsKey( key ) )
539         {
540             return conf.getConfigurationProperties().get( key );
541         }
542
543         String value = userConfiguration.getString( key, defaultValue );
544
545         if ( value == null )
546         {
547             return null;
548         }
549
550         conf.getConfigurationProperties().put( key, value );
551         try
552         {
553             updateRedbackRuntimeConfiguration( conf );
554         }
555         catch ( RepositoryAdminException e )
556         {
557             log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
558             throw new RuntimeException( e.getMessage(), e );
559         }
560
561         return value;
562     }
563
564     @Override
565     public int getInt( String key )
566     {
567         final RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
568         if (LDAP_MAPPER.isIntMapping( key )) {
569             return LDAP_MAPPER.getInt( key, conf.getLdapConfiguration( ) );
570         }
571
572         if ( conf.getConfigurationProperties().containsKey( key ) )
573         {
574             return Integer.valueOf( conf.getConfigurationProperties().get( key ) );
575         }
576
577         int value = userConfiguration.getInt( key );
578
579         conf.getConfigurationProperties().put( key, Integer.toString( value ) );
580         try
581         {
582             updateRedbackRuntimeConfiguration( conf );
583         }
584         catch ( RepositoryAdminException e )
585         {
586             log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
587             throw new RuntimeException( e.getMessage(), e );
588         }
589
590         return value;
591     }
592
593     @Override
594     public int getInt( String key, int defaultValue )
595     {
596         final RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
597         if (LDAP_MAPPER.isIntMapping( key )) {
598             return LDAP_MAPPER.getInt( key, conf.getLdapConfiguration( ) );
599         }
600
601         if ( conf.getConfigurationProperties().containsKey( key ) )
602         {
603             return Integer.valueOf( conf.getConfigurationProperties().get( key ) );
604         }
605
606         int value = userConfiguration.getInt( key, defaultValue );
607
608         conf.getConfigurationProperties().put( key, Integer.toString( value ) );
609         try
610         {
611             updateRedbackRuntimeConfiguration( conf );
612         }
613         catch ( RepositoryAdminException e )
614         {
615             log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
616             throw new RuntimeException( e.getMessage(), e );
617         }
618
619         return value;
620     }
621
622     @Override
623     public boolean getBoolean( String key )
624     {
625         RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
626         if (LDAP_MAPPER.isBooleanMapping( key )) {
627             return LDAP_MAPPER.getBoolean( key, conf.getLdapConfiguration( ) );
628         }
629
630         if ( conf.getConfigurationProperties().containsKey( key ) )
631         {
632             return Boolean.valueOf( conf.getConfigurationProperties().get( key ) );
633         }
634
635         boolean value = userConfiguration.getBoolean( key );
636
637         conf.getConfigurationProperties().put( key, Boolean.toString( value ) );
638         try
639         {
640             updateRedbackRuntimeConfiguration( conf );
641         }
642         catch ( RepositoryAdminException e )
643         {
644             log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
645             throw new RuntimeException( e.getMessage(), e );
646         }
647
648         return value;
649     }
650
651     @Override
652     public boolean getBoolean( String key, boolean defaultValue )
653     {
654         RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
655         if (LDAP_MAPPER.isBooleanMapping( key )) {
656             return LDAP_MAPPER.getBoolean( key, conf.getLdapConfiguration( ) );
657         }
658
659         if ( conf.getConfigurationProperties().containsKey( key ) )
660         {
661             return Boolean.valueOf( conf.getConfigurationProperties().get( key ) );
662         }
663
664         boolean value = userConfiguration.getBoolean( key, defaultValue );
665
666         conf.getConfigurationProperties().put( key, Boolean.toString( value ) );
667         try
668         {
669             updateRedbackRuntimeConfiguration( conf );
670         }
671         catch ( RepositoryAdminException e )
672         {
673             log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
674             throw new RuntimeException( e.getMessage(), e );
675         }
676
677         return value;
678     }
679
680     @Override
681     public List<String> getList( String key )
682     {
683         RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
684         if (conf.getConfigurationProperties().containsKey(key)) {
685             return Arrays.asList(conf.getConfigurationProperties().get(key).split(","));
686         }
687
688         List<String> value = userConfiguration.getList( key );
689
690         conf.getConfigurationProperties().put( key, "" );
691         try
692         {
693             updateRedbackRuntimeConfiguration( conf );
694         }
695         catch ( RepositoryAdminException e )
696         {
697             log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
698             throw new RuntimeException( e.getMessage(), e );
699         }
700
701         return value;
702     }
703
704     @Override
705     public String getConcatenatedList( String key, String defaultValue )
706     {
707         if (LDAP_MAPPER.isStringMapping( key )) {
708             return LDAP_MAPPER.getString( key,  getRedbackRuntimeConfiguration().getLdapConfiguration());
709         }
710         return userConfiguration.getConcatenatedList( key, defaultValue );
711     }
712
713     @Override
714     public Collection<String> getKeys()
715     {
716         Collection<String> keys = userConfiguration.getKeys();
717
718         Set<String> keysSet = new HashSet<>( keys );
719
720         keysSet.addAll( getRedbackRuntimeConfiguration().getConfigurationProperties().keySet() );
721
722         return keysSet;
723     }
724
725
726 }