Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.PluginInfrastructure/BaseClasses/PluginBase.cs @ 1729

Last change on this file since 1729 was 1229, checked in by gkronber, 16 years ago

Merged implementation of #471 (OnLoad hook for plugins) (r1228) from CEDMA branch into the trunk.

File size: 4.3 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Reflection;
26using System.Diagnostics;
27
28namespace HeuristicLab.PluginInfrastructure {
29  /// <summary>
30  /// Default implementation of the IPlugin interface.
31  /// </summary>
32  public abstract class PluginBase : IPlugin {
33    private string name;
34    private Version version;
35    private string[] files;
36    private string description;
37
38    /// <summary>
39    /// Initializes a new instance of <see cref="PluginBase"/>.
40    /// </summary>
41    public PluginBase() {
42      ReadAttributes();
43    }
44
45    private void ReadAttributes() {
46      object[] pluginAttributes = this.GetType().GetCustomAttributes(typeof(ClassInfoAttribute), false);
47
48      // exactly one attribute of the type ClassInfoAttribute must be given
49      if (pluginAttributes.Length != 1) {
50        throw new InvalidPluginException();
51      }
52
53      // after the assertion we are sure that the array access will not fail
54      ClassInfoAttribute pluginAttribute = (ClassInfoAttribute)pluginAttributes[0];
55
56      // if the plugin name is not explicitly set in the attribute then the default plugin name is the FullName of the type
57      if (pluginAttribute != null && pluginAttribute.Name != null) {
58        this.name = pluginAttribute.Name;
59      } else {
60        this.name = this.GetType().FullName;
61      }
62
63      // if the version is not explicitly set in the attribute then the version of the assembly is used as default
64      if (pluginAttribute != null && pluginAttribute.Version != null) {
65        this.version = new Version(pluginAttribute.Version);
66      } else {
67        this.version = this.GetType().Assembly.GetName().Version;
68      }
69
70      // if the description is not explicitly set in the attribute then the name of name of the plugin is used as default
71      if (pluginAttribute != null && pluginAttribute.Description != null) {
72        this.description = pluginAttribute.Description;
73      } else {
74        this.description = name;
75      }
76
77      // get all attributes of type PluginFileAttribute, multiple usage is possible
78      PluginFileAttribute[] fileAttributes = (PluginFileAttribute[])this.GetType().GetCustomAttributes(typeof(PluginFileAttribute), false);
79
80      // exctract the file names from the attributes
81      this.files = new string[fileAttributes.Length];
82      int i = 0;
83      foreach (PluginFileAttribute fileAttr in fileAttributes) {
84        files[i++] = fileAttr.Filename;
85      }
86    }
87
88    #region IPlugin Members
89    /// <summary>
90    /// Gets the name of the plugin.
91    /// </summary>
92    public string Name {
93      get {
94        return name;
95      }
96    }
97
98    /// <summary>
99    /// Gets the version of the plugin.
100    /// </summary>
101    public Version Version {
102      get {
103        return version;
104      }
105    }
106
107    /// <summary>
108    /// Gets the description of the plugin.
109    /// </summary>
110    public string Description {
111      get {
112        return description;
113      }
114    }
115
116    /// <inheritdoc/>
117    public  string[] Files {
118      get {
119        return files;
120      }
121    }
122
123    /// <inhertidoc>
124    public virtual void OnLoad() { }
125    /// <inhertidoc>
126    public virtual void OnInstall() { }
127    /// <inhertidoc>
128    public virtual void OnDelete() { }
129    /// <inhertidoc>
130    public virtual void OnPreUpdate() { }
131    /// <inhertidoc>
132    public virtual void OnPostUpdate() { }
133
134    #endregion
135
136  }
137}
Note: See TracBrowser for help on using the repository browser.