KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > config > XMLConfigManager


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2005, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol.config;
38
39 import net.sourceforge.cruisecontrol.ConfigManager;
40 import net.sourceforge.cruisecontrol.CruiseControlConfig;
41 import net.sourceforge.cruisecontrol.ProjectConfig;
42 import net.sourceforge.cruisecontrol.CruiseControlException;
43
44 import net.sourceforge.cruisecontrol.util.Util;
45
46 import java.io.File JavaDoc;
47 import java.io.IOException JavaDoc;
48 import java.io.ByteArrayOutputStream JavaDoc;
49 import java.util.Set JavaDoc;
50 import java.util.Collections JavaDoc;
51
52 import org.jdom.Element;
53 import org.jdom.output.XMLOutputter;
54
55 import org.apache.log4j.Logger;
56 import com.twmacinta.util.MD5OutputStream;
57
58 /**
59  *
60  * @author jerome@coffeebreaks.org
61  * @version $Id: XMLConfigManager.java,v 1.5 2006/01/14 12:19:24 jkuipers Exp $
62  */

63 public class XMLConfigManager implements ConfigManager {
64
65     private static final Logger LOG = Logger.getLogger(XMLConfigManager.class);
66     private File JavaDoc configFile;
67     private CruiseControlConfig config = new CruiseControlConfig();
68     private String JavaDoc hash;
69
70     public XMLConfigManager(File JavaDoc file) throws CruiseControlException {
71         setConfigFile(file);
72     }
73
74     private void loadConfig(File JavaDoc file) throws CruiseControlException {
75         LOG.info("reading settings from config file [" + file.getAbsolutePath() + "]");
76         Element element = Util.loadConfigFile(file);
77         config = new CruiseControlConfig();
78         config.configure(element);
79     }
80
81     public void setConfigFile(File JavaDoc fileName) throws CruiseControlException {
82         LOG.debug("Config file set to [" + fileName + "]");
83         configFile = fileName;
84         LOG.debug("Calculating MD5 [" + configFile.getAbsolutePath() + "]");
85         hash = calculateMD5(configFile);
86         loadConfig(configFile);
87     }
88
89     public Set JavaDoc getProjectNames() {
90         return Collections.unmodifiableSet(config.getProjectNames());
91     }
92
93     public boolean reloadIfNecessary() throws CruiseControlException {
94         LOG.debug("Calculating MD5 [" + configFile.getAbsolutePath() + "]");
95         String JavaDoc newHash = calculateMD5(configFile);
96         final boolean fileChanged = !newHash.equals(hash);
97         if (fileChanged) {
98             loadConfig(configFile);
99             hash = newHash;
100         }
101         return fileChanged;
102     }
103
104     public ProjectConfig getConfig(String JavaDoc projectName) throws CruiseControlException {
105         LOG.info("using settings from config file [" + configFile.getAbsolutePath() + "]");
106         return config.getConfig(projectName);
107     }
108
109     public File JavaDoc getConfigFile() {
110         return configFile;
111     }
112
113     public static String JavaDoc calculateMD5(File JavaDoc file) {
114         String JavaDoc md5 = null;
115         MD5OutputStream stream = null;
116         try {
117             Element element = Util.loadConfigFile(file);
118             stream = new MD5OutputStream(new ByteArrayOutputStream JavaDoc());
119             XMLOutputter outputter = new XMLOutputter();
120             outputter.output(element, stream);
121             md5 = stream.getMD5().asHex();
122         } catch (IOException JavaDoc e) {
123             LOG.error("exception calculating MD5 of config file " + file.getAbsolutePath(), e);
124         } catch (CruiseControlException e) {
125             LOG.error("exception calculating MD5 of config file " + file.getAbsolutePath(), e);
126         } finally {
127             if (stream != null) {
128                 try {
129                     stream.close();
130                 } catch (IOException JavaDoc ignore) {
131                 }
132             }
133         }
134         return md5;
135     }
136
137     /** For tests purposes. FIXME. move tests in same package */
138     public CruiseControlConfig getCruiseControlConfig() {
139         return config;
140     }
141 }
142
Popular Tags