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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Concurrent;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using System.IO;
|
---|
26 | using System.Linq;
|
---|
27 | using System.Reflection;
|
---|
28 | using System.Web.Http;
|
---|
29 | using System.Web.Http.Controllers;
|
---|
30 |
|
---|
31 | namespace 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 | }
|
---|