diff options
Diffstat (limited to 'src/WixToolset.Mba.Host/BootstrapperApplicationFactory.cs')
-rw-r--r-- | src/WixToolset.Mba.Host/BootstrapperApplicationFactory.cs | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/src/WixToolset.Mba.Host/BootstrapperApplicationFactory.cs b/src/WixToolset.Mba.Host/BootstrapperApplicationFactory.cs new file mode 100644 index 00000000..9385d1d1 --- /dev/null +++ b/src/WixToolset.Mba.Host/BootstrapperApplicationFactory.cs | |||
@@ -0,0 +1,85 @@ | |||
1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. | ||
2 | |||
3 | namespace WixToolset.Mba.Host | ||
4 | { | ||
5 | using System; | ||
6 | using System.Configuration; | ||
7 | using System.Reflection; | ||
8 | using System.Runtime.InteropServices; | ||
9 | using WixToolset.Mba.Core; | ||
10 | |||
11 | /// <summary> | ||
12 | /// Entry point for the MBA host to create and return the BA to the engine. | ||
13 | /// </summary> | ||
14 | [ClassInterface(ClassInterfaceType.None)] | ||
15 | public sealed class BootstrapperApplicationFactory : MarshalByRefObject, IBootstrapperApplicationFactory | ||
16 | { | ||
17 | /// <summary> | ||
18 | /// Creates a new instance of the <see cref="BootstrapperApplicationFactory"/> class. | ||
19 | /// </summary> | ||
20 | public BootstrapperApplicationFactory() | ||
21 | { | ||
22 | } | ||
23 | |||
24 | /// <summary> | ||
25 | /// Loads the bootstrapper application assembly and calls its IBootstrapperApplicationFactory.Create method. | ||
26 | /// </summary> | ||
27 | /// <param name="pArgs">Pointer to BOOTSTRAPPER_CREATE_ARGS struct.</param> | ||
28 | /// <param name="pResults">Pointer to BOOTSTRAPPER_CREATE_RESULTS struct.</param> | ||
29 | /// <exception cref="MissingAttributeException">The bootstrapper application assembly | ||
30 | /// does not define the <see cref="BootstrapperApplicationFactoryAttribute"/>.</exception> | ||
31 | public void Create(IntPtr pArgs, IntPtr pResults) | ||
32 | { | ||
33 | // Get the wix.boostrapper section group to get the name of the bootstrapper application assembly to host. | ||
34 | var section = ConfigurationManager.GetSection("wix.bootstrapper/host") as HostSection; | ||
35 | if (null == section) | ||
36 | { | ||
37 | throw new MissingAttributeException(); // TODO: throw a more specific exception than this. | ||
38 | } | ||
39 | |||
40 | // Load the BA's IBootstrapperApplicationFactory. | ||
41 | var baFactoryType = BootstrapperApplicationFactory.GetBAFactoryTypeFromAssembly(section.AssemblyName); | ||
42 | var baFactory = (IBootstrapperApplicationFactory)Activator.CreateInstance(baFactoryType); | ||
43 | if (null == baFactory) | ||
44 | { | ||
45 | throw new InvalidBootstrapperApplicationFactoryException(); | ||
46 | } | ||
47 | |||
48 | baFactory.Create(pArgs, pResults); | ||
49 | } | ||
50 | |||
51 | /// <summary> | ||
52 | /// Locates the <see cref="BootstrapperApplicationFactoryAttribute"/> and returns the specified type. | ||
53 | /// </summary> | ||
54 | /// <param name="assemblyName">The assembly that defines the IBootstrapperApplicationFactory implementation.</param> | ||
55 | /// <returns>The bootstrapper application factory <see cref="Type"/>.</returns> | ||
56 | private static Type GetBAFactoryTypeFromAssembly(string assemblyName) | ||
57 | { | ||
58 | Type baFactoryType = null; | ||
59 | |||
60 | // Load the requested assembly. | ||
61 | Assembly asm = AppDomain.CurrentDomain.Load(assemblyName); | ||
62 | |||
63 | // If an assembly was loaded and is not the current assembly, check for the required attribute. | ||
64 | // This is done to avoid using the BootstrapperApplicationFactoryAttribute which we use at build time | ||
65 | // to specify the BootstrapperApplicationFactory assembly in the manifest. | ||
66 | if (!Assembly.GetExecutingAssembly().Equals(asm)) | ||
67 | { | ||
68 | // There must be one and only one BootstrapperApplicationFactoryAttribute. | ||
69 | // The attribute prevents multiple declarations already. | ||
70 | var attrs = (BootstrapperApplicationFactoryAttribute[])asm.GetCustomAttributes(typeof(BootstrapperApplicationFactoryAttribute), false); | ||
71 | if (null != attrs) | ||
72 | { | ||
73 | baFactoryType = attrs[0].BootstrapperApplicationFactoryType; | ||
74 | } | ||
75 | } | ||
76 | |||
77 | if (null == baFactoryType) | ||
78 | { | ||
79 | throw new MissingAttributeException(); | ||
80 | } | ||
81 | |||
82 | return baFactoryType; | ||
83 | } | ||
84 | } | ||
85 | } | ||