#region License Information
/* HeuristicLab
* Copyright (C) 2002-2011 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.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.
///
public string Description {
get { return description; }
internal set { description = value; }
}
private Version version;
///
/// Gets the version of the plugin.
///
public Version Version {
get { return version; }
internal set { version = value; }
}
private string contactName;
///
/// Gets or sets the name of the contact person for this plugin.
///
public string ContactName {
get { return contactName; }
internal set { contactName = value; }
}
private string contactEmail;
///
/// Gets or sets the e-mail address of the contact person for this plugin.
///
public string ContactEmail {
get { return contactEmail; }
internal set { contactEmail = value; }
}
private string licenseText;
///
/// Gets or sets the license text of the plugin.
///
public string LicenseText {
get { return licenseText; }
internal set { licenseText = value; }
}
private PluginState pluginState;
///
/// Gets or sets the plugin state.
///
public PluginState PluginState {
get { return pluginState; }
}
private string loadingErrorInformation;
///
/// Gets the error message why this plugin has been disabled.
///
internal string LoadingErrorInformation {
get {
return loadingErrorInformation;
}
}
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.Cast(); }
}
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);
}
///
/// Gets the locations (file names) of the assemblies that belong to this plugin.
///
public IEnumerable AssemblyLocations {
get { return Files.Where(f => f.Type == PluginFileType.Assembly).Select(f => f.Name); }
}
internal PluginDescription() {
pluginState = PluginState.Undefined;
}
internal void Disable(string loadingErrorInformation) {
if (pluginState != PluginState.Undefined)
throw new InvalidOperationException("Cannot disable a plugin in state " + pluginState);
pluginState = PluginState.Disabled;
this.loadingErrorInformation = loadingErrorInformation;
}
internal void Enable() {
if (pluginState != PluginState.Undefined)
throw new InvalidOperationException("Cannot enable a plugin in state " + pluginState);
pluginState = PluginState.Enabled;
}
internal void Load() {
if (!(pluginState == PluginState.Enabled || pluginState == PluginState.Loaded))
throw new InvalidOperationException("Cannot load a plugin in state " + pluginState);
pluginState = PluginState.Loaded;
nTimesLoaded++;
}
internal void Unload() {
if (pluginState != PluginState.Loaded)
throw new InvalidOperationException("Cannot 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 + " " + Version;
}
// 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();
}
}
}