1 | package org.apache.continuum.configuration; |
2 | |
3 | /* |
4 | * Licensed to the Apache Software Foundation (ASF) under one |
5 | * or more contributor license agreements. See the NOTICE file |
6 | * distributed with this work for additional information |
7 | * regarding copyright ownership. The ASF licenses this file |
8 | * to you under the Apache License, Version 2.0 (the |
9 | * "License"); you may not use this file except in compliance |
10 | * with the License. You may obtain a copy of the License at |
11 | * |
12 | * http://www.apache.org/licenses/LICENSE-2.0 |
13 | * |
14 | * Unless required by applicable law or agreed to in writing, |
15 | * software distributed under the License is distributed on an |
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
17 | * KIND, either express or implied. See the License for the |
18 | * specific language governing permissions and limitations |
19 | * under the License. |
20 | */ |
21 | |
22 | import java.io.File; |
23 | import java.io.FileInputStream; |
24 | import java.io.FileWriter; |
25 | import java.io.IOException; |
26 | import java.io.InputStreamReader; |
27 | import java.util.ArrayList; |
28 | import java.util.List; |
29 | |
30 | import org.apache.commons.lang.StringUtils; |
31 | import org.apache.continuum.configuration.model.ContinuumConfigurationModel; |
32 | import org.apache.continuum.configuration.model.io.xpp3.ContinuumConfigurationModelXpp3Reader; |
33 | import org.apache.continuum.configuration.model.io.xpp3.ContinuumConfigurationModelXpp3Writer; |
34 | import org.codehaus.plexus.util.IOUtil; |
35 | import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
36 | import org.slf4j.Logger; |
37 | import org.slf4j.LoggerFactory; |
38 | |
39 | /** |
40 | * @author <a href="mailto:olamy@apache.org">olamy</a> |
41 | * @version $Id: DefaultContinuumConfiguration.java 780207 2009-05-30 08:51:12Z evenisse $ |
42 | * @since 17 juin 2008 |
43 | */ |
44 | public class DefaultContinuumConfiguration |
45 | implements ContinuumConfiguration |
46 | { |
47 | private static final Logger log = LoggerFactory.getLogger( DefaultContinuumConfiguration.class ); |
48 | |
49 | private File configurationFile; |
50 | |
51 | private GeneralConfiguration generalConfiguration; |
52 | |
53 | public static final String CONFIGURATION_FILE = "continuum.xml"; |
54 | |
55 | //---------------------------------------------------- |
56 | // Initialize method configured in the Spring xml |
57 | // configuration file |
58 | //---------------------------------------------------- |
59 | protected void initialize() |
60 | { |
61 | if ( log.isDebugEnabled() ) |
62 | { |
63 | log.debug( "configurationFile null " + ( configurationFile.getPath() == null ) ); |
64 | } |
65 | if ( configurationFile != null && configurationFile.exists() ) |
66 | { |
67 | try |
68 | { |
69 | reload( configurationFile ); |
70 | } |
71 | catch ( ContinuumConfigurationException e ) |
72 | { |
73 | // skip this and only log a warn |
74 | log.warn( " error on loading configuration from file " + configurationFile.getPath() ); |
75 | } |
76 | } |
77 | else |
78 | { |
79 | log.info( "configuration file not exists" ); |
80 | this.generalConfiguration = new GeneralConfiguration(); |
81 | } |
82 | } |
83 | |
84 | public void reload() |
85 | throws ContinuumConfigurationException |
86 | { |
87 | this.initialize(); |
88 | } |
89 | |
90 | public void save() |
91 | throws ContinuumConfigurationException |
92 | { |
93 | if ( !configurationFile.exists() ) |
94 | { |
95 | configurationFile.getParentFile().mkdirs(); |
96 | } |
97 | save( configurationFile ); |
98 | } |
99 | |
100 | /** |
101 | * @see org.apache.continuum.configuration.ContinuumConfiguration#getGeneralConfiguration() |
102 | */ |
103 | public GeneralConfiguration getGeneralConfiguration() |
104 | throws ContinuumConfigurationException |
105 | { |
106 | return this.generalConfiguration; |
107 | } |
108 | |
109 | public void setGeneralConfiguration( GeneralConfiguration generalConfiguration ) |
110 | throws ContinuumConfigurationException |
111 | { |
112 | this.generalConfiguration = generalConfiguration; |
113 | } |
114 | |
115 | public void reload( File file ) |
116 | throws ContinuumConfigurationException |
117 | { |
118 | FileInputStream fis = null; |
119 | try |
120 | { |
121 | fis = new FileInputStream( file ); |
122 | ContinuumConfigurationModelXpp3Reader configurationXpp3Reader = new ContinuumConfigurationModelXpp3Reader(); |
123 | ContinuumConfigurationModel configuration = configurationXpp3Reader.read( new InputStreamReader( fis ) ); |
124 | |
125 | this.generalConfiguration = new GeneralConfiguration(); |
126 | |
127 | this.generalConfiguration.setNumberOfBuildsInParallel( configuration.getNumberOfBuildsInParallel() ); |
128 | this.generalConfiguration.setBaseUrl( configuration.getBaseUrl() ); |
129 | if ( StringUtils.isNotEmpty( configuration.getBuildOutputDirectory() ) ) |
130 | { |
131 | // TODO take care if file exists ? |
132 | this.generalConfiguration.setBuildOutputDirectory( |
133 | new File( configuration.getBuildOutputDirectory() ) ); |
134 | } |
135 | if ( StringUtils.isNotEmpty( configuration.getDeploymentRepositoryDirectory() ) ) |
136 | { |
137 | // TODO take care if file exists ? |
138 | this.generalConfiguration.setDeploymentRepositoryDirectory( |
139 | new File( configuration.getDeploymentRepositoryDirectory() ) ); |
140 | } |
141 | if ( StringUtils.isNotEmpty( configuration.getWorkingDirectory() ) ) |
142 | { |
143 | // TODO take care if file exists ? |
144 | this.generalConfiguration.setWorkingDirectory( new File( configuration.getWorkingDirectory() ) ); |
145 | } |
146 | if ( configuration.getProxyConfiguration() != null ) |
147 | { |
148 | ProxyConfiguration proxyConfiguration = |
149 | new ProxyConfiguration( configuration.getProxyConfiguration().getProxyHost(), |
150 | configuration.getProxyConfiguration().getProxyPassword(), |
151 | configuration.getProxyConfiguration().getProxyPort(), |
152 | configuration.getProxyConfiguration().getProxyUser() ); |
153 | this.generalConfiguration.setProxyConfiguration( proxyConfiguration ); |
154 | } |
155 | if ( StringUtils.isNotEmpty( configuration.getReleaseOutputDirectory() ) ) |
156 | { |
157 | // TODO take care if file exists? |
158 | this.generalConfiguration.setReleaseOutputDirectory( |
159 | new File( configuration.getReleaseOutputDirectory() ) ); |
160 | } |
161 | if ( configuration.getBuildAgents() != null ) |
162 | { |
163 | List<BuildAgentConfiguration> buildAgents = new ArrayList<BuildAgentConfiguration>(); |
164 | |
165 | List<org.apache.continuum.configuration.model.BuildAgentConfiguration> agents = |
166 | configuration.getBuildAgents(); |
167 | for ( org.apache.continuum.configuration.model.BuildAgentConfiguration agent : agents ) |
168 | { |
169 | BuildAgentConfiguration buildAgent = |
170 | new BuildAgentConfiguration( agent.getUrl(), agent.getDescription(), agent.isEnabled() ); |
171 | buildAgents.add( buildAgent ); |
172 | } |
173 | |
174 | this.generalConfiguration.setBuildAgents( buildAgents ); |
175 | } |
176 | if ( configuration.getBuildAgentGroups() != null ) |
177 | { |
178 | List<BuildAgentGroupConfiguration> buildAgentGroups = new ArrayList<BuildAgentGroupConfiguration>(); |
179 | List<org.apache.continuum.configuration.model.BuildAgentGroupConfiguration> groups = |
180 | configuration.getBuildAgentGroups(); |
181 | |
182 | for ( org.apache.continuum.configuration.model.BuildAgentGroupConfiguration group : groups ) |
183 | { |
184 | List<BuildAgentConfiguration> agents = new ArrayList<BuildAgentConfiguration>(); |
185 | List<org.apache.continuum.configuration.model.BuildAgentConfiguration> modelAgents = |
186 | group.getBuildAgents(); |
187 | for ( org.apache.continuum.configuration.model.BuildAgentConfiguration modelAgent : modelAgents ) |
188 | { |
189 | BuildAgentConfiguration buildAgent = |
190 | new BuildAgentConfiguration( modelAgent.getUrl(), modelAgent.getDescription(), |
191 | modelAgent.isEnabled() ); |
192 | agents.add( buildAgent ); |
193 | } |
194 | |
195 | BuildAgentGroupConfiguration groupAgent = |
196 | new BuildAgentGroupConfiguration( group.getName(), agents ); |
197 | buildAgentGroups.add( groupAgent ); |
198 | } |
199 | |
200 | this.generalConfiguration.setBuildAgentGroups( buildAgentGroups ); |
201 | } |
202 | } |
203 | catch ( IOException e ) |
204 | { |
205 | log.error( e.getMessage(), e ); |
206 | throw new RuntimeException( e.getMessage(), e ); |
207 | } |
208 | catch ( XmlPullParserException e ) |
209 | { |
210 | log.error( e.getMessage(), e ); |
211 | throw new RuntimeException( e.getMessage(), e ); |
212 | } |
213 | finally |
214 | { |
215 | if ( fis != null ) |
216 | { |
217 | IOUtil.close( fis ); |
218 | } |
219 | } |
220 | } |
221 | |
222 | public void save( File file ) |
223 | throws ContinuumConfigurationException |
224 | { |
225 | try |
226 | { |
227 | ContinuumConfigurationModel configurationModel = new ContinuumConfigurationModel(); |
228 | configurationModel.setBaseUrl( this.generalConfiguration.getBaseUrl() ); |
229 | configurationModel.setNumberOfBuildsInParallel( this.generalConfiguration.getNumberOfBuildsInParallel() ); |
230 | |
231 | // normally not null but NPE free is better ! |
232 | if ( this.generalConfiguration.getBuildOutputDirectory() != null ) |
233 | { |
234 | configurationModel.setBuildOutputDirectory( |
235 | this.generalConfiguration.getBuildOutputDirectory().getPath() ); |
236 | } |
237 | if ( this.generalConfiguration.getWorkingDirectory() != null ) |
238 | { |
239 | configurationModel.setWorkingDirectory( this.generalConfiguration.getWorkingDirectory().getPath() ); |
240 | } |
241 | if ( this.generalConfiguration.getDeploymentRepositoryDirectory() != null ) |
242 | { |
243 | configurationModel.setDeploymentRepositoryDirectory( |
244 | this.generalConfiguration.getDeploymentRepositoryDirectory().getPath() ); |
245 | } |
246 | if ( this.generalConfiguration.getProxyConfiguration() != null ) |
247 | { |
248 | configurationModel.setProxyConfiguration( |
249 | new org.apache.continuum.configuration.model.ProxyConfiguration() ); |
250 | configurationModel.getProxyConfiguration().setProxyHost( |
251 | this.generalConfiguration.getProxyConfiguration().getProxyHost() ); |
252 | configurationModel.getProxyConfiguration().setProxyPassword( |
253 | this.generalConfiguration.getProxyConfiguration().getProxyPassword() ); |
254 | configurationModel.getProxyConfiguration().setProxyPort( |
255 | this.generalConfiguration.getProxyConfiguration().getProxyPort() ); |
256 | configurationModel.getProxyConfiguration().setProxyHost( |
257 | this.generalConfiguration.getProxyConfiguration().getProxyHost() ); |
258 | } |
259 | if ( this.generalConfiguration.getReleaseOutputDirectory() != null ) |
260 | { |
261 | configurationModel.setReleaseOutputDirectory( |
262 | this.generalConfiguration.getReleaseOutputDirectory().getPath() ); |
263 | } |
264 | if ( this.generalConfiguration.getBuildAgents() != null ) |
265 | { |
266 | List<org.apache.continuum.configuration.model.BuildAgentConfiguration> buildAgents = |
267 | new ArrayList<org.apache.continuum.configuration.model.BuildAgentConfiguration>(); |
268 | |
269 | for ( BuildAgentConfiguration agent : this.generalConfiguration.getBuildAgents() ) |
270 | { |
271 | org.apache.continuum.configuration.model.BuildAgentConfiguration buildAgent = |
272 | new org.apache.continuum.configuration.model.BuildAgentConfiguration(); |
273 | buildAgent.setUrl( agent.getUrl() ); |
274 | buildAgent.setDescription( agent.getDescription() ); |
275 | buildAgent.setEnabled( agent.isEnabled() ); |
276 | |
277 | buildAgents.add( buildAgent ); |
278 | } |
279 | configurationModel.setBuildAgents( buildAgents ); |
280 | } |
281 | if ( this.generalConfiguration.getBuildAgentGroups() != null ) |
282 | { |
283 | List<org.apache.continuum.configuration.model.BuildAgentGroupConfiguration> buildAgentGroups = |
284 | new ArrayList<org.apache.continuum.configuration.model.BuildAgentGroupConfiguration>(); |
285 | |
286 | for ( BuildAgentGroupConfiguration group : this.generalConfiguration.getBuildAgentGroups() ) |
287 | { |
288 | org.apache.continuum.configuration.model.BuildAgentGroupConfiguration buildAgentGroup = |
289 | new org.apache.continuum.configuration.model.BuildAgentGroupConfiguration(); |
290 | buildAgentGroup.setName( group.getName() ); |
291 | |
292 | List<org.apache.continuum.configuration.model.BuildAgentConfiguration> buildAgents = |
293 | new ArrayList<org.apache.continuum.configuration.model.BuildAgentConfiguration>(); |
294 | |
295 | if ( group.getBuildAgents() != null ) |
296 | { |
297 | for ( BuildAgentConfiguration agent : group.getBuildAgents() ) |
298 | { |
299 | org.apache.continuum.configuration.model.BuildAgentConfiguration buildAgent = |
300 | new org.apache.continuum.configuration.model.BuildAgentConfiguration(); |
301 | buildAgent.setUrl( agent.getUrl() ); |
302 | buildAgent.setDescription( agent.getDescription() ); |
303 | buildAgent.setEnabled( agent.isEnabled() ); |
304 | |
305 | buildAgents.add( buildAgent ); |
306 | } |
307 | |
308 | buildAgentGroup.setBuildAgents( buildAgents ); |
309 | } |
310 | |
311 | buildAgentGroups.add( buildAgentGroup ); |
312 | } |
313 | configurationModel.setBuildAgentGroups( buildAgentGroups ); |
314 | } |
315 | |
316 | ContinuumConfigurationModelXpp3Writer writer = new ContinuumConfigurationModelXpp3Writer(); |
317 | FileWriter fileWriter = new FileWriter( file ); |
318 | writer.write( fileWriter, configurationModel ); |
319 | fileWriter.flush(); |
320 | fileWriter.close(); |
321 | } |
322 | catch ( IOException e ) |
323 | { |
324 | throw new ContinuumConfigurationException( e.getMessage(), e ); |
325 | } |
326 | |
327 | } |
328 | |
329 | // ---------------------------------------- |
330 | // Spring injection |
331 | // ---------------------------------------- |
332 | |
333 | |
334 | public File getConfigurationFile() |
335 | { |
336 | return configurationFile; |
337 | } |
338 | |
339 | public void setConfigurationFile( File configurationFile ) |
340 | { |
341 | this.configurationFile = configurationFile; |
342 | } |
343 | |
344 | } |