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;
|
---|
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 |
|
---|
40 | public static string PluginsDirectory {
|
---|
41 | get { return string.Format(@"{0}WebApp\plugins", HttpRuntime.AppDomainAppPath); }
|
---|
42 | }
|
---|
43 |
|
---|
44 | public PluginManager() {
|
---|
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 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | public Plugin GetPlugin(string name) {
|
---|
90 | Plugin plugin = LookupPlugin(name);
|
---|
91 | if (plugin == null) {
|
---|
92 | string directory = string.Format(@"{0}\{1}", PluginsDirectory, name);
|
---|
93 | if (Directory.Exists(directory)) {
|
---|
94 | plugin = new Plugin {
|
---|
95 | Name = name,
|
---|
96 | Directory = directory
|
---|
97 | };
|
---|
98 | plugin.Configure(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 {
|
---|
116 | Name = pluginName,
|
---|
117 | Directory = directory
|
---|
118 | };
|
---|
119 | plugin.Configure(Configuration);
|
---|
120 | plugins.Add(pluginName, plugin);
|
---|
121 | }
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | private Plugin LookupPlugin(string name) {
|
---|
126 | Plugin plugin;
|
---|
127 | plugins.TryGetValue(name, out plugin);
|
---|
128 | return plugin;
|
---|
129 | }
|
---|
130 | }
|
---|
131 | } |
---|