Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2648 was 2592, checked in by gkronber, 15 years ago

Merged missing changes (r2587, r2589) in plugin infrastructure from branch to trunk. #799

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;
26
27namespace HeuristicLab.PluginInfrastructure.Manager {
28  /// <summary>
29  /// Holds information of loaded plugins that is needed for plugin management.
30  /// Used to represent plugins in AppDomains without loading the plugin assemblies.
31  /// </summary>
32  [Serializable]
33  public sealed class PluginDescription : IPluginDescription {
34    private int nTimesLoaded;
35
36    private string name;
37    /// <summary>
38    /// Gets the name of the plugin.
39    /// </summary>
40    public string Name {
41      get { return name; }
42      internal set { name = value; }
43    }
44
45    private string description;
46    /// <summary>
47    /// Gets or sets the description of the plugin.
48    /// </summary>
49    internal string Description {
50      get { return description; }
51      set { description = value; }
52    }
53    private Version version;
54    /// <summary>
55    /// Gets the version of the plugin.
56    /// </summary>
57    public Version Version {
58      get { return version; }
59      internal set { version = value; }
60    }
61    private DateTime buildDate;
62    /// <summary>
63    /// Gets the build date of the plugin.
64    /// </summary>
65    public DateTime BuildDate {
66      get { return buildDate; }
67      internal set { buildDate = value; }
68    }
69
70    private PluginState pluginState;
71    /// <summary>
72    /// Gets or sets the plugin state.
73    /// </summary>
74    public PluginState PluginState {
75      get { return pluginState; }
76    }
77
78
79    private List<string> files = new List<string>();
80    /// <summary>
81    /// Gets the names of all files that belong to this plugin.
82    /// These files are deleted when the plugin is removed or updated.
83    /// </summary>
84    public IEnumerable<string> Files {
85      get { return files; }
86    }
87
88    internal void AddFiles(IEnumerable<string> fileNames) {
89      files.AddRange(fileNames);
90    }
91
92    private List<PluginDescription> dependencies = new List<PluginDescription>();
93    internal IEnumerable<PluginDescription> Dependencies {
94      get { return dependencies; }
95    }
96    /// <summary>
97    /// Gets all dependencies of the plugin.
98    /// </summary>
99    IEnumerable<IPluginDescription> IPluginDescription.Dependencies {
100      get { return dependencies.Cast<IPluginDescription>(); }
101    }
102
103    internal void AddDependency(PluginDescription dependency) {
104      dependencies.Add(dependency);
105    }
106
107    private List<string> assemblies = new List<string>();
108    /// <summary>
109    /// Gets the names of the assemblies that belong to this plugin.
110    /// </summary>
111    internal IEnumerable<string> Assemblies {
112      get { return assemblies; }
113      // set { assemblies = value; }
114    }
115
116    internal void AddAssemblies(IEnumerable<string> assemblyNames) {
117      assemblies.AddRange(assemblyNames);
118    }
119
120    internal PluginDescription() {
121      pluginState = PluginState.Undefined;
122    }
123
124    internal void Disable() {
125      if (pluginState != PluginState.Undefined)
126        throw new InvalidOperationException("Can't disabled a plugin in state " + pluginState);
127      pluginState = PluginState.Disabled;
128    }
129
130    internal void Enable() {
131      if (pluginState != PluginState.Undefined)
132        throw new InvalidOperationException("Can't enabled a plugin in state " + pluginState);
133      pluginState = PluginState.Enabled;
134    }
135
136    internal void Load() {
137      if (!(pluginState == PluginState.Enabled || pluginState == PluginState.Loaded))
138        throw new InvalidOperationException("Can't loaded a plugin in state " + pluginState);
139      pluginState = PluginState.Loaded;
140      nTimesLoaded++;
141    }
142
143    internal void Unload() {
144      if (pluginState != PluginState.Loaded)
145        throw new InvalidOperationException("Can't unload a plugin in state " + pluginState);
146      nTimesLoaded--;
147      if (nTimesLoaded == 0) pluginState = PluginState.Enabled;
148    }
149
150
151    /// <summary>
152    /// Gets the string representation of the plugin.
153    /// </summary>
154    /// <returns>The name of the plugin.</returns>
155    public override string ToString() {
156      return Name;
157    }
158
159    // equals and hashcode have to be implemented because we want to compare PluginDescriptions from
160    // different AppDomains and serialization destroys reference equality
161    /// <summary>
162    /// Checks whether the given object is equal to the current plugin.
163    /// </summary>
164    /// <param name="obj">The object to compare.</param>
165    /// <returns><c>true</c> if it is equal, <c>false</c> otherwise.</returns>
166    public override bool Equals(object obj) {
167      PluginDescription other = obj as PluginDescription;
168      if (other == null) return false;
169
170      return other.Name == this.Name && other.Version == this.Version;
171    }
172    /// <summary>
173    /// Gets the hash code of the current plugin.
174    /// </summary>
175    /// <returns>The hash code of the plugin.</returns>
176    public override int GetHashCode() {
177      if (version != null) {
178        return name.GetHashCode() + version.GetHashCode();
179      } else return name.GetHashCode();
180    }
181  }
182}
Note: See TracBrowser for help on using the repository browser.