Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2778 was 2778, checked in by gkronber, 14 years ago

Implemented ContactInformationAttribute. #868 (Attribute to declare contact information details for plugins)

File size: 6.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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    internal string Description {
51      get { return description; }
52      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    internal string ContactName {
78      get { return contactName; }
79      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    internal string ContactEmail {
87      get { return contactEmail; }
88      set { contactEmail = value; }
89    }
90
91    private PluginState pluginState;
92    /// <summary>
93    /// Gets or sets the plugin state.
94    /// </summary>
95    public PluginState PluginState {
96      get { return pluginState; }
97    }
98
99
100    private List<PluginFile> files = new List<PluginFile>();
101    /// <summary>
102    /// Gets the names of all files that belong to this plugin.
103    /// These files are deleted when the plugin is removed or updated.
104    /// </summary>
105    public IEnumerable<IPluginFile> Files {
106      get { return files.Cast<IPluginFile>(); }
107    }
108
109    internal void AddFiles(IEnumerable<PluginFile> fileNames) {
110      files.AddRange(fileNames);
111    }
112
113    private List<PluginDescription> dependencies = new List<PluginDescription>();
114    internal IEnumerable<PluginDescription> Dependencies {
115      get { return dependencies; }
116    }
117    /// <summary>
118    /// Gets all dependencies of the plugin.
119    /// </summary>
120    IEnumerable<IPluginDescription> IPluginDescription.Dependencies {
121      get { return dependencies.Cast<IPluginDescription>(); }
122    }
123
124    internal void AddDependency(PluginDescription dependency) {
125      dependencies.Add(dependency);
126    }
127
128   
129    /// <summary>
130    /// Gets the locations (file names) of the assemblies that belong to this plugin.
131    /// </summary>
132    public IEnumerable<string> AssemblyLocations {
133      get { return Files.Where(f => f.Type == PluginFileType.Assembly).Select(f => f.Name); }
134    }
135
136    internal PluginDescription() {
137      pluginState = PluginState.Undefined;
138    }
139
140    internal void Disable() {
141      if (pluginState != PluginState.Undefined)
142        throw new InvalidOperationException("Can't disabled a plugin in state " + pluginState);
143      pluginState = PluginState.Disabled;
144    }
145
146    internal void Enable() {
147      if (pluginState != PluginState.Undefined)
148        throw new InvalidOperationException("Can't enabled a plugin in state " + pluginState);
149      pluginState = PluginState.Enabled;
150    }
151
152    internal void Load() {
153      if (!(pluginState == PluginState.Enabled || pluginState == PluginState.Loaded))
154        throw new InvalidOperationException("Can't load a plugin in state " + pluginState);
155      pluginState = PluginState.Loaded;
156      nTimesLoaded++;
157    }
158
159    internal void Unload() {
160      if (pluginState != PluginState.Loaded)
161        throw new InvalidOperationException("Can't unload a plugin in state " + pluginState);
162      nTimesLoaded--;
163      if (nTimesLoaded == 0) pluginState = PluginState.Enabled;
164    }
165
166
167    /// <summary>
168    /// Gets the string representation of the plugin.
169    /// </summary>
170    /// <returns>The name of the plugin.</returns>
171    public override string ToString() {
172      return Name;
173    }
174
175    // equals and hashcode have to be implemented because we want to compare PluginDescriptions from
176    // different AppDomains and serialization destroys reference equality
177    /// <summary>
178    /// Checks whether the given object is equal to the current plugin.
179    /// </summary>
180    /// <param name="obj">The object to compare.</param>
181    /// <returns><c>true</c> if it is equal, <c>false</c> otherwise.</returns>
182    public override bool Equals(object obj) {
183      PluginDescription other = obj as PluginDescription;
184      if (other == null) return false;
185
186      return other.Name == this.Name && other.Version == this.Version;
187    }
188    /// <summary>
189    /// Gets the hash code of the current plugin.
190    /// </summary>
191    /// <returns>The hash code of the plugin.</returns>
192    public override int GetHashCode() {
193      if (version != null) {
194        return name.GetHashCode() + version.GetHashCode();
195      } else return name.GetHashCode();
196    }
197  }
198}
Note: See TracBrowser for help on using the repository browser.