From 2d06095447b972c8c7239277428e2c67c8b7ca86 Mon Sep 17 00:00:00 2001 From: LukePulverenti Date: Mon, 25 Feb 2013 22:43:04 -0500 Subject: plugin security fixes and other abstractions --- .../BaseApplicationHost.cs | 295 +++++++++++++++++++++ 1 file changed, 295 insertions(+) create mode 100644 MediaBrowser.Common.Implementations/BaseApplicationHost.cs (limited to 'MediaBrowser.Common.Implementations/BaseApplicationHost.cs') diff --git a/MediaBrowser.Common.Implementations/BaseApplicationHost.cs b/MediaBrowser.Common.Implementations/BaseApplicationHost.cs new file mode 100644 index 0000000000..c5af5059f3 --- /dev/null +++ b/MediaBrowser.Common.Implementations/BaseApplicationHost.cs @@ -0,0 +1,295 @@ +using System.IO; +using System.Linq; +using System.Reflection; +using MediaBrowser.Model.Logging; +using MediaBrowser.Model.Serialization; +using SimpleInjector; +using System; +using System.Collections.Generic; +using System.Threading; + +namespace MediaBrowser.Common.Implementations +{ + public abstract class BaseApplicationHost : IDisposable + { + /// + /// Gets or sets the logger. + /// + /// The logger. + public ILogger Logger { get; protected set; } + + /// + /// The container + /// + protected readonly Container Container = new Container(); + + /// + /// Gets assemblies that failed to load + /// + public List FailedAssemblies { get; protected set; } + + /// + /// Gets all types within all running assemblies + /// + /// All types. + public Type[] AllTypes { get; protected set; } + + /// + /// Gets all concrete types. + /// + /// All concrete types. + public Type[] AllConcreteTypes { get; protected set; } + + /// + /// The disposable parts + /// + protected readonly List DisposableParts = new List(); + + /// + /// The _protobuf serializer initialized + /// + private bool _protobufSerializerInitialized; + /// + /// The _protobuf serializer sync lock + /// + private object _protobufSerializerSyncLock = new object(); + /// + /// Gets a dynamically compiled generated serializer that can serialize protocontracts without reflection + /// + private IProtobufSerializer _protobufSerializer; + /// + /// Gets the protobuf serializer. + /// + /// The protobuf serializer. + protected IProtobufSerializer ProtobufSerializer + { + get + { + // Lazy load + LazyInitializer.EnsureInitialized(ref _protobufSerializer, ref _protobufSerializerInitialized, ref _protobufSerializerSyncLock, () => Serialization.ProtobufSerializer.Create(AllTypes)); + return _protobufSerializer; + } + private set + { + _protobufSerializer = value; + _protobufSerializerInitialized = value != null; + } + } + + /// + /// Initializes a new instance of the class. + /// + protected BaseApplicationHost() + { + FailedAssemblies = new List(); + } + + /// + /// Gets the composable part assemblies. + /// + /// IEnumerable{Assembly}. + protected abstract IEnumerable GetComposablePartAssemblies(); + + /// + /// Discovers the types. + /// + protected void DiscoverTypes() + { + FailedAssemblies.Clear(); + + AllTypes = GetComposablePartAssemblies().SelectMany(GetTypes).ToArray(); + + AllConcreteTypes = AllTypes.Where(t => t.IsClass && !t.IsAbstract && !t.IsInterface && !t.IsGenericType).ToArray(); + } + + /// + /// Gets a list of types within an assembly + /// This will handle situations that would normally throw an exception - such as a type within the assembly that depends on some other non-existant reference + /// + /// The assembly. + /// IEnumerable{Type}. + /// assembly + protected IEnumerable GetTypes(Assembly assembly) + { + if (assembly == null) + { + throw new ArgumentNullException("assembly"); + } + + try + { + return assembly.GetTypes(); + } + catch (ReflectionTypeLoadException ex) + { + // If it fails we can still get a list of the Types it was able to resolve + return ex.Types.Where(t => t != null); + } + } + + /// + /// Creates an instance of type and resolves all constructor dependancies + /// + /// The type. + /// System.Object. + public object CreateInstance(Type type) + { + try + { + return Container.GetInstance(type); + } + catch + { + Logger.Error("Error creating {0}", type.Name); + + throw; + } + } + + /// + /// Registers the specified obj. + /// + /// + /// The obj. + protected void RegisterSingleInstance(T obj) + where T : class + { + Container.RegisterSingle(obj); + } + + /// + /// Registers the specified func. + /// + /// + /// The func. + protected void Register(Func func) + where T : class + { + Container.Register(func); + } + + /// + /// Registers the single instance. + /// + /// + /// The func. + protected void RegisterSingleInstance(Func func) + where T : class + { + Container.RegisterSingle(func); + } + + /// + /// Resolves this instance. + /// + /// + /// ``0. + public T Resolve() + { + return (T)Container.GetRegistration(typeof(T), true).GetInstance(); + } + + /// + /// Resolves this instance. + /// + /// + /// ``0. + public T TryResolve() + { + var result = Container.GetRegistration(typeof(T), false); + + if (result == null) + { + return default(T); + } + return (T)result.GetInstance(); + } + + /// + /// Registers the specified service type. + /// + /// Type of the service. + /// Type of the concrete. + protected void Register(Type serviceType, Type implementation) + { + Container.Register(serviceType, implementation); + } + + /// + /// Loads the assembly. + /// + /// The file. + /// Assembly. + protected Assembly LoadAssembly(string file) + { + try + { + return Assembly.Load(File.ReadAllBytes((file))); + } + catch (Exception ex) + { + FailedAssemblies.Add(file); + Logger.ErrorException("Error loading assembly {0}", ex, file); + return null; + } + } + + /// + /// Gets the exports. + /// + /// + /// if set to true [manage liftime]. + /// IEnumerable{``0}. + public IEnumerable GetExports(bool manageLiftime = true) + { + var currentType = typeof(T); + + Logger.Info("Composing instances of " + currentType.Name); + + var parts = AllConcreteTypes.Where(currentType.IsAssignableFrom).Select(CreateInstance).Cast().ToArray(); + + if (manageLiftime) + { + DisposableParts.AddRange(parts.OfType()); + } + + return parts; + } + + /// + /// Gets the current application version + /// + /// The application version. + public Version ApplicationVersion + { + get + { + return GetType().Assembly.GetName().Version; + } + } + + /// + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// + public void Dispose() + { + Dispose(true); + } + + /// + /// Releases unmanaged and - optionally - managed resources. + /// + /// true to release both managed and unmanaged resources; false to release only unmanaged resources. + protected virtual void Dispose(bool dispose) + { + foreach (var part in DisposableParts) + { + part.Dispose(); + } + + var b = Container.GetCurrentRegistrations(); + + DisposableParts.Clear(); + } + } +} -- cgit v1.2.3