This repository was archived by the owner on Aug 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathConfigurationFile.cs
More file actions
332 lines (302 loc) · 12.8 KB
/
ConfigurationFile.cs
File metadata and controls
332 lines (302 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
using Org.Apache.REEF.Tang.Exceptions;
using Org.Apache.REEF.Tang.Implementations.Configuration;
using Org.Apache.REEF.Tang.Implementations.Tang;
using Org.Apache.REEF.Tang.Interface;
using Org.Apache.REEF.Tang.Types;
using Org.Apache.REEF.Tang.Util;
using Org.Apache.REEF.Utilities.Logging;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace Org.Apache.REEF.Tang.Formats
{
public static class ConfigurationFile
{
private static readonly Logger LOGGER = Logger.GetLogger(typeof(ConfigurationFile));
#region text file serialization
public static void WriteConfigurationFile(IConfiguration c, string fileName)
{
using (FileStream aFile = new FileStream(fileName, FileMode.Create))
{
using (StreamWriter sw = new StreamWriter(aFile))
{
sw.Write(ToConfigurationString(c));
}
}
}
public static string ToConfigurationString(IConfiguration c)
{
StringBuilder sb = new StringBuilder();
foreach (string s in ToConfigurationStringList(c))
{
sb.Append(s);
sb.Append('\n');
}
return sb.ToString();
}
private static string GetFullName(INode n)
{
string s = n.GetFullName();
Type t = ReflectionUtilities.GetTypeByName(s);
return t.FullName;
}
private static string GetFullName(string name)
{
try
{
Type t = ReflectionUtilities.GetTypeByName(name);
return t.FullName;
}
catch (TangApplicationException e)
{
Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Caught(e, Level.Warning, LOGGER);
return name; // if name is not a type, return as it was
}
}
private static string GetAssemblyName(string s)
{
try
{
Type t = ReflectionUtilities.GetTypeByName(s);
return ReflectionUtilities.GetAssemblyQualifiedName(t);
}
catch (TangApplicationException e)
{
Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Caught(e, Level.Warning, LOGGER);
return s; // if name is not a type, return as it was
}
}
public static HashSet<string> ToConfigurationStringList(IConfiguration c)
{
ConfigurationImpl conf = (ConfigurationImpl)c;
HashSet<string> l = new HashSet<string>();
foreach (IClassNode opt in conf.GetBoundImplementations())
{
l.Add(GetFullName(opt) + '=' + Escape(GetFullName(conf.GetBoundImplementation(opt))));
}
foreach (IClassNode opt in conf.GetBoundConstructors())
{
l.Add(GetFullName(opt) + '=' + Escape(GetFullName(conf.GetBoundConstructor(opt))));
}
foreach (INamedParameterNode opt in conf.GetNamedParameters())
{
l.Add(GetFullName(opt) + '=' + Escape(GetFullName(conf.GetNamedParameter(opt))));
}
foreach (IClassNode cn in conf.GetLegacyConstructors())
{
StringBuilder sb = new StringBuilder();
Join(sb, "-", conf.GetLegacyConstructor(cn).GetArgs().ToArray<IConstructorArg>());
l.Add(GetFullName(cn) + Escape('=' + ConfigurationBuilderImpl.INIT + '(' + sb.ToString() + ')'));
}
IEnumerator bs = conf.GetBoundSets();
while (bs.MoveNext())
{
KeyValuePair<INamedParameterNode, object> e = (KeyValuePair<INamedParameterNode, object>)bs.Current;
string val = null;
if (e.Value is string)
{
val = GetFullName((string)e.Value);
}
else if (e.Value is INode)
{
val = GetFullName((INode)e.Value);
}
else
{
throw new BindException($"Failed to serialize set of unsupported type {e.Value.GetType()}");
}
l.Add(GetFullName(e.Key) + '=' + Escape(val));
}
foreach(var kvp in conf.GetBoundList())
{
foreach (var item in kvp.Value)
{
string val = null;
if (item is string)
{
val = GetFullName((string)item);
}
else if (kvp.Value is INode)
{
val = GetFullName((INode)kvp.Value);
}
else
{
throw new BindException($"Failed to serialize list of unsupported type {item.GetType()}");
}
l.Add(GetFullName(kvp.Key) + '=' + Escape(val));
}
}
return l;
}
public static IConfiguration GetConfiguration(string configString)
{
byte[] array = Encoding.GetEncoding(0).GetBytes(configString);
return GetConfiguration(array);
}
public static IConfiguration GetConfiguration(byte[] configStream)
{
ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder();
AddConfigurationFromStream(cb, configStream);
return cb.Build();
}
public static void AddConfigurationFromStream(IConfigurationBuilder conf, byte[] configData)
{
using (StreamReader reader = new StreamReader(new MemoryStream(configData), Encoding.GetEncoding(0)))
{
AddConfiguration(conf, reader);
}
}
public static void AddConfigurationFromFile(IConfigurationBuilder conf, string configFileName)
{
using (StreamReader reader = File.OpenText(configFileName))
{
AddConfiguration(conf, reader);
}
}
public static void AddConfigurationFromString(IConfigurationBuilder conf, string configData)
{
byte[] array = Encoding.ASCII.GetBytes(configData);
AddConfigurationFromStream(conf, array);
}
private static void AddConfiguration(IConfigurationBuilder conf, StreamReader reader)
{
IList<KeyValuePair<string, string>> settings = new List<KeyValuePair<string, string>>();
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
string[] p = line.Split('=');
if (p.Length == 2)
{
settings.Add(new KeyValuePair<string, string>(GetAssemblyName(p[0]), GetAssemblyName(p[1])));
}
else if (p.Length > 2)
{
string v = line.Substring(p[0].Length + 1, line.Length - p[0].Length - 1);
settings.Add(new KeyValuePair<string, string>(GetAssemblyName(p[0]), GetAssemblyName(v)));
}
else
{
var e = new TangApplicationException("Config data is not in format of KeyValuePair: " + line);
Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
}
}
ProcessConfigData(conf, settings);
}
public static IDictionary<string, string> FromFile(string fileName)
{
IDictionary<string, string> property = new Dictionary<string, string>();
using (StreamReader sr = File.OpenText(fileName))
{
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
string[] p = line.Split('=');
property.Add(ConfigurationFile.GetAssemblyName(p[0]), ConfigurationFile.GetAssemblyName(p[1]));
}
}
return property;
}
public static void ProcessConfigData(IConfigurationBuilder conf, IList<KeyValuePair<string, string>> settings, string language)
{
foreach (KeyValuePair<string, string> kv in settings)
{
try
{
conf.Bind(kv.Key, kv.Value, language);
}
catch (BindException ex)
{
Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Caught(ex, Level.Error, LOGGER);
var e = new BindException("Failed to process configuration tuple: [" + kv.Key + "=" + kv.Value + "]", ex);
Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
}
catch (ClassHierarchyException ex)
{
Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Caught(ex, Level.Error, LOGGER);
var e = new ClassHierarchyException("Failed to process configuration tuple: [" + kv.Key + "=" + kv.Value + "]", ex);
Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
}
}
}
internal static void ProcessConfigData(IConfigurationBuilder conf, IList<KeyValuePair<string, string>> settings)
{
foreach (KeyValuePair<string, string> kv in settings)
{
try
{
conf.Bind(kv.Key, kv.Value);
}
catch (BindException ex)
{
Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Caught(ex, Level.Error, LOGGER);
var e = new BindException("Failed to process configuration tuple: [" + kv.Key + "=" + kv.Value + "]", ex);
Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
}
catch (ClassHierarchyException ex)
{
Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Caught(ex, Level.Error, LOGGER);
var e = new ClassHierarchyException("Failed to process configuration tuple: [" + kv.Key + "=" + kv.Value + "]", ex);
Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
}
}
}
public static void ProcessConfigData(IConfigurationBuilder conf, IDictionary<string, string> settings)
{
IList<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
foreach (KeyValuePair<string, string> kv in settings)
{
list.Add(kv);
}
ProcessConfigData(conf, list);
}
/**
* Replace any \'s in the input string with \\. and any "'s with \".
* @param in
* @return
*/
private static string Escape(string str)
{
return str; // TODO
// After regexp escaping \\\\ = 1 slash, \\\\\\\\ = 2 slashes.
// Also, the second args of replaceAll are neither strings nor regexps, and
// are instead a special DSL used by Matcher. Therefore, we need to double
// escape slashes (4 slashes) and quotes (3 slashes + ") in those strings.
// Since we need to write \\ and \", we end up with 8 and 7 slashes,
// respectively.
// return in.ReplaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\\\"");
}
public static StringBuilder Join(StringBuilder sb, string sep, IConstructorArg[] types)
{
if (types.Length > 0)
{
sb.Append(types[0].GetType());
for (int i = 1; i < types.Length; i++)
{
sb.Append(sep).Append(types[i].GetType());
}
}
return sb;
}
#endregion text file serialization
}
}