Changeset 12425 for branches/HiveStatistics/sources/HeuristicLab.Services.WebApp/Controllers/WebAppControllerSelector.cs
- Timestamp:
- 06/10/15 14:05:24 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HiveStatistics/sources/HeuristicLab.Services.WebApp/Controllers/WebAppControllerSelector.cs
r12419 r12425 2 2 using System.Collections.Concurrent; 3 3 using System.Collections.Generic; 4 using System.IO;5 4 using System.Linq; 6 5 using System.Net.Http; 7 6 using System.Reflection; 8 using System.Web;9 7 using System.Web.Http; 10 8 using System.Web.Http.Controllers; … … 14 12 public class WebAppHttpControllerSelector : DefaultHttpControllerSelector { 15 13 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; 17 16 18 17 public WebAppHttpControllerSelector(HttpConfiguration configuration) 19 18 : base(configuration) { 20 19 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 } 22 33 } 23 34 … … 31 42 throw new ArgumentException("invalid request path"); 32 43 } 33 string pluginName = parts[startIndex + 1] ;44 string pluginName = parts[startIndex + 1].ToLower(); 34 45 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; 61 52 } 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)); 64 57 } 65 HttpControllerDescriptor controller; 66 controllers.TryGetValue(controllerName, out controller); 67 return controller; 58 return plugin.GetController(controllerName); 68 59 } 69 60 }
Note: See TracChangeset
for help on using the changeset viewer.