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