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

Last change on this file since 12514 was 12514, checked in by dglaser, 5 years ago

#2394:

HeuristicLab.Services.WebApp-3.3:

  • implemented review comments
  • fixed a bug which caused the sidebar menu to collapse on non mobile devices

HeuristicLab.Services.WebApp.Status-3.3:

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