Moved plugin framework into its own assembly (it's really not such a good idea anymore with the Managed Extensibility Framework and all); added Xbox360 and Windows Phone 7 projects for Nuclex.Support.Transactions

git-svn-id: file:///srv/devel/repo-conversion/nusu@275 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2012-03-11 08:57:24 +00:00
parent 9ddd8770b2
commit c9b20cd2cd
21 changed files with 0 additions and 1957 deletions

View File

@ -223,40 +223,6 @@
<Compile Include="Source\Parsing\CommandLine.Parser.cs">
<DependentUpon>CommandLine.cs</DependentUpon>
</Compile>
<Compile Include="Source\Plugins\AssemblyLoadEventArgs.cs" />
<Compile Include="Source\Plugins\AssemblyLoadEventArgs.Test.cs">
<DependentUpon>AssemblyLoadEventArgs.cs</DependentUpon>
</Compile>
<Compile Include="Source\Plugins\IAbstractFactory.cs" />
<Compile Include="Source\Plugins\Employer.Test.cs">
<DependentUpon>Employer.cs</DependentUpon>
</Compile>
<Compile Include="Source\Plugins\FactoryEmployer.Test.cs">
<DependentUpon>FactoryEmployer.cs</DependentUpon>
</Compile>
<Compile Include="Source\Plugins\PrototypeFactory.cs" />
<Compile Include="Source\Plugins\PrototypeFactory.Test.cs">
<DependentUpon>PrototypeFactory.cs</DependentUpon>
</Compile>
<Compile Include="Source\Plugins\IAssemblyLoader.cs" />
<Compile Include="Source\Plugins\InstanceEmployer.Test.cs">
<DependentUpon>InstanceEmployer.cs</DependentUpon>
</Compile>
<Compile Include="Source\Plugins\NoPluginAttribute.cs" />
<Compile Include="Source\Plugins\Employer.cs" />
<Compile Include="Source\Plugins\FactoryEmployer.cs" />
<Compile Include="Source\Plugins\InstanceEmployer.cs" />
<Compile Include="Source\Plugins\NoPluginAttribute.Test.cs">
<DependentUpon>NoPluginAttribute.cs</DependentUpon>
</Compile>
<Compile Include="Source\Plugins\PluginHost.cs" />
<Compile Include="Source\Plugins\PluginHost.Test.cs">
<DependentUpon>PluginHost.cs</DependentUpon>
</Compile>
<Compile Include="Source\Plugins\PluginRepository.cs" />
<Compile Include="Source\Plugins\PluginRepository.Test.cs">
<DependentUpon>PluginRepository.cs</DependentUpon>
</Compile>
<Compile Include="Source\PropertyChangedEventArgsHelper.cs" />
<Compile Include="Source\PropertyChangedEventArgsHelper.Test.cs">
<DependentUpon>PropertyChangedEventArgsHelper.cs</DependentUpon>

View File

@ -254,40 +254,6 @@
<Compile Include="Source\Parsing\CommandLine.Parser.cs">
<DependentUpon>CommandLine.cs</DependentUpon>
</Compile>
<Compile Include="Source\Plugins\AssemblyLoadEventArgs.cs" />
<Compile Include="Source\Plugins\AssemblyLoadEventArgs.Test.cs">
<DependentUpon>AssemblyLoadEventArgs.cs</DependentUpon>
</Compile>
<Compile Include="Source\Plugins\IAbstractFactory.cs" />
<Compile Include="Source\Plugins\Employer.Test.cs">
<DependentUpon>Employer.cs</DependentUpon>
</Compile>
<Compile Include="Source\Plugins\FactoryEmployer.Test.cs">
<DependentUpon>FactoryEmployer.cs</DependentUpon>
</Compile>
<Compile Include="Source\Plugins\PrototypeFactory.cs" />
<Compile Include="Source\Plugins\PrototypeFactory.Test.cs">
<DependentUpon>PrototypeFactory.cs</DependentUpon>
</Compile>
<Compile Include="Source\Plugins\IAssemblyLoader.cs" />
<Compile Include="Source\Plugins\InstanceEmployer.Test.cs">
<DependentUpon>InstanceEmployer.cs</DependentUpon>
</Compile>
<Compile Include="Source\Plugins\NoPluginAttribute.cs" />
<Compile Include="Source\Plugins\Employer.cs" />
<Compile Include="Source\Plugins\FactoryEmployer.cs" />
<Compile Include="Source\Plugins\InstanceEmployer.cs" />
<Compile Include="Source\Plugins\NoPluginAttribute.Test.cs">
<DependentUpon>NoPluginAttribute.cs</DependentUpon>
</Compile>
<Compile Include="Source\Plugins\PluginHost.cs" />
<Compile Include="Source\Plugins\PluginHost.Test.cs">
<DependentUpon>PluginHost.cs</DependentUpon>
</Compile>
<Compile Include="Source\Plugins\PluginRepository.cs" />
<Compile Include="Source\Plugins\PluginRepository.Test.cs">
<DependentUpon>PluginRepository.cs</DependentUpon>
</Compile>
<Compile Include="Source\PropertyChangedEventArgsHelper.cs" />
<Compile Include="Source\PropertyChangedEventArgsHelper.Test.cs">
<DependentUpon>PropertyChangedEventArgsHelper.cs</DependentUpon>

View File

@ -265,40 +265,6 @@
<Compile Include="Source\Parsing\CommandLine.Parser.cs">
<DependentUpon>CommandLine.cs</DependentUpon>
</Compile>
<Compile Include="Source\Plugins\AssemblyLoadEventArgs.cs" />
<Compile Include="Source\Plugins\AssemblyLoadEventArgs.Test.cs">
<DependentUpon>AssemblyLoadEventArgs.cs</DependentUpon>
</Compile>
<Compile Include="Source\Plugins\IAbstractFactory.cs" />
<Compile Include="Source\Plugins\Employer.Test.cs">
<DependentUpon>Employer.cs</DependentUpon>
</Compile>
<Compile Include="Source\Plugins\FactoryEmployer.Test.cs">
<DependentUpon>FactoryEmployer.cs</DependentUpon>
</Compile>
<Compile Include="Source\Plugins\PrototypeFactory.cs" />
<Compile Include="Source\Plugins\PrototypeFactory.Test.cs">
<DependentUpon>PrototypeFactory.cs</DependentUpon>
</Compile>
<Compile Include="Source\Plugins\IAssemblyLoader.cs" />
<Compile Include="Source\Plugins\InstanceEmployer.Test.cs">
<DependentUpon>InstanceEmployer.cs</DependentUpon>
</Compile>
<Compile Include="Source\Plugins\NoPluginAttribute.cs" />
<Compile Include="Source\Plugins\Employer.cs" />
<Compile Include="Source\Plugins\FactoryEmployer.cs" />
<Compile Include="Source\Plugins\InstanceEmployer.cs" />
<Compile Include="Source\Plugins\NoPluginAttribute.Test.cs">
<DependentUpon>NoPluginAttribute.cs</DependentUpon>
</Compile>
<Compile Include="Source\Plugins\PluginHost.cs" />
<Compile Include="Source\Plugins\PluginHost.Test.cs">
<DependentUpon>PluginHost.cs</DependentUpon>
</Compile>
<Compile Include="Source\Plugins\PluginRepository.cs" />
<Compile Include="Source\Plugins\PluginRepository.Test.cs">
<DependentUpon>PluginRepository.cs</DependentUpon>
</Compile>
<Compile Include="Source\PropertyChangedEventArgsHelper.cs" />
<Compile Include="Source\PropertyChangedEventArgsHelper.Test.cs">
<DependentUpon>PropertyChangedEventArgsHelper.cs</DependentUpon>

View File

@ -1,51 +0,0 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2012 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
#if UNITTEST
using System;
using System.IO;
using System.Reflection;
using NUnit.Framework;
namespace Nuclex.Support.Plugins {
/// <summary>Unit Test for the assembly load event argument container</summary>
[TestFixture]
internal class AssemblyLoadEventArgsTest {
/// <summary>
/// Tests whether the argument container correctly stores an assembly reference
/// </summary>
[Test]
public void TestEmployerDefaultConstructorDetection() {
Assembly assembly = Assembly.GetExecutingAssembly();
AssemblyLoadEventArgs testArguments = new AssemblyLoadEventArgs(assembly);
Assert.AreSame(assembly, testArguments.LoadedAssembly);
}
}
} // namespace Nuclex.Support.Plugins
#endif // UNITTEST

View File

@ -1,57 +0,0 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2012 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Nuclex.Support.Plugins {
#if XBOX360 || WINDOWS_PHONE
/// <summary>Signature for the AssemblyLoad event</summary>
/// <param name="sender">Object that is reporting that an assembly was loaded</param>
/// <param name="arguments">Contains the loaded assembly</param>
public delegate void AssemblyLoadEventHandler(
object sender, AssemblyLoadEventArgs arguments
);
/// <summary>Argument container for the AssemblyLoad event arguments</summary>
public class AssemblyLoadEventArgs : EventArgs {
/// <summary>Initializes a new event argument container</summary>
/// <param name="loadedAssembly">Assembly that has been loaded</param>
public AssemblyLoadEventArgs(Assembly loadedAssembly) {
this.loadedAssembly = loadedAssembly;
}
/// <summary>Assembly that was loaded by the sender of the event</summary>
public Assembly LoadedAssembly {
get { return this.loadedAssembly; }
}
/// <summary>Loaded assembly that will be provided to the event receivers</summary>
private Assembly loadedAssembly;
}
#endif // XBOX360 || WINDOWS_PHONE
} // namespace Nuclex.Support.Plugins

View File

@ -1,92 +0,0 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2012 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
#if UNITTEST
using System;
using System.IO;
using NUnit.Framework;
namespace Nuclex.Support.Plugins {
/// <summary>Unit Test for the employer class</summary>
[TestFixture]
internal class EmployerTest {
#region class TestEmployer
/// <summary>Dummy implementation for testing the employer class</summary>
private class TestEmployer : Employer {
/// <summary>Employs the specified plugin type</summary>
/// <param name="type">Type to be employed</param>
public override void Employ(Type type) { }
}
#endregion // class TestEmployer
#region class NoDefaultConstructor
/// <summary>Test class that doesn't have a default constructor</summary>
private class NoDefaultConstructor {
/// <summary>Initializes a new instance of the test class</summary>
/// <param name="dummy">Dummy argument so this is no default constructor</param>
public NoDefaultConstructor(int dummy) { }
}
#endregion // class NoDefaultConstructor
#region class NonPublicDefaultConstructor
/// <summary>Test class that has a non-public default constructor</summary>
private class NonPublicDefaultConstructor {
/// <summary>Initializes a new instance of the test class</summary>
protected NonPublicDefaultConstructor() { }
}
#endregion // class NonPublicDefaultConstructor
#region class PublicDefaultConstructor
/// <summary>Test class that has a public default constructor</summary>
private class PublicDefaultConstructor {
/// <summary>Initializes a new instance of the test class</summary>
public PublicDefaultConstructor() { }
}
#endregion // class PublicDefaultConstructor
/// <summary>Tests whether the employer can detect a default constructor</summary>
[Test]
public void TestEmployerDefaultConstructorDetection() {
TestEmployer testEmployer = new TestEmployer();
Assert.IsFalse(testEmployer.CanEmploy(typeof(NoDefaultConstructor)));
Assert.IsFalse(testEmployer.CanEmploy(typeof(NonPublicDefaultConstructor)));
Assert.IsTrue(testEmployer.CanEmploy(typeof(PublicDefaultConstructor)));
}
}
} // namespace Nuclex.Support.Plugins
#endif // UNITTEST

View File

@ -1,48 +0,0 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2012 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
namespace Nuclex.Support.Plugins {
/// <summary>Plugin type employer</summary>
/// <remarks>
/// This class is used by the plugin host to assess whether a concrete type found
/// in a plugin assembly is suited to be processed the plugin user. If so,
/// the employer can employ the type. Employing can typically mean to create an
/// instance of the type in the plugin assembly or to build a runtime-factory
/// that can create instances of the type when it is needed.
/// </remarks>
public abstract class Employer {
/// <summary>Determines whether the type suites the employer's requirements</summary>
/// <param name="type">Type which will be assessed</param>
/// <returns>True if the type can be employed, otherwise false</returns>
public virtual bool CanEmploy(Type type) {
return type.HasDefaultConstructor();
}
/// <summary>Employs the specified plugin type</summary>
/// <param name="type">Type to be employed</param>
public abstract void Employ(Type type);
}
} // namespace Nuclex.Support.Plugins

View File

@ -1,169 +0,0 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2012 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.IO;
#if UNITTEST
using NUnit.Framework;
namespace Nuclex.Support.Plugins {
/// <summary>Unit Test for the factory employer class</summary>
[TestFixture]
internal class FactoryEmployerTest {
#region class Base
/// <summary>
/// Abstract base class to serve as abstract product for testing the factory employer
/// </summary>
private abstract class Base { }
#endregion // class Base
#region class Derived
/// <summary>
/// Class derived from the abstract base to serve as concrete product for
/// testing the factory employer
/// </summary>
private class Derived : Base { }
#endregion // class Derived
#region class GenericDerived
/// <summary>
/// Generic class derived from the abstract base to serve as concrete product
/// for testing the factory employer
/// </summary>
private class GenericDerived<SomeType> : Base { }
#endregion // class GenericDerived
#region class Unrelated
/// <summary>Unrelated class used to test the factory employer</summary>
private class Unrelated { }
#endregion // class Unrelated
/// <summary>
/// Tests whether the factory employer can detect employable types
/// </summary>
[Test]
public void TestCanEmploy() {
FactoryEmployer<Base> testEmployer = new FactoryEmployer<Base>();
Assert.IsFalse(testEmployer.CanEmploy(typeof(Base)));
Assert.IsTrue(testEmployer.CanEmploy(typeof(Derived)));
Assert.IsFalse(testEmployer.CanEmploy(typeof(Unrelated)));
}
/// <summary>
/// Tests whether the factory employer can use the non-generic IAbstractFactory
/// interface instead of its generic variant
/// </summary>
[Test]
public void TestNonGenericCreateInstance() {
FactoryEmployer<Base> testEmployer = new FactoryEmployer<Base>();
testEmployer.Employ(typeof(Derived));
Assert.That(testEmployer.Factories.Count, Is.AtLeast(1));
IAbstractFactory factory = testEmployer.Factories[0] as IAbstractFactory;
Assert.IsNotNull(factory);
Assert.IsInstanceOf<Derived>(factory.CreateInstance());
}
/// <summary>
/// Tests whether the factory employer throws an exception when it is asked to
/// employ an abstract class
/// </summary>
[Test]
public void TestThrowOnEmployAbstractClass() {
FactoryEmployer<Base> testEmployer = new FactoryEmployer<Base>();
Assert.Throws<MissingMethodException>(
delegate() { testEmployer.Employ(typeof(Base)); }
);
}
/// <summary>
/// Tests whether the factory employer throws an exception when it is asked to
/// employ a class that is not the product type or a derivative thereof
/// </summary>
[Test]
public void TestThrowOnEmployUnrelatedClass() {
FactoryEmployer<Base> testEmployer = new FactoryEmployer<Base>();
Assert.Throws<InvalidCastException>(
delegate() { testEmployer.Employ(typeof(Unrelated)); }
);
}
/// <summary>
/// Tests whether the factory employer throws an exception when it is asked to
/// employ a class that requires generic parameters for instantiation
/// </summary>
[Test]
public void TestThrowOnEmployGenericClass() {
FactoryEmployer<Base> testEmployer = new FactoryEmployer<Base>();
Assert.Throws<ArgumentException>(
delegate() { testEmployer.Employ(typeof(GenericDerived<>)); }
);
}
/// <summary>
/// Tests whether the factory employer can employ a class derived from the product
/// </summary>
[Test]
public void TestEmployClassDerivedFromProduct() {
FactoryEmployer<Base> testEmployer = new FactoryEmployer<Base>();
testEmployer.Employ(typeof(Derived));
Assert.AreEqual(1, testEmployer.Factories.Count);
Assert.IsInstanceOf<Derived>(testEmployer.Factories[0].CreateInstance());
}
/// <summary>
/// Tests whether the factory employer can employ the product class itself if it
/// isn't abstract
/// </summary>
[Test]
public void TestEmployProduct() {
FactoryEmployer<Unrelated> testEmployer = new FactoryEmployer<Unrelated>();
testEmployer.Employ(typeof(Unrelated));
Assert.AreEqual(1, testEmployer.Factories.Count);
Assert.IsInstanceOf<Unrelated>(testEmployer.Factories[0].CreateInstance());
}
}
} // namespace Nuclex.Support.Plugins
#endif // UNITTEST

View File

@ -1,125 +0,0 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2012 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Collections.Generic;
namespace Nuclex.Support.Plugins {
/// <summary>Employer to create factories of suiting types found in plugins</summary>
/// <typeparam name="TProduct">
/// Interface or base class that the types need to implement
/// </typeparam>
/// <remarks>
/// <para>
/// This employer will not directly instanciate any compatible types found in
/// plugin assemblies, but generated runtime-factories of these types, enabling the
/// user to decide when and how many instances of a type will be created.
/// </para>
/// <para>
/// This approach has the advantage that it enables even assemblies that were not
/// intended to be plugins can be loaded as plugins, without risking an instanciation
/// or complex and possibly heavy-weight types. The disadvantage is that the
/// runtime-factory can not provide decent informationa about the plugin type like
/// a human-readable name, capabilities or an icon.
/// </para>
/// </remarks>
public class FactoryEmployer<TProduct> : Employer where TProduct : class {
#region class ConcreteFactory
/// <summary>Concrete factory for the types in a plugin assembly</summary>
private class ConcreteFactory : IAbstractFactory<TProduct>, IAbstractFactory {
/// <summary>
/// Initializes a factory and configures it for the specified product
/// </summary>
/// <param name="type">Type of which the factory creates instances</param>
public ConcreteFactory(Type type) {
this.concreteType = type;
}
/// <summary>Create a new instance of the type the factory is configured to</summary>
/// <returns>The newly created instance</returns>
public TProduct CreateInstance() {
return (TProduct)Activator.CreateInstance(this.concreteType);
}
/// <summary>Create a new instance of the type the factory is configured to</summary>
/// <returns>The newly created instance</returns>
object IAbstractFactory.CreateInstance() {
return Activator.CreateInstance(this.concreteType);
}
/// <summary>Concrete product which the factory instance creates</summary>
private Type concreteType;
}
#endregion // class Factory
/// <summary>Initializes a new FactoryEmployer</summary>
public FactoryEmployer() {
this.employedFactories = new List<IAbstractFactory<TProduct>>();
}
/// <summary>List of all factories that the instance employer has created</summary>
public List<IAbstractFactory<TProduct>> Factories {
get { return this.employedFactories; }
}
/// <summary>Determines whether the type suites the employer's requirements</summary>
/// <param name="type">Type which will be assessed</param>
/// <returns>True if the type can be employed</returns>
public override bool CanEmploy(Type type) {
return
type.HasDefaultConstructor() &&
typeof(TProduct).IsAssignableFrom(type) &&
!type.ContainsGenericParameters;
}
/// <summary>Employs the specified plugin type</summary>
/// <param name="type">Type to be employed</param>
public override void Employ(Type type) {
if(!type.HasDefaultConstructor()) {
throw new MissingMethodException(
"Cannot employ type because it does not have a public default constructor"
);
}
if(!typeof(TProduct).IsAssignableFrom(type)) {
throw new InvalidCastException(
"Cannot employ type because it cannot be cast to the factory's product type"
);
}
if(type.ContainsGenericParameters) {
throw new ArgumentException(
"Cannot employ type because it requires generic parameters", "type"
);
}
this.employedFactories.Add(new ConcreteFactory(type));
}
/// <summary>All factories that the instance employer has created</summary>
private List<IAbstractFactory<TProduct>> employedFactories;
}
} // namespace Nuclex.Support.Plugins

View File

@ -1,51 +0,0 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2012 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Collections.Generic;
namespace Nuclex.Support.Plugins {
/// <summary>Abstract factory for a concrete type</summary>
public interface IAbstractFactory {
/// <summary>
/// Creates a new instance of the type to which the factory is specialized
/// </summary>
/// <returns>The newly created instance</returns>
object CreateInstance();
}
/// <summary>Abstract factory for a concrete type</summary>
/// <typeparam name="ProductType">
/// Interface or base class of the product of the factory
/// </typeparam>
public interface IAbstractFactory<ProductType> {
/// <summary>
/// Creates a new instance of the type to which the factory is specialized
/// </summary>
/// <returns>The newly created instance</returns>
ProductType CreateInstance();
}
} // namespace Nuclex.Support.Plugins

View File

@ -1,40 +0,0 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2012 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Nuclex.Support.Plugins {
/// <summary>Interface for an assembly loading helper</summary>
public interface IAssemblyLoader {
/// <summary>Tries to loads an assembly from a file</summary>
/// <param name="path">Path to the file that is loaded as an assembly</param>
/// <param name="loadedAssembly">
/// Output parameter that receives the loaded assembly or null
/// </param>
/// <returns>True if the assembly was loaded successfully, otherwise false</returns>
bool TryLoadFile(string path, out Assembly loadedAssembly);
}
} // namespace Nuclex.Support.Plugins

View File

@ -1,155 +0,0 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2012 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.IO;
#if UNITTEST
using NUnit.Framework;
namespace Nuclex.Support.Plugins {
/// <summary>Unit Test for the instance employer class</summary>
[TestFixture]
internal class InstanceEmployerTest {
#region class Base
/// <summary>
/// Abstract base class to serve as abstract product for testing the instance employer
/// </summary>
private abstract class Base { }
#endregion // class Base
#region class Derived
/// <summary>
/// Class derived from the abstract base to serve as concrete product for
/// testing the instance employer
/// </summary>
private class Derived : Base { }
#endregion // class Derived
#region class GenericDerived
/// <summary>
/// Generic class derived from the abstract base to serve as concrete product
/// for testing the instance employer
/// </summary>
private class GenericDerived<SomeType> : Base { }
#endregion // class GenericDerived
#region class Unrelated
/// <summary>Unrelated class used to test the instance employer</summary>
private class Unrelated { }
#endregion // class Unrelated
/// <summary>
/// Tests whether the instance employer can detect employable types
/// </summary>
[Test]
public void TestCanEmploy() {
InstanceEmployer<Base> testEmployer = new InstanceEmployer<Base>();
Assert.IsFalse(testEmployer.CanEmploy(typeof(Base)));
Assert.IsTrue(testEmployer.CanEmploy(typeof(Derived)));
Assert.IsFalse(testEmployer.CanEmploy(typeof(Unrelated)));
}
/// <summary>
/// Tests whether the instance employer throws an exception when it is asked to
/// employ an abstract class
/// </summary>
[Test]
public void TestThrowOnEmployAbstractClass() {
InstanceEmployer<Base> testEmployer = new InstanceEmployer<Base>();
Assert.Throws<MissingMethodException>(
delegate() { testEmployer.Employ(typeof(Base)); }
);
}
/// <summary>
/// Tests whether the instance employer throws an exception when it is asked to
/// employ a class that is not the product type or a derivative thereof
/// </summary>
[Test]
public void TestThrowOnEmployUnrelatedClass() {
InstanceEmployer<Base> testEmployer = new InstanceEmployer<Base>();
Assert.Throws<InvalidCastException>(
delegate() { testEmployer.Employ(typeof(Unrelated)); }
);
}
/// <summary>
/// Tests whether the instance employer throws an exception when it is asked to
/// employ a class that requires generic parameters for instantiation
/// </summary>
[Test]
public void TestThrowOnEmployGenericClass() {
InstanceEmployer<Base> testEmployer = new InstanceEmployer<Base>();
Assert.Throws<ArgumentException>(
delegate() { testEmployer.Employ(typeof(GenericDerived<>)); }
);
}
/// <summary>
/// Tests whether the instance employer can employ a class derived from the product
/// </summary>
[Test]
public void TestEmployClassDerivedFromProduct() {
InstanceEmployer<Base> testEmployer = new InstanceEmployer<Base>();
testEmployer.Employ(typeof(Derived));
Assert.AreEqual(1, testEmployer.Instances.Count);
Assert.AreEqual(typeof(Derived), testEmployer.Instances[0].GetType());
Assert.IsInstanceOf<Derived>(testEmployer.Instances[0]);
}
/// <summary>
/// Tests whether the instance employer can employ the product class itself if it
/// isn't abstract
/// </summary>
[Test]
public void TestEmployProduct() {
InstanceEmployer<Unrelated> testEmployer = new InstanceEmployer<Unrelated>();
testEmployer.Employ(typeof(Unrelated));
Assert.AreEqual(1, testEmployer.Instances.Count);
Assert.AreEqual(typeof(Unrelated), testEmployer.Instances[0].GetType());
Assert.IsInstanceOf<Unrelated>(testEmployer.Instances[0]);
}
}
} // namespace Nuclex.Support.Plugins
#endif // UNITTEST

View File

@ -1,80 +0,0 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2012 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Collections.Generic;
namespace Nuclex.Support.Plugins {
/// <summary>Employer that directly creates instances of the types in a plugin</summary>
/// <typeparam name="TEmployedType">Interface or base class required for the employed types</typeparam>
/// <remarks>
/// <para>
/// This employer directly creates an instance of any type in a plugin assembly that
/// implements or is derived from the type the generic InstanceEmployer is instanced
/// to. This is useful when the plugin user already has a special plugin interface
/// through which additional informations about a plugin type can be queried or
/// when actually exactly one instance per plugin type is wanted (think of the
/// prototype pattern for example)
/// </para>
/// <para>
/// Because this employer blindly creates an instance of any compatible type found
/// in a plugin assembly it should be used with care. If big types with high
/// construction time or huge memory requirements are loaded this can become
/// a real resource hog. The intention of this employer was to let the plugin user
/// define his own factory interface which possibly provides further details about
/// the type the factory is reponsible for (like a description field). This
/// factory would then be implemented on the plugin side.
/// </para>
/// </remarks>
public class InstanceEmployer<TEmployedType> : Employer {
/// <summary>Initializes a new instance employer</summary>
public InstanceEmployer() {
this.employedInstances = new List<TEmployedType>();
}
/// <summary>All instances that have been employed</summary>
public List<TEmployedType> Instances {
get { return this.employedInstances; }
}
/// <summary>Determines whether the type suites the employer's requirements</summary>
/// <param name="type">Type that is checked for employability</param>
/// <returns>True if the type can be employed</returns>
public override bool CanEmploy(Type type) {
return
type.HasDefaultConstructor() &&
typeof(TEmployedType).IsAssignableFrom(type) &&
!type.ContainsGenericParameters;
}
/// <summary>Employs the specified plugin type</summary>
/// <param name="type">Type to be employed</param>
public override void Employ(Type type) {
this.employedInstances.Add((TEmployedType)Activator.CreateInstance(type));
}
/// <summary>All instances employed by the instance employer</summary>
private List<TEmployedType> employedInstances;
}
} // namespace Nuclex.Support.Plugins

View File

@ -1,46 +0,0 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2012 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.IO;
#if UNITTEST
using NUnit.Framework;
namespace Nuclex.Support.Plugins {
/// <summary>Unit Test for the no plugin attribute class</summary>
[TestFixture]
internal class NoPluginAttributeTest {
/// <summary>
/// Tests whether the default consturctor of the no plugin attribute works
/// </summary>
[Test]
public void HasDefaultConstructor() {
Assert.NotNull(new NoPluginAttribute());
}
}
} // namespace Nuclex.Support.Plugins
#endif // UNITTEST

View File

@ -1,38 +0,0 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2012 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
namespace Nuclex.Support.Plugins {
/// <summary>Attribute that prevents a class from being seen by the PluginHost</summary>
/// <remarks>
/// When this attribute is attached to a class it will be invisible to the
/// PluginHost and not become accessable as a plugin.
/// </remarks>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class NoPluginAttribute : Attribute {
/// <summary>Initializes an instance of the NoPluginAttributes</summary>
public NoPluginAttribute() : base() { }
}
} // namespace Nuclex.Support.Plugins

View File

@ -1,168 +0,0 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2012 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.IO;
using System.Reflection;
#if UNITTEST
using NUnit.Framework;
namespace Nuclex.Support.Plugins {
/// <summary>Unit Test for the plugin host class</summary>
[TestFixture, NoPlugin] // NoPlugin is used in one of the unit tests
internal class PluginHostTest {
#region class FailingEmployer
/// <summary>Employer that unexpectedly fails to employ a given type</summary>
private class FailingEmployer : Employer {
/// <summary>Employs the specified plugin type</summary>
/// <param name="type">Type to be employed</param>
public override void Employ(Type type) {
if(type.Equals(typeof(PluginRepository))) {
throw new InvalidOperationException();
}
}
}
#endregion // class FailingEmployer
/// <summary>Tests whether the simple constructor is working</summary>
[Test]
public void TestSimpleConstructor() {
new PluginHost(new FactoryEmployer<PluginHostTest>());
}
/// <summary>Tests whether the full constructor is working</summary>
[Test]
public void TestFullConstructor() {
new PluginHost(new FactoryEmployer<PluginHostTest>(), new PluginRepository());
}
/// <summary>
/// Tests whether the AddAssembly() method works by adding the test assembly
/// itself to the repository
/// </summary>
[Test]
public void TestFullConstructorWithPreloadedAssembly() {
PluginRepository testRepository = new PluginRepository();
FactoryEmployer<PluginRepository> testEmployer = new FactoryEmployer<PluginRepository>();
// Might also use Assembly.GetCallingAssembly() here, but this leads to the exe of
// the unit testing tool
Assembly self = Assembly.GetAssembly(GetType());
testRepository.AddAssembly(self);
PluginHost testHost = new PluginHost(testEmployer, testRepository);
Assert.AreSame(testEmployer, testHost.Employer);
Assert.AreEqual(1, testEmployer.Factories.Count);
}
/// <summary>
/// Verifies that the plugin host correctly stores the provided repository
/// </summary>
[Test]
public void TestRepositoryStorage() {
PluginRepository testRepository = new PluginRepository();
FactoryEmployer<PluginRepository> testEmployer = new FactoryEmployer<PluginRepository>();
PluginHost testHost = new PluginHost(testEmployer, testRepository);
Assert.AreSame(testRepository, testHost.Repository);
}
/// <summary>
/// Verifies that the plugin host correctly stores the provided employer
/// </summary>
[Test]
public void TestEmployerStorage() {
PluginRepository testRepository = new PluginRepository();
FactoryEmployer<PluginRepository> testEmployer = new FactoryEmployer<PluginRepository>();
PluginHost testHost = new PluginHost(testEmployer, testRepository);
Assert.AreSame(testEmployer, testHost.Employer);
}
/// <summary>
/// Tests whether the plugin host noticed when new assemblies are loaded into
/// the repository
/// </summary>
[Test]
public void TestAssemblyLoading() {
PluginRepository testRepository = new PluginRepository();
FactoryEmployer<PluginRepository> testEmployer = new FactoryEmployer<PluginRepository>();
PluginHost testHost = new PluginHost(testEmployer, testRepository);
// Might also use Assembly.GetCallingAssembly() here, but this leads to the exe of
// the unit testing tool
Assembly self = Assembly.GetAssembly(GetType());
testRepository.AddAssembly(self);
Assert.AreSame(testEmployer, testHost.Employer);
Assert.AreEqual(1, testEmployer.Factories.Count);
}
/// <summary>
/// Tests whether the plugin host isolates the caller from an exception when the
/// employer fails to employ a type in the assembly
/// </summary>
[Test]
public void TestAssemblyLoadingWithEmployFailure() {
PluginRepository testRepository = new PluginRepository();
PluginHost testHost = new PluginHost(new FailingEmployer(), testRepository);
// Might also use Assembly.GetCallingAssembly() here, but this leads to the exe of
// the unit testing tool
Assembly self = Assembly.GetAssembly(GetType());
testRepository.AddAssembly(self);
Assert.AreSame(testRepository, testHost.Repository);
}
/// <summary>
/// Verifies that the plugin host ignores types which have the NoPluginAttribute
/// assigned to them
/// </summary>
[Test]
public void TestAssemblyLoadingWithNoPluginAttribute() {
PluginRepository testRepository = new PluginRepository();
FactoryEmployer<PluginHostTest> testEmployer = new FactoryEmployer<PluginHostTest>();
PluginHost testHost = new PluginHost(testEmployer, testRepository);
// Might also use Assembly.GetCallingAssembly() here, but this leads to the exe of
// the unit testing tool
Assembly self = Assembly.GetAssembly(GetType());
testRepository.AddAssembly(self);
Assert.AreSame(testRepository, testHost.Repository);
Assert.AreEqual(0, testEmployer.Factories.Count);
}
}
} // namespace Nuclex.Support.Plugins
#endif // UNITTEST

View File

@ -1,121 +0,0 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2012 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Diagnostics;
using System.Reflection;
namespace Nuclex.Support.Plugins {
/// <summary>Integration host for plugins</summary>
/// <remarks>
/// This class is created by the party that is interested in loading plugins,
/// herein referred to as the "plugin user". The plugin host will monitor a
/// repository and react to any assembly being loaded into that repository by
/// iterating over all types (as in classes and structures) found in the
/// assembly and using the employer to do whatever the plugin user intends
/// to do with the types found in that assembly
/// </remarks>
public class PluginHost {
/// <summary>Initializes a plugin host using a new repository</summary>
/// <param name="employer">Employer used assess and employ the plugin types</param>
public PluginHost(Employer employer) :
this(employer, new PluginRepository()) { }
/// <summary>Initializes the plugin using an existing repository</summary>
/// <param name="employer">Employer used assess and employ the plugin types</param>
/// <param name="repository">Repository in which plugins will be stored</param>
public PluginHost(Employer employer, PluginRepository repository) {
this.employer = employer;
this.repository = repository;
foreach(Assembly assembly in this.repository.LoadedAssemblies) {
employAssemblyTypes(assembly);
}
this.repository.AssemblyLoaded += new AssemblyLoadEventHandler(assemblyLoadHandler);
}
/// <summary>The repository containing all loaded plugins</summary>
public PluginRepository Repository {
get { return this.repository; }
}
/// <summary>The employer that is used by this plugin integration host</summary>
public Employer Employer {
get { return this.employer; }
}
/// <summary>Responds to a new plugin being loaded into the repository</summary>
/// <param name="sender">Repository into which the assembly was loaded</param>
/// <param name="arguments">Event arguments; contains the loaded assembly</param>
private void assemblyLoadHandler(object sender, AssemblyLoadEventArgs arguments) {
employAssemblyTypes(arguments.LoadedAssembly);
}
/// <summary>Employs all employable types in an assembly</summary>
/// <param name="assembly">Assembly whose types to assess and to employ</param>
private void employAssemblyTypes(Assembly assembly) {
// Iterate all types contained in the assembly
Type[] types = assembly.GetTypes();
for(int index = 0; index < types.Length; ++index) {
Type type = types[index];
// We'll ignore abstract and non-public types
if(!type.IsPublic || type.IsAbstract) {
continue;
}
// Types that have been tagged with the [NoPlugin] attribute will be ignored
if(type.HasAttribute<NoPluginAttribute>()) {
continue;
}
// Type seems to be acceptable, assess and possibly employ it
try {
if(this.employer.CanEmploy(type)) {
this.employer.Employ(type);
}
}
catch(Exception exception) {
reportError("Could not employ " + type.ToString() + ": " + exception.Message);
}
}
}
/// <summary>Reports an error to the debugging console</summary>
/// <param name="error">Error message that will be reported</param>
private static void reportError(string error) {
#if WINDOWS
Trace.WriteLine(error);
#endif
}
/// <summary>Employs and manages types in the loaded plugin assemblies</summary>
private Employer employer;
/// <summary>Repository containing all plugins loaded, shared with other hosts</summary>
private PluginRepository repository;
}
} // namespace Nuclex.Support.Plugins

View File

@ -1,222 +0,0 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2012 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
#if UNITTEST
using NUnit.Framework;
using NMock;
namespace Nuclex.Support.Plugins {
/// <summary>Unit Test for the plugin repository class</summary>
[TestFixture]
internal class PluginRepositoryTest {
#region interface IAssemblyLoadedSubscriber
/// <summary>Interface used to test the progress tracker</summary>
public interface IAssemblyLoadedSubscriber {
/// <summary>
/// Represents the method that handles the System.AppDomain.AssemblyLoad event
/// of an System.AppDomain
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="arguments">
/// An System.AssemblyLoadEventArgs that contains the event data
/// </param>
void AssemblyLoaded(object sender, AssemblyLoadEventArgs arguments);
}
#endregion // interface IProgressTrackerSubscriber
#region class TestAssemblyLoader
/// <summary>Special assembly loader for the unit test</summary>
public class TestAssemblyLoader : PluginRepository.DefaultAssemblyLoader {
/// <summary>Loads an assembly from a file system path</summary>
/// <param name="path">Path the assembly will be loaded from</param>
/// <returns>The loaded assembly</returns>
protected override Assembly LoadAssemblyFromFile(string path) {
switch(path) {
case "DllNotFound": {
Trace.WriteLine("Simulating DllNotFoundException for unit test");
throw new DllNotFoundException();
}
case "UnauthorizedAccess": {
Trace.WriteLine("Simulating UnauthorizedAccessException for unit test");
throw new UnauthorizedAccessException();
}
case "BadImageFormat": {
Trace.WriteLine("Simulating BadImageFormatException for unit test");
throw new BadImageFormatException();
}
case "IO": {
Trace.WriteLine("Simulating IOException for unit test");
throw new IOException();
}
default: { return Assembly.LoadFile(path); }
}
}
}
#endregion // class TestAssemblyLoader
/// <summary>
/// Tests whether the default constructor of the plugin repository class works
/// </summary>
[Test]
public void TestDefaultConstructor() {
new PluginRepository();
}
/// <summary>
/// Tests whether the AddFiles() method accepts a file mask to which there are
/// no matching files
/// </summary>
[Test]
public void TestAddFilesWithZeroMatches() {
PluginRepository testRepository = new PluginRepository();
testRepository.AddFiles(Guid.NewGuid().ToString());
}
/// <summary>
/// Tests whether the AddFiles() method accepts a file mask to which there is
/// exactly one matching file
/// </summary>
[Test]
public void TestAddFilesWithOwnAssembly() {
PluginRepository testRepository = new PluginRepository();
Assembly self = Assembly.GetAssembly(GetType());
testRepository.AddFiles(self.Location);
Assert.AreEqual(1, testRepository.LoadedAssemblies.Count);
}
/// <summary>
/// Tests whether the AddAssembly() method works by adding the test assembly
/// itself to the repository
/// </summary>
[Test]
public void TestAddAssembly() {
PluginRepository testRepository = new PluginRepository();
// Might also use Assembly.GetCallingAssembly() here, but this leads to the exe of
// the unit testing tool
Assembly self = Assembly.GetAssembly(GetType());
testRepository.AddAssembly(self);
Assert.AreEqual(1, testRepository.LoadedAssemblies.Count);
}
/// <summary>
/// Tests whether the AddAssembly() method works by adding the test assembly
/// itself to the repository
/// </summary>
[Test]
public void TestAssemblyLoadedEvent() {
MockFactory mockery = new MockFactory();
PluginRepository testRepository = new PluginRepository();
Mock<IAssemblyLoadedSubscriber> subscriber = mockSubscriber(mockery, testRepository);
subscriber.Expects.One.Method(m => m.AssemblyLoaded(null, null)).WithAnyArguments();
Assembly self = Assembly.GetAssembly(GetType());
testRepository.AddAssembly(self);
mockery.VerifyAllExpectationsHaveBeenMet();
}
/// <summary>
/// Verifies that no exceptions come through when a DllNotFoundException is thrown
/// during assembly loading
/// </summary>
[Test]
public void TestDllNotFoundExceptionDuringAssemblyLoad() {
TestAssemblyLoader loader = new TestAssemblyLoader();
Assembly loadedAssembly;
Assert.IsFalse(loader.TryLoadFile("DllNotFound", out loadedAssembly));
}
/// <summary>
/// Verifies that no exceptions come through when a UnauthorizedAccessException is
/// thrown during assembly loading
/// </summary>
[Test]
public void TestUnauthorizedAccessExceptionDuringAssemblyLoad() {
TestAssemblyLoader loader = new TestAssemblyLoader();
Assembly loadedAssembly;
Assert.IsFalse(loader.TryLoadFile("UnauthorizedAccess", out loadedAssembly));
}
/// <summary>
/// Verifies that no exceptions come through when a BadImageFormatException is
/// thrown during assembly loading
/// </summary>
[Test]
public void TestBadImageFormatExceptionDuringAssemblyLoad() {
TestAssemblyLoader loader = new TestAssemblyLoader();
Assembly loadedAssembly;
Assert.IsFalse(loader.TryLoadFile("BadImageFormat", out loadedAssembly));
}
/// <summary>
/// Verifies that no exceptions come through when an IOException is
/// thrown during assembly loading
/// </summary>
[Test]
public void TestIOExceptionDuringAssemblyLoad() {
TestAssemblyLoader loader = new TestAssemblyLoader();
Assembly loadedAssembly;
Assert.IsFalse(loader.TryLoadFile("IO", out loadedAssembly));
}
/// <summary>Mocks a subscriber for the events of a plugin repository</summary>
/// <param name="mockery">Mockery to create an event subscriber in</param>
/// <param name="repository">Repository to subscribe the mocked subscriber to</param>
/// <returns>The mocked event subscriber</returns>
private static Mock<IAssemblyLoadedSubscriber> mockSubscriber(
MockFactory mockery, PluginRepository repository
) {
Mock<IAssemblyLoadedSubscriber> mockedSubscriber =
mockery.CreateMock<IAssemblyLoadedSubscriber>();
repository.AssemblyLoaded += new AssemblyLoadEventHandler(
mockedSubscriber.MockObject.AssemblyLoaded
);
return mockedSubscriber;
}
}
} // namespace Nuclex.Support.Plugins
#endif // UNITTEST

View File

@ -1,193 +0,0 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2012 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
namespace Nuclex.Support.Plugins {
/// <summary>Stores loaded plugins</summary>
/// <remarks>
/// This class manages a set of assemblies that have been dynamically loaded
/// as plugins. It usually is shared by multiple PluginHosts that handle
/// different interfaces of one plugin type.
/// </remarks>
public class PluginRepository {
#region class DefaultAssemblyLoader
/// <summary>Default assembly loader used to read assemblies from files</summary>
public class DefaultAssemblyLoader : IAssemblyLoader {
/// <summary>Initializes a new default assembly loader</summary>
/// <remarks>
/// Made protected to provide users with a small incentive for using
/// the Instance property instead of creating new instances all around.
/// </remarks>
protected DefaultAssemblyLoader() { }
/// <summary>Loads an assembly from a file system path</summary>
/// <param name="path">Path the assembly will be loaded from</param>
/// <returns>The loaded assembly</returns>
protected virtual Assembly LoadAssemblyFromFile(string path) {
return Assembly.LoadFrom(path);
}
/// <summary>Tries to loads an assembly from a file</summary>
/// <param name="path">Path to the file that is loaded as an assembly</param>
/// <param name="loadedAssembly">
/// Output parameter that receives the loaded assembly or null
/// </param>
/// <returns>True if the assembly was loaded successfully, otherwise false</returns>
public bool TryLoadFile(string path, out Assembly loadedAssembly) {
// A lot of errors can occur when attempting to load an assembly...
try {
loadedAssembly = LoadAssemblyFromFile(path);
return true;
}
#if WINDOWS
// File not found - Most likely a missing dependency of the assembly we
// attempted to load since the assembly itself has been found by the GetFiles() method
catch(DllNotFoundException) {
reportError(
"Assembly '" + path + "' or one of its dependencies is missing"
);
}
#endif
// Unauthorized acccess - Either the assembly is not trusted because it contains
// code that imposes a security risk on the system or a user rights problem
catch(UnauthorizedAccessException) {
reportError(
"Not authorized to load assembly '" + path + "', " +
"possible rights problem"
);
}
// Bad image format - This exception is often thrown when the assembly we
// attempted to load requires a different version of the .NET framework
catch(BadImageFormatException) {
reportError(
"'" + path + "' is not a .NET assembly, requires a different version " +
"of the .NET Runtime or does not support the current instruction set (x86/x64)"
);
}
// Unknown error - Our last resort is to show a default error message
catch(Exception exception) {
reportError(
"Failed to load plugin assembly '" + path + "': " + exception.Message
);
}
loadedAssembly = null;
return false;
}
/// <summary>The only instance of the DefaultAssemblyLoader</summary>
public static readonly DefaultAssemblyLoader Instance =
new DefaultAssemblyLoader();
}
#endregion // class DefaultAssemblyLoader
/// <summary>Triggered whenever a new assembly is loaded into this repository</summary>
public event AssemblyLoadEventHandler AssemblyLoaded;
/// <summary>Initializes a new instance of the plugin repository</summary>
public PluginRepository() : this(DefaultAssemblyLoader.Instance) { }
/// <summary>Initializes a new instance of the plugin repository</summary>
/// <param name="loader">
/// Loader to use for loading assemblies into this repository
/// </param>
public PluginRepository(IAssemblyLoader loader) {
this.assemblies = new List<Assembly>();
this.assemblyLoader = loader;
}
/// <summary>Loads all plugins matching a wildcard specification</summary>
/// <param name="wildcard">Path of one or more plugins via wildcard</param>
/// <remarks>
/// This function always assumes that a plugin is optional. This means that
/// even when you specify a unique file name and a matching file is not found,
/// no exception will be raised and the error is silently ignored.
/// </remarks>
public void AddFiles(string wildcard) {
string directory = Path.GetDirectoryName(wildcard);
string search = Path.GetFileName(wildcard);
// If no directory was specified, use the current working directory
if((directory == null) || (directory == string.Empty)) {
directory = ".";
}
// We'll scan the specified directory for all files matching the specified
// wildcard. If only a single file is specified, only that file will match
// the supposed wildcard and everything works as expected
string[] assemblyFiles = Directory.GetFiles(directory, search);
foreach(string assemblyFile in assemblyFiles) {
Assembly loadedAssembly;
if(this.assemblyLoader.TryLoadFile(assemblyFile, out loadedAssembly)) {
AddAssembly(loadedAssembly);
}
}
}
/// <summary>Adds the specified assembly to the repository</summary>
/// <remarks>
/// Also used internally, so any assembly that is to be put into the repository,
/// not matter how, wanders through this method
/// </remarks>
public void AddAssembly(Assembly assembly) {
this.assemblies.Add(assembly);
// Trigger event in case any subscribers have been registered
if(AssemblyLoaded != null) {
AssemblyLoaded(this, new AssemblyLoadEventArgs(assembly));
}
}
/// <summary>List of all loaded plugin assemblies in the repository</summary>
public List<Assembly> LoadedAssemblies {
get { return this.assemblies; }
}
/// <summary>Reports an error to the debugging console</summary>
/// <param name="error">Error message that will be reported</param>
private static void reportError(string error) {
#if WINDOWS
Trace.WriteLine(error);
#endif
}
/// <summary>Loaded plugin assemblies</summary>
private List<Assembly> assemblies;
/// <summary>Takes care of loading assemblies for the repositories</summary>
private IAssemblyLoader assemblyLoader;
}
} // namespace Nuclex.Support.Plugins

View File

@ -1,141 +0,0 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2012 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
#if UNITTEST
using System;
using System.IO;
using NUnit.Framework;
namespace Nuclex.Support.Plugins {
/// <summary>Unit Test for the prototype-based factory class</summary>
[TestFixture]
internal class PrototypeFactoryTest {
#region interface IProduct
/// <summary>Interface used for the product in the unit test</summary>
private interface IProduct {
/// <summary>Some value associated with the product</summary>
int Value { get; }
}
#endregion // interface IProduct
#region class ConcretePrototype
/// <summary>
/// Class derived from the abstract base to serve as concrete product for
/// testing the factory employer
/// </summary>
private class ConcretePrototype : IProduct, ICloneable, IDisposable {
/// <summary>Initializes a new instance of the prototype product</summary>
/// <param name="value">Value that will be associated with this instance</param>
public ConcretePrototype(int value) {
this.value = value;
}
/// <summary>Immediately releases all resources owned by the instance</summary>
public void Dispose() {
this.disposed = true;
}
/// <summary>Value the product has been associated with</summary>
public int Value { get { return this.value; } }
/// <summary>Whether the prototype instance has been disposed</summary>
public bool IsDisposed {
get { return this.disposed; }
}
/// <summary>Creates an identical copy of the instance</summary>
/// <returns>An identical copy of the instance</returns>
object ICloneable.Clone() {
return new ConcretePrototype(this.value);
}
/// <summary>Value associated with the product</summary>
private int value;
/// <summary>Whether the instance has been disposed</summary>
private bool disposed;
}
#endregion // class ConcretePrototype
/// <summary>
/// Tests whether the prototype-based factory behaves correctly by creating
/// new instances of its product using clones of its assigned prototype.
/// </summary>
[Test]
public void TestGenericInstanceCreation() {
ConcretePrototype template = new ConcretePrototype(42);
IAbstractFactory<IProduct> factory = new PrototypeFactory<
IProduct, ConcretePrototype
>(template);
IProduct factoryCreatedProduct = factory.CreateInstance();
Assert.AreEqual(template.Value, factoryCreatedProduct.Value);
}
/// <summary>
/// Tests whether the prototype-based factory behaves correctly by creating
/// new instances of its product using clones of its assigned prototype.
/// </summary>
[Test]
public void TestInstanceCreation() {
ConcretePrototype template = new ConcretePrototype(42);
IAbstractFactory factory = new PrototypeFactory<
IProduct, ConcretePrototype
>(template);
IProduct factoryCreatedProduct = (IProduct)factory.CreateInstance();
Assert.AreEqual(template.Value, factoryCreatedProduct.Value);
}
/// <summary>
/// Tests whether the prototype is disposed if it implements the IDisposable
/// interface and the factory is explicitely disposed.
/// </summary>
[Test]
public void TestPrototypeDisposal() {
ConcretePrototype template = new ConcretePrototype(42);
PrototypeFactory<IProduct, ConcretePrototype> factory = new PrototypeFactory<
IProduct, ConcretePrototype
>(template);
Assert.IsFalse(template.IsDisposed);
factory.Dispose();
Assert.IsTrue(template.IsDisposed);
}
}
} // namespace Nuclex.Support.Plugins
#endif // UNITTEST

View File

@ -1,58 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Nuclex.Support.Plugins {
#if !NO_CLONING
/// <summary>Factory that creates instances by cloning a prototype</summary>
/// <typeparam name="TProduct">Type of product created by the factory</typeparam>
/// <typeparam name="TConcreteType">Type of the prototype that will be cloned</typeparam>
public class PrototypeFactory<TProduct, TConcreteType> :
IAbstractFactory<TProduct>, IAbstractFactory, IDisposable
where TProduct : class
where TConcreteType : class, ICloneable {
/// <summary>Initializes a new prototype based factory</summary>
/// <param name="prototype">Prototype instance that will be cloned</param>
public PrototypeFactory(TConcreteType prototype) {
this.prototype = prototype;
}
/// <summary>
/// Creates a new instance of the type to which the factory is specialized
/// </summary>
/// <returns>The newly created instance</returns>
public TProduct CreateInstance() {
return (TProduct)this.prototype.Clone();
}
/// <summary>
/// Creates a new instance of the type to which the factory is specialized
/// </summary>
/// <returns>The newly created instance</returns>
object IAbstractFactory.CreateInstance() {
return this.prototype.Clone();
}
/// <summary>Immediately releases all resources owned by the instance</summary>
public void Dispose() {
if(this.prototype != null) {
IDisposable disposablePrototype = this.prototype as IDisposable;
if(disposablePrototype != null) {
disposablePrototype.Dispose();
}
this.prototype = null;
}
}
/// <summary>The prototype object</summary>
private TConcreteType prototype;
}
#endif // !NO_CLONING
} // namespace Nuclex.Support.Plugins