Free cookie consent management tool by TermsFeed Policy Generator

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

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

Updated year of copyrights (#1406)

File size: 6.7 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 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
[2504]141    internal PluginDescription() {
[2488]142      pluginState = PluginState.Undefined;
143    }
144
[2779]145    internal void Disable(string loadingErrorInformation) {
[2504]146      if (pluginState != PluginState.Undefined)
[5196]147        throw new InvalidOperationException("Cannot disable a plugin in state " + pluginState);
[2504]148      pluginState = PluginState.Disabled;
[2779]149      this.loadingErrorInformation = loadingErrorInformation;
[2504]150    }
151
152    internal void Enable() {
153      if (pluginState != PluginState.Undefined)
[5196]154        throw new InvalidOperationException("Cannot enable a plugin in state " + pluginState);
[2504]155      pluginState = PluginState.Enabled;
156    }
157
158    internal void Load() {
159      if (!(pluginState == PluginState.Enabled || pluginState == PluginState.Loaded))
[5196]160        throw new InvalidOperationException("Cannot load a plugin in state " + pluginState);
[2504]161      pluginState = PluginState.Loaded;
162      nTimesLoaded++;
163    }
164
165    internal void Unload() {
166      if (pluginState != PluginState.Loaded)
[5196]167        throw new InvalidOperationException("Cannot unload a plugin in state " + pluginState);
[2504]168      nTimesLoaded--;
169      if (nTimesLoaded == 0) pluginState = PluginState.Enabled;
170    }
171
172
[1189]173    /// <summary>
174    /// Gets the string representation of the plugin.
175    /// </summary>
176    /// <returns>The name of the plugin.</returns>
[2]177    public override string ToString() {
[2922]178      return Name + " " + Version;
[2]179    }
[2481]180
[2497]181    // equals and hashcode have to be implemented because we want to compare PluginDescriptions from
182    // different AppDomains and serialization destroys reference equality
183    /// <summary>
184    /// Checks whether the given object is equal to the current plugin.
185    /// </summary>
186    /// <param name="obj">The object to compare.</param>
187    /// <returns><c>true</c> if it is equal, <c>false</c> otherwise.</returns>
188    public override bool Equals(object obj) {
[2504]189      PluginDescription other = obj as PluginDescription;
190      if (other == null) return false;
[2]191
[2497]192      return other.Name == this.Name && other.Version == this.Version;
193    }
194    /// <summary>
195    /// Gets the hash code of the current plugin.
196    /// </summary>
197    /// <returns>The hash code of the plugin.</returns>
198    public override int GetHashCode() {
199      if (version != null) {
200        return name.GetHashCode() + version.GetHashCode();
201      } else return name.GetHashCode();
202    }
[2]203  }
204}
Note: See TracBrowser for help on using the repository browser.