Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.PluginInfrastructure/3.3/Manager/PluginDescription.cs @ 14875

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

Updated year of copyrights (#1406)

File size: 6.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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    internal PluginDescription() {
142      pluginState = PluginState.Undefined;
143    }
144
145    internal void Disable(string loadingErrorInformation) {
146      if (pluginState != PluginState.Undefined)
147        throw new InvalidOperationException("Cannot disable a plugin in state " + pluginState);
148      pluginState = PluginState.Disabled;
149      this.loadingErrorInformation = loadingErrorInformation;
150    }
151
152    internal void Enable() {
153      if (pluginState != PluginState.Undefined)
154        throw new InvalidOperationException("Cannot enable a plugin in state " + pluginState);
155      pluginState = PluginState.Enabled;
156    }
157
158    internal void Load() {
159      if (!(pluginState == PluginState.Enabled || pluginState == PluginState.Loaded))
160        throw new InvalidOperationException("Cannot load a plugin in state " + pluginState);
161      pluginState = PluginState.Loaded;
162      nTimesLoaded++;
163    }
164
165    internal void Unload() {
166      if (pluginState != PluginState.Loaded)
167        throw new InvalidOperationException("Cannot unload a plugin in state " + pluginState);
168      nTimesLoaded--;
169      if (nTimesLoaded == 0) pluginState = PluginState.Enabled;
170    }
171
172
173    /// <summary>
174    /// Gets the string representation of the plugin.
175    /// </summary>
176    /// <returns>The name of the plugin.</returns>
177    public override string ToString() {
178      return Name + " " + Version;
179    }
180
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) {
189      PluginDescription other = obj as PluginDescription;
190      if (other == null) return false;
191
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    }
203  }
204}
Note: See TracBrowser for help on using the repository browser.