Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12720 was 12563, checked in by ascheibe, 9 years ago

#2394 merged r12428, r12429, r12430, r12435, r12442, r12443, r12445, r12514, r12517, r12519, r12520, r12521, r12523, r12532, r12542, r12546, r12552, r12553, r12556, r12557, r12559, r12561, r12146, r12457 into stable

File size: 4.2 KB
Line 
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;
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 { return string.Format(@"{0}WebApp\plugins", HttpRuntime.AppDomainAppPath); }
46    }
47
48    public PluginManager() {
49      plugins = new ConcurrentDictionary<string, Plugin>();
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;
58    }
59
60    private void OnFilesChanged(object sender, FileSystemEventArgs args) {
61      string path = args.FullPath.Remove(0, PluginsDirectory.Length + 1);
62      var pathParts = path.Split(Path.PathSeparator);
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);
77            var oldPathParts = oldPath.Split(Path.PathSeparator);
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
93    public Plugin GetPlugin(string name) {
94      Plugin plugin = LookupPlugin(name);
95      if (plugin == null) {
96        string directory = string.Format(@"{0}\{1}", PluginsDirectory, name);
97        if (Directory.Exists(directory)) {
98          plugin = new Plugin(name, directory, Configuration);
99          plugins.Add(name, plugin);
100        }
101      }
102      return plugin;
103    }
104
105    public IEnumerable<Plugin> GetPlugins() {
106      return plugins.Values;
107    }
108
109    public void DiscoverPlugins() {
110      var pluginDirectories = Directory.GetDirectories(PluginsDirectory);
111      foreach (var directory in pluginDirectories) {
112        string pluginName = Path.GetFileName(directory);
113        Plugin plugin = LookupPlugin(pluginName);
114        if (plugin == null) {
115          plugin = new Plugin(pluginName, directory, Configuration);
116          plugins.Add(pluginName, plugin);
117        }
118      }
119    }
120
121    private Plugin LookupPlugin(string name) {
122      Plugin plugin;
123      plugins.TryGetValue(name, out plugin);
124      return plugin;
125    }
126  }
127}
Note: See TracBrowser for help on using the repository browser.