1 #region License Information
3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 * This file is part of HeuristicLab.
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.
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.
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/>.
23 using System
.Collections
.Generic
;
26 using System
.Reflection
;
27 using HeuristicLab
.PluginInfrastructure
.Manager
;
29 namespace HeuristicLab
.PluginInfrastructure
{
32 /// The SandboxApplicationManager provides properties to retrieve the list of available plugins and applications.
33 /// It also provides methods for type discovery and instantiation for types declared in plugins.
34 /// The SandboxApplicationManager is used in sandboxed Application Domains where permissions are restricted and
35 /// only partially-trusted code can be executed.
37 internal class SandboxApplicationManager
: MarshalByRefObject
, IApplicationManager
{
39 /// Fired when a plugin is loaded.
41 internal event EventHandler
<PluginInfrastructureEventArgs
> PluginLoaded
;
43 /// Fired when a plugin is unloaded (when the application terminates).
45 internal event EventHandler
<PluginInfrastructureEventArgs
> PluginUnloaded
;
47 // cache for the AssemblyResolveEvent
48 // which must be handled when assemblies are loaded dynamically after the application start
49 protected internal Dictionary
<string, Assembly
> loadedAssemblies
;
51 private List
<IPlugin
> loadedPlugins
;
53 private List
<PluginDescription
> plugins
;
57 public IEnumerable
<IPluginDescription
> Plugins
{
58 get { return plugins.Cast<IPluginDescription>(); }
61 private List
<ApplicationDescription
> applications
;
63 /// Gets all installed applications.
65 public IEnumerable
<IApplicationDescription
> Applications
{
66 get { return applications.Cast<IApplicationDescription>(); }
69 internal SandboxApplicationManager()
71 loadedAssemblies
= new Dictionary
<string, Assembly
>();
72 loadedPlugins
= new List
<IPlugin
>();
74 AppDomain
.CurrentDomain
.DomainUnload
+= CurrentDomain_DomainUnload
;
78 /// The sandbox application manager can also be used to load plugins even when no actual application is executed.
79 /// In such cases, plugins that have been loaded also have to be unloaded again before the application domain is unloaded.
81 private void CurrentDomain_DomainUnload(object sender
, EventArgs e
) {
86 /// Prepares the application domain for the execution of an HL application.
87 /// Pre-loads all <paramref name="plugins"/>.
89 /// <param name="apps">Enumerable of available HL applications.</param>
90 /// <param name="plugins">Enumerable of plugins that should be pre-loaded.</param>
91 internal void PrepareApplicationDomain(IEnumerable
<ApplicationDescription
> apps
, IEnumerable
<PluginDescription
> plugins
) {
92 this.plugins
= new List
<PluginDescription
>(plugins
);
93 this.applications
= new List
<ApplicationDescription
>(apps
);
94 ApplicationManager
.RegisterApplicationManager(this);
99 /// Loads the <paramref name="plugins"/> into this application domain.
101 /// <param name="plugins">Enumerable of plugins that should be loaded.</param>
102 private void LoadPlugins(IEnumerable
<PluginDescription
> plugins
) {
103 // load all loadable plugins (all dependencies available) into the execution context
104 foreach (var desc
in PluginDescriptionIterator
.IterateDependenciesBottomUp(plugins
.Where(x
=> x
.PluginState
!= PluginState
.Disabled
))) {
105 foreach (string fileName
in desc
.AssemblyLocations
) {
106 // load assembly reflection only first to get the full assembly name
107 var reflectionOnlyAssembly
= Assembly
.ReflectionOnlyLoadFrom(fileName
);
108 // load the assembly into execution context using full assembly name
109 var asm
= Assembly
.Load(reflectionOnlyAssembly
.FullName
);
110 RegisterLoadedAssembly(asm
);
111 // instantiate and load all plugins in this assembly
112 foreach (var plugin
in GetInstances
<IPlugin
>(asm
)) {
114 loadedPlugins
.Add(plugin
);
117 OnPluginLoaded(new PluginInfrastructureEventArgs(desc
));
123 /// Unloads the <paramref name="plugins"/> that were loaded into this application domain.
125 private void UnloadPlugins() {
126 // unload plugins in reverse order
127 foreach (var plugin
in loadedPlugins
.Reverse
<IPlugin
>()) {
130 loadedPlugins
.Clear(); // remove all plugins once unloaded
132 foreach (var desc
in PluginDescriptionIterator
.IterateDependenciesBottomUp(plugins
.Where(x
=> x
.PluginState
!= PluginState
.Disabled
))) {
134 OnPluginUnloaded(new PluginInfrastructureEventArgs(desc
));
136 plugins
.Clear(); // remove all plugin descriptions once unloaded
140 /// Runs the application declared in <paramref name="appInfo"/>.
141 /// This is a synchronous call. When the application is terminated all plugins are unloaded.
143 /// <param name="appInfo">Description of the application to run</param>
144 internal void Run(ApplicationDescription appInfo
, ICommandLineArgument
[] args
) {
145 IApplication runnablePlugin
= (IApplication
)Activator
.CreateInstance(appInfo
.DeclaringAssemblyName
, appInfo
.DeclaringTypeName
).Unwrap();
147 runnablePlugin
.Run(args
);
153 // register assembly in the assembly cache for the AssemblyResolveEvent
154 private void RegisterLoadedAssembly(Assembly asm
) {
155 if (loadedAssemblies
.ContainsKey(asm
.FullName
) || loadedAssemblies
.ContainsKey(asm
.GetName().Name
)) {
156 throw new ArgumentException("An assembly with the name " + asm
.GetName().Name
+ " has been registered already.", "asm");
158 loadedAssemblies
.Add(asm
.FullName
, asm
);
159 loadedAssemblies
.Add(asm
.GetName().Name
, asm
); // add short name
163 /// Creates an instance of all types that are subtypes or the same type of the specified type and declared in <paramref name="plugin"/>
165 /// <typeparam name="T">Most general type.</typeparam>
166 /// <returns>Enumerable of the created instances.</returns>
167 internal static IEnumerable
<T
> GetInstances
<T
>(IPluginDescription plugin
) where T
: class {
168 List
<T
> instances
= new List
<T
>();
169 foreach (Type t
in GetTypes(typeof(T
), plugin
, onlyInstantiable
: true, includeGenericTypeDefinitions
: false)) {
171 try { instance = (T)Activator.CreateInstance(t); }
173 if (instance
!= null) instances
.Add(instance
);
178 /// Creates an instance of all types declared in assembly <paramref name="asm"/> that are subtypes or the same type of the specified <typeparamref name="type"/>.
180 /// <typeparam name="T">Most general type.</typeparam>
181 /// <param name="asm">Declaring assembly.</param>
182 /// <returns>Enumerable of the created instances.</returns>
183 private static IEnumerable
<T
> GetInstances
<T
>(Assembly asm
) where T
: class {
184 List
<T
> instances
= new List
<T
>();
185 foreach (Type t
in GetTypes(typeof(T
), asm
, onlyInstantiable
: true, includeGenericTypeDefinitions
: false)) {
187 try { instance = (T)Activator.CreateInstance(t); }
189 if (instance
!= null) instances
.Add(instance
);
194 /// Creates an instance of all types that are subtypes or the same type of the specified type
196 /// <typeparam name="T">Most general type.</typeparam>
197 /// <returns>Enumerable of the created instances.</returns>
198 internal static IEnumerable
<T
> GetInstances
<T
>() where T
: class {
199 return from i
in GetInstances(typeof(T
))
204 /// Creates an instance of all types that are subtypes or the same type of the specified type
206 /// <param name="type">Most general type.</param>
207 /// <returns>Enumerable of the created instances.</returns>
208 internal static IEnumerable
<object> GetInstances(Type type
) {
209 List
<object> instances
= new List
<object>();
210 foreach (Type t
in GetTypes(type
, onlyInstantiable
: true, includeGenericTypeDefinitions
: false)) {
211 object instance
= null;
212 try { instance = Activator.CreateInstance(t); }
214 if (instance
!= null) instances
.Add(instance
);
220 /// Finds all types that are subtypes or equal to the specified type.
222 /// <param name="type">Most general type for which to find matching types.</param>
223 /// <param name="onlyInstantiable">Return only types that are instantiable
224 /// <param name="includeGenericTypeDefinitions">Specifies if generic type definitions shall be included</param>
225 /// (interfaces, abstract classes... are not returned)</param>
226 /// <returns>Enumerable of the discovered types.</returns>
227 internal static IEnumerable
<Type
> GetTypes(Type type
, bool onlyInstantiable
, bool includeGenericTypeDefinitions
) {
228 return from asm
in AppDomain
.CurrentDomain
.GetAssemblies()
229 where
!asm
.IsDynamic
&& !string.IsNullOrEmpty(asm
.Location
)
230 from t
in GetTypes(type
, asm
, onlyInstantiable
, includeGenericTypeDefinitions
)
234 internal static IEnumerable
<Type
> GetTypes(IEnumerable
<Type
> types
, bool onlyInstantiable
, bool includeGenericTypeDefinitions
, bool assignableToAllTypes
) {
235 IEnumerable
<Type
> result
= GetTypes(types
.First(), onlyInstantiable
, includeGenericTypeDefinitions
);
236 foreach (Type type
in types
.Skip(1)) {
237 IEnumerable
<Type
> discoveredTypes
= GetTypes(type
, onlyInstantiable
, includeGenericTypeDefinitions
);
238 if (assignableToAllTypes
) result
= result
.Intersect(discoveredTypes
);
239 else result
= result
.Union(discoveredTypes
);
245 /// Finds all types that are subtypes or equal to the specified type if they are part of the given
246 /// <paramref name="pluginDescription"/>.
248 /// <param name="type">Most general type for which to find matching types.</param>
249 /// <param name="pluginDescription">The plugin the subtypes must be part of.</param>
250 /// <param name="onlyInstantiable">Return only types that are instantiable
251 /// <param name="includeGenericTypeDefinitions">Specifies if generic type definitions shall be included</param>
252 /// (interfaces, abstract classes... are not returned)</param>
253 /// <returns>Enumerable of the discovered types.</returns>
254 internal static IEnumerable
<Type
> GetTypes(Type type
, IPluginDescription pluginDescription
, bool onlyInstantiable
, bool includeGenericTypeDefinitions
) {
255 PluginDescription pluginDesc
= (PluginDescription
)pluginDescription
;
256 return from asm
in AppDomain
.CurrentDomain
.GetAssemblies()
257 where
!asm
.IsDynamic
&& !string.IsNullOrEmpty(asm
.Location
)
258 where pluginDesc
.AssemblyLocations
.Any(location
=> location
.Equals(Path
.GetFullPath(asm
.Location
), StringComparison
.CurrentCultureIgnoreCase
))
259 from t
in GetTypes(type
, asm
, onlyInstantiable
, includeGenericTypeDefinitions
)
263 internal static IEnumerable
<Type
> GetTypes(IEnumerable
<Type
> types
, IPluginDescription pluginDescription
, bool onlyInstantiable
, bool includeGenericTypeDefinitions
, bool assignableToAllTypes
) {
264 IEnumerable
<Type
> result
= GetTypes(types
.First(), pluginDescription
, onlyInstantiable
, includeGenericTypeDefinitions
);
265 foreach (Type type
in types
.Skip(1)) {
266 IEnumerable
<Type
> discoveredTypes
= GetTypes(type
, pluginDescription
, onlyInstantiable
, includeGenericTypeDefinitions
);
267 if (assignableToAllTypes
) result
= result
.Intersect(discoveredTypes
);
268 else result
= result
.Union(discoveredTypes
);
274 /// Gets types that are assignable (same of subtype) to the specified type only from the given assembly.
276 /// <param name="type">Most general type we want to find.</param>
277 /// <param name="assembly">Assembly that should be searched for types.</param>
278 /// <param name="onlyInstantiable">Return only types that are instantiable
279 /// (interfaces, abstract classes... are not returned)</param>
280 /// <param name="includeGenericTypeDefinitions">Specifies if generic type definitions shall be included</param>
281 /// <returns>Enumerable of the discovered types.</returns>
282 internal static IEnumerable
<Type
> GetTypes(Type type
, Assembly assembly
, bool onlyInstantiable
, bool includeGenericTypeDefinitions
) {
283 var matchingTypes
= from assemblyType
in assembly
.GetTypes()
284 let t
= assemblyType
.BuildType(type
)
286 where t
.IsSubTypeOf(type
)
287 where
!t
.IsNonDiscoverableType()
288 where onlyInstantiable
== false || (!t
.IsAbstract
&& !t
.IsInterface
&& !t
.HasElementType
)
289 where includeGenericTypeDefinitions
|| !t
.IsGenericTypeDefinition
292 return matchingTypes
;
296 /// Discovers all types implementing or inheriting all or any type in <paramref name="types"/> (directly and indirectly) that are declared in the assembly <paramref name="assembly"/>.
298 /// <param name="types">The types to discover.</param>
299 /// <param name="assembly">The declaring assembly.</param>
300 /// <param name="onlyInstantiable">Return only types that are instantiable (instance, abstract... are not returned)</param>
301 /// /// <param name="assignableToAllTypes">Specifies if discovered types must implement or inherit all given <paramref name="types"/>.</param>
302 /// <returns>An enumerable of discovered types.</returns>
303 internal static IEnumerable
<Type
> GetTypes(IEnumerable
<Type
> types
, Assembly assembly
, bool onlyInstantiable
= true, bool includeGenericTypeDefinitions
= false, bool assignableToAllTypes
= true) {
304 IEnumerable
<Type
> result
= GetTypes(types
.First(), assembly
, onlyInstantiable
, includeGenericTypeDefinitions
);
305 foreach (Type type
in types
.Skip(1)) {
306 IEnumerable
<Type
> discoveredTypes
= GetTypes(type
, assembly
, onlyInstantiable
, includeGenericTypeDefinitions
);
307 if (assignableToAllTypes
) result
= result
.Intersect(discoveredTypes
);
308 else result
= result
.Union(discoveredTypes
);
313 private void OnPluginLoaded(PluginInfrastructureEventArgs e
) {
314 if (PluginLoaded
!= null) PluginLoaded(this, e
);
317 private void OnPluginUnloaded(PluginInfrastructureEventArgs e
) {
318 if (PluginUnloaded
!= null) PluginUnloaded(this, e
);
321 #region IApplicationManager Members
323 IEnumerable
<T
> IApplicationManager
.GetInstances
<T
>() {
324 return GetInstances
<T
>();
327 IEnumerable
<object> IApplicationManager
.GetInstances(Type type
) {
328 return GetInstances(type
);
331 IEnumerable
<Type
> IApplicationManager
.GetTypes(Type type
, bool onlyInstantiable
, bool includeGenericTypeDefinitions
) {
332 return GetTypes(type
, onlyInstantiable
, includeGenericTypeDefinitions
);
334 IEnumerable
<Type
> IApplicationManager
.GetTypes(IEnumerable
<Type
> types
, bool onlyInstantiable
, bool includeGenericTypeDefinitions
, bool assignableToAllTypes
) {
335 return GetTypes(types
, onlyInstantiable
, includeGenericTypeDefinitions
, assignableToAllTypes
);
338 IEnumerable
<Type
> IApplicationManager
.GetTypes(Type type
, IPluginDescription plugin
, bool onlyInstantiable
, bool includeGenericTypeDefinitions
) {
339 return GetTypes(type
, plugin
, onlyInstantiable
, includeGenericTypeDefinitions
);
341 IEnumerable
<Type
> IApplicationManager
.GetTypes(IEnumerable
<Type
> types
, IPluginDescription plugin
, bool onlyInstantiable
, bool includeGenericTypeDefinitions
, bool assignableToAllTypes
) {
342 return GetTypes(types
, plugin
, onlyInstantiable
, includeGenericTypeDefinitions
, assignableToAllTypes
);
345 IEnumerable
<Type
> IApplicationManager
.GetTypes(Type type
, Assembly assembly
, bool onlyInstantiable
, bool includeGenericTypeDefinitions
) {
346 return GetTypes(type
, assembly
, onlyInstantiable
, includeGenericTypeDefinitions
);
348 IEnumerable
<Type
> IApplicationManager
.GetTypes(IEnumerable
<Type
> types
, Assembly assembly
, bool onlyInstantiable
, bool includeGenericTypeDefinitions
, bool assignableToAllTypes
) {
349 return GetTypes(types
, assembly
, onlyInstantiable
, includeGenericTypeDefinitions
, assignableToAllTypes
);
353 /// Finds the plugin that declares the <paramref name="type">type</paramref>.
355 /// <param name="type">The type of interest.</param>
356 /// <returns>The description of the plugin that declares the given type or null if the type has not been declared by a known plugin.</returns>
357 public IPluginDescription
GetDeclaringPlugin(Type type
) {
358 if (type
== null) throw new ArgumentNullException("type");
359 foreach (PluginDescription info
in Plugins
) {
360 if (info
.AssemblyLocations
.Contains(Path
.GetFullPath(type
.Assembly
.Location
))) return info
;