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