Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.PluginInfrastructure/Manager/PluginDescription.cs @ 2688

Last change on this file since 2688 was 2688, checked in by gkronber, 14 years ago

Implemented an enumerable to iterate through all PluginFiles as suggested by swagner, replaced the Assemblies enumerable with an AssemblyName enumerable for internal usage in the plugin infrastructure and replaced Assembly.LoadFrom calls with Assembly.Load() to prevent loading from GAC as far as possible.

#850 (PluginInfrastructure should provide a way to get assemblies associated with a plug-in)

File size: 6.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Text;
25using System.Linq;
26using System.Reflection;
27
28namespace HeuristicLab.PluginInfrastructure.Manager {
29  /// <summary>
30  /// Holds information of loaded plugins that is needed for plugin management.
31  /// Used to represent plugins in AppDomains without loading the plugin assemblies.
32  /// </summary>
33  [Serializable]
34  public sealed class PluginDescription : IPluginDescription {
35    private int nTimesLoaded;
36
37    private string name;
38    /// <summary>
39    /// Gets the name of the plugin.
40    /// </summary>
41    public string Name {
42      get { return name; }
43      internal set { name = value; }
44    }
45
46    private string description;
47    /// <summary>
48    /// Gets or sets the description of the plugin.
49    /// </summary>
50    internal string Description {
51      get { return description; }
52      set { description = value; }
53    }
54    private Version version;
55    /// <summary>
56    /// Gets the version of the plugin.
57    /// </summary>
58    public Version Version {
59      get { return version; }
60      internal set { version = value; }
61    }
62    private DateTime buildDate;
63    /// <summary>
64    /// Gets the build date of the plugin.
65    /// </summary>
66    public DateTime BuildDate {
67      get { return buildDate; }
68      internal set { buildDate = value; }
69    }
70
71    private PluginState pluginState;
72    /// <summary>
73    /// Gets or sets the plugin state.
74    /// </summary>
75    public PluginState PluginState {
76      get { return pluginState; }
77    }
78
79
80    private List<PluginFile> files = new List<PluginFile>();
81    /// <summary>
82    /// Gets the names of all files that belong to this plugin.
83    /// These files are deleted when the plugin is removed or updated.
84    /// </summary>
85    public IEnumerable<IPluginFile> Files {
86      get { return files.Cast<IPluginFile>(); }
87    }
88
89    internal void AddFiles(IEnumerable<PluginFile> fileNames) {
90      files.AddRange(fileNames);
91    }
92
93    private List<PluginDescription> dependencies = new List<PluginDescription>();
94    internal IEnumerable<PluginDescription> Dependencies {
95      get { return dependencies; }
96    }
97    /// <summary>
98    /// Gets all dependencies of the plugin.
99    /// </summary>
100    IEnumerable<IPluginDescription> IPluginDescription.Dependencies {
101      get { return dependencies.Cast<IPluginDescription>(); }
102    }
103
104    internal void AddDependency(PluginDescription dependency) {
105      dependencies.Add(dependency);
106    }
107
108    private List<AssemblyName> assemblyNames = new List<AssemblyName>();
109    /// <summary>
110    /// Gets the names of the assemblies that belong to this plugin.
111    /// </summary>
112    public IEnumerable<AssemblyName> AssemblyNames {
113      get { return assemblyNames; }
114    }
115
116    internal void AddAssemblyNames(IEnumerable<AssemblyName> assemblyNames) {
117      this.assemblyNames.AddRange(assemblyNames);
118    }
119
120    internal PluginDescription() {
121      pluginState = PluginState.Undefined;
122    }
123
124    internal void Disable() {
125      if (pluginState != PluginState.Undefined)
126        throw new InvalidOperationException("Can't disabled a plugin in state " + pluginState);
127      pluginState = PluginState.Disabled;
128    }
129
130    internal void Enable() {
131      if (pluginState != PluginState.Undefined)
132        throw new InvalidOperationException("Can't enabled a plugin in state " + pluginState);
133      pluginState = PluginState.Enabled;
134    }
135
136    internal void Load() {
137      if (!(pluginState == PluginState.Enabled || pluginState == PluginState.Loaded))
138        throw new InvalidOperationException("Can't loaded a plugin in state " + pluginState);
139      pluginState = PluginState.Loaded;
140      nTimesLoaded++;
141    }
142
143    internal void Unload() {
144      if (pluginState != PluginState.Loaded)
145        throw new InvalidOperationException("Can't unload a plugin in state " + pluginState);
146      nTimesLoaded--;
147      if (nTimesLoaded == 0) pluginState = PluginState.Enabled;
148    }
149
150
151    /// <summary>
152    /// Gets the string representation of the plugin.
153    /// </summary>
154    /// <returns>The name of the plugin.</returns>
155    public override string ToString() {
156      return Name;
157    }
158
159    // equals and hashcode have to be implemented because we want to compare PluginDescriptions from
160    // different AppDomains and serialization destroys reference equality
161    /// <summary>
162    /// Checks whether the given object is equal to the current plugin.
163    /// </summary>
164    /// <param name="obj">The object to compare.</param>
165    /// <returns><c>true</c> if it is equal, <c>false</c> otherwise.</returns>
166    public override bool Equals(object obj) {
167      PluginDescription other = obj as PluginDescription;
168      if (other == null) return false;
169
170      return other.Name == this.Name && other.Version == this.Version;
171    }
172    /// <summary>
173    /// Gets the hash code of the current plugin.
174    /// </summary>
175    /// <returns>The hash code of the plugin.</returns>
176    public override int GetHashCode() {
177      if (version != null) {
178        return name.GetHashCode() + version.GetHashCode();
179      } else return name.GetHashCode();
180    }
181  }
182}
Note: See TracBrowser for help on using the repository browser.