-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathConfigurationFactory.cs
151 lines (137 loc) · 6.49 KB
/
ConfigurationFactory.cs
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
//-----------------------------------------------------------------------
// <copyright file="ConfigurationFactory.cs" company="Akka.NET Project">
// Copyright (C) 2009-2022 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2025 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using Akka.Configuration.Hocon;
using Newtonsoft.Json;
namespace Akka.Configuration
{
/// <summary>
/// This class contains methods used to retrieve configuration information
/// from a variety of sources including user-supplied strings, configuration
/// files and assembly resources.
/// </summary>
public class ConfigurationFactory
{
/// <summary>
/// Generates an empty configuration.
/// </summary>
public static Config Empty
{
get { return ParseString(""); }
}
/// <summary>
/// Generates a configuration defined in the supplied
/// HOCON (Human-Optimized Config Object Notation) string.
/// </summary>
/// <param name="hocon">A string that contains configuration options to use.</param>
/// <param name="includeCallback">callback used to resolve includes</param>
/// <returns>The configuration defined in the supplied HOCON string.</returns>
public static Config ParseString(string hocon, Func<string,HoconRoot> includeCallback)
{
HoconRoot res = Parser.Parse(hocon, includeCallback);
return new Config(res);
}
/// <summary>
/// Generates a configuration defined in the supplied
/// HOCON (Human-Optimized Config Object Notation) string.
/// </summary>
/// <param name="hocon">A string that contains configuration options to use.</param>
/// <returns>The configuration defined in the supplied HOCON string.</returns>
public static Config ParseString(string hocon)
{
//TODO: add default include resolver
return ParseString(hocon, null);
}
/// <summary>
/// Loads a configuration defined in the current application's
/// configuration file, e.g. app.config or web.config
/// </summary>
/// <returns>The configuration defined in the configuration file.</returns>
public static Config Load()
{
var section = (AkkaConfigurationSection)System.Configuration.ConfigurationManager.GetSection("akka") ?? new AkkaConfigurationSection();
return section.AkkaConfig;
}
/// <summary>
/// Retrieves the default configuration that Akka.NET uses
/// when no configuration has been defined.
/// </summary>
/// <returns>The configuration that contains default values for all options.</returns>
public static Config Default()
{
return FromResource("Akka.Configuration.akka.conf");
}
/// <summary>
/// Retrieves a configuration defined in a resource of the
/// current executing assembly.
/// </summary>
/// <param name="resourceName">The name of the resource that contains the configuration.</param>
/// <returns>The configuration defined in the current executing assembly.</returns>
internal static Config FromResource(string resourceName)
{
var assembly = typeof(ConfigurationFactory).Assembly;
return FromResource(resourceName, assembly);
}
/// <summary>
/// Retrieves a configuration defined in a resource of the
/// assembly containing the supplied instance object.
/// </summary>
/// <param name="resourceName">The name of the resource that contains the configuration.</param>
/// <param name="instanceInAssembly">An instance object located in the assembly to search.</param>
/// <returns>The configuration defined in the assembly that contains the instanced object.</returns>
public static Config FromResource(string resourceName, object instanceInAssembly)
{
if (instanceInAssembly is Type type)
return FromResource(resourceName, type.Assembly);
var assembly = instanceInAssembly as Assembly;
return FromResource(resourceName, assembly != null ? assembly : instanceInAssembly.GetType().Assembly);
}
/// <summary>
/// Retrieves a configuration defined in a resource of the assembly
/// containing the supplied type <typeparamref name="TAssembly"/>.
/// </summary>
/// <typeparam name="TAssembly">A type located in the assembly to search.</typeparam>
/// <param name="resourceName">The name of the resource that contains the configuration.</param>
/// <returns>The configuration defined in the assembly that contains the type <typeparamref name="TAssembly"/>.</returns>
public static Config FromResource<TAssembly>(string resourceName)
{
return FromResource(resourceName, typeof(TAssembly).Assembly);
}
/// <summary>
/// Retrieves a configuration defined in a resource of the supplied assembly.
/// </summary>
/// <param name="resourceName">The name of the resource that contains the configuration.</param>
/// <param name="assembly">The assembly that contains the given resource.</param>
/// <returns>The configuration defined in the assembly that contains the given resource.</returns>
public static Config FromResource(string resourceName, Assembly assembly)
{
using(Stream stream = assembly.GetManifestResourceStream(resourceName))
{
Debug.Assert(stream != null, "stream != null");
using (var reader = new StreamReader(stream))
{
string result = reader.ReadToEnd();
return ParseString(result);
}
}
}
/// <summary>
/// Creates a configuration based on the supplied source object
/// </summary>
/// <param name="source">The source object</param>
/// <returns>The configuration created from the source object</returns>
public static Config FromObject(object source)
{
var json = JsonConvert.SerializeObject(source);
var config = ParseString(json);
return config;
}
}
}