Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2924_DotNetCoreMigration/HeuristicLab.PluginInfrastructure/3.3/PluginDescription.cs @ 16993

Last change on this file since 16993 was 16859, checked in by dpiringe, 5 years ago

#2924:

  • migrated PluginInfrastructure to .NET Standard
  • created a new .NET Core project HeuristicLab.PluginInfrastructure.Runner, which contains the logic to load and isolate assemblies, based on the new interfaces from HeuristicLab.PluginInfrastructure
  • recycled old plugin validation code
File size: 7.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Linq;
25using System.Runtime.CompilerServices;
26
27[assembly: InternalsVisibleTo("HeuristicLab.PluginInfrastructure.Runner-3.4, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e3d38bc66a0dd8dd36f57285e34632ec04b3049866ab1e64cf230e95ffcbfbb90c437b4d11dfe74ba981f746274290bb03f3e636e139e685b501031dc6e0bc8409153f0c842721eb9e8e2a703c9e4d102283f3ddbdfab4078c08de51869715992a694d2f608d0fa865c9d17c06b8d6a9135004e982fd862cdb2277e4ad15a4a6")]
28namespace HeuristicLab.PluginInfrastructure {
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    public string Description {
51      get { return description; }
52      internal 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
63    private string contactName;
64    /// <summary>
65    /// Gets or sets the name of the contact person for this plugin.
66    /// </summary>
67    public string ContactName {
68      get { return contactName; }
69      internal set { contactName = value; }
70    }
71
72    private string contactEmail;
73    /// <summary>
74    /// Gets or sets the e-mail address of the contact person for this plugin.
75    /// </summary>
76    public string ContactEmail {
77      get { return contactEmail; }
78      internal set { contactEmail = value; }
79    }
80    private string licenseText;
81    /// <summary>
82    /// Gets or sets the license text of the plugin.
83    /// </summary>
84    public string LicenseText {
85      get { return licenseText; }
86      internal set { licenseText = value; }
87    }
88
89    private PluginState pluginState;
90    /// <summary>
91    /// Gets or sets the plugin state.
92    /// </summary>
93    public PluginState PluginState {
94      get { return pluginState; }
95    }
96
97    private string loadingErrorInformation;
98    /// <summary>
99    /// Gets the error message why this plugin has been disabled.
100    /// </summary>
101    public string LoadingErrorInformation {
102      get {
103        return loadingErrorInformation;
104      }
105    }
106
107    private List<PluginFile> files = new List<PluginFile>();
108    /// <summary>
109    /// Gets the names of all files that belong to this plugin.
110    /// These files are deleted when the plugin is removed or updated.
111    /// </summary>
112    public IEnumerable<IPluginFile> Files {
113      get { return files.Cast<IPluginFile>(); }
114    }
115
116    internal void AddFiles(IEnumerable<PluginFile> fileNames) {
117      files.AddRange(fileNames);
118    }
119
120    private List<PluginDescription> dependencies = new List<PluginDescription>();
121    internal IEnumerable<PluginDescription> Dependencies {
122      get { return dependencies; }
123    }
124    /// <summary>
125    /// Gets all dependencies of the plugin.
126    /// </summary>
127    IEnumerable<IPluginDescription> IPluginDescription.Dependencies {
128      get { return dependencies.Cast<IPluginDescription>(); }
129    }
130
131    internal void AddDependency(PluginDescription dependency) {
132      dependencies.Add(dependency);
133    }
134
135
136    /// <summary>
137    /// Gets the locations (file names) of the assemblies that belong to this plugin.
138    /// </summary>
139    public IEnumerable<string> AssemblyLocations {
140      get { return Files.Where(f => f.Type == PluginFileType.Assembly).Select(f => f.Name); }
141    }
142
143    /// <summary>
144    /// Gets and sets the list of assembly names for this plugin. Assembly names are only available after the plugin has been loaded.
145    /// </summary>
146    private List<string> assemblyNames;
147    public IEnumerable<string> AssemblyNames {
148      get { return assemblyNames; }
149      set { this.assemblyNames = new List<string>(value); }
150    }
151
152    internal PluginDescription() {
153      pluginState = PluginState.Undefined;
154    }
155
156    internal void Disable(string loadingErrorInformation) {
157      if (pluginState != PluginState.Undefined)
158        throw new InvalidOperationException("Cannot disable a plugin in state " + pluginState);
159      pluginState = PluginState.Disabled;
160      this.loadingErrorInformation = loadingErrorInformation;
161    }
162
163    internal void Enable() {
164      if (pluginState != PluginState.Undefined)
165        throw new InvalidOperationException("Cannot enable a plugin in state " + pluginState);
166      pluginState = PluginState.Enabled;
167    }
168
169    internal void Load() {
170      if (!(pluginState == PluginState.Enabled || pluginState == PluginState.Loaded))
171        throw new InvalidOperationException("Cannot load a plugin in state " + pluginState);
172      pluginState = PluginState.Loaded;
173      nTimesLoaded++;
174    }
175
176    internal void Unload() {
177      if (pluginState != PluginState.Loaded)
178        throw new InvalidOperationException("Cannot unload a plugin in state " + pluginState);
179      nTimesLoaded--;
180      if (nTimesLoaded == 0) pluginState = PluginState.Enabled;
181    }
182
183
184    /// <summary>
185    /// Gets the string representation of the plugin.
186    /// </summary>
187    /// <returns>The name of the plugin.</returns>
188    public override string ToString() {
189      return Name + " " + Version;
190    }
191
192    // equals and hashcode have to be implemented because we want to compare PluginDescriptions from
193    // different AppDomains and serialization destroys reference equality
194    /// <summary>
195    /// Checks whether the given object is equal to the current plugin.
196    /// </summary>
197    /// <param name="obj">The object to compare.</param>
198    /// <returns><c>true</c> if it is equal, <c>false</c> otherwise.</returns>
199    public override bool Equals(object obj) {
200      PluginDescription other = obj as PluginDescription;
201      if (other == null) return false;
202
203      return other.Name == this.Name && other.Version == this.Version;
204    }
205    /// <summary>
206    /// Gets the hash code of the current plugin.
207    /// </summary>
208    /// <returns>The hash code of the plugin.</returns>
209    public override int GetHashCode() {
210      if (version != null) {
211        return name.GetHashCode() + version.GetHashCode();
212      } else return name.GetHashCode();
213    }
214  }
215}
Note: See TracBrowser for help on using the repository browser.