Fixing an issue with output parameters that are of type IntPtr
[castle.git] / InversionOfControl / Castle.Windsor / Installer / DefaultComponentInstaller.cs
blob7cf47457dc9e52b3d1c197baf01f7e0c4b50f423
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.Windsor.Installer
17 using System;
18 using System.Configuration;
20 using Castle.Core.Configuration;
21 using Castle.Core.Resource;
22 using Castle.MicroKernel;
23 using Castle.Windsor.Configuration.Interpreters;
25 /// <summary>
26 /// Default <see cref="IComponentsInstaller"/> implementation.
27 /// </summary>
28 public class DefaultComponentInstaller : IComponentsInstaller
30 /// <summary>
31 /// Initializes a new instance of the <see cref="DefaultComponentInstaller"/> class.
32 /// </summary>
33 public DefaultComponentInstaller()
37 #region IComponentsInstaller Members
39 /// <summary>
40 /// Perform installation.
41 /// </summary>
42 /// <param name="container">Target container</param>
43 /// <param name="store">Configuration store</param>
44 public void SetUp(IWindsorContainer container, IConfigurationStore store)
46 SetUpComponents(store.GetBootstrapComponents(), container);
47 SetUpFacilities(store.GetFacilities(), container);
48 SetUpComponents(store.GetComponents(), container);
49 SetUpChildContainers(store.GetConfigurationForChildContainers(), container);
52 #endregion
54 protected virtual void SetUpFacilities(IConfiguration[] configurations, IWindsorContainer container)
56 foreach(IConfiguration facility in configurations)
58 String id = facility.Attributes["id"];
59 String typeName = facility.Attributes["type"];
60 if (typeName == null || typeName.Length == 0) continue;
62 Type type = ObtainType(typeName);
64 IFacility facilityInstance = InstantiateFacility(type);
66 System.Diagnostics.Debug.Assert( id != null );
67 System.Diagnostics.Debug.Assert( facilityInstance != null );
69 container.AddFacility(id, facilityInstance);
73 protected virtual void SetUpComponents(IConfiguration[] configurations, IWindsorContainer container)
75 foreach(IConfiguration component in configurations)
77 String id = component.Attributes["id"];
79 String typeName = component.Attributes["type"];
80 String serviceTypeName = component.Attributes["service"];
82 if (typeName == null || typeName.Length == 0) continue;
84 Type type = ObtainType(typeName);
85 Type service = type;
87 if (serviceTypeName != null && serviceTypeName.Length != 0)
89 service = ObtainType(serviceTypeName);
92 System.Diagnostics.Debug.Assert( id != null );
93 System.Diagnostics.Debug.Assert( type != null );
94 System.Diagnostics.Debug.Assert( service != null );
96 container.AddComponent(id, service, type);
100 private void SetUpChildContainers(IConfiguration[] configurations, IWindsorContainer parentContainer)
102 foreach(IConfiguration childContainerConfig in configurations)
104 String id = childContainerConfig.Attributes["name"];
106 System.Diagnostics.Debug.Assert( id != null );
108 new WindsorContainer(id, parentContainer,
109 new XmlInterpreter(new StaticContentResource(childContainerConfig.Value)));
113 private Type ObtainType(String typeName)
115 Type type = Type.GetType(typeName, false, false);
117 if (type == null)
119 String message = String.Format("The type name {0} could not be located", typeName);
121 throw new ConfigurationErrorsException(message);
124 return type;
127 private IFacility InstantiateFacility(Type facilityType)
129 if (!typeof(IFacility).IsAssignableFrom( facilityType ))
131 String message = String.Format("Type {0} does not implement the interface IFacility", facilityType.FullName);
133 throw new ConfigurationErrorsException(message);
138 return (IFacility) Activator.CreateInstance(facilityType);
140 catch(Exception)
142 String message = String.Format("Could not instantiate {0}. Does it have a public default constructor?", facilityType.FullName);
144 throw new ConfigurationErrorsException(message);