Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Services.WebApp/3.3/PluginManager.cs @ 12782

Last change on this file since 12782 was 12514, checked in by dglaser, 9 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: 4.2 KB
RevLine 
[12435]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.Collections.Concurrent;
[12428]23using System.Collections.Generic;
24using System.IO;
25using System.Web;
26using System.Web.Http;
27
28namespace HeuristicLab.Services.WebApp {
29  public class PluginManager {
30
31    private static PluginManager instance;
32    public static PluginManager Instance {
33      get { return instance ?? (instance = new PluginManager()); }
34    }
35
36    private readonly IDictionary<string, Plugin> plugins;
37
38    public HttpConfiguration Configuration { get; set; }
39
[12514]40    public IEnumerable<Plugin> Plugins {
41      get { return plugins.Values; }
42    }
43
[12435]44    public static string PluginsDirectory {
[12428]45      get { return string.Format(@"{0}WebApp\plugins", HttpRuntime.AppDomainAppPath); }
46    }
47
48    public PluginManager() {
49      plugins = new ConcurrentDictionary<string, Plugin>();
[12435]50      var fileWatcher = new FileSystemWatcher(PluginsDirectory, "*") {
51        IncludeSubdirectories = true,
52        EnableRaisingEvents = true
53      };
54      fileWatcher.Created += OnFilesChanged;
55      fileWatcher.Changed += OnFilesChanged;
56      fileWatcher.Deleted += OnFilesChanged;
57      fileWatcher.Renamed += OnFilesChanged;
[12428]58    }
59
[12435]60    private void OnFilesChanged(object sender, FileSystemEventArgs args) {
61      string path = args.FullPath.Remove(0, PluginsDirectory.Length + 1);
[12514]62      var pathParts = path.Split(Path.PathSeparator);
[12435]63      string pluginName = pathParts[0];
64      if (pathParts.Length == 1) {
65        switch (args.ChangeType) {
66          case WatcherChangeTypes.Created:
67            GetPlugin(pluginName);
68            break;
69
70          case WatcherChangeTypes.Deleted:
71            plugins.Remove(pluginName);
72            break;
73
74          case WatcherChangeTypes.Renamed:
75            RenamedEventArgs renamedArgs = (RenamedEventArgs)args;
76            string oldPath = renamedArgs.OldFullPath.Remove(0, PluginsDirectory.Length + 1);
[12514]77            var oldPathParts = oldPath.Split(Path.PathSeparator);
[12435]78            string oldPluginName = oldPathParts[0];
79            plugins.Remove(oldPluginName);
80            GetPlugin(pluginName);
81            break;
82
83          case WatcherChangeTypes.Changed:
84            Plugin plugin = LookupPlugin(pluginName);
85            if (plugin != null) {
86              plugin.ReloadControllers();
87            }
88            break;
89        }
90      }
91    }
92
[12428]93    public Plugin GetPlugin(string name) {
[12435]94      Plugin plugin = LookupPlugin(name);
[12428]95      if (plugin == null) {
96        string directory = string.Format(@"{0}\{1}", PluginsDirectory, name);
97        if (Directory.Exists(directory)) {
[12514]98          plugin = new Plugin(name, directory, Configuration);
[12428]99          plugins.Add(name, plugin);
100        }
101      }
102      return plugin;
103    }
104
105    public IEnumerable<Plugin> GetPlugins() {
106      return plugins.Values;
107    }
108
[12435]109    public void DiscoverPlugins() {
[12428]110      var pluginDirectories = Directory.GetDirectories(PluginsDirectory);
111      foreach (var directory in pluginDirectories) {
112        string pluginName = Path.GetFileName(directory);
[12435]113        Plugin plugin = LookupPlugin(pluginName);
[12428]114        if (plugin == null) {
[12514]115          plugin = new Plugin(pluginName, directory, Configuration);
[12428]116          plugins.Add(pluginName, plugin);
117        }
118      }
119    }
[12435]120
121    private Plugin LookupPlugin(string name) {
122      Plugin plugin;
123      plugins.TryGetValue(name, out plugin);
124      return plugin;
125    }
[12428]126  }
127}
Note: See TracBrowser for help on using the repository browser.