- Timestamp:
- 06/12/15 14:59:54 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Services.WebApp/3.3/PluginManager.cs
r12428 r12435 1 using System.Collections.Concurrent; 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; 2 23 using System.Collections.Generic; 3 24 using System.IO; … … 17 38 public HttpConfiguration Configuration { get; set; } 18 39 19 p rivatestring PluginsDirectory {40 public static string PluginsDirectory { 20 41 get { return string.Format(@"{0}WebApp\plugins", HttpRuntime.AppDomainAppPath); } 21 42 } … … 23 44 public PluginManager() { 24 45 plugins = new ConcurrentDictionary<string, Plugin>(); 46 var fileWatcher = new FileSystemWatcher(PluginsDirectory, "*") { 47 IncludeSubdirectories = true, 48 EnableRaisingEvents = true 49 }; 50 fileWatcher.Created += OnFilesChanged; 51 fileWatcher.Changed += OnFilesChanged; 52 fileWatcher.Deleted += OnFilesChanged; 53 fileWatcher.Renamed += OnFilesChanged; 54 } 55 56 private void OnFilesChanged(object sender, FileSystemEventArgs args) { 57 string path = args.FullPath.Remove(0, PluginsDirectory.Length + 1); 58 var pathParts = path.Split('\\'); 59 string pluginName = pathParts[0]; 60 if (pathParts.Length == 1) { 61 switch (args.ChangeType) { 62 case WatcherChangeTypes.Created: 63 GetPlugin(pluginName); 64 break; 65 66 case WatcherChangeTypes.Deleted: 67 plugins.Remove(pluginName); 68 break; 69 70 case WatcherChangeTypes.Renamed: 71 RenamedEventArgs renamedArgs = (RenamedEventArgs)args; 72 string oldPath = renamedArgs.OldFullPath.Remove(0, PluginsDirectory.Length + 1); 73 var oldPathParts = oldPath.Split('\\'); 74 string oldPluginName = oldPathParts[0]; 75 plugins.Remove(oldPluginName); 76 GetPlugin(pluginName); 77 break; 78 79 case WatcherChangeTypes.Changed: 80 Plugin plugin = LookupPlugin(pluginName); 81 if (plugin != null) { 82 plugin.ReloadControllers(); 83 } 84 break; 85 } 86 } 25 87 } 26 88 27 89 public Plugin GetPlugin(string name) { 28 Plugin plugin; 29 plugins.TryGetValue(name, out plugin); 90 Plugin plugin = LookupPlugin(name); 30 91 if (plugin == null) { 31 92 string directory = string.Format(@"{0}\{1}", PluginsDirectory, name); … … 43 104 44 105 public IEnumerable<Plugin> GetPlugins() { 45 DiscoverPlugins();46 106 return plugins.Values; 47 107 } 48 108 49 p rivatevoid DiscoverPlugins() {109 public void DiscoverPlugins() { 50 110 var pluginDirectories = Directory.GetDirectories(PluginsDirectory); 51 111 foreach (var directory in pluginDirectories) { 52 112 string pluginName = Path.GetFileName(directory); 53 Plugin plugin; 54 plugins.TryGetValue(pluginName, out plugin); 113 Plugin plugin = LookupPlugin(pluginName); 55 114 if (plugin == null) { 56 115 plugin = new Plugin { … … 63 122 } 64 123 } 124 125 private Plugin LookupPlugin(string name) { 126 Plugin plugin; 127 plugins.TryGetValue(name, out plugin); 128 return plugin; 129 } 65 130 } 66 131 }
Note: See TracChangeset
for help on using the changeset viewer.