Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/tools/ConfigMerger/ConfigMerger.cs @ 14235

Last change on this file since 14235 was 6173, checked in by ascheibe, 14 years ago

#1517

  • rewrote Logger to use Diagnostics.Trace
  • removed HiveLogger as it's not used anymore
  • removed dependency from Persistence to log4net
  • ConfigMerger now also merges <system.diagnostics> sections
File size: 14.4 KB
RevLine 
[848]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Text;
[849]24using System.Xml;
[848]25
26namespace HeuristicLab.Tools.ConfigMerger {
[849]27  public class ConfigMerger {
28    public static void Main(string[] args) {
29      try {
30        Merge(args[0], args[1]);
31      }
32      catch (Exception ex) {
[2602]33        Console.Out.WriteLine(BuildErrorMessage(ex));
[849]34      }
[848]35    }
[849]36
37    public static void Merge(string sourceFile, string destinationFile) {
[3836]38      Console.Out.WriteLine("ConfigMerger: Merge started ...");
39      Console.Out.WriteLine("ConfigMerger: Merge source:      \"" + sourceFile + "\"");
40      Console.Out.WriteLine("ConfigMerger: Merge destination: \"" + destinationFile + "\"");
41
[849]42      XmlDocument source = new XmlDocument();
43      source.Load(sourceFile);
44      XmlDocument destination = new XmlDocument();
45      destination.Load(destinationFile);
46
47      XmlNode sourceNode;
48      XmlNode destinationNode;
49
[3284]50      #region Merge 'system.serviceModel/behaviors/*'
[943]51      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/behaviors");
[3284]52      if (destinationNode == null) {
53        destinationNode = destination.CreateElement("behaviors");
54        destination.SelectSingleNode("/configuration/system.serviceModel").AppendChild(destinationNode);
55      }
[943]56
[3284]57      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/behaviors/endpointBehaviors");
58      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/behaviors/endpointBehaviors");
59      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/behaviors");
60
61      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/behaviors/serviceBehaviors");
62      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/behaviors/serviceBehaviors");
63      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/behaviors");
64      #endregion
65
[943]66      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/services");
67      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/services");
68      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel");
[6173]69
[2697]70      #region Merge 'system.serviceModel/bindings/*'
[2725]71      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings");
72      if (destinationNode == null) {
73        destinationNode = destination.CreateElement("bindings");
74        destination.SelectSingleNode("/configuration/system.serviceModel").AppendChild(destinationNode);
75      }
[2697]76      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/basicHttpBinding");
77      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/basicHttpBinding");
78      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
[943]79
[2697]80      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/basicHttpContextBinding");
81      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/basicHttpContextBinding");
82      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
[951]83
[2697]84      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/customBinding");
85      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/customBinding");
86      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
87
88      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/mexHttpBinding");
89      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/mexHttpBinding");
90      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
91
92      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/mexHttpsBinding");
93      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/mexHttpsBinding");
94      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
95
96      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/mexNamedPipeBinding");
97      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/mexNamedPipeBinding");
98      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
99
100      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/mexTcpBinding");
101      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/mexTcpBinding");
102      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
103
104      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/msmqIntegrationBinding");
105      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/msmqIntegrationBinding");
106      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
107
108      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/netMsmqBinding");
109      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/netMsmqBinding");
110      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
111
112      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/netNamedPipeBinding");
113      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/netNamedPipeBinding");
114      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
115
116      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/netPeerTcpBinding");
117      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/netPeerTcpBinding");
118      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
119
120      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/netTcpBinding");
121      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/netTcpBinding");
122      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
123
124      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/netTcpContextBinding");
125      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/netTcpContextBinding");
126      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
127
128      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/webHttpBinding");
129      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/webHttpBinding");
130      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
131
132      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/ws2007FederationHttpBinding");
133      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/ws2007FederationHttpBinding");
134      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
135
136      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/ws2007HttpBinding");
137      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/ws2007HttpBinding");
138      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
139
140      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/wsDualHttpBinding");
141      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/wsDualHttpBinding");
142      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
143
144      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/wsFederationHttpBinding");
145      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/wsFederationHttpBinding");
146      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
147
148      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/wsHttpBinding");
149      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/wsHttpBinding");
150      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
151
152      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/bindings/wsHttpContextBinding");
153      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/bindings/wsHttpContextBinding");
154      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel/bindings");
155      #endregion
156
[951]157      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/client");
158      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/client");
159      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel");
160
[849]161      sourceNode = source.SelectSingleNode("/configuration/configSections/sectionGroup[@name='applicationSettings']");
162      destinationNode = destination.SelectSingleNode("/configuration/configSections/sectionGroup[@name='applicationSettings']");
163      Merge(sourceNode, destinationNode, destination, "/configuration/configSections");
164
165      sourceNode = source.SelectSingleNode("/configuration/configSections/sectionGroup[@name='userSettings']");
166      destinationNode = destination.SelectSingleNode("/configuration/configSections/sectionGroup[@name='userSettings']");
167      Merge(sourceNode, destinationNode, destination, "/configuration/configSections");
168
169      sourceNode = source.SelectSingleNode("/configuration/applicationSettings");
170      destinationNode = destination.SelectSingleNode("/configuration/applicationSettings");
171      Merge(sourceNode, destinationNode, destination, "/configuration");
172
173      sourceNode = source.SelectSingleNode("/configuration/userSettings");
174      destinationNode = destination.SelectSingleNode("/configuration/userSettings");
175      Merge(sourceNode, destinationNode, destination, "/configuration");
176
[1142]177      sourceNode = source.SelectSingleNode("/configuration/connectionStrings");
178      destinationNode = destination.SelectSingleNode("/configuration/connectionStrings");
179      Merge(sourceNode, destinationNode, destination, "/configuration");
180
[2692]181      sourceNode = source.SelectSingleNode("/configuration/system.data/DbProviderFactories");
182      destinationNode = destination.SelectSingleNode("/configuration/system.data/DbProviderFactories");
183      Merge(sourceNode, destinationNode, destination, "/configuration");
184
[6173]185      sourceNode = source.SelectSingleNode("/configuration/system.diagnostics");
186      destinationNode = destination.SelectSingleNode("/configuration/system.diagnostics");
187      Merge(sourceNode, destinationNode, destination, "/configuration");
188
189
190
191
[849]192      destination.Save(destinationFile);
[3836]193
194      Console.Out.WriteLine("ConfigMerger: Merge successfully finished!");
[849]195    }
196
197    private static void Merge(XmlNode source, XmlNode destination, XmlDocument document, string root) {
[2602]198      try {
199        if (source != null) {
200          if (destination == null) {
201            XmlNode newNode = document.ImportNode(source, true);
202            document.SelectSingleNode(root).AppendChild(newNode);
203          } else {
204            foreach (XmlNode node in source.ChildNodes) {
205              XmlNode newNode = document.ImportNode(node, true);
206              XmlNode oldNode = destination.SelectSingleNode(BuildXPathString(newNode));
207              if (oldNode != null)
208                destination.ReplaceChild(newNode, oldNode);
209              else
210                destination.AppendChild(newNode);
211            }
[849]212          }
213        }
214      }
[2602]215      catch (Exception ex) {
216        StringBuilder sb = new StringBuilder();
217        sb.Append("Error while merging node \"").Append(source.Name).Append("\"");
218        throw new Exception(sb.ToString(), ex);
219      }
[849]220    }
[875]221
222    private static string BuildXPathString(XmlNode node) {
223      StringBuilder builder = new StringBuilder();
224      builder.Append(node.Name);
225      if (node.Attributes.Count > 0) {
226        XmlAttribute attrib = node.Attributes[0];
227        builder.Append("[");
228        builder.Append("@" + attrib.Name + "='" + attrib.Value + "'");
229        for (int i = 1; i < node.Attributes.Count; i++) {
230          attrib = node.Attributes[i];
231          builder.Append(" and @" + attrib.Name + "='" + attrib.Value + "'");
232        }
233        builder.Append("]");
234      }
235      return builder.ToString();
236    }
[2602]237
238    private static string BuildErrorMessage(Exception ex) {
239      StringBuilder sb = new StringBuilder();
240      sb.Append("\n\n");
241      sb.Append("### ConfigMerger ERROR ###########################################\n" + ex.Message + "\n" + ex.StackTrace + "\n");
242      while (ex.InnerException != null) {
243        ex = ex.InnerException;
244        sb.Append("-----\n" + ex.Message + "\n" + ex.StackTrace + "\n");
245      }
246      sb.Append("##################################################################\n\n");
247      return sb.ToString();
248    }
[848]249  }
250}
Note: See TracBrowser for help on using the repository browser.