]> source.dussan.org Git - archiva.git/blob
3c6704d8b794c7de8e9663340151016a94684e82
[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.ArchivaRuntimeConfiguration;
24 import org.apache.archiva.admin.model.beans.CacheConfiguration;
25 import org.apache.archiva.admin.model.beans.FileLockConfiguration;
26 import org.apache.archiva.admin.model.runtime.ArchivaRuntimeConfigurationAdmin;
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.springframework.stereotype.Service;
33
34 import javax.annotation.PostConstruct;
35 import javax.inject.Inject;
36 import javax.inject.Named;
37
38 /**
39  * @author Olivier Lamy
40  * @since 1.4-M4
41  */
42 @Service( "archivaRuntimeConfigurationAdmin#default" )
43 public class DefaultArchivaRuntimeConfigurationAdmin
44     implements ArchivaRuntimeConfigurationAdmin
45 {
46
47     @Inject
48     private ArchivaConfiguration archivaConfiguration;
49
50     @Inject
51     @Named( value = "cache#url-failures-cache" )
52     private Cache urlFailureCache;
53
54     @PostConstruct
55     public void initialize()
56         throws RepositoryAdminException
57     {
58         ArchivaRuntimeConfiguration archivaRuntimeConfiguration = getArchivaRuntimeConfiguration();
59
60         boolean save = false;
61
62         // NPE free
63         if ( archivaRuntimeConfiguration.getFileLockConfiguration() == null )
64         {
65             archivaRuntimeConfiguration.setFileLockConfiguration( new FileLockConfiguration() );
66         }
67
68         // NPE free
69         if ( archivaRuntimeConfiguration.getUrlFailureCacheConfiguration() == null )
70         {
71             archivaRuntimeConfiguration.setUrlFailureCacheConfiguration( new CacheConfiguration() );
72         }
73
74         // if -1 it means non initialized to take values from the spring bean
75         if ( archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().getTimeToIdleSeconds() < 0 )
76         {
77             archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().setTimeToIdleSeconds(
78                 urlFailureCache.getTimeToIdleSeconds() );
79             save = true;
80
81         }
82         urlFailureCache.setTimeToIdleSeconds(
83             archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().getTimeToIdleSeconds() );
84
85         if ( archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().getTimeToLiveSeconds() < 0 )
86         {
87             archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().setTimeToLiveSeconds(
88                 urlFailureCache.getTimeToLiveSeconds() );
89             save = true;
90
91         }
92         urlFailureCache.setTimeToLiveSeconds(
93             archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().getTimeToLiveSeconds() );
94
95         if ( archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().getMaxElementsInMemory() < 0 )
96         {
97             archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().setMaxElementsInMemory(
98                 urlFailureCache.getMaxElementsInMemory() );
99             save = true;
100         }
101         urlFailureCache.setMaxElementsInMemory(
102             archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().getMaxElementsInMemory() );
103
104         if ( archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().getMaxElementsOnDisk() < 0 )
105         {
106             archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().setMaxElementsOnDisk(
107                 urlFailureCache.getMaxElementsOnDisk() );
108             save = true;
109         }
110         urlFailureCache.setMaxElementsOnDisk(
111             archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().getMaxElementsOnDisk() );
112
113         if ( save )
114         {
115             updateArchivaRuntimeConfiguration( archivaRuntimeConfiguration );
116         }
117
118     }
119
120     public ArchivaRuntimeConfiguration getArchivaRuntimeConfiguration()
121         throws RepositoryAdminException
122     {
123         return build( archivaConfiguration.getConfiguration().getArchivaRuntimeConfiguration() );
124     }
125
126     public void updateArchivaRuntimeConfiguration( ArchivaRuntimeConfiguration archivaRuntimeConfiguration )
127         throws RepositoryAdminException
128     {
129         Configuration configuration = archivaConfiguration.getConfiguration();
130         configuration.setArchivaRuntimeConfiguration( build( archivaRuntimeConfiguration ) );
131         try
132         {
133             archivaConfiguration.save( configuration );
134         }
135         catch ( RegistryException e )
136         {
137             throw new RepositoryAdminException( e.getMessage(), e );
138         }
139         catch ( IndeterminateConfigurationException e )
140         {
141             throw new RepositoryAdminException( e.getMessage(), e );
142         }
143     }
144
145     protected ArchivaRuntimeConfiguration build(
146         org.apache.archiva.configuration.ArchivaRuntimeConfiguration archivaRuntimeConfiguration )
147     {
148         if ( archivaRuntimeConfiguration == null )
149         {
150             return new ArchivaRuntimeConfiguration();
151         }
152
153         ArchivaRuntimeConfiguration res =
154             new BeanReplicator().replicateBean( archivaRuntimeConfiguration, ArchivaRuntimeConfiguration.class );
155
156         if ( archivaRuntimeConfiguration.getUrlFailureCacheConfiguration() != null )
157         {
158
159             res.setUrlFailureCacheConfiguration(
160                 new BeanReplicator().replicateBean( archivaRuntimeConfiguration.getUrlFailureCacheConfiguration(),
161                                                     CacheConfiguration.class ) );
162
163         }
164
165         return res;
166     }
167
168     protected org.apache.archiva.configuration.ArchivaRuntimeConfiguration build(
169         ArchivaRuntimeConfiguration archivaRuntimeConfiguration )
170     {
171         if ( archivaRuntimeConfiguration == null )
172         {
173             return new org.apache.archiva.configuration.ArchivaRuntimeConfiguration();
174         }
175
176         org.apache.archiva.configuration.ArchivaRuntimeConfiguration res =
177             new BeanReplicator().replicateBean( archivaRuntimeConfiguration,
178                                                 org.apache.archiva.configuration.ArchivaRuntimeConfiguration.class );
179
180         if ( archivaRuntimeConfiguration.getUrlFailureCacheConfiguration() != null )
181         {
182
183             res.setUrlFailureCacheConfiguration(
184                 new BeanReplicator().replicateBean( archivaRuntimeConfiguration.getUrlFailureCacheConfiguration(),
185                                                     org.apache.archiva.configuration.CacheConfiguration.class ) );
186
187         }
188
189         if ( archivaRuntimeConfiguration.getFileLockConfiguration() != null )
190         {
191             res.setFileLockConfiguration(
192                 new BeanReplicator().replicateBean( archivaRuntimeConfiguration.getFileLockConfiguration(),
193                                                     org.apache.archiva.configuration.FileLockConfiguration.class ) );
194         }
195
196         return res;
197     }
198 }
199
200