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
|
/*
*
* 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 System;
using System.Collections;
using System.Configuration;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Xml;
using log4net;
namespace Qpid.Common
{
/// <summary>
///
/// Mike Woodring
/// Bear Canyon Consulting LLC
/// http://www.bearcanyon.com
///
/// AssemblySettings usage:
///
/// If you know the keys you're after, the following is probably
/// the most convenient:
///
/// AssemblySettings settings = new AssemblySettings();
/// string someSetting1 = settings["someKey1"];
/// string someSetting2 = settings["someKey2"];
///
/// If you want to enumerate over the settings (or just as an
/// alternative approach), you can do this too:
///
/// IDictionary settings = AssemblySettings.GetConfig();
///
/// foreach( DictionaryEntry entry in settings )
/// {
/// // Use entry.Key or entry.Value as desired...
/// }
///
/// In either of the above two scenarios, the calling assembly
/// (the one that called the constructor or GetConfig) is used
/// to determine what file to parse and what the name of the
/// settings collection element is. For example, if the calling
/// assembly is c:\foo\bar\TestLib.dll, then the configuration file
/// that's parsed is c:\foo\bar\TestLib.dll.config, and the
/// configuration section that's parsed must be named <assemblySettings>.
///
/// To retrieve the configuration information for an arbitrary assembly,
/// use the overloaded constructor or GetConfig method that takes an
/// Assembly reference as input.
///
/// If your assembly is being automatically downloaded from a web
/// site by an "href-exe" (an application that's run directly from a link
/// on a web page), then the enclosed web.config shows the mechanism
/// for allowing the AssemblySettings library to download the
/// configuration files you're using for your assemblies (while not
/// allowing web.config itself to be downloaded).
///
/// If the assembly you are trying to use this with is installed in, and loaded
/// from, the GAC then you'll need to place the config file in the GAC directory where
/// the assembly is installed. On the first release of the CLR, this directory is
/// <windir>\assembly\gac\libName\verNum__pubKeyToken]]>. For example,
/// the assembly "SomeLib, Version=1.2.3.4, Culture=neutral, PublicKeyToken=abcd1234"
/// would be installed to the c:\winnt\assembly\gac\SomeLib\1.2.3.4__abcd1234 diretory
/// (assuming the OS is installed in c:\winnt). For future versions of the CLR, this
/// directory scheme may change, so you'll need to check the <code>CodeBase</code> property
/// of a GAC-loaded assembly in the debugger to determine the correct directory location.
///
/// </summary>
public class AssemblySettings
{
private static readonly ILog _log = LogManager.GetLogger(typeof(AssemblySettings));
private IDictionary settings;
[MethodImpl(MethodImplOptions.NoInlining)]
public AssemblySettings()
: this(Assembly.GetCallingAssembly())
{
}
public AssemblySettings(Assembly asm)
{
settings = GetConfig(asm);
}
public string this[string key]
{
get
{
string settingValue = null;
if (settings != null)
{
settingValue = settings[key] as string;
}
return (settingValue == null ? "" : settingValue);
}
}
public static IDictionary GetConfig()
{
return GetConfig(Assembly.GetCallingAssembly());
}
public static IDictionary GetConfig(Assembly asm)
{
// Open and parse configuration file for specified
// assembly, returning collection to caller for future
// use outside of this class.
string cfgFile = asm.CodeBase + ".config";
try
{
const string nodeName = "assemblySettings";
XmlDocument doc = new XmlDocument();
doc.Load(new XmlTextReader(cfgFile));
XmlNodeList nodes = doc.GetElementsByTagName(nodeName);
foreach (XmlNode node in nodes)
{
if (node.LocalName == nodeName)
{
DictionarySectionHandler handler = new DictionarySectionHandler();
return (IDictionary)handler.Create(null, null, node);
}
}
}
catch (FileNotFoundException)
{
_log.Warn("Assembly configuration file not found: " + cfgFile);
}
catch (Exception e)
{
_log.Warn("Failed to load .config file: " + cfgFile, e);
}
return null;
}
}
}
|