Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/10/15 14:05:24 (9 years ago)
Author:
dglaser
Message:

#2388: Added PluginManager and updated WebApp project

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HiveStatistics/sources/HeuristicLab.Services.WebApp/Controllers/WebAppControllerSelector.cs

    r12419 r12425  
    22using System.Collections.Concurrent;
    33using System.Collections.Generic;
    4 using System.IO;
    54using System.Linq;
    65using System.Net.Http;
    76using System.Reflection;
    8 using System.Web;
    97using System.Web.Http;
    108using System.Web.Http.Controllers;
     
    1412  public class WebAppHttpControllerSelector : DefaultHttpControllerSelector {
    1513    private readonly HttpConfiguration configuration;
    16     private readonly IDictionary<string, IDictionary<string, HttpControllerDescriptor>> plugins;
     14    private readonly IDictionary<string, HttpControllerDescriptor> controllers;
     15    private readonly PluginManager pluginManager = PluginManager.Instance;
    1716
    1817    public WebAppHttpControllerSelector(HttpConfiguration configuration)
    1918      : base(configuration) {
    2019      this.configuration = configuration;
    21       plugins = new ConcurrentDictionary<string, IDictionary<string, HttpControllerDescriptor>>();
     20      pluginManager.Configuration = configuration;
     21      controllers = new ConcurrentDictionary<string, HttpControllerDescriptor>();
     22      LoadAppControllers();
     23    }
     24
     25    private void LoadAppControllers() {
     26      var assembly = Assembly.GetExecutingAssembly();
     27      var assemblyTypes = assembly.GetTypes();
     28      var apiControllers = assemblyTypes.Where(c => typeof(ApiController).IsAssignableFrom(c)).ToList();
     29      foreach (var apiController in apiControllers) {
     30        var apiControllerName = apiController.Name.Remove(apiController.Name.Length - 10).ToLower();
     31        controllers.Add(apiControllerName, new HttpControllerDescriptor(configuration, apiControllerName, apiController));
     32      }
    2233    }
    2334
     
    3142        throw new ArgumentException("invalid request path");
    3243      }
    33       string pluginName = parts[startIndex + 1];
     44      string pluginName = parts[startIndex + 1].ToLower();
    3445      string controllerName = parts[startIndex + 2].ToLower();
    35       IDictionary<string, HttpControllerDescriptor> controllers;
    36       if (!plugins.TryGetValue(pluginName, out controllers)) {
    37         var assembly = Assembly.GetExecutingAssembly();
    38         try {
    39           if (pluginName != "App") {
    40             string directory = string.Format(@"{0}WebApp\plugins\{1}\", HttpRuntime.AppDomainAppPath, pluginName);
    41             string[] assemblies = Directory.GetFiles(directory, string.Format("HeuristicLab.Services.WebApp.{0}*.dll", pluginName));
    42             if (assemblies.Length < 1) {
    43               throw new ArgumentException("invalid plugin '{0}'", pluginName);
    44             }
    45             assembly = Assembly.Load(File.ReadAllBytes(assemblies.First()));
    46           }
    47           var assemblyTypes = assembly.GetTypes();
    48           var apiControllers = assemblyTypes.Where(c => typeof(ApiController).IsAssignableFrom(c)).ToList();
    49           if (apiControllers.Any()) {
    50             controllers = new ConcurrentDictionary<string, HttpControllerDescriptor>();
    51             foreach (var apiController in apiControllers) {
    52               var apiControllerName = apiController.Name.Remove(apiController.Name.Length - 10).ToLower();
    53               controllers.Add(apiControllerName, new HttpControllerDescriptor(configuration, apiControllerName, apiController));
    54             }
    55             plugins.Add(pluginName, controllers);
    56           }
    57         }
    58         catch (Exception) {
    59           throw new ArgumentException("error loading plugin '{0}'", pluginName);
    60         }
     46      // load controller
     47      if (pluginName == "app") {
     48        // from main app
     49        HttpControllerDescriptor controller;
     50        controllers.TryGetValue(controllerName, out controller);
     51        return controller;
    6152      }
    62       if (controllers == null) {
    63         throw new ArgumentException("invalid plugin '{0}'", pluginName);
     53      // from plugin
     54      var plugin = pluginManager.GetPlugin(pluginName);
     55      if (plugin == null) {
     56        throw new ArgumentException(string.Format("invalid plugin '{0}'", pluginName));
    6457      }
    65       HttpControllerDescriptor controller;
    66       controllers.TryGetValue(controllerName, out controller);
    67       return controller;
     58      return plugin.GetController(controllerName);
    6859    }
    6960  }
Note: See TracChangeset for help on using the changeset viewer.