]> source.dussan.org Git - archiva.git/blob
3b8e30a3a472f462802b620ab87ed300b6609f48
[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 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;
39
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;
46 import java.util.Set;
47
48 /**
49  * @author Olivier Lamy
50  * @since 1.4-M4
51  */
52 @Service("userConfiguration#archiva")
53 public class DefaultRedbackRuntimeConfigurationAdmin
54     implements RedbackRuntimeConfigurationAdmin, UserConfiguration
55 {
56
57     protected Logger log = LoggerFactory.getLogger( getClass() );
58
59     @Inject
60     private ArchivaConfiguration archivaConfiguration;
61
62     @Inject
63     @Named(value = "userConfiguration#redback")
64     UserConfiguration userConfiguration;
65
66     @Inject
67     @Named(value = "cache#users")
68     private Cache usersCache;
69
70     @PostConstruct
71     public void initialize()
72         throws UserConfigurationException
73     {
74         try
75         {
76             RedbackRuntimeConfiguration redbackRuntimeConfiguration = getRedbackRuntimeConfiguration();
77             // migrate or not data from redback
78             if ( !redbackRuntimeConfiguration.isMigratedFromRedbackConfiguration() )
79             {
80                 // so migrate if available
81                 String userManagerImpl =
82                     userConfiguration.getConcatenatedList( UserConfigurationKeys.USER_MANAGER_IMPL, "jdo" );
83                 if ( StringUtils.isNotEmpty( userManagerImpl ) )
84                 {
85                     if ( StringUtils.contains( userManagerImpl, ',' ) )
86                     {
87                         String[] impls = StringUtils.split( userManagerImpl, ',' );
88                         for ( String impl : impls )
89                         {
90                             redbackRuntimeConfiguration.getUserManagerImpls().add( impl );
91                         }
92                     }
93                     else
94                     {
95                         redbackRuntimeConfiguration.getUserManagerImpls().add( userManagerImpl );
96                     }
97                 }
98
99                 String rbacManagerImpls =
100                     userConfiguration.getConcatenatedList( UserConfigurationKeys.RBAC_MANAGER_IMPL, "cached" );
101
102                 if ( StringUtils.isNotEmpty( rbacManagerImpls ) )
103                 {
104                     if ( StringUtils.contains( rbacManagerImpls, ',' ) )
105                     {
106                         String[] impls = StringUtils.split( rbacManagerImpls, ',' );
107                         for ( String impl : impls )
108                         {
109                             redbackRuntimeConfiguration.getRbacManagerImpls().add( impl );
110                         }
111                     }
112                     else
113                     {
114                         redbackRuntimeConfiguration.getRbacManagerImpls().add( userManagerImpl );
115                     }
116                 }
117
118                 // now ldap
119
120                 LdapConfiguration ldapConfiguration = redbackRuntimeConfiguration.getLdapConfiguration();
121                 if ( ldapConfiguration == null )
122                 {
123                     ldapConfiguration = new LdapConfiguration();
124                     redbackRuntimeConfiguration.setLdapConfiguration( ldapConfiguration );
125                 }
126
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 ) );
141
142                 redbackRuntimeConfiguration.setMigratedFromRedbackConfiguration( true );
143
144                 updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration );
145
146             }
147             // we must ensure userManagerImpls list is not empty if so put at least jdo one !
148             if ( redbackRuntimeConfiguration.getUserManagerImpls().isEmpty() )
149             {
150                 log.info(
151                     "redbackRuntimeConfiguration with empty userManagerImpls so force at least jdo implementation !" );
152                 redbackRuntimeConfiguration.getUserManagerImpls().add( "jdo" );
153                 updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration );
154             }
155
156             // we ensure authorizerImpls is not empty if so put
157             if ( redbackRuntimeConfiguration.getRbacManagerImpls().isEmpty() )
158             {
159                 log.info(
160                     "redbackRuntimeConfiguration with empty rbacManagerImpls so force at least cached implementation !" );
161                 redbackRuntimeConfiguration.getRbacManagerImpls().add( "cached" );
162                 updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration );
163             }
164
165             boolean save = false;
166
167             // NPE free
168             if ( redbackRuntimeConfiguration.getUsersCacheConfiguration() == null )
169             {
170                 redbackRuntimeConfiguration.setUsersCacheConfiguration( new CacheConfiguration() );
171             }
172             // if -1 it means non initialized to take values from the spring bean
173             if ( redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToIdleSeconds() < 0 )
174             {
175                 redbackRuntimeConfiguration.getUsersCacheConfiguration().setTimeToIdleSeconds(
176                     usersCache.getTimeToIdleSeconds() );
177                 save = true;
178
179             }
180             usersCache.setTimeToIdleSeconds(
181                 redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToIdleSeconds() );
182
183             if ( redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToLiveSeconds() < 0 )
184             {
185                 redbackRuntimeConfiguration.getUsersCacheConfiguration().setTimeToLiveSeconds(
186                     usersCache.getTimeToLiveSeconds() );
187                 save = true;
188
189             }
190             usersCache.setTimeToLiveSeconds(
191                 redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToLiveSeconds() );
192
193             if ( redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsInMemory() < 0 )
194             {
195                 redbackRuntimeConfiguration.getUsersCacheConfiguration().setMaxElementsInMemory(
196                     usersCache.getMaxElementsInMemory() );
197                 save = true;
198             }
199             usersCache.setMaxElementsInMemory(
200                 redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsInMemory() );
201
202             if ( redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsOnDisk() < 0 )
203             {
204                 redbackRuntimeConfiguration.getUsersCacheConfiguration().setMaxElementsOnDisk(
205                     usersCache.getMaxElementsOnDisk() );
206                 save = true;
207             }
208             usersCache.setMaxElementsOnDisk(
209                 redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsOnDisk() );
210
211             if ( save )
212             {
213                 updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration );
214             }
215
216         }
217         catch ( RepositoryAdminException e )
218         {
219             throw new UserConfigurationException( e.getMessage(), e );
220         }
221     }
222
223     public RedbackRuntimeConfiguration getRedbackRuntimeConfiguration()
224     {
225         return build( archivaConfiguration.getConfiguration().getRedbackRuntimeConfiguration() );
226     }
227
228     public void updateRedbackRuntimeConfiguration( RedbackRuntimeConfiguration redbackRuntimeConfiguration )
229         throws RepositoryAdminException
230     {
231         org.apache.archiva.configuration.RedbackRuntimeConfiguration runtimeConfiguration =
232             build( redbackRuntimeConfiguration );
233         Configuration configuration = archivaConfiguration.getConfiguration();
234         configuration.setRedbackRuntimeConfiguration( runtimeConfiguration );
235         try
236         {
237             archivaConfiguration.save( configuration );
238         }
239         catch ( RegistryException e )
240         {
241             throw new RepositoryAdminException( e.getMessage(), e );
242         }
243         catch ( IndeterminateConfigurationException e )
244         {
245             throw new RepositoryAdminException( e.getMessage(), e );
246         }
247     }
248
249     private RedbackRuntimeConfiguration build(
250         org.apache.archiva.configuration.RedbackRuntimeConfiguration runtimeConfiguration )
251     {
252         RedbackRuntimeConfiguration redbackRuntimeConfiguration =
253             new BeanReplicator().replicateBean( runtimeConfiguration, RedbackRuntimeConfiguration.class );
254
255         if ( runtimeConfiguration.getLdapConfiguration() != null )
256         {
257             redbackRuntimeConfiguration.setLdapConfiguration(
258                 new BeanReplicator().replicateBean( runtimeConfiguration.getLdapConfiguration(),
259                                                     LdapConfiguration.class ) );
260         }
261
262         if ( runtimeConfiguration.getUsersCacheConfiguration() != null )
263         {
264             redbackRuntimeConfiguration.setUsersCacheConfiguration(
265                 new BeanReplicator().replicateBean( runtimeConfiguration.getUsersCacheConfiguration(),
266                                                     CacheConfiguration.class ) );
267         }
268
269         if ( redbackRuntimeConfiguration.getLdapConfiguration() == null )
270         {
271             // prevent NPE
272             redbackRuntimeConfiguration.setLdapConfiguration( new LdapConfiguration() );
273         }
274
275         if ( redbackRuntimeConfiguration.getUsersCacheConfiguration() == null )
276         {
277             redbackRuntimeConfiguration.setUsersCacheConfiguration( new CacheConfiguration() );
278         }
279
280         return redbackRuntimeConfiguration;
281     }
282
283     private org.apache.archiva.configuration.RedbackRuntimeConfiguration build(
284         RedbackRuntimeConfiguration archivaRuntimeConfiguration )
285     {
286         org.apache.archiva.configuration.RedbackRuntimeConfiguration redbackRuntimeConfiguration =
287             new BeanReplicator().replicateBean( archivaRuntimeConfiguration,
288                                                 org.apache.archiva.configuration.RedbackRuntimeConfiguration.class );
289
290         if ( archivaRuntimeConfiguration.getLdapConfiguration() == null )
291         {
292             archivaRuntimeConfiguration.setLdapConfiguration( new LdapConfiguration() );
293         }
294         redbackRuntimeConfiguration.setLdapConfiguration(
295             new BeanReplicator().replicateBean( archivaRuntimeConfiguration.getLdapConfiguration(),
296                                                 org.apache.archiva.configuration.LdapConfiguration.class ) );
297
298         if ( archivaRuntimeConfiguration.getUsersCacheConfiguration() == null )
299         {
300             archivaRuntimeConfiguration.setUsersCacheConfiguration( new CacheConfiguration() );
301         }
302
303         redbackRuntimeConfiguration.setUsersCacheConfiguration(
304             new BeanReplicator().replicateBean( archivaRuntimeConfiguration.getUsersCacheConfiguration(),
305                                                 org.apache.archiva.configuration.CacheConfiguration.class ) );
306
307         return redbackRuntimeConfiguration;
308     }
309
310     // wrapper for UserConfiguration to intercept values (and store it not yet migrated
311
312
313     public String getString( String key )
314     {
315         if ( UserConfigurationKeys.USER_MANAGER_IMPL.equals( key ) )
316         {
317             // possible false for others than archiva user manager
318             return getRedbackRuntimeConfiguration().getUserManagerImpls().get( 0 );
319         }
320
321         RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
322
323         if ( conf.getConfigurationProperties().containsKey( key ) )
324         {
325             return conf.getConfigurationProperties().get( key );
326         }
327
328         String value = userConfiguration.getString( key );
329         if ( value == null )
330         {
331             return null;
332         }
333         conf.getConfigurationProperties().put( key, value );
334
335         try
336         {
337             updateRedbackRuntimeConfiguration( conf );
338         }
339         catch ( RepositoryAdminException e )
340         {
341             log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
342             throw new RuntimeException( e.getMessage(), e );
343         }
344
345         return value;
346     }
347
348     public String getString( String key, String defaultValue )
349     {
350         if ( UserConfigurationKeys.LDAP_HOSTNAME.equals( key ) )
351         {
352             return getRedbackRuntimeConfiguration().getLdapConfiguration().getHostName();
353         }
354         if ( UserConfigurationKeys.LDAP_CONTEX_FACTORY.equals( key ) )
355         {
356             return getRedbackRuntimeConfiguration().getLdapConfiguration().getContextFactory();
357         }
358         if ( UserConfigurationKeys.LDAP_PASSWORD.equals( key ) )
359         {
360             return getRedbackRuntimeConfiguration().getLdapConfiguration().getPassword();
361         }
362         if ( UserConfigurationKeys.LDAP_AUTHENTICATION_METHOD.equals( key ) )
363         {
364             return getRedbackRuntimeConfiguration().getLdapConfiguration().getAuthenticationMethod();
365         }
366
367         RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
368
369         if ( conf.getConfigurationProperties().containsKey( key ) )
370         {
371             return conf.getConfigurationProperties().get( key );
372         }
373
374         String value = userConfiguration.getString( key, defaultValue );
375
376         if ( value == null )
377         {
378             return null;
379         }
380
381         conf.getConfigurationProperties().put( key, value );
382         try
383         {
384             updateRedbackRuntimeConfiguration( conf );
385         }
386         catch ( RepositoryAdminException e )
387         {
388             log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
389             throw new RuntimeException( e.getMessage(), e );
390         }
391
392         return value;
393     }
394
395     public int getInt( String key )
396     {
397         RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
398
399         if ( conf.getConfigurationProperties().containsKey( key ) )
400         {
401             return Integer.valueOf( conf.getConfigurationProperties().get( key ) );
402         }
403
404         int value = userConfiguration.getInt( key );
405
406         conf.getConfigurationProperties().put( key, Integer.toString( value ) );
407         try
408         {
409             updateRedbackRuntimeConfiguration( conf );
410         }
411         catch ( RepositoryAdminException e )
412         {
413             log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
414             throw new RuntimeException( e.getMessage(), e );
415         }
416
417         return value;
418     }
419
420     public int getInt( String key, int defaultValue )
421     {
422         if ( UserConfigurationKeys.LDAP_PORT.equals( key ) )
423         {
424             return getRedbackRuntimeConfiguration().getLdapConfiguration().getPort();
425         }
426
427         RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
428
429         if ( conf.getConfigurationProperties().containsKey( key ) )
430         {
431             return Integer.valueOf( conf.getConfigurationProperties().get( key ) );
432         }
433
434         int value = userConfiguration.getInt( key, defaultValue );
435
436         conf.getConfigurationProperties().put( key, Integer.toString( value ) );
437         try
438         {
439             updateRedbackRuntimeConfiguration( conf );
440         }
441         catch ( RepositoryAdminException e )
442         {
443             log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
444             throw new RuntimeException( e.getMessage(), e );
445         }
446
447         return value;
448     }
449
450     public boolean getBoolean( String key )
451     {
452         RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
453
454         if ( conf.getConfigurationProperties().containsKey( key ) )
455         {
456             return Boolean.valueOf( conf.getConfigurationProperties().get( key ) );
457         }
458
459         boolean value = userConfiguration.getBoolean( key );
460
461         conf.getConfigurationProperties().put( key, Boolean.toString( value ) );
462         try
463         {
464             updateRedbackRuntimeConfiguration( conf );
465         }
466         catch ( RepositoryAdminException e )
467         {
468             log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
469             throw new RuntimeException( e.getMessage(), e );
470         }
471
472         return value;
473     }
474
475     public boolean getBoolean( String key, boolean defaultValue )
476     {
477         if ( UserConfigurationKeys.LDAP_SSL.equals( key ) )
478         {
479             return getRedbackRuntimeConfiguration().getLdapConfiguration().isSsl();
480         }
481
482         RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
483
484         if ( conf.getConfigurationProperties().containsKey( key ) )
485         {
486             return Boolean.valueOf( conf.getConfigurationProperties().get( key ) );
487         }
488
489         boolean value = userConfiguration.getBoolean( key, defaultValue );
490
491         conf.getConfigurationProperties().put( key, Boolean.toString( value ) );
492         try
493         {
494             updateRedbackRuntimeConfiguration( conf );
495         }
496         catch ( RepositoryAdminException e )
497         {
498             log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
499             throw new RuntimeException( e.getMessage(), e );
500         }
501
502         return value;
503     }
504
505     public List<String> getList( String key )
506     {
507         List<String> value = userConfiguration.getList( key );
508
509         RedbackRuntimeConfiguration conf = getRedbackRuntimeConfiguration();
510         // TODO concat values
511         conf.getConfigurationProperties().put( key, "" );
512         try
513         {
514             updateRedbackRuntimeConfiguration( conf );
515         }
516         catch ( RepositoryAdminException e )
517         {
518             log.error( "fail to save RedbackRuntimeConfiguration: {}", e.getMessage(), e );
519             throw new RuntimeException( e.getMessage(), e );
520         }
521
522         return value;
523     }
524
525     public String getConcatenatedList( String key, String defaultValue )
526     {
527         if ( UserConfigurationKeys.LDAP_BASEDN.equals( key ) )
528         {
529             return getRedbackRuntimeConfiguration().getLdapConfiguration().getBaseDn();
530         }
531         if ( UserConfigurationKeys.LDAP_BINDDN.equals( key ) )
532         {
533             return getRedbackRuntimeConfiguration().getLdapConfiguration().getBindDn();
534         }
535         return userConfiguration.getConcatenatedList( key, defaultValue );
536     }
537
538     public Collection<String> getKeys()
539     {
540         Collection<String> keys = userConfiguration.getKeys();
541
542         Set<String> keysSet = new HashSet<String>( keys );
543
544         keysSet.addAll( getRedbackRuntimeConfiguration().getConfigurationProperties().keySet() );
545
546         return keysSet;
547     }
548 }