Changeset 2504 for branches/PluginInfrastructure Refactoring/HeuristicLab.PluginInfrastructure/Manager
- Timestamp:
- 11/18/09 18:33:30 (15 years ago)
- Location:
- branches/PluginInfrastructure Refactoring/HeuristicLab.PluginInfrastructure/Manager
- Files:
-
- 6 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/PluginInfrastructure Refactoring/HeuristicLab.PluginInfrastructure/Manager/ApplicationDescription.cs
r2503 r2504 29 29 /// </summary> 30 30 [Serializable] 31 publicclass ApplicationDescription : IApplicationDescription {31 internal sealed class ApplicationDescription : IApplicationDescription { 32 32 private string name; 33 33 … … 44 44 /// Gets or sets the version of the application. 45 45 /// </summary> 46 publicVersion Version {46 internal Version Version { 47 47 get { return version; } 48 48 set { version = value; } … … 62 62 /// Gets or sets the boolean flag if the application should be automatically restarted. 63 63 /// </summary> 64 publicbool AutoRestart {64 internal bool AutoRestart { 65 65 get { return autoRestart; } 66 66 set { autoRestart = value; } … … 71 71 /// Gets or sets the name of the assembly that contains the IApplication type. 72 72 /// </summary> 73 publicstring DeclaringAssemblyName {73 internal string DeclaringAssemblyName { 74 74 get { return declaringAssemblyName; } 75 75 set { declaringAssemblyName = value; } … … 80 80 /// Gets or sets the name of the type that implements the interface IApplication. 81 81 /// </summary> 82 publicstring DeclaringTypeName {82 internal string DeclaringTypeName { 83 83 get { return declaringTypeName; } 84 84 set { declaringTypeName = value; } -
branches/PluginInfrastructure Refactoring/HeuristicLab.PluginInfrastructure/Manager/DefaultApplicationManager.cs
r2503 r2504 32 32 namespace HeuristicLab.PluginInfrastructure.Manager { 33 33 34 publicsealed class DefaultApplicationManager : MarshalByRefObject, IApplicationManager {34 internal sealed class DefaultApplicationManager : MarshalByRefObject, IApplicationManager { 35 35 internal event EventHandler<PluginInfrastructureEventArgs> PluginLoaded; 36 36 internal event EventHandler<PluginInfrastructureEventArgs> PluginUnloaded; … … 42 42 private List<IPlugin> loadedPlugins; 43 43 44 private List< IPluginDescription> plugins;44 private List<PluginDescription> plugins; 45 45 /// <summary> 46 46 /// Gets all plugins. 47 47 /// </summary> 48 48 public IEnumerable<IPluginDescription> Plugins { 49 get { return plugins; } 50 internal set { plugins = new List<IPluginDescription>(value); } 51 } 52 53 private List<IApplicationDescription> applications; 49 get { return plugins.Cast<IPluginDescription>(); } 50 } 51 52 private List<ApplicationDescription> applications; 54 53 /// <summary> 55 54 /// Gets all installed applications. 56 55 /// </summary> 57 56 public IEnumerable<IApplicationDescription> Applications { 58 get { return applications ; }57 get { return applications.Cast<IApplicationDescription>(); } 59 58 } 60 59 … … 72 71 } 73 72 74 internal void PrepareApplicationDomain(IEnumerable< IApplicationDescription> apps, IEnumerable<IPluginDescription> plugins) {75 this.plugins = new List< IPluginDescription>(plugins);76 this.applications = new List< IApplicationDescription>(apps);73 internal void PrepareApplicationDomain(IEnumerable<ApplicationDescription> apps, IEnumerable<PluginDescription> plugins) { 74 this.plugins = new List<PluginDescription>(plugins); 75 this.applications = new List<ApplicationDescription>(apps); 77 76 PluginInfrastructure.ApplicationManager.RegisterApplicationManager(this); 78 77 LoadPlugins(plugins); 79 78 } 80 79 81 private void LoadPlugins(IEnumerable< IPluginDescription> plugins) {80 private void LoadPlugins(IEnumerable<PluginDescription> plugins) { 82 81 // load all loadable plugins (all dependencies available) into the execution context 83 82 foreach (var desc in PluginDescriptionIterator.IterateInDependencyOrder(plugins.Where(x => x.PluginState != PluginState.Disabled))) { … … 96 95 } 97 96 98 internal void Run( IApplicationDescription appInfo) {97 internal void Run(ApplicationDescription appInfo) { 99 98 IApplication runnablePlugin = (IApplication)Activator.CreateInstance(appInfo.DeclaringAssemblyName, appInfo.DeclaringTypeName).Unwrap(); 100 99 try { 101 100 runnablePlugin.Run(); 102 }103 catch (Exception e) {104 throw new Exception(String.Format(105 "Unexpected exception caught: \"{0}\"\r\n" +106 "Type: {1}\r\n" +107 "Plugin {2}:\r\n{3}",108 e.Message,109 e.GetType().FullName,110 appInfo.Name,111 e.ToString()));112 101 } 113 102 finally { … … 156 145 /// <param name="asm">Declaring assembly.</param> 157 146 /// <returns>Enumerable of the created instances.</returns> 158 p ublic IEnumerable<T> GetInstances<T>(Assembly asm) where T : class {147 private static IEnumerable<T> GetInstances<T>(Assembly asm) where T : class { 159 148 return from t in GetTypes(typeof(T), asm) 160 149 where !t.IsAbstract && !t.IsInterface && !t.HasElementType … … 201 190 /// <returns>Enumerable of the discovered types.</returns> 202 191 public IEnumerable<Type> GetTypes(Type type, IPluginDescription pluginDescription) { 192 PluginDescription pluginDesc = (PluginDescription)pluginDescription; 203 193 return from asm in AppDomain.CurrentDomain.GetAssemblies() 204 where pluginDesc ription.Assemblies.Any(asmPath => Path.GetFullPath(asmPath) == Path.GetFullPath(asm.Location))194 where pluginDesc.Assemblies.Any(asmPath => Path.GetFullPath(asmPath) == Path.GetFullPath(asm.Location)) 205 195 from t in GetTypes(type, asm) 206 196 select t; … … 213 203 /// <param name="assembly">Assembly that should be searched for types.</param> 214 204 /// <returns>Enumerable of the discovered types.</returns> 215 private IEnumerable<Type> GetTypes(Type type, Assembly assembly) {205 private static IEnumerable<Type> GetTypes(Type type, Assembly assembly) { 216 206 return from t in assembly.GetTypes() 217 207 where type.IsAssignableFrom(t) -
branches/PluginInfrastructure Refactoring/HeuristicLab.PluginInfrastructure/Manager/PluginDescription.cs
r2503 r2504 30 30 /// </summary> 31 31 [Serializable] 32 public class PluginDescription : IPluginDescription { 32 internal sealed class PluginDescription : IPluginDescription { 33 private int nTimesLoaded; 34 33 35 private string name; 34 36 /// <summary> … … 37 39 public string Name { 38 40 get { return name; } 39 set { name = value; }41 internal set { name = value; } 40 42 } 41 43 private Version version; … … 43 45 /// Gets or sets the version of the plugin. 44 46 /// </summary> 45 publicVersion Version {47 internal Version Version { 46 48 get { return version; } 47 49 set { version = value; } … … 51 53 /// Gets or sets the build date of the plugin. 52 54 /// </summary> 53 publicDateTime BuildDate {55 internal DateTime BuildDate { 54 56 get { return buildDate; } 55 57 set { buildDate = value; } … … 64 66 } 65 67 66 private int nTimesLoaded;67 public void Disable() {68 if (pluginState != PluginState.Undefined)69 throw new InvalidOperationException("Can't disabled a plugin in state " + pluginState);70 pluginState = PluginState.Disabled;71 }72 73 public void Enable() {74 if (pluginState != PluginState.Undefined)75 throw new InvalidOperationException("Can't enabled a plugin in state " + pluginState);76 pluginState = PluginState.Enabled;77 }78 79 public void Load() {80 if (!(pluginState == PluginState.Enabled || pluginState == PluginState.Loaded))81 throw new InvalidOperationException("Can't loaded a plugin in state " + pluginState);82 pluginState = PluginState.Loaded;83 nTimesLoaded++;84 }85 86 public void Unload() {87 if (pluginState != PluginState.Loaded)88 throw new InvalidOperationException("Can't unload a plugin in state " + pluginState);89 nTimesLoaded--;90 if (nTimesLoaded == 0) pluginState = PluginState.Enabled;91 }92 68 93 69 private List<string> files = new List<string>(); … … 96 72 /// These files are deleted when the plugin is removed or updated. 97 73 /// </summary> 98 publicIEnumerable<string> Files {74 internal IEnumerable<string> Files { 99 75 get { return files; } 100 76 } … … 104 80 } 105 81 106 private List< IPluginDescription> dependencies = new List<IPluginDescription>();82 private List<PluginDescription> dependencies = new List<PluginDescription>(); 107 83 /// <summary> 108 84 /// Gets all dependencies of the plugin. 109 85 /// </summary> 110 public IEnumerable<IPluginDescription> Dependencies {86 internal IEnumerable<PluginDescription> Dependencies { 111 87 get { return dependencies; } 112 88 } 113 89 114 publicvoid AddDependency(PluginDescription dependency) {90 internal void AddDependency(PluginDescription dependency) { 115 91 dependencies.Add(dependency); 116 92 } … … 121 97 /// Gets the names of the assemblies that belong to this plugin. 122 98 /// </summary> 123 publicIEnumerable<string> Assemblies {99 internal IEnumerable<string> Assemblies { 124 100 get { return assemblies; } 125 101 // set { assemblies = value; } … … 130 106 } 131 107 132 public PluginDescription() { 133 nTimesLoaded = 0; 108 internal PluginDescription() { 134 109 pluginState = PluginState.Undefined; 135 110 } 111 112 internal void Disable() { 113 if (pluginState != PluginState.Undefined) 114 throw new InvalidOperationException("Can't disabled a plugin in state " + pluginState); 115 pluginState = PluginState.Disabled; 116 } 117 118 internal void Enable() { 119 if (pluginState != PluginState.Undefined) 120 throw new InvalidOperationException("Can't enabled a plugin in state " + pluginState); 121 pluginState = PluginState.Enabled; 122 } 123 124 internal void Load() { 125 if (!(pluginState == PluginState.Enabled || pluginState == PluginState.Loaded)) 126 throw new InvalidOperationException("Can't loaded a plugin in state " + pluginState); 127 pluginState = PluginState.Loaded; 128 nTimesLoaded++; 129 } 130 131 internal void Unload() { 132 if (pluginState != PluginState.Loaded) 133 throw new InvalidOperationException("Can't unload a plugin in state " + pluginState); 134 nTimesLoaded--; 135 if (nTimesLoaded == 0) pluginState = PluginState.Enabled; 136 } 137 136 138 137 139 /// <summary> … … 151 153 /// <returns><c>true</c> if it is equal, <c>false</c> otherwise.</returns> 152 154 public override bool Equals(object obj) { 153 if (!(obj is PluginDescription)) 154 return false; 155 PluginDescription other = (PluginDescription)obj; 155 PluginDescription other = obj as PluginDescription; 156 if (other == null) return false; 156 157 157 158 return other.Name == this.Name && other.Version == this.Version; -
branches/PluginInfrastructure Refactoring/HeuristicLab.PluginInfrastructure/Manager/PluginInfrastructureEventArgs.cs
r2503 r2504 27 27 // to be replaced by GenericEventArgs 28 28 [Serializable] 29 publicclass PluginInfrastructureEventArgs : EventArgs {30 publicstring Action { get; private set; }31 publicobject Entity { get; private set; }32 publicPluginInfrastructureEventArgs(string action, object entity) {29 internal sealed class PluginInfrastructureEventArgs : EventArgs { 30 internal string Action { get; private set; } 31 internal object Entity { get; private set; } 32 internal PluginInfrastructureEventArgs(string action, object entity) { 33 33 this.Action = action; 34 34 this.Entity = entity; -
branches/PluginInfrastructure Refactoring/HeuristicLab.PluginInfrastructure/Manager/PluginManager.cs
r2503 r2504 35 35 /// Class to manage different plugins. 36 36 /// </summary> 37 publicsealed class PluginManager : MarshalByRefObject {37 internal sealed class PluginManager : MarshalByRefObject { 38 38 /// <summary> 39 39 /// Event handler for actions in the plugin manager. 40 40 /// </summary> 41 publicevent EventHandler<PluginInfrastructureEventArgs> Action;41 internal event EventHandler<PluginInfrastructureEventArgs> Action; 42 42 43 43 private string pluginDir; … … 49 49 /// Gets all installed applications. 50 50 /// </summary> 51 publicIEnumerable<ApplicationDescription> Applications {51 internal IEnumerable<ApplicationDescription> Applications { 52 52 get { return applications; } 53 53 } … … 56 56 private bool initialized; 57 57 58 publicPluginManager(string pluginDir) {58 internal PluginManager(string pluginDir) { 59 59 this.pluginDir = pluginDir; 60 60 plugins = new List<PluginDescription>(); … … 63 63 } 64 64 65 publicvoid Reset() {65 internal void Reset() { 66 66 initialized = false; 67 67 if (plugins != null && plugins.Any(x => x.PluginState == PluginState.Loaded)) throw new InvalidOperationException("Reset() is not allowed while applications are active."); … … 73 73 /// Determines installed plugins and checks if all plugins are loadable. 74 74 /// </summary> 75 publicvoid DiscoverAndCheckPlugins() {75 internal void DiscoverAndCheckPlugins() { 76 76 OnAction(new PluginInfrastructureEventArgs("Initializing", "PluginInfrastructure")); 77 77 AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation; … … 80 80 try { 81 81 pluginDomain = AppDomain.CreateDomain("plugin domain", null, setup); 82 PluginValidator remoteValidator = (PluginValidator)pluginDomain.CreateInstanceAndUnwrap("HeuristicLab.PluginInfraStructure.Manager", "HeuristicLab.PluginInfrastructure.Manager.Loader"); 82 Type pluginValidatorType = typeof(PluginValidator); 83 PluginValidator remoteValidator = (PluginValidator)pluginDomain.CreateInstanceAndUnwrap(pluginValidatorType.Assembly.FullName, pluginValidatorType.FullName, true, BindingFlags.NonPublic | BindingFlags.Instance, null, null, null, null, null); 83 84 remoteValidator.PluginDir = pluginDir; 84 85 // forward all events from the remoteValidator to listeners … … 109 110 /// </summary> 110 111 /// <param name="appInfo">application to run</param> 111 publicvoid Run(ApplicationDescription appInfo) {112 internal void Run(ApplicationDescription appInfo) { 112 113 if (!initialized) throw new InvalidOperationException("PluginManager is not initialized. DiscoverAndCheckPlugins() must be called before Run()"); 113 114 // create a separate AppDomain for the application … … 121 122 setup.PrivateBinPath = pluginDir; 122 123 applicationDomain = AppDomain.CreateDomain(appInfo.Name, null, setup); 124 Type applicationManagerType = typeof(DefaultApplicationManager); 123 125 DefaultApplicationManager applicationManager = 124 (DefaultApplicationManager)applicationDomain.CreateInstanceAndUnwrap( "HeuristicLab.PluginInfraStructure.Manager", "HeuristicLab.PluginInfrastructure.Manager.DefaultApplicationManager");126 (DefaultApplicationManager)applicationDomain.CreateInstanceAndUnwrap(applicationManagerType.Assembly.FullName, applicationManagerType.FullName, true, BindingFlags.NonPublic | BindingFlags.Instance, null, null, null, null, null); 125 127 applicationManager.PluginLoaded += applicationManager_PluginLoaded; 126 128 applicationManager.PluginUnloaded += applicationManager_PluginUnloaded; 127 applicationManager.PrepareApplicationDomain( 128 new List<IApplicationDescription>(applications.Cast<IApplicationDescription>()), 129 new List<IPluginDescription>(plugins.Cast<IPluginDescription>())); 129 applicationManager.PrepareApplicationDomain(applications, plugins); 130 130 OnAction(new PluginInfrastructureEventArgs("Started application", appInfo)); 131 131 applicationManager.Run(appInfo); -
branches/PluginInfrastructure Refactoring/HeuristicLab.PluginInfrastructure/Manager/PluginValidator.cs
r2503 r2504 57 57 } 58 58 59 publicstring PluginDir { get; set; }60 61 publicPluginValidator() {59 internal string PluginDir { get; set; } 60 61 internal PluginValidator() { 62 62 this.pluginDependencies = new Dictionary<PluginDescription, List<string>>(); 63 63 … … 114 114 115 115 foreach (IApplication application in GetApplications()) { 116 Type appType = application.GetType(); 117 ApplicationAttribute attr = (from x in appType.GetCustomAttributes(typeof(ApplicationAttribute), false) 118 select (ApplicationAttribute)x).Single(); 116 119 ApplicationDescription info = new ApplicationDescription(); 117 120 info.Name = application.Name; 118 info.Version = app lication.Version;121 info.Version = appType.Assembly.GetName().Version; 119 122 info.Description = application.Description; 120 info.AutoRestart = a pplication.RestartOnErrors;121 info.DeclaringAssemblyName = app lication.GetType().Assembly.GetName().Name;122 info.DeclaringTypeName = app lication.GetType().Namespace + "." + application.GetType().Name;123 info.AutoRestart = attr.RestartOnErrors; 124 info.DeclaringAssemblyName = appType.Assembly.GetName().Name; 125 info.DeclaringTypeName = appType.Namespace + "." + application.GetType().Name; 123 126 124 127 applications.Add(info); … … 126 129 } 127 130 128 private IEnumerable<IApplication> GetApplications() {131 private static IEnumerable<IApplication> GetApplications() { 129 132 return from asm in AppDomain.CurrentDomain.GetAssemblies() 130 133 from t in asm.GetTypes() … … 206 209 } 207 210 211 var buildDates = from attr in CustomAttributeData.GetCustomAttributes(pluginType.Assembly) 212 where IsAttributeDataForType(attr, typeof(AssemblyBuildDateAttribute)) 213 select (string)attr.ConstructorArguments[0].Value; 214 208 215 // minimal sanity check of the attribute values 209 216 if (!string.IsNullOrEmpty(pluginName) && 210 217 pluginFiles.Count > 0 && 211 pluginAssemblies.Count > 0) { 218 pluginAssemblies.Count > 0 && 219 buildDates.Count() == 1) { 212 220 // create a temporary PluginDescription that contains the attribute values 213 221 PluginDescription info = new PluginDescription(); 214 222 info.Name = pluginName; 215 223 info.Version = pluginType.Assembly.GetName().Version; 224 info.BuildDate = DateTime.Parse(buildDates.Single(), System.Globalization.CultureInfo.InvariantCulture); 216 225 info.AddAssemblies(pluginAssemblies); 217 226 info.AddFiles(pluginFiles); … … 224 233 } 225 234 226 private bool IsAttributeDataForType(CustomAttributeData attributeData, Type attributeType) {235 private static bool IsAttributeDataForType(CustomAttributeData attributeData, Type attributeType) { 227 236 return attributeData.Constructor.DeclaringType.AssemblyQualifiedName == attributeType.AssemblyQualifiedName; 228 237 } … … 266 275 // load all loadable plugins (all dependencies available) into the execution context 267 276 foreach (var desc in PluginDescriptionIterator.IterateInDependencyOrder(pluginDescriptions 268 .Cast<IPluginDescription>()269 277 .Where(x => x.PluginState != PluginState.Disabled))) { 270 278 List<Type> types = new List<Type>(); … … 290 298 291 299 // checks if all declared plugin files are actually available and disables plugins with missing files 292 private void CheckPluginFiles(IEnumerable<PluginDescription> pluginDescriptions) {300 private static void CheckPluginFiles(IEnumerable<PluginDescription> pluginDescriptions) { 293 301 foreach (PluginDescription desc in pluginDescriptions) { 294 302 if (!CheckPluginFiles(desc)) { … … 298 306 } 299 307 300 private bool CheckPluginFiles(PluginDescription PluginDescription) {301 foreach (string filename in PluginDescription.Files) {308 private static bool CheckPluginFiles(PluginDescription pluginDescription) { 309 foreach (string filename in pluginDescription.Files) { 302 310 if (!File.Exists(filename)) { 303 311 return false;
Note: See TracChangeset
for help on using the changeset viewer.