1 | using System;
|
---|
2 | using System.Collections.Concurrent;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.IO;
|
---|
5 | using System.Linq;
|
---|
6 | using System.Reflection;
|
---|
7 | using System.Text;
|
---|
8 | using System.Web.Http;
|
---|
9 | using System.Web.Http.Controllers;
|
---|
10 |
|
---|
11 | namespace HeuristicLab.Services.WebApp {
|
---|
12 | public class Plugin {
|
---|
13 | private HttpConfiguration configuration;
|
---|
14 | public string Name { get; set; }
|
---|
15 | public string Directory { get; set; }
|
---|
16 | public string AssemblyName { get; set; }
|
---|
17 | public DateTime? LastReload { get; set; }
|
---|
18 |
|
---|
19 | private IDictionary<string, HttpControllerDescriptor> controllers;
|
---|
20 | public IDictionary<string, HttpControllerDescriptor> Controllers {
|
---|
21 | get { return controllers ?? (controllers = new ConcurrentDictionary<string, HttpControllerDescriptor>()); }
|
---|
22 | }
|
---|
23 |
|
---|
24 | public void Configure(HttpConfiguration configuration) {
|
---|
25 | if (this.configuration != configuration) {
|
---|
26 | this.configuration = configuration;
|
---|
27 | ReloadControllers();
|
---|
28 | }
|
---|
29 | }
|
---|
30 |
|
---|
31 | public HttpControllerDescriptor GetController(string name) {
|
---|
32 | HttpControllerDescriptor controller;
|
---|
33 | Controllers.TryGetValue(name, out controller);
|
---|
34 | return controller;
|
---|
35 | }
|
---|
36 |
|
---|
37 | public void ReloadControllers() {
|
---|
38 | AssemblyName = null;
|
---|
39 | Controllers.Clear();
|
---|
40 | LastReload = DateTime.Now;
|
---|
41 | if (configuration == null)
|
---|
42 | return;
|
---|
43 | try {
|
---|
44 | string searchPattern = string.Format("HeuristicLab.Services.WebApp.{0}*.dll", Name);
|
---|
45 | var assemblies = System.IO.Directory.GetFiles(Directory, searchPattern);
|
---|
46 | if (!assemblies.Any())
|
---|
47 | return;
|
---|
48 | var assemblyPath = assemblies.First();
|
---|
49 | var assembly = Assembly.Load(File.ReadAllBytes(assemblyPath));
|
---|
50 | var assemblyTypes = assembly.GetTypes();
|
---|
51 | var apiControllers = assemblyTypes.Where(c => typeof(ApiController).IsAssignableFrom(c)).ToList();
|
---|
52 | foreach (var apiController in apiControllers) {
|
---|
53 | var controllerName = apiController.Name.Remove(apiController.Name.Length - 10).ToLower();
|
---|
54 | Controllers.Add(controllerName, new HttpControllerDescriptor(configuration, controllerName, apiController));
|
---|
55 | }
|
---|
56 | AssemblyName = Path.GetFileName(assemblyPath);
|
---|
57 | }
|
---|
58 | catch (ReflectionTypeLoadException ex) {
|
---|
59 | StringBuilder sb = new StringBuilder();
|
---|
60 | foreach (Exception exSub in ex.LoaderExceptions) {
|
---|
61 | sb.AppendLine(exSub.Message);
|
---|
62 | FileNotFoundException exFileNotFound = exSub as FileNotFoundException;
|
---|
63 | if (exFileNotFound != null) {
|
---|
64 | if (!string.IsNullOrEmpty(exFileNotFound.FusionLog)) {
|
---|
65 | sb.AppendLine("Fusion Log:");
|
---|
66 | sb.AppendLine(exFileNotFound.FusionLog);
|
---|
67 | }
|
---|
68 | }
|
---|
69 | sb.AppendLine();
|
---|
70 | }
|
---|
71 | AssemblyName = "Error loading assembly: " + sb.ToString();
|
---|
72 | Controllers.Clear();
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|
76 | } |
---|