001: /********************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2005, ThoughtWorks, Inc.
004: * 200 E. Randolph, 25th Floor
005: * Chicago, IL 60601 USA
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * + Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * + Redistributions in binary form must reproduce the above
016: * copyright notice, this list of conditions and the following
017: * disclaimer in the documentation and/or other materials provided
018: * with the distribution.
019: *
020: * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
021: * names of its contributors may be used to endorse or promote
022: * products derived from this software without specific prior
023: * written permission.
024: *
025: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
026: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
027: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
028: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
029: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
030: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
031: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
032: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
033: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
034: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
035: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
036: ********************************************************************************/package net.sourceforge.cruisecontrol.config;
037:
038: import java.io.ByteArrayOutputStream;
039: import java.io.File;
040: import java.io.IOException;
041: import java.util.HashSet;
042: import java.util.Iterator;
043: import java.util.Set;
044:
045: import net.sourceforge.cruisecontrol.CruiseControlConfig;
046: import net.sourceforge.cruisecontrol.CruiseControlController;
047: import net.sourceforge.cruisecontrol.CruiseControlException;
048: import net.sourceforge.cruisecontrol.ProjectInterface;
049: import net.sourceforge.cruisecontrol.util.IO;
050: import net.sourceforge.cruisecontrol.util.Util;
051:
052: import org.apache.log4j.Logger;
053: import org.jdom.Element;
054: import org.jdom.output.XMLOutputter;
055:
056: import com.twmacinta.util.MD5OutputStream;
057:
058: /**
059: *
060: * @author jerome@coffeebreaks.org
061: * @version $Id: XMLConfigManager.java 3168 2007-08-01 08:12:30Z pauljulius $
062: */
063: public class XMLConfigManager {
064:
065: private static final Logger LOG = Logger
066: .getLogger(XMLConfigManager.class);
067: private final File configFile;
068: private CruiseControlConfig config;
069: private String hash;
070: private Resolver resolver = new Resolver();
071: private final CruiseControlController controller;
072:
073: public XMLConfigManager(File configurationFile)
074: throws CruiseControlException {
075: this (configurationFile, null);
076: }
077:
078: public XMLConfigManager(File file,
079: CruiseControlController controller)
080: throws CruiseControlException {
081: configFile = file;
082: this .controller = controller;
083: loadConfig(configFile);
084: hash = calculateMD5(configFile);
085: }
086:
087: private void loadConfig(File file) throws CruiseControlException {
088: LOG.info("reading settings from config file ["
089: + file.getAbsolutePath() + "]");
090: Element element = Util.loadRootElement(file);
091: resolver.resetResolvedFiles();
092: config = new CruiseControlConfig(element, resolver, controller);
093: }
094:
095: public File getConfigFile() {
096: return configFile;
097: }
098:
099: public CruiseControlConfig getCruiseControlConfig() {
100: return config;
101: }
102:
103: public ProjectInterface getProject(String projectName) {
104: LOG.info("using settings from config file ["
105: + configFile.getAbsolutePath() + "]");
106: return config.getProject(projectName);
107: }
108:
109: public boolean reloadIfNecessary() throws CruiseControlException {
110: LOG.debug("Calculating MD5 [" + configFile.getAbsolutePath()
111: + "]");
112: String newHash = calculateMD5(configFile);
113: final boolean fileChanged = !newHash.equals(hash);
114: if (fileChanged) {
115: loadConfig(configFile);
116: hash = newHash;
117: }
118: return fileChanged;
119: }
120:
121: private String calculateMD5(File file)
122: throws CruiseControlException {
123: LOG.debug("Calculating MD5 [" + configFile.getAbsolutePath()
124: + "]");
125: String md5 = calculatePartialMD5(file);
126: Set includedFiles = resolver.getResolvedFiles();
127: for (Iterator iter = includedFiles.iterator(); iter.hasNext();) {
128: md5 += calculatePartialMD5((File) iter.next());
129: }
130: return md5;
131: }
132:
133: private String calculatePartialMD5(File file) {
134: String md5 = "";
135: MD5OutputStream stream = null;
136: try {
137: Element element = Util.loadRootElement(file);
138: stream = new MD5OutputStream(new ByteArrayOutputStream());
139: XMLOutputter outputter = new XMLOutputter();
140: outputter.output(element, stream);
141: md5 = stream.getMD5().asHex();
142: } catch (IOException e) {
143: LOG.error("exception calculating MD5 of config file "
144: + file.getAbsolutePath(), e);
145: } catch (CruiseControlException e) {
146: LOG.error("exception calculating MD5 of config file "
147: + file.getAbsolutePath(), e);
148: } finally {
149: IO.close(stream);
150: }
151: return md5;
152: }
153:
154: class Resolver implements XmlResolver {
155: private Set resolvedFiles = new HashSet();
156:
157: public Element getElement(String path)
158: throws CruiseControlException {
159: File file = new File(configFile.getParentFile(), path);
160: resolvedFiles.add(file);
161: return Util.loadRootElement(file);
162: }
163:
164: public Set getResolvedFiles() {
165: return resolvedFiles;
166: }
167:
168: public void resetResolvedFiles() {
169: resolvedFiles.clear();
170: }
171:
172: }
173:
174: }
|