Configuration and serialization improvements

This commit is contained in:
LukePulverenti Luke Pulverenti luke pulverenti 2012-07-29 11:19:25 -04:00
parent 77e81432f7
commit 5d88dc8575
11 changed files with 79 additions and 46 deletions

View File

@ -29,7 +29,8 @@ namespace MediaBrowser.Api
{ {
Item = item, Item = item,
UserItemData = Kernel.Instance.GetUserItemData(userId, item.Id), UserItemData = Kernel.Instance.GetUserItemData(userId, item.Id),
ItemType = item.GetType() Type = item.GetType().Name,
IsFolder = (item is Folder)
}; };
if (includeChildren) if (includeChildren)

View File

@ -18,7 +18,7 @@ namespace MediaBrowser.Api
get { return "WebAPI"; } get { return "WebAPI"; }
} }
public override void InitInServer() public override void Init()
{ {
var httpServer = Kernel.Instance.HttpServer; var httpServer = Kernel.Instance.HttpServer;

View File

@ -5,12 +5,12 @@ namespace MediaBrowser.Common.Configuration
/// <summary> /// <summary>
/// Serves as a common base class for the Server and UI application Configurations /// Serves as a common base class for the Server and UI application Configurations
/// </summary> /// </summary>
public class BaseConfiguration public class BaseApplicationConfiguration
{ {
public LogSeverity LogSeverity { get; set; } public LogSeverity LogSeverity { get; set; }
public int HttpServerPortNumber { get; set; } public int HttpServerPortNumber { get; set; }
public BaseConfiguration() public BaseApplicationConfiguration()
{ {
LogSeverity = LogSeverity.Info; LogSeverity = LogSeverity.Info;
HttpServerPortNumber = 8096; HttpServerPortNumber = 8096;

View File

@ -1,5 +1,5 @@
using System.IO; using System;
using System; using System.IO;
namespace MediaBrowser.Common.Json namespace MediaBrowser.Common.Json
{ {
@ -22,6 +22,16 @@ namespace MediaBrowser.Common.Json
} }
} }
public static object DeserializeFromFile(Type type, string file)
{
Configure();
using (Stream stream = File.OpenRead(file))
{
return ServiceStack.Text.JsonSerializer.DeserializeFromStream(type, stream);
}
}
public static T DeserializeFromFile<T>(string file) public static T DeserializeFromFile<T>(string file)
{ {
Configure(); Configure();

View File

@ -18,7 +18,7 @@ namespace MediaBrowser.Common.Kernel
/// Represents a shared base kernel for both the UI and server apps /// Represents a shared base kernel for both the UI and server apps
/// </summary> /// </summary>
public abstract class BaseKernel<TConfigurationType> public abstract class BaseKernel<TConfigurationType>
where TConfigurationType : BaseConfiguration, new() where TConfigurationType : BaseApplicationConfiguration, new()
{ {
/// <summary> /// <summary>
/// Gets the path to the program data folder /// Gets the path to the program data folder
@ -139,18 +139,13 @@ namespace MediaBrowser.Common.Kernel
plugin.Version = assemblyName.Version; plugin.Version = assemblyName.Version;
plugin.Path = Path.Combine(PluginsPath, assemblyName.Name); plugin.Path = Path.Combine(PluginsPath, assemblyName.Name);
plugin.Context = KernelContext;
plugin.ReloadConfiguration(); plugin.ReloadConfiguration();
if (plugin.Enabled) if (plugin.Enabled)
{ {
if (KernelContext == KernelContext.Server) plugin.Init();
{
plugin.InitInServer();
}
else
{
plugin.InitInUI();
}
} }
} }
} }

View File

@ -48,7 +48,7 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Configuration\BaseConfiguration.cs" /> <Compile Include="Configuration\BaseApplicationConfiguration.cs" />
<Compile Include="Events\GenericItemEventArgs.cs" /> <Compile Include="Events\GenericItemEventArgs.cs" />
<Compile Include="Json\JsonSerializer.cs" /> <Compile Include="Json\JsonSerializer.cs" />
<Compile Include="Kernel\BaseKernel.cs" /> <Compile Include="Kernel\BaseKernel.cs" />

View File

@ -2,6 +2,7 @@
using System.IO; using System.IO;
using MediaBrowser.Common.Json; using MediaBrowser.Common.Json;
using MediaBrowser.Model.Plugins; using MediaBrowser.Model.Plugins;
using MediaBrowser.Common.Kernel;
namespace MediaBrowser.Common.Plugins namespace MediaBrowser.Common.Plugins
{ {
@ -23,31 +24,45 @@ namespace MediaBrowser.Common.Plugins
} }
} }
public override void ReloadConfiguration() protected override Type ConfigurationType
{ {
if (!File.Exists(ConfigurationPath)) get { return typeof(TConfigurationType); }
{
Configuration = new TConfigurationType();
}
else
{
Configuration = JsonSerializer.DeserializeFromFile<TConfigurationType>(ConfigurationPath);
Configuration.DateLastModified = File.GetLastWriteTime(ConfigurationPath);
}
} }
} }
/// <summary> /// <summary>
/// Provides a common base class for all plugins /// Provides a common base class for all plugins
/// </summary> /// </summary>
public abstract class BasePlugin public abstract class BasePlugin : IDisposable
{ {
/// <summary>
/// Gets or sets the plugin's current context
/// </summary>
public KernelContext Context { get; set; }
/// <summary>
/// Gets the name of the plugin
/// </summary>
public abstract string Name { get; } public abstract string Name { get; }
/// <summary>
/// Gets the type of configuration this plugin uses
/// </summary>
protected abstract Type ConfigurationType { get; }
/// <summary>
/// Gets or sets the path to the plugin's folder
/// </summary>
public string Path { get; set; } public string Path { get; set; }
/// <summary>
/// Gets or sets the plugin version
/// </summary>
public Version Version { get; set; } public Version Version { get; set; }
/// <summary>
/// Gets or sets the current plugin configuration
/// </summary>
public BasePluginConfiguration Configuration { get; protected set; } public BasePluginConfiguration Configuration { get; protected set; }
protected string ConfigurationPath protected string ConfigurationPath
@ -85,15 +100,31 @@ namespace MediaBrowser.Common.Plugins
} }
} }
public abstract void ReloadConfiguration(); public void ReloadConfiguration()
{
if (!File.Exists(ConfigurationPath))
{
Configuration = Activator.CreateInstance(ConfigurationType) as BasePluginConfiguration;
}
else
{
Configuration = JsonSerializer.DeserializeFromFile(ConfigurationType, ConfigurationPath) as BasePluginConfiguration;
Configuration.DateLastModified = File.GetLastWriteTime(ConfigurationPath);
}
}
public virtual void InitInServer() /// <summary>
/// Starts the plugin.
/// </summary>
public virtual void Init()
{ {
} }
public virtual void InitInUI() /// <summary>
/// Disposes the plugins. Undos all actions performed during Init.
/// </summary>
public virtual void Dispose()
{ {
} }
} }
} }

View File

@ -4,7 +4,7 @@ using MediaBrowser.Model.Configuration;
namespace MediaBrowser.Controller.Configuration namespace MediaBrowser.Controller.Configuration
{ {
public class ServerConfiguration : BaseConfiguration public class ServerConfiguration : BaseApplicationConfiguration
{ {
public string ImagesByNamePath { get; set; } public string ImagesByNamePath { get; set; }

View File

@ -13,7 +13,7 @@ namespace MediaBrowser.HtmlBrowser
get { return "Html Library Browser"; } get { return "Html Library Browser"; }
} }
public override void InitInServer() public override void Init()
{ {
var httpServer = Kernel.Instance.HttpServer; var httpServer = Kernel.Instance.HttpServer;

View File

@ -1,6 +1,4 @@
using System; using System.Collections.Generic;
using System.Collections.Generic;
using System.Runtime.Serialization;
using MediaBrowser.Model.Users; using MediaBrowser.Model.Users;
namespace MediaBrowser.Model.Entities namespace MediaBrowser.Model.Entities
@ -25,15 +23,8 @@ namespace MediaBrowser.Model.Entities
public IEnumerable<ApiBaseItemWrapper<T>> Children { get; set; } public IEnumerable<ApiBaseItemWrapper<T>> Children { get; set; }
[IgnoreDataMember] public bool IsFolder { get; set; }
public Type ItemType { get; set; }
public string Type public string Type { get; set; }
{
get
{
return ItemType.Name;
}
}
} }
} }

View File

@ -16,11 +16,16 @@ namespace MediaBrowser.TV
get { return "TV"; } get { return "TV"; }
} }
public override void InitInServer() public override void Init()
{ {
Kernel.Instance.ItemController.PreBeginResolvePath += ItemController_PreBeginResolvePath; Kernel.Instance.ItemController.PreBeginResolvePath += ItemController_PreBeginResolvePath;
} }
public override void Dispose()
{
Kernel.Instance.ItemController.PreBeginResolvePath -= ItemController_PreBeginResolvePath;
}
void ItemController_PreBeginResolvePath(object sender, PreBeginResolveEventArgs e) void ItemController_PreBeginResolvePath(object sender, PreBeginResolveEventArgs e)
{ {
if (e.IsFolder && System.IO.Path.GetFileName(e.Path).Equals("metadata", StringComparison.OrdinalIgnoreCase)) if (e.IsFolder && System.IO.Path.GetFileName(e.Path).Equals("metadata", StringComparison.OrdinalIgnoreCase))