Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/12/15 14:59:54 (9 years ago)
Author:
dglaser
Message:

#2394: Improved PluginManager and updated hive status monitor.

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
     22using System.Collections.Concurrent;
    223using System.Collections.Generic;
    324using System.IO;
     
    1738    public HttpConfiguration Configuration { get; set; }
    1839
    19     private string PluginsDirectory {
     40    public static string PluginsDirectory {
    2041      get { return string.Format(@"{0}WebApp\plugins", HttpRuntime.AppDomainAppPath); }
    2142    }
     
    2344    public PluginManager() {
    2445      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      }
    2587    }
    2688
    2789    public Plugin GetPlugin(string name) {
    28       Plugin plugin;
    29       plugins.TryGetValue(name, out plugin);
     90      Plugin plugin = LookupPlugin(name);
    3091      if (plugin == null) {
    3192        string directory = string.Format(@"{0}\{1}", PluginsDirectory, name);
     
    43104
    44105    public IEnumerable<Plugin> GetPlugins() {
    45       DiscoverPlugins();
    46106      return plugins.Values;
    47107    }
    48108
    49     private void DiscoverPlugins() {
     109    public void DiscoverPlugins() {
    50110      var pluginDirectories = Directory.GetDirectories(PluginsDirectory);
    51111      foreach (var directory in pluginDirectories) {
    52112        string pluginName = Path.GetFileName(directory);
    53         Plugin plugin;
    54         plugins.TryGetValue(pluginName, out plugin);
     113        Plugin plugin = LookupPlugin(pluginName);
    55114        if (plugin == null) {
    56115          plugin = new Plugin {
     
    63122      }
    64123    }
     124
     125    private Plugin LookupPlugin(string name) {
     126      Plugin plugin;
     127      plugins.TryGetValue(name, out plugin);
     128      return plugin;
     129    }
    65130  }
    66131}
Note: See TracChangeset for help on using the changeset viewer.