Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2754 was 2690, checked in by gkronber, 15 years ago

Fixed caching and resolve event handler in plugin infrastructure to fix the bug that the SqlServerCompact assembly is not found when opening a db connection. #854 (System.Data.SqlServerCe assembly is not loaded correctly)

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