Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.PluginInfrastructure/LightweightApplicationManager.cs @ 4593

Last change on this file since 4593 was 3247, checked in by gkronber, 15 years ago

Implemented a LightweightApplicationManager for unit testing and extracted the DefaultApplicationManager out of class ApplicationManager. #954 (ApplicationManager.Manager should have a default instance to enable unit testing)

File size: 6.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
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.
11 *
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.
16 *
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/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Security.Policy;
25using System.Reflection;
26using System.Diagnostics;
27using System.Security.Permissions;
28using System.Security;
29using System.Linq;
30using HeuristicLab.PluginInfrastructure.Manager;
31using System.IO;
32
33namespace HeuristicLab.PluginInfrastructure {
34
35  /// <summary>
36  /// Lightweight application manager is set as the application manager as long as the plugin infrastructure is uninitialized.
37  /// The list of plugins and applications is empty. The default application manager is necessary to provide the type discovery
38  /// functionality in unit tests.
39  /// </summary>
40  internal sealed class LightweightApplicationManager : IApplicationManager {
41
42    #region IApplicationManager Members
43    /// <summary>
44    /// Gets an empty list of plugins. (LightweightApplicationManager doesn't support plugin discovery)
45    /// </summary>
46    public IEnumerable<IPluginDescription> Plugins {
47      get { return new IPluginDescription[0]; }
48    }
49
50    /// <summary>
51    /// Gets an empty list of applications. (LightweightApplicationManager doesn't support application discovery)
52    /// </summary>
53    public IEnumerable<IApplicationDescription> Applications {
54      get { return new IApplicationDescription[0]; }
55    }
56
57    /// <summary>
58    /// Creates an instance of all types that are subtypes or the same type of the specified type
59    /// </summary>
60    /// <typeparam name="T">Most general type.</typeparam>
61    /// <returns>Enumerable of the created instances.</returns>
62    public IEnumerable<T> GetInstances<T>() where T : class {
63      return GetInstances(typeof(T)).Cast<T>();
64    }
65
66    /// <summary>
67    /// Creates an instance of all types that are subtypes or the same type of the specified type
68    /// </summary>
69    /// <param name="type">Most general type.</param>
70    /// <returns>Enumerable of the created instances.</returns>
71    public IEnumerable<object> GetInstances(Type type) {
72      return from t in GetTypes(type, true)
73             select Activator.CreateInstance(t);
74    }
75
76
77    /// <summary>
78    /// Finds all instantiable types that are subtypes or equal to the specified type.
79    /// </summary>
80    /// <param name="type">Most general type for which to find matching types.</param>
81    /// <remarks>Return only types that are instantiable
82    /// (interfaces, abstract classes... are not returned)</remarks>
83    /// <returns>Enumerable of the discovered types.</returns>
84    public IEnumerable<Type> GetTypes(Type type) {
85      return GetTypes(type, true);
86    }
87
88    /// <summary>
89    /// Finds all types that are subtypes or equal to the specified type.
90    /// </summary>
91    /// <param name="type">Most general type for which to find matching types.</param>
92    /// <param name="onlyInstantiable">Return only types that are instantiable
93    /// (interfaces, abstract classes... are not returned)</param>
94    /// <returns>Enumerable of the discovered types.</returns>
95    public IEnumerable<Type> GetTypes(Type type, bool onlyInstantiable) {
96      return from asm in AppDomain.CurrentDomain.GetAssemblies()
97             from t in GetTypes(type, asm, onlyInstantiable)
98             select t;
99    }
100
101    /// <summary>
102    /// Gets types that are assignable (same of subtype) to the specified type only from the given assembly.
103    /// </summary>
104    /// <param name="type">Most general type we want to find.</param>
105    /// <param name="assembly">Assembly that should be searched for types.</param>
106    /// <param name="onlyInstantiable">Return only types that are instantiable
107    /// (interfaces, abstract classes...  are not returned)</param>
108    /// <returns>Enumerable of the discovered types.</returns>
109    private static IEnumerable<Type> GetTypes(Type type, Assembly assembly, bool onlyInstantiable) {
110      return from t in assembly.GetTypes()
111             where type.IsAssignableFrom(t)
112             where onlyInstantiable == false || (!t.IsAbstract && !t.IsInterface && !t.HasElementType)
113             select t;
114    }
115
116    /// <summary>
117    /// Not supported by the LightweightApplicationManager
118    /// </summary>
119    /// <param name="type"></param>
120    /// <param name="plugin"></param>
121    /// <returns></returns>
122    /// <throws>NotSupportedException</throws>
123    public IEnumerable<Type> GetTypes(Type type, IPluginDescription plugin) {
124      throw new NotSupportedException("LightweightApplicationManager doesn't support type discovery for plugins.");
125    }
126
127    /// <summary>
128    /// Not supported by the LightweightApplicationManager
129    /// </summary>
130    /// <param name="type"></param>
131    /// <param name="plugin"></param>
132    /// <param name="onlyInstantiable"></param>
133    /// <returns></returns>
134    /// <throws>NotSupportedException</throws>
135    public IEnumerable<Type> GetTypes(Type type, IPluginDescription plugin, bool onlyInstantiable) {
136      throw new NotSupportedException("LightweightApplicationManager doesn't support type discovery for plugins.");
137    }
138
139    /// <summary>
140    /// Not supported by the LightweightApplicationManager
141    /// </summary>
142    /// <param name="type"></param>
143    /// <returns></returns>
144    /// <throws>NotSupportedException</throws>
145    public IPluginDescription GetDeclaringPlugin(Type type) {
146      throw new NotSupportedException("LightweightApplicationManager doesn't support type discovery for plugins.");
147    }
148
149    #endregion
150  }
151}
Note: See TracBrowser for help on using the repository browser.