Free cookie consent management tool by TermsFeed Policy Generator

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

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

Implemented changes as suggested by abeham after code review and simplified method PluginValidator.GetPluginDescription(). #863.

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