Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.PluginInfrastructure/3.3/Manager/PluginDescription.cs @ 17078

Last change on this file since 17078 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 7.1 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[16565]3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2]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;
[2592]24using System.Linq;
[2]25
[2481]26namespace HeuristicLab.PluginInfrastructure.Manager {
[2]27  /// <summary>
28  /// Holds information of loaded plugins that is needed for plugin management.
29  /// Used to represent plugins in AppDomains without loading the plugin assemblies.
30  /// </summary>
31  [Serializable]
[2517]32  public sealed class PluginDescription : IPluginDescription {
[2504]33    private int nTimesLoaded;
34
[2]35    private string name;
[1189]36    /// <summary>
[2592]37    /// Gets the name of the plugin.
[1189]38    /// </summary>
[2]39    public string Name {
40      get { return name; }
[2504]41      internal set { name = value; }
[2]42    }
[2513]43
44    private string description;
45    /// <summary>
46    /// Gets or sets the description of the plugin.
47    /// </summary>
[2922]48    public string Description {
[2513]49      get { return description; }
[2922]50      internal set { description = value; }
[2513]51    }
[2]52    private Version version;
[1189]53    /// <summary>
[2592]54    /// Gets the version of the plugin.
[1189]55    /// </summary>
[2592]56    public Version Version {
[2]57      get { return version; }
[2592]58      internal set { version = value; }
[2]59    }
[2481]60
[2778]61    private string contactName;
62    /// <summary>
63    /// Gets or sets the name of the contact person for this plugin.
64    /// </summary>
[2815]65    public string ContactName {
[2778]66      get { return contactName; }
[2815]67      internal set { contactName = value; }
[2778]68    }
69
70    private string contactEmail;
71    /// <summary>
72    /// Gets or sets the e-mail address of the contact person for this plugin.
73    /// </summary>
[2815]74    public string ContactEmail {
[2778]75      get { return contactEmail; }
[2815]76      internal set { contactEmail = value; }
[2778]77    }
[2815]78    private string licenseText;
79    /// <summary>
80    /// Gets or sets the license text of the plugin.
81    /// </summary>
82    public string LicenseText {
83      get { return licenseText; }
84      internal set { licenseText = value; }
85    }
[2778]86
[2481]87    private PluginState pluginState;
88    /// <summary>
89    /// Gets or sets the plugin state.
90    /// </summary>
91    public PluginState PluginState {
92      get { return pluginState; }
93    }
94
[2779]95    private string loadingErrorInformation;
96    /// <summary>
97    /// Gets the error message why this plugin has been disabled.
98    /// </summary>
99    internal string LoadingErrorInformation {
100      get {
101        return loadingErrorInformation;
102      }
103    }
[2488]104
[2688]105    private List<PluginFile> files = new List<PluginFile>();
[2]106    /// <summary>
[1189]107    /// Gets the names of all files that belong to this plugin.
[2]108    /// These files are deleted when the plugin is removed or updated.
109    /// </summary>
[2688]110    public IEnumerable<IPluginFile> Files {
111      get { return files.Cast<IPluginFile>(); }
[2]112    }
113
[2688]114    internal void AddFiles(IEnumerable<PluginFile> fileNames) {
[2481]115      files.AddRange(fileNames);
116    }
117
[2504]118    private List<PluginDescription> dependencies = new List<PluginDescription>();
[2592]119    internal IEnumerable<PluginDescription> Dependencies {
120      get { return dependencies; }
121    }
[1189]122    /// <summary>
123    /// Gets all dependencies of the plugin.
124    /// </summary>
[2592]125    IEnumerable<IPluginDescription> IPluginDescription.Dependencies {
126      get { return dependencies.Cast<IPluginDescription>(); }
[2]127    }
[2481]128
[2504]129    internal void AddDependency(PluginDescription dependency) {
[2481]130      dependencies.Add(dependency);
131    }
132
[2779]133
[2]134    /// <summary>
[2690]135    /// Gets the locations (file names) of the assemblies that belong to this plugin.
[2]136    /// </summary>
[2690]137    public IEnumerable<string> AssemblyLocations {
138      get { return Files.Where(f => f.Type == PluginFileType.Assembly).Select(f => f.Name); }
[2]139    }
[2481]140
[8193]141    /// <summary>
142    /// Gets and sets the list of assembly names for this plugin. Assembly names are only available after the plugin has been loaded.
143    /// </summary>
144    private List<string> assemblyNames;
145    public IEnumerable<string> AssemblyNames {
146      get { return assemblyNames; }
147      set { this.assemblyNames = new List<string>(value); }
148    }
149
[2504]150    internal PluginDescription() {
[2488]151      pluginState = PluginState.Undefined;
152    }
153
[2779]154    internal void Disable(string loadingErrorInformation) {
[2504]155      if (pluginState != PluginState.Undefined)
[5196]156        throw new InvalidOperationException("Cannot disable a plugin in state " + pluginState);
[2504]157      pluginState = PluginState.Disabled;
[2779]158      this.loadingErrorInformation = loadingErrorInformation;
[2504]159    }
160
161    internal void Enable() {
162      if (pluginState != PluginState.Undefined)
[5196]163        throw new InvalidOperationException("Cannot enable a plugin in state " + pluginState);
[2504]164      pluginState = PluginState.Enabled;
165    }
166
167    internal void Load() {
168      if (!(pluginState == PluginState.Enabled || pluginState == PluginState.Loaded))
[5196]169        throw new InvalidOperationException("Cannot load a plugin in state " + pluginState);
[2504]170      pluginState = PluginState.Loaded;
171      nTimesLoaded++;
172    }
173
174    internal void Unload() {
175      if (pluginState != PluginState.Loaded)
[5196]176        throw new InvalidOperationException("Cannot unload a plugin in state " + pluginState);
[2504]177      nTimesLoaded--;
178      if (nTimesLoaded == 0) pluginState = PluginState.Enabled;
179    }
180
181
[1189]182    /// <summary>
183    /// Gets the string representation of the plugin.
184    /// </summary>
185    /// <returns>The name of the plugin.</returns>
[2]186    public override string ToString() {
[2922]187      return Name + " " + Version;
[2]188    }
[2481]189
[2497]190    // equals and hashcode have to be implemented because we want to compare PluginDescriptions from
191    // different AppDomains and serialization destroys reference equality
192    /// <summary>
193    /// Checks whether the given object is equal to the current plugin.
194    /// </summary>
195    /// <param name="obj">The object to compare.</param>
196    /// <returns><c>true</c> if it is equal, <c>false</c> otherwise.</returns>
197    public override bool Equals(object obj) {
[2504]198      PluginDescription other = obj as PluginDescription;
199      if (other == null) return false;
[2]200
[2497]201      return other.Name == this.Name && other.Version == this.Version;
202    }
203    /// <summary>
204    /// Gets the hash code of the current plugin.
205    /// </summary>
206    /// <returns>The hash code of the plugin.</returns>
207    public override int GetHashCode() {
208      if (version != null) {
209        return name.GetHashCode() + version.GetHashCode();
210      } else return name.GetHashCode();
211    }
[2]212  }
213}
Note: See TracBrowser for help on using the repository browser.