forked from dotnet/orleans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SiloAssemblyLoader.cs
198 lines (168 loc) · 8.24 KB
/
SiloAssemblyLoader.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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Orleans.CodeGeneration;
using Orleans.Providers;
using Orleans.Runtime.Configuration;
namespace Orleans.Runtime
{
internal class SiloAssemblyLoader
{
private readonly List<string> excludedGrains;
private readonly LoggerImpl logger = LogManager.GetLogger("AssemblyLoader.Silo");
private List<string> discoveredAssemblyLocations;
private Dictionary<string, SearchOption> directories;
public SiloAssemblyLoader(NodeConfiguration nodeConfig)
: this(nodeConfig.AdditionalAssemblyDirectories, nodeConfig.ExcludedGrainTypes)
{
}
public SiloAssemblyLoader(IDictionary<string, SearchOption> additionalDirectories, IEnumerable<string> excludedGrains = null)
{
this.excludedGrains = excludedGrains != null
? new List<string>(excludedGrains)
: new List<string>();
var exeRoot = Path.GetDirectoryName(typeof(SiloAssemblyLoader).GetTypeInfo().Assembly.Location);
var appRoot = Path.Combine(exeRoot, "Applications");
var cwd = Directory.GetCurrentDirectory();
directories = new Dictionary<string, SearchOption>
{
{ exeRoot, SearchOption.TopDirectoryOnly },
{ appRoot, SearchOption.AllDirectories }
};
foreach (var kvp in additionalDirectories)
{
// Make sure the path is clean (get rid of ..\'s)
directories[new DirectoryInfo(kvp.Key).FullName] = kvp.Value;
}
if (!directories.ContainsKey(cwd))
{
directories.Add(cwd, SearchOption.TopDirectoryOnly);
}
LoadApplicationAssemblies();
}
private void LoadApplicationAssemblies()
{
#if !NETSTANDARD_TODO
AssemblyLoaderPathNameCriterion[] excludeCriteria =
{
AssemblyLoaderCriteria.ExcludeResourceAssemblies,
AssemblyLoaderCriteria.ExcludeSystemBinaries()
};
AssemblyLoaderReflectionCriterion[] loadCriteria =
{
AssemblyLoaderReflectionCriterion.NewCriterion(
TypeUtils.IsConcreteGrainClass,
"Assembly does not contain any acceptable grain types."),
AssemblyLoaderCriteria.LoadTypesAssignableFrom(
typeof(IProvider))
};
discoveredAssemblyLocations = AssemblyLoader.LoadAssemblies(directories, excludeCriteria, loadCriteria, logger);
#endif
}
public IDictionary<string, GrainTypeData> GetGrainClassTypes(bool strict)
{
var result = new Dictionary<string, GrainTypeData>();
Type[] grainTypes = strict
? TypeUtils.GetTypes(TypeUtils.IsConcreteGrainClass, logger).ToArray()
: TypeUtils.GetTypes(discoveredAssemblyLocations, TypeUtils.IsConcreteGrainClass, logger).ToArray();
foreach (var grainType in grainTypes)
{
var className = TypeUtils.GetFullName(grainType);
if (excludedGrains.Contains(className))
continue;
if (result.ContainsKey(className))
throw new InvalidOperationException(
string.Format("Precondition violated: GetLoadedGrainTypes should not return a duplicate type ({0})", className));
Type grainStateType = null;
// check if grainType derives from Grain<T> where T is a concrete class
var parentType = grainType.GetTypeInfo().BaseType;
while (parentType != typeof(Grain) && parentType != typeof(object))
{
TypeInfo parentTypeInfo = parentType.GetTypeInfo();
if (parentTypeInfo.IsGenericType)
{
var definition = parentTypeInfo.GetGenericTypeDefinition();
if (definition == typeof(Grain<>))
{
var stateArg = parentType.GetGenericArguments()[0];
if (stateArg.GetTypeInfo().IsClass || stateArg.GetTypeInfo().IsValueType)
{
grainStateType = stateArg;
break;
}
}
}
parentType = parentTypeInfo.BaseType;
}
GrainTypeData typeData = GetTypeData(grainType, grainStateType);
result.Add(className, typeData);
}
LogGrainTypesFound(logger, result);
return result;
}
public IEnumerable<KeyValuePair<int, Type>> GetGrainMethodInvokerTypes(bool strict)
{
var result = new Dictionary<int, Type>();
Type[] types = strict
? TypeUtils.GetTypes(TypeUtils.IsGrainMethodInvokerType, logger).ToArray()
: TypeUtils.GetTypes(discoveredAssemblyLocations, TypeUtils.IsGrainMethodInvokerType, logger).ToArray();
foreach (var type in types)
{
var attrib = type.GetTypeInfo().GetCustomAttribute<MethodInvokerAttribute>(true);
int ifaceId = attrib.InterfaceId;
if (result.ContainsKey(ifaceId))
throw new InvalidOperationException(string.Format("Grain method invoker classes {0} and {1} use the same interface id {2}", result[ifaceId].FullName, type.FullName, ifaceId));
result[ifaceId] = type;
}
return result;
}
/// <summary>
/// Get type data for the given grain type
/// </summary>
private static GrainTypeData GetTypeData(Type grainType, Type stateObjectType)
{
return grainType.GetTypeInfo().IsGenericTypeDefinition ?
new GenericGrainTypeData(grainType, stateObjectType) :
new GrainTypeData(grainType, stateObjectType);
}
private static void LogGrainTypesFound(LoggerImpl logger, Dictionary<string, GrainTypeData> grainTypeData)
{
var sb = new StringBuilder();
sb.AppendLine(String.Format("Loaded grain type summary for {0} types: ", grainTypeData.Count));
foreach (var grainType in grainTypeData.Values.OrderBy(gtd => gtd.Type.FullName))
{
// Skip system targets and Orleans grains
var assemblyName = grainType.Type.GetTypeInfo().Assembly.FullName.Split(',')[0];
if (!typeof(ISystemTarget).IsAssignableFrom(grainType.Type))
{
int grainClassTypeCode = CodeGeneration.GrainInterfaceUtils.GetGrainClassTypeCode(grainType.Type);
sb.AppendFormat("Grain class {0}.{1} [{2} (0x{3})] from {4}.dll implementing interfaces: ",
grainType.Type.Namespace,
TypeUtils.GetTemplatedName(grainType.Type),
grainClassTypeCode,
grainClassTypeCode.ToString("X"),
assemblyName);
var first = true;
foreach (var iface in grainType.RemoteInterfaceTypes)
{
if (!first)
sb.Append(", ");
sb.Append(iface.Namespace).Append(".").Append(TypeUtils.GetTemplatedName(iface));
if (CodeGeneration.GrainInterfaceUtils.IsGrainType(iface))
{
int ifaceTypeCode = CodeGeneration.GrainInterfaceUtils.GetGrainInterfaceId(iface);
sb.AppendFormat(" [{0} (0x{1})]", ifaceTypeCode, ifaceTypeCode.ToString("X"));
}
first = false;
}
sb.AppendLine();
}
}
var report = sb.ToString();
logger.LogWithoutBulkingAndTruncating(Severity.Info, ErrorCode.Loader_GrainTypeFullList, report);
}
}
}