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

Last change on this file since 14524 was 14524, checked in by sraggl, 3 years ago

Extended ConfigMerger to also merge
"/configuration/oracle.manageddataaccess.client/version/dataSources"
We need this in PPOVCockpit.

File size: 14.7 KB
Line 
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;
24using System.Xml;
25
26namespace HeuristicLab.Tools.ConfigMerger {
27  public class ConfigMerger {
28    public static void Main(string[] args) {
29      try {
30        Merge(args[0], args[1]);
31      }
32      catch (Exception ex) {
33        Console.Out.WriteLine(BuildErrorMessage(ex));
34      }
35    }
36
37    public static void Merge(string sourceFile, string destinationFile) {
38      Console.Out.WriteLine("ConfigMerger: Merge started ...");
39      Console.Out.WriteLine("ConfigMerger: Merge source:      \"" + sourceFile + "\"");
40      Console.Out.WriteLine("ConfigMerger: Merge destination: \"" + destinationFile + "\"");
41
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
50      #region Merge 'system.serviceModel/behaviors/*'
51      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/behaviors");
52      if (destinationNode == null) {
53        destinationNode = destination.CreateElement("behaviors");
54        destination.SelectSingleNode("/configuration/system.serviceModel").AppendChild(destinationNode);
55      }
56
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
66      sourceNode = source.SelectSingleNode("/configuration/system.serviceModel/services");
67      destinationNode = destination.SelectSingleNode("/configuration/system.serviceModel/services");
68      Merge(sourceNode, destinationNode, destination, "/configuration/system.serviceModel");
69
70      #region Merge 'system.serviceModel/bindings/*'
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      }
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");
79
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");
83
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
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
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
177      sourceNode = source.SelectSingleNode("/configuration/connectionStrings");
178      destinationNode = destination.SelectSingleNode("/configuration/connectionStrings");
179      Merge(sourceNode, destinationNode, destination, "/configuration");
180
181      sourceNode = source.SelectSingleNode("/configuration/system.data/DbProviderFactories");
182      destinationNode = destination.SelectSingleNode("/configuration/system.data/DbProviderFactories");
183      Merge(sourceNode, destinationNode, destination, "/configuration");
184
185      sourceNode = source.SelectSingleNode("/configuration/system.diagnostics");
186      destinationNode = destination.SelectSingleNode("/configuration/system.diagnostics");
187      Merge(sourceNode, destinationNode, destination, "/configuration");
188
189      sourceNode = source.SelectSingleNode("/configuration/oracle.manageddataaccess.client/version/dataSources");
190      destinationNode = destination.SelectSingleNode("/configuration/oracle.manageddataaccess.client/version/dataSources");
191      Merge(sourceNode, destinationNode, destination, "/configuration");
192
193
194
195      destination.Save(destinationFile);
196
197      Console.Out.WriteLine("ConfigMerger: Merge successfully finished!");
198    }
199
200    private static void Merge(XmlNode source, XmlNode destination, XmlDocument document, string root) {
201      try {
202        if (source != null) {
203          if (destination == null) {
204            XmlNode newNode = document.ImportNode(source, true);
205            document.SelectSingleNode(root).AppendChild(newNode);
206          } else {
207            foreach (XmlNode node in source.ChildNodes) {
208              XmlNode newNode = document.ImportNode(node, true);
209              XmlNode oldNode = destination.SelectSingleNode(BuildXPathString(newNode));
210              if (oldNode != null)
211                destination.ReplaceChild(newNode, oldNode);
212              else
213                destination.AppendChild(newNode);
214            }
215          }
216        }
217      }
218      catch (Exception ex) {
219        StringBuilder sb = new StringBuilder();
220        sb.Append("Error while merging node \"").Append(source.Name).Append("\"");
221        throw new Exception(sb.ToString(), ex);
222      }
223    }
224
225    private static string BuildXPathString(XmlNode node) {
226      StringBuilder builder = new StringBuilder();
227      builder.Append(node.Name);
228      if (node.Attributes.Count > 0) {
229        XmlAttribute attrib = node.Attributes[0];
230        builder.Append("[");
231        builder.Append("@" + attrib.Name + "='" + attrib.Value + "'");
232        for (int i = 1; i < node.Attributes.Count; i++) {
233          attrib = node.Attributes[i];
234          builder.Append(" and @" + attrib.Name + "='" + attrib.Value + "'");
235        }
236        builder.Append("]");
237      }
238      return builder.ToString();
239    }
240
241    private static string BuildErrorMessage(Exception ex) {
242      StringBuilder sb = new StringBuilder();
243      sb.Append("\n\n");
244      sb.Append("### ConfigMerger ERROR ###########################################\n" + ex.Message + "\n" + ex.StackTrace + "\n");
245      while (ex.InnerException != null) {
246        ex = ex.InnerException;
247        sb.Append("-----\n" + ex.Message + "\n" + ex.StackTrace + "\n");
248      }
249      sb.Append("##################################################################\n\n");
250      return sb.ToString();
251    }
252  }
253}
Note: See TracBrowser for help on using the repository browser.