1 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 ; 47 import java.io.IOException ; 48 import java.io.ByteArrayOutputStream ; 49 import java.util.Set ; 50 import java.util.Collections ; 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 63 public class XMLConfigManager implements ConfigManager { 64 65 private static final Logger LOG = Logger.getLogger(XMLConfigManager.class); 66 private File configFile; 67 private CruiseControlConfig config = new CruiseControlConfig(); 68 private String hash; 69 70 public XMLConfigManager(File file) throws CruiseControlException { 71 setConfigFile(file); 72 } 73 74 private void loadConfig(File 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 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 getProjectNames() { 90 return Collections.unmodifiableSet(config.getProjectNames()); 91 } 92 93 public boolean reloadIfNecessary() throws CruiseControlException { 94 LOG.debug("Calculating MD5 [" + configFile.getAbsolutePath() + "]"); 95 String 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 projectName) throws CruiseControlException { 105 LOG.info("using settings from config file [" + configFile.getAbsolutePath() + "]"); 106 return config.getConfig(projectName); 107 } 108 109 public File getConfigFile() { 110 return configFile; 111 } 112 113 public static String calculateMD5(File file) { 114 String md5 = null; 115 MD5OutputStream stream = null; 116 try { 117 Element element = Util.loadConfigFile(file); 118 stream = new MD5OutputStream(new ByteArrayOutputStream ()); 119 XMLOutputter outputter = new XMLOutputter(); 120 outputter.output(element, stream); 121 md5 = stream.getMD5().asHex(); 122 } catch (IOException 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 ignore) { 131 } 132 } 133 } 134 return md5; 135 } 136 137 138 public CruiseControlConfig getCruiseControlConfig() { 139 return config; 140 } 141 } 142 | Popular Tags |