1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Json;
using MediaBrowser.Common.Logging;
using MediaBrowser.Common.Net;
using MediaBrowser.Common.Plugins;
namespace MediaBrowser.Common.Kernel
{
/// <summary>
/// Represents a shared base kernel for both the UI and server apps
/// </summary>
public abstract class BaseKernel<TConfigurationType>
where TConfigurationType : BaseConfiguration, new()
{
/// <summary>
/// Gets the path to the program data folder
/// </summary>
public string ProgramDataPath { get; private set; }
protected string PluginsPath
{
get
{
return Path.Combine(ProgramDataPath, "plugins");
}
}
protected string ConfigurationPath
{
get
{
return Path.Combine(ProgramDataPath, "config.js");
}
}
/// <summary>
/// Gets the current configuration
/// </summary>
public TConfigurationType Configuration { get; private set; }
/// <summary>
/// Gets the list of currently loaded plugins
/// </summary>
[ImportMany(typeof(BasePlugin))]
public IEnumerable<BasePlugin> Plugins { get; private set; }
/// <summary>
/// Both the UI and server will have a built-in HttpServer.
/// People will inevitably want remote control apps so it's needed in the UI too.
/// </summary>
public HttpServer HttpServer { get; private set; }
/// <summary>
/// Gets the kernel context. The UI kernel will have to override this.
/// </summary>
protected KernelContext KernelContext { get { return KernelContext.Server; } }
public BaseKernel()
{
ProgramDataPath = GetProgramDataPath();
Logger.LoggerInstance = new FileLogger(Path.Combine(ProgramDataPath, "Logs"));
}
public virtual void Init()
{
ReloadConfiguration();
ReloadHttpServer();
ReloadComposableParts();
}
protected void ReloadComposableParts()
{
if (!Directory.Exists(PluginsPath))
{
Directory.CreateDirectory(PluginsPath);
}
var catalog = new AggregateCatalog(Directory.GetDirectories(PluginsPath, "*", SearchOption.TopDirectoryOnly).Select(f => new DirectoryCatalog(f)));
//catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
//catalog.Catalogs.Add(new AssemblyCatalog(GetType().Assembly));
new CompositionContainer(catalog).ComposeParts(this);
OnComposablePartsLoaded();
}
protected virtual void OnComposablePartsLoaded()
{
StartPlugins();
}
private void StartPlugins()
{
Parallel.For(0, Plugins.Count(), i =>
{
var plugin = Plugins.ElementAt(i);
plugin.ReloadConfiguration();
if (plugin.Enabled)
{
if (KernelContext == KernelContext.Server)
{
plugin.InitInServer();
}
else
{
plugin.InitInUI();
}
}
});
}
/// <summary>
/// Gets the path to the application's ProgramDataFolder
/// </summary>
private string GetProgramDataPath()
{
string programDataPath = ConfigurationManager.AppSettings["ProgramDataPath"];
// If it's a relative path, e.g. "..\"
if (!Path.IsPathRooted(programDataPath))
{
string path = Assembly.GetExecutingAssembly().Location;
path = Path.GetDirectoryName(path);
programDataPath = Path.Combine(path, programDataPath);
programDataPath = Path.GetFullPath(programDataPath);
}
if (!Directory.Exists(programDataPath))
{
Directory.CreateDirectory(programDataPath);
}
return programDataPath;
}
private void ReloadConfiguration()
{
// Deserialize config
if (!File.Exists(ConfigurationPath))
{
Configuration = new TConfigurationType();
}
else
{
Configuration = JsonSerializer.DeserializeFromFile<TConfigurationType>(ConfigurationPath);
}
Logger.LoggerInstance.LogSeverity = Configuration.LogSeverity;
}
public void SaveConfiguration()
{
JsonSerializer.SerializeToFile(Configuration, ConfigurationPath);
}
private void ReloadHttpServer()
{
if (HttpServer != null)
{
HttpServer.Dispose();
}
HttpServer = new HttpServer("http://+:" + Configuration.HttpServerPortNumber + "/mediabrowser/");
}
private static TConfigurationType GetConfiguration(string directory)
{
string file = Path.Combine(directory, "config.js");
if (!File.Exists(file))
{
return new TConfigurationType();
}
return JsonSerializer.DeserializeFromFile<TConfigurationType>(file);
}
}
}
|