#region License Information
/* HeuristicLab
* Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace HeuristicLab.PluginInfrastructure.Manager {
///
/// Holds information of loaded plugins that is needed for plugin management.
/// Used to represent plugins in AppDomains without loading the plugin assemblies.
///
[Serializable]
public sealed class PluginDescription : IPluginDescription {
private int nTimesLoaded;
private string name;
///
/// Gets the name of the plugin.
///
public string Name {
get { return name; }
internal set { name = value; }
}
private string description;
///
/// Gets or sets the description of the plugin.
///
internal string Description {
get { return description; }
set { description = value; }
}
private Version version;
///
/// Gets the version of the plugin.
///
public Version Version {
get { return version; }
internal set { version = value; }
}
private DateTime buildDate;
///
/// Gets the build date of the plugin.
///
public DateTime BuildDate {
get { return buildDate; }
internal set { buildDate = value; }
}
private PluginState pluginState;
///
/// Gets or sets the plugin state.
///
public PluginState PluginState {
get { return pluginState; }
}
private List files = new List();
///
/// Gets the names of all files that belong to this plugin.
/// These files are deleted when the plugin is removed or updated.
///
public IEnumerable Files {
get { return files; }
}
internal void AddFiles(IEnumerable fileNames) {
files.AddRange(fileNames);
}
private List dependencies = new List();
internal IEnumerable Dependencies {
get { return dependencies; }
}
///
/// Gets all dependencies of the plugin.
///
IEnumerable IPluginDescription.Dependencies {
get { return dependencies.Cast(); }
}
internal void AddDependency(PluginDescription dependency) {
dependencies.Add(dependency);
}
private List assemblies = new List();
///
/// Gets the names of the assemblies that belong to this plugin.
///
internal IEnumerable Assemblies {
get { return assemblies; }
// set { assemblies = value; }
}
internal void AddAssemblies(IEnumerable assemblyNames) {
assemblies.AddRange(assemblyNames);
}
internal PluginDescription() {
pluginState = PluginState.Undefined;
}
internal void Disable() {
if (pluginState != PluginState.Undefined)
throw new InvalidOperationException("Can't disabled a plugin in state " + pluginState);
pluginState = PluginState.Disabled;
}
internal void Enable() {
if (pluginState != PluginState.Undefined)
throw new InvalidOperationException("Can't enabled a plugin in state " + pluginState);
pluginState = PluginState.Enabled;
}
internal void Load() {
if (!(pluginState == PluginState.Enabled || pluginState == PluginState.Loaded))
throw new InvalidOperationException("Can't loaded a plugin in state " + pluginState);
pluginState = PluginState.Loaded;
nTimesLoaded++;
}
internal void Unload() {
if (pluginState != PluginState.Loaded)
throw new InvalidOperationException("Can't unload a plugin in state " + pluginState);
nTimesLoaded--;
if (nTimesLoaded == 0) pluginState = PluginState.Enabled;
}
///
/// Gets the string representation of the plugin.
///
/// The name of the plugin.
public override string ToString() {
return Name;
}
// equals and hashcode have to be implemented because we want to compare PluginDescriptions from
// different AppDomains and serialization destroys reference equality
///
/// Checks whether the given object is equal to the current plugin.
///
/// The object to compare.
/// true if it is equal, false otherwise.
public override bool Equals(object obj) {
PluginDescription other = obj as PluginDescription;
if (other == null) return false;
return other.Name == this.Name && other.Version == this.Version;
}
///
/// Gets the hash code of the current plugin.
///
/// The hash code of the plugin.
public override int GetHashCode() {
if (version != null) {
return name.GetHashCode() + version.GetHashCode();
} else return name.GetHashCode();
}
}
}