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