Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CEDMA-Refactoring-Ticket419/HeuristicLab.PluginInfrastructure/BaseClasses/ApplicationBase.cs @ 978

Last change on this file since 978 was 242, checked in by gkronber, 17 years ago

added support for "service" applications that are restarted automatically when they crash with an exception. (related to #149)

File size: 3.1 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.Diagnostics;
26
27namespace HeuristicLab.PluginInfrastructure {
28  public abstract class ApplicationBase : IApplication {
29    private string name;
30    private Version version;
31    private string description;
32    private bool autoRestart;
33
34    public ApplicationBase() {
35      ReadAttributes();
36    }
37
38    private void ReadAttributes() {
39      object[] pluginAttributes = this.GetType().GetCustomAttributes(typeof(ClassInfoAttribute), false);
40
41      // exactly one attribute of the type ClassInfoAttribute must be given
42      if(pluginAttributes.Length != 1) {
43        throw new InvalidPluginException();
44      }
45
46      // after the assertion we are sure that the array access will not fail
47      ClassInfoAttribute pluginAttribute = (ClassInfoAttribute)pluginAttributes[0];
48      if(pluginAttribute != null) {
49        // if the plugin name is not explicitly set in the attribute then the default plugin name is the FullName of the type
50        if(pluginAttribute.Name != null) {
51          this.name = pluginAttribute.Name;
52        } else {
53          this.name = this.GetType().FullName;
54        }
55
56        // if the version is not explicitly set in the attribute then the version of the assembly is used as default
57        if(pluginAttribute.Version != null) {
58          this.version = new Version(pluginAttribute.Version);
59        } else {
60          this.version = this.GetType().Assembly.GetName().Version;
61        }
62
63        // if the description is not explicitly set in the attribute then the name of name of the application is used as default
64        if(pluginAttribute.Description != null) {
65          this.description = pluginAttribute.Description;
66        } else {
67          this.description = name;
68        }
69
70        this.autoRestart = pluginAttribute.AutoRestart;
71      }
72    }
73
74
75    #region IApplication Members
76
77    public string Name {
78      get { return name; }
79    }
80
81    public Version Version {
82      get { return version; }
83    }
84
85    public string Description {
86      get { return description; }
87    }
88
89    public bool AutoRestart {
90      get { return autoRestart; }
91    }
92
93    public abstract void Run();
94
95    #endregion
96  }
97}
Note: See TracBrowser for help on using the repository browser.