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