Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2790 was 2790, checked in by swagner, 14 years ago

Operator architecture refactoring (#95)

  • implemented reviewers' comments
  • added additional plugins HeuristicLab.Evolutionary, HeuristicLab.Permutation, HeuristicLab.Selection, and HeuristicLab.Routing.TSP
File size: 6.8 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.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    [Obsolete]
63    private DateTime buildDate;
64    /// <summary>
65    /// Gets the build date of the plugin.
66    /// </summary>
67    [Obsolete]
68    public DateTime BuildDate {
69      get { return buildDate; }
70      internal set { buildDate = value; }
71    }
72
73    private string contactName;
74    /// <summary>
75    /// Gets or sets the name of the contact person for this plugin.
76    /// </summary>
77    internal string ContactName {
78      get { return contactName; }
79      set { contactName = value; }
80    }
81
82    private string contactEmail;
83    /// <summary>
84    /// Gets or sets the e-mail address of the contact person for this plugin.
85    /// </summary>
86    internal string ContactEmail {
87      get { return contactEmail; }
88      set { contactEmail = value; }
89    }
90
91    private PluginState pluginState;
92    /// <summary>
93    /// Gets or sets the plugin state.
94    /// </summary>
95    public PluginState PluginState {
96      get { return pluginState; }
97    }
98
99    private string loadingErrorInformation;
100    /// <summary>
101    /// Gets the error message why this plugin has been disabled.
102    /// </summary>
103    internal string LoadingErrorInformation {
104      get {
105        return loadingErrorInformation;
106      }
107    }
108
109    private List<PluginFile> files = new List<PluginFile>();
110    /// <summary>
111    /// Gets the names of all files that belong to this plugin.
112    /// These files are deleted when the plugin is removed or updated.
113    /// </summary>
114    public IEnumerable<IPluginFile> Files {
115      get { return files.Cast<IPluginFile>(); }
116    }
117
118    internal void AddFiles(IEnumerable<PluginFile> fileNames) {
119      files.AddRange(fileNames);
120    }
121
122    private List<PluginDescription> dependencies = new List<PluginDescription>();
123    internal IEnumerable<PluginDescription> Dependencies {
124      get { return dependencies; }
125    }
126    /// <summary>
127    /// Gets all dependencies of the plugin.
128    /// </summary>
129    IEnumerable<IPluginDescription> IPluginDescription.Dependencies {
130      get { return dependencies.Cast<IPluginDescription>(); }
131    }
132
133    internal void AddDependency(PluginDescription dependency) {
134      dependencies.Add(dependency);
135    }
136
137
138    /// <summary>
139    /// Gets the locations (file names) of the assemblies that belong to this plugin.
140    /// </summary>
141    public IEnumerable<string> AssemblyLocations {
142      get { return Files.Where(f => f.Type == PluginFileType.Assembly).Select(f => f.Name); }
143    }
144
145    internal PluginDescription() {
146      pluginState = PluginState.Undefined;
147    }
148
149    internal void Disable(string loadingErrorInformation) {
150      if (pluginState != PluginState.Undefined)
151        throw new InvalidOperationException("Can't disabled a plugin in state " + pluginState);
152      pluginState = PluginState.Disabled;
153      this.loadingErrorInformation = loadingErrorInformation;
154    }
155
156    internal void Enable() {
157      if (pluginState != PluginState.Undefined)
158        throw new InvalidOperationException("Can't enabled a plugin in state " + pluginState);
159      pluginState = PluginState.Enabled;
160    }
161
162    internal void Load() {
163      if (!(pluginState == PluginState.Enabled || pluginState == PluginState.Loaded))
164        throw new InvalidOperationException("Can't load a plugin in state " + pluginState);
165      pluginState = PluginState.Loaded;
166      nTimesLoaded++;
167    }
168
169    internal void Unload() {
170      if (pluginState != PluginState.Loaded)
171        throw new InvalidOperationException("Can't unload a plugin in state " + pluginState);
172      nTimesLoaded--;
173      if (nTimesLoaded == 0) pluginState = PluginState.Enabled;
174    }
175
176
177    /// <summary>
178    /// Gets the string representation of the plugin.
179    /// </summary>
180    /// <returns>The name of the plugin.</returns>
181    public override string ToString() {
182      return Name;
183    }
184
185    // equals and hashcode have to be implemented because we want to compare PluginDescriptions from
186    // different AppDomains and serialization destroys reference equality
187    /// <summary>
188    /// Checks whether the given object is equal to the current plugin.
189    /// </summary>
190    /// <param name="obj">The object to compare.</param>
191    /// <returns><c>true</c> if it is equal, <c>false</c> otherwise.</returns>
192    public override bool Equals(object obj) {
193      PluginDescription other = obj as PluginDescription;
194      if (other == null) return false;
195
196      return other.Name == this.Name && other.Version == this.Version;
197    }
198    /// <summary>
199    /// Gets the hash code of the current plugin.
200    /// </summary>
201    /// <returns>The hash code of the plugin.</returns>
202    public override int GetHashCode() {
203      if (version != null) {
204        return name.GetHashCode() + version.GetHashCode();
205      } else return name.GetHashCode();
206    }
207  }
208}
Note: See TracBrowser for help on using the repository browser.