[2] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Text;
|
---|
[2592] | 25 | using System.Linq;
|
---|
[2] | 26 |
|
---|
[2481] | 27 | namespace HeuristicLab.PluginInfrastructure.Manager {
|
---|
[2] | 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]
|
---|
[2517] | 33 | public sealed class PluginDescription : IPluginDescription {
|
---|
[2504] | 34 | private int nTimesLoaded;
|
---|
| 35 |
|
---|
[2] | 36 | private string name;
|
---|
[1189] | 37 | /// <summary>
|
---|
[2592] | 38 | /// Gets the name of the plugin.
|
---|
[1189] | 39 | /// </summary>
|
---|
[2] | 40 | public string Name {
|
---|
| 41 | get { return name; }
|
---|
[2504] | 42 | internal set { name = value; }
|
---|
[2] | 43 | }
|
---|
[2513] | 44 |
|
---|
| 45 | private string description;
|
---|
| 46 | /// <summary>
|
---|
| 47 | /// Gets or sets the description of the plugin.
|
---|
| 48 | /// </summary>
|
---|
[2517] | 49 | internal string Description {
|
---|
[2513] | 50 | get { return description; }
|
---|
[2517] | 51 | set { description = value; }
|
---|
[2513] | 52 | }
|
---|
[2] | 53 | private Version version;
|
---|
[1189] | 54 | /// <summary>
|
---|
[2592] | 55 | /// Gets the version of the plugin.
|
---|
[1189] | 56 | /// </summary>
|
---|
[2592] | 57 | public Version Version {
|
---|
[2] | 58 | get { return version; }
|
---|
[2592] | 59 | internal set { version = value; }
|
---|
[2] | 60 | }
|
---|
[91] | 61 | private DateTime buildDate;
|
---|
[1189] | 62 | /// <summary>
|
---|
[2592] | 63 | /// Gets the build date of the plugin.
|
---|
[1189] | 64 | /// </summary>
|
---|
[2592] | 65 | public DateTime BuildDate {
|
---|
[91] | 66 | get { return buildDate; }
|
---|
[2592] | 67 | internal set { buildDate = value; }
|
---|
[91] | 68 | }
|
---|
[2481] | 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 |
|
---|
[2488] | 78 |
|
---|
[91] | 79 | private List<string> files = new List<string>();
|
---|
[2] | 80 | /// <summary>
|
---|
[1189] | 81 | /// Gets the names of all files that belong to this plugin.
|
---|
[2] | 82 | /// These files are deleted when the plugin is removed or updated.
|
---|
| 83 | /// </summary>
|
---|
[2592] | 84 | public IEnumerable<string> Files {
|
---|
[2] | 85 | get { return files; }
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[2481] | 88 | internal void AddFiles(IEnumerable<string> fileNames) {
|
---|
| 89 | files.AddRange(fileNames);
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[2504] | 92 | private List<PluginDescription> dependencies = new List<PluginDescription>();
|
---|
[2592] | 93 | internal IEnumerable<PluginDescription> Dependencies {
|
---|
| 94 | get { return dependencies; }
|
---|
| 95 | }
|
---|
[1189] | 96 | /// <summary>
|
---|
| 97 | /// Gets all dependencies of the plugin.
|
---|
| 98 | /// </summary>
|
---|
[2592] | 99 | IEnumerable<IPluginDescription> IPluginDescription.Dependencies {
|
---|
| 100 | get { return dependencies.Cast<IPluginDescription>(); }
|
---|
[2] | 101 | }
|
---|
[2481] | 102 |
|
---|
[2504] | 103 | internal void AddDependency(PluginDescription dependency) {
|
---|
[2481] | 104 | dependencies.Add(dependency);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[2] | 107 | private List<string> assemblies = new List<string>();
|
---|
| 108 | /// <summary>
|
---|
[1189] | 109 | /// Gets the names of the assemblies that belong to this plugin.
|
---|
[2] | 110 | /// </summary>
|
---|
[2666] | 111 | public IEnumerable<string> Assemblies {
|
---|
[2] | 112 | get { return assemblies; }
|
---|
[2481] | 113 | // set { assemblies = value; }
|
---|
[2] | 114 | }
|
---|
[2481] | 115 |
|
---|
| 116 | internal void AddAssemblies(IEnumerable<string> assemblyNames) {
|
---|
| 117 | assemblies.AddRange(assemblyNames);
|
---|
[37] | 118 | }
|
---|
[2481] | 119 |
|
---|
[2504] | 120 | internal PluginDescription() {
|
---|
[2488] | 121 | pluginState = PluginState.Undefined;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[2504] | 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 |
|
---|
[1189] | 151 | /// <summary>
|
---|
| 152 | /// Gets the string representation of the plugin.
|
---|
| 153 | /// </summary>
|
---|
| 154 | /// <returns>The name of the plugin.</returns>
|
---|
[2] | 155 | public override string ToString() {
|
---|
| 156 | return Name;
|
---|
| 157 | }
|
---|
[2481] | 158 |
|
---|
[2497] | 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) {
|
---|
[2504] | 167 | PluginDescription other = obj as PluginDescription;
|
---|
| 168 | if (other == null) return false;
|
---|
[2] | 169 |
|
---|
[2497] | 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 | }
|
---|
[2] | 181 | }
|
---|
| 182 | }
|
---|