Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2965_CancelablePersistence/HeuristicLab.PluginInfrastructure/3.3/Manager/PluginDescription.cs @ 16321

Last change on this file since 16321 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 7.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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;
25
26namespace HeuristicLab.PluginInfrastructure.Manager {
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]
32  public sealed class PluginDescription : IPluginDescription {
33    private int nTimesLoaded;
34
35    private string name;
36    /// <summary>
37    /// Gets the name of the plugin.
38    /// </summary>
39    public string Name {
40      get { return name; }
41      internal set { name = value; }
42    }
43
44    private string description;
45    /// <summary>
46    /// Gets or sets the description of the plugin.
47    /// </summary>
48    public string Description {
49      get { return description; }
50      internal set { description = value; }
51    }
52    private Version version;
53    /// <summary>
54    /// Gets the version of the plugin.
55    /// </summary>
56    public Version Version {
57      get { return version; }
58      internal set { version = value; }
59    }
60
61    private string contactName;
62    /// <summary>
63    /// Gets or sets the name of the contact person for this plugin.
64    /// </summary>
65    public string ContactName {
66      get { return contactName; }
67      internal set { contactName = value; }
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>
74    public string ContactEmail {
75      get { return contactEmail; }
76      internal set { contactEmail = value; }
77    }
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    }
86
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
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    }
104
105    private List<PluginFile> files = new List<PluginFile>();
106    /// <summary>
107    /// Gets the names of all files that belong to this plugin.
108    /// These files are deleted when the plugin is removed or updated.
109    /// </summary>
110    public IEnumerable<IPluginFile> Files {
111      get { return files.Cast<IPluginFile>(); }
112    }
113
114    internal void AddFiles(IEnumerable<PluginFile> fileNames) {
115      files.AddRange(fileNames);
116    }
117
118    private List<PluginDescription> dependencies = new List<PluginDescription>();
119    internal IEnumerable<PluginDescription> Dependencies {
120      get { return dependencies; }
121    }
122    /// <summary>
123    /// Gets all dependencies of the plugin.
124    /// </summary>
125    IEnumerable<IPluginDescription> IPluginDescription.Dependencies {
126      get { return dependencies.Cast<IPluginDescription>(); }
127    }
128
129    internal void AddDependency(PluginDescription dependency) {
130      dependencies.Add(dependency);
131    }
132
133
134    /// <summary>
135    /// Gets the locations (file names) of the assemblies that belong to this plugin.
136    /// </summary>
137    public IEnumerable<string> AssemblyLocations {
138      get { return Files.Where(f => f.Type == PluginFileType.Assembly).Select(f => f.Name); }
139    }
140
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
150    internal PluginDescription() {
151      pluginState = PluginState.Undefined;
152    }
153
154    internal void Disable(string loadingErrorInformation) {
155      if (pluginState != PluginState.Undefined)
156        throw new InvalidOperationException("Cannot disable a plugin in state " + pluginState);
157      pluginState = PluginState.Disabled;
158      this.loadingErrorInformation = loadingErrorInformation;
159    }
160
161    internal void Enable() {
162      if (pluginState != PluginState.Undefined)
163        throw new InvalidOperationException("Cannot enable a plugin in state " + pluginState);
164      pluginState = PluginState.Enabled;
165    }
166
167    internal void Load() {
168      if (!(pluginState == PluginState.Enabled || pluginState == PluginState.Loaded))
169        throw new InvalidOperationException("Cannot load a plugin in state " + pluginState);
170      pluginState = PluginState.Loaded;
171      nTimesLoaded++;
172    }
173
174    internal void Unload() {
175      if (pluginState != PluginState.Loaded)
176        throw new InvalidOperationException("Cannot unload a plugin in state " + pluginState);
177      nTimesLoaded--;
178      if (nTimesLoaded == 0) pluginState = PluginState.Enabled;
179    }
180
181
182    /// <summary>
183    /// Gets the string representation of the plugin.
184    /// </summary>
185    /// <returns>The name of the plugin.</returns>
186    public override string ToString() {
187      return Name + " " + Version;
188    }
189
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) {
198      PluginDescription other = obj as PluginDescription;
199      if (other == null) return false;
200
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    }
212  }
213}
Note: See TracBrowser for help on using the repository browser.