Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3667 was 2922, checked in by gkronber, 15 years ago

Worked on GUI for plugin management. #891 (Refactor GUI for plugin management)

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