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