Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Services.WebApp/3.3/Plugin.cs @ 12435

Last change on this file since 12435 was 12435, checked in by dglaser, 9 years ago

#2394: Improved PluginManager and updated hive status monitor.

File size: 3.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Concurrent;
24using System.Collections.Generic;
25using System.IO;
26using System.Linq;
27using System.Reflection;
28using System.Web.Http;
29using System.Web.Http.Controllers;
30
31namespace HeuristicLab.Services.WebApp {
32  public class Plugin {
33    private HttpConfiguration configuration;
34    public string Name { get; set; }
35    public string Directory { get; set; }
36    public string AssemblyName { get; set; }
37    public DateTime? LastReload { get; set; }
38
39    private IDictionary<string, HttpControllerDescriptor> controllers;
40    public IDictionary<string, HttpControllerDescriptor> Controllers {
41      get { return controllers ?? (controllers = new ConcurrentDictionary<string, HttpControllerDescriptor>()); }
42    }
43
44    public void Configure(HttpConfiguration configuration) {
45      if (this.configuration != configuration) {
46        this.configuration = configuration;
47        ReloadControllers();
48      }
49    }
50
51    public HttpControllerDescriptor GetController(string name) {
52      HttpControllerDescriptor controller;
53      Controllers.TryGetValue(name, out controller);
54      return controller;
55    }
56
57    public void ReloadControllers() {
58      AssemblyName = null;
59      Controllers.Clear();
60      LastReload = DateTime.Now;
61      if (configuration == null)
62        return;
63      try {
64        string searchPattern = string.Format("HeuristicLab.Services.WebApp.{0}*.dll", Name);
65        var assemblies = System.IO.Directory.GetFiles(Directory, searchPattern);
66        if (!assemblies.Any())
67          return;
68        var assemblyPath = assemblies.First();
69        var assembly = Assembly.Load(File.ReadAllBytes(assemblyPath));
70        var assemblyTypes = assembly.GetTypes();
71        var apiControllers = assemblyTypes.Where(c => typeof(ApiController).IsAssignableFrom(c)).ToList();
72        foreach (var apiController in apiControllers) {
73          var controllerName = apiController.Name.Remove(apiController.Name.Length - 10).ToLower();
74          Controllers.Add(controllerName, new HttpControllerDescriptor(configuration, controllerName, apiController));
75        }
76        AssemblyName = Path.GetFileName(assemblyPath);
77      }
78      catch (Exception) {
79        AssemblyName = "Error loading assembly";
80        Controllers.Clear();
81      }
82    }
83  }
84}
Note: See TracBrowser for help on using the repository browser.