Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2877_HiveImprovements/HeuristicLab.Services.WebApp/3.3/Plugin.cs @ 16755

Last change on this file since 16755 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 3.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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    public string Name { get; set; }
34    public string Directory { get; set; }
35    public string AssemblyName { get; set; }
36    public string Exception { get; set; }
37    public DateTime? LastReload { get; set; }
38
39    private HttpConfiguration configuration;
40    public HttpConfiguration Configuration {
41      get { return configuration; }
42      set {
43        if (configuration != value) {
44          configuration = value;
45          ReloadControllers();
46        }
47      }
48    }
49
50    private IDictionary<string, HttpControllerDescriptor> controllers;
51    public IDictionary<string, HttpControllerDescriptor> Controllers {
52      get { return controllers ?? (controllers = new ConcurrentDictionary<string, HttpControllerDescriptor>()); }
53    }
54
55    public Plugin(string name, string directory, HttpConfiguration configuration) {
56      Name = name;
57      Directory = directory;
58      Configuration = configuration;
59    }
60
61    public HttpControllerDescriptor GetController(string name) {
62      HttpControllerDescriptor controller;
63      Controllers.TryGetValue(name, out controller);
64      return controller;
65    }
66
67    public void ReloadControllers() {
68      AssemblyName = null;
69      Exception = null;
70      Controllers.Clear();
71      LastReload = DateTime.Now;
72      if (configuration == null)
73        return;
74      try {
75        string searchPattern = string.Format("HeuristicLab.Services.WebApp.{0}*.dll", Name);
76        var assemblies = System.IO.Directory.GetFiles(Directory, searchPattern);
77        if (!assemblies.Any())
78          return;
79
80        var assemblyPath = assemblies.First();
81        AssemblyName = Path.GetFileName(assemblyPath);
82        var assembly = Assembly.Load(File.ReadAllBytes(assemblyPath));
83        var assemblyTypes = assembly.GetTypes();
84        var apiControllers = assemblyTypes.Where(c => typeof(ApiController).IsAssignableFrom(c)).ToList();
85        foreach (var apiController in apiControllers) {
86          var controllerName = apiController.Name.Remove(apiController.Name.Length - 10).ToLower();
87          Controllers.Add(controllerName, new HttpControllerDescriptor(configuration, controllerName, apiController));
88        }
89      }
90      catch (Exception e) {
91        Exception = e.ToString();
92        Controllers.Clear();
93      }
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.