Free cookie consent management tool by TermsFeed Policy Generator

source: misc/tools/ConfigMerger/ConfigMerger.cs @ 17429

Last change on this file since 17429 was 16715, checked in by abeham, 6 years ago

updated config merger to include further subsettings for oracle clients

File size: 15.3 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      sourceNode = source.SelectSingleNode("/configuration/oracle.manageddataaccess.client/version/settings");
194      destinationNode = destination.SelectSingleNode("/configuration/oracle.manageddataaccess.client/version/settings");
195      Merge(sourceNode, destinationNode, destination, "/configuration");
196     
197      sourceNode = source.SelectSingleNode("/configuration/oracle.manageddataaccess.client/version/LDAPsettings");
198      destinationNode = destination.SelectSingleNode("/configuration/oracle.manageddataaccess.client/version/LDAPsettings");
199      Merge(sourceNode, destinationNode, destination, "/configuration");
200
201
202
203      destination.Save(destinationFile);
204
205      Console.Out.WriteLine("ConfigMerger: Merge successfully finished!");
206    }
207
208    private static void Merge(XmlNode source, XmlNode destination, XmlDocument document, string root) {
209      try {
210        if (source != null) {
211          if (destination == null) {
212            XmlNode newNode = document.ImportNode(source, true);
213            document.SelectSingleNode(root).AppendChild(newNode);
214          } else {
215            foreach (XmlNode node in source.ChildNodes) {
216              XmlNode newNode = document.ImportNode(node, true);
217              XmlNode oldNode = destination.SelectSingleNode(BuildXPathString(newNode));
218              if (oldNode != null)
219                destination.ReplaceChild(newNode, oldNode);
220              else
221                destination.AppendChild(newNode);
222            }
223          }
224        }
225      }
226      catch (Exception ex) {
227        StringBuilder sb = new StringBuilder();
228        sb.Append("Error while merging node \"").Append(source.Name).Append("\"");
229        throw new Exception(sb.ToString(), ex);
230      }
231    }
232
233    private static string BuildXPathString(XmlNode node) {
234      StringBuilder builder = new StringBuilder();
235      builder.Append(node.Name);
236      if (node.Attributes.Count > 0) {
237        XmlAttribute attrib = node.Attributes[0];
238        builder.Append("[");
239        builder.Append("@" + attrib.Name + "='" + attrib.Value + "'");
240        for (int i = 1; i < node.Attributes.Count; i++) {
241          attrib = node.Attributes[i];
242          builder.Append(" and @" + attrib.Name + "='" + attrib.Value + "'");
243        }
244        builder.Append("]");
245      }
246      return builder.ToString();
247    }
248
249    private static string BuildErrorMessage(Exception ex) {
250      StringBuilder sb = new StringBuilder();
251      sb.Append("\n\n");
252      sb.Append("### ConfigMerger ERROR ###########################################\n" + ex.Message + "\n" + ex.StackTrace + "\n");
253      while (ex.InnerException != null) {
254        ex = ex.InnerException;
255        sb.Append("-----\n" + ex.Message + "\n" + ex.StackTrace + "\n");
256      }
257      sb.Append("##################################################################\n\n");
258      return sb.ToString();
259    }
260  }
261}
Note: See TracBrowser for help on using the repository browser.