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