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