Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Services.WebApp/3.3/PluginManager.cs @ 15908

Last change on this file since 15908 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 4.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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;
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
40    public IEnumerable<Plugin> Plugins {
41      get { return plugins.Values; }
42    }
43
44    public static string PluginsDirectory {
45      get {
46        return Path.Combine(HttpRuntime.AppDomainAppPath, "WebApp", "plugins");
47      }
48    }
49
50    public PluginManager() {
51      plugins = new ConcurrentDictionary<string, Plugin>();
52      var fileWatcher = new FileSystemWatcher(PluginsDirectory, "*") {
53        IncludeSubdirectories = true,
54        EnableRaisingEvents = true
55      };
56      fileWatcher.Created += OnFilesChanged;
57      fileWatcher.Changed += OnFilesChanged;
58      fileWatcher.Deleted += OnFilesChanged;
59      fileWatcher.Renamed += OnFilesChanged;
60    }
61
62    private void OnFilesChanged(object sender, FileSystemEventArgs args) {
63      string path = args.FullPath.Remove(0, PluginsDirectory.Length + 1);
64      var pathParts = path.Split(Path.DirectorySeparatorChar);
65      string pluginName = pathParts[0];
66      if (pathParts.Length <= 2) {
67        switch (args.ChangeType) {
68          case WatcherChangeTypes.Created:
69            GetPlugin(pluginName);
70            break;
71
72          case WatcherChangeTypes.Deleted:
73            plugins.Remove(pluginName);
74            break;
75
76          case WatcherChangeTypes.Renamed:
77            RenamedEventArgs renamedArgs = (RenamedEventArgs)args;
78            string oldPath = renamedArgs.OldFullPath.Remove(0, PluginsDirectory.Length + 1);
79            var oldPathParts = oldPath.Split(Path.DirectorySeparatorChar);
80            string oldPluginName = oldPathParts[0];
81            plugins.Remove(oldPluginName);
82            GetPlugin(pluginName);
83            break;
84
85          case WatcherChangeTypes.Changed:
86            Plugin plugin = LookupPlugin(pluginName);
87            if (plugin != null) {
88              plugin.ReloadControllers();
89            }
90            break;
91        }
92      }
93    }
94
95    public Plugin GetPlugin(string name) {
96      Plugin plugin = LookupPlugin(name);
97      if (plugin == null) {
98        string directory = Path.Combine(PluginsDirectory, name);
99        if (Directory.Exists(directory)) {
100          plugin = new Plugin(name, directory, Configuration);
101          plugins.Add(name, plugin);
102        }
103      }
104      return plugin;
105    }
106
107    public IEnumerable<Plugin> GetPlugins() {
108      return plugins.Values;
109    }
110
111    public void DiscoverPlugins() {
112      var pluginDirectories = Directory.GetDirectories(PluginsDirectory);
113      foreach (var directory in pluginDirectories) {
114        string pluginName = Path.GetFileName(directory);
115        Plugin plugin = LookupPlugin(pluginName);
116        if (plugin == null) {
117          plugin = new Plugin(pluginName, directory, Configuration);
118          plugins.Add(pluginName, plugin);
119        }
120      }
121    }
122
123    private Plugin LookupPlugin(string name) {
124      Plugin plugin;
125      plugins.TryGetValue(name, out plugin);
126      return plugin;
127    }
128  }
129}
Note: See TracBrowser for help on using the repository browser.