Added default constructor to PropertyDescriptor. This will normally be used to provi...
[castle.git] / Facilities / BatchRegistration / Castle.Facilities.BatchRegistration / BatchRegistrationFacility.cs
blobbb21b9460e90bbe0228c9d87ed3b4ce5e5082aa4
1 // Copyright 2004-2007 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.Facilities.BatchRegistration
17 using System;
18 using System.Configuration;
20 using Castle.Core.Configuration;
22 using Castle.MicroKernel;
23 using Castle.MicroKernel.Facilities;
26 public class BatchRegistrationFacility : AbstractFacility
28 protected override void Init()
30 if (FacilityConfig == null) return;
32 foreach(IConfiguration config in FacilityConfig.Children)
34 if ("assemblyBatch".Equals(config.Name))
36 AddComponents(config);
38 else if ("addFacility".Equals(config.Name))
40 AddFacility(config);
42 else
44 String message = "Invalid node inside facility configuration. Expected assemblyBatch";
46 throw new ConfigurationErrorsException(message);
51 private void AddComponents(IConfiguration config)
53 String assemblyName = config.Attributes["name"];
55 if (assemblyName == null || assemblyName.Length == 0)
57 String message = "The assemblyBatch node must have a 'name' attribute with the name of the assembly";
59 throw new ConfigurationErrorsException(message);
62 ComponentScanner scanner = new ComponentScanner(assemblyName);
64 ConfigureScanner(config, scanner);
66 ComponentDefinition[] definitions = scanner.Process();
68 foreach(ComponentDefinition definition in definitions)
70 if (definition.ServiceType == null)
72 Kernel.AddComponent( definition.Key, definition.ClassType );
74 else
76 Kernel.AddComponent( definition.Key, definition.ServiceType, definition.ClassType );
81 private void AddFacility(IConfiguration config)
83 String id = config.Attributes["id"];
84 String type = config.Attributes["type"];
86 if (type == null || type.Length == 0)
88 String message = "The addFacility node must have a 'type' attribute with the Type's name";
90 throw new ConfigurationErrorsException(message);
92 if (id == null || id.Length == 0)
94 String message = "The addFacility node must have a 'id' attribute with facility's key";
96 throw new ConfigurationErrorsException(message);
99 Kernel.AddFacility( id, InstatiateFacility( type ) );
102 private IFacility InstatiateFacility(String facilityType)
104 Type type = TypeLoadUtil.GetType(facilityType);
106 return (IFacility) Activator.CreateInstance( type );
109 private void ConfigureScanner(IConfiguration config, ComponentScanner scanner)
111 if (config.Attributes["useAttributes"] != null)
113 scanner.UseAttributes = config.Attributes["useAttributes"].Equals("true");
116 foreach(IConfiguration innerConfig in config.Children)
118 if ("include".Equals(innerConfig.Name) )
120 String key = innerConfig.Attributes["key"];
121 String service = innerConfig.Attributes["service"];
122 String component = innerConfig.Attributes["component"];
124 scanner.AddInclude( key, service, component );
126 else if ("exclude".Equals(innerConfig.Name) )
128 String type = innerConfig.Attributes["type"];
130 scanner.AddExclude( type );
132 else
134 String message = "Invalid node inside assemblyBatch configuration. Expected 'include' or 'exclude'";
136 throw new ConfigurationErrorsException(message);