[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 |
|
---|
| 22 | using System.Collections.Concurrent;
|
---|
[12428] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.IO;
|
---|
| 25 | using System.Web;
|
---|
| 26 | using System.Web.Http;
|
---|
| 27 |
|
---|
| 28 | namespace 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 {
|
---|
[12931] | 45 | get {
|
---|
| 46 | return Path.Combine(HttpRuntime.AppDomainAppPath, "WebApp", "plugins");
|
---|
| 47 | }
|
---|
[12428] | 48 | }
|
---|
| 49 |
|
---|
| 50 | public PluginManager() {
|
---|
| 51 | plugins = new ConcurrentDictionary<string, Plugin>();
|
---|
[12435] | 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;
|
---|
[12428] | 60 | }
|
---|
| 61 |
|
---|
[12435] | 62 | private void OnFilesChanged(object sender, FileSystemEventArgs args) {
|
---|
| 63 | string path = args.FullPath.Remove(0, PluginsDirectory.Length + 1);
|
---|
[12931] | 64 | var pathParts = path.Split(Path.DirectorySeparatorChar);
|
---|
[12435] | 65 | string pluginName = pathParts[0];
|
---|
[12931] | 66 | if (pathParts.Length <= 2) {
|
---|
[12435] | 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);
|
---|
[12931] | 79 | var oldPathParts = oldPath.Split(Path.DirectorySeparatorChar);
|
---|
[12435] | 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 |
|
---|
[12428] | 95 | public Plugin GetPlugin(string name) {
|
---|
[12435] | 96 | Plugin plugin = LookupPlugin(name);
|
---|
[12428] | 97 | if (plugin == null) {
|
---|
[12931] | 98 | string directory = Path.Combine(PluginsDirectory, name);
|
---|
[12428] | 99 | if (Directory.Exists(directory)) {
|
---|
[12514] | 100 | plugin = new Plugin(name, directory, Configuration);
|
---|
[12428] | 101 | plugins.Add(name, plugin);
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 | return plugin;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | public IEnumerable<Plugin> GetPlugins() {
|
---|
| 108 | return plugins.Values;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[12435] | 111 | public void DiscoverPlugins() {
|
---|
[12428] | 112 | var pluginDirectories = Directory.GetDirectories(PluginsDirectory);
|
---|
| 113 | foreach (var directory in pluginDirectories) {
|
---|
| 114 | string pluginName = Path.GetFileName(directory);
|
---|
[12435] | 115 | Plugin plugin = LookupPlugin(pluginName);
|
---|
[12428] | 116 | if (plugin == null) {
|
---|
[12514] | 117 | plugin = new Plugin(pluginName, directory, Configuration);
|
---|
[12428] | 118 | plugins.Add(pluginName, plugin);
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
[12435] | 122 |
|
---|
| 123 | private Plugin LookupPlugin(string name) {
|
---|
| 124 | Plugin plugin;
|
---|
| 125 | plugins.TryGetValue(name, out plugin);
|
---|
| 126 | return plugin;
|
---|
| 127 | }
|
---|
[12428] | 128 | }
|
---|
| 129 | } |
---|