Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/01/15 14:59:47 (9 years ago)
Author:
ascheibe
Message:

#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

Location:
stable
Files:
2 edited
1 copied

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Services.WebApp/3.3/PluginManager.cs

    r12428 r12563  
    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 IEnumerable<Plugin> Plugins {
     41      get { return plugins.Values; }
     42    }
     43
     44    public static string PluginsDirectory {
    2045      get { return string.Format(@"{0}WebApp\plugins", HttpRuntime.AppDomainAppPath); }
    2146    }
     
    2348    public PluginManager() {
    2449      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      }
    2591    }
    2692
    2793    public Plugin GetPlugin(string name) {
    28       Plugin plugin;
    29       plugins.TryGetValue(name, out plugin);
     94      Plugin plugin = LookupPlugin(name);
    3095      if (plugin == null) {
    3196        string directory = string.Format(@"{0}\{1}", PluginsDirectory, name);
    3297        if (Directory.Exists(directory)) {
    33           plugin = new Plugin {
    34             Name = name,
    35             Directory = directory
    36           };
    37           plugin.Configure(Configuration);
     98          plugin = new Plugin(name, directory, Configuration);
    3899          plugins.Add(name, plugin);
    39100        }
     
    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) {
    56           plugin = new Plugin {
    57             Name = pluginName,
    58             Directory = directory
    59           };
    60           plugin.Configure(Configuration);
     115          plugin = new Plugin(pluginName, directory, Configuration);
    61116          plugins.Add(pluginName, plugin);
    62117        }
    63118      }
    64119    }
     120
     121    private Plugin LookupPlugin(string name) {
     122      Plugin plugin;
     123      plugins.TryGetValue(name, out plugin);
     124      return plugin;
     125    }
    65126  }
    66127}
Note: See TracChangeset for help on using the changeset viewer.