Merged branch back to the trunk. Build is passing with no changes.
[castle.git] / MonoRail / Castle.MonoRail.Framework / Services / DefaultControllerFactory.cs
blob2bb34937ffffb830748bf9420d83aedad16e4a06
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.MonoRail.Framework.Services
17 using System;
18 using System.Collections.Generic;
19 using System.Reflection;
21 using Castle.Core;
22 using Castle.Core.Logging;
23 using Castle.MonoRail.Framework.Configuration;
24 using Castle.MonoRail.Framework.Descriptors;
25 using Castle.MonoRail.Framework.Services.Utils;
27 /// <summary>
28 /// Standard implementation of <see cref="IControllerFactory"/>.
29 /// It inspects assemblies looking for concrete classes
30 /// that extend <see cref="IController"/>.
31 /// </summary>
32 public class DefaultControllerFactory : AbstractControllerFactory, IInitializable
34 /// <summary>
35 /// The logger instance
36 /// </summary>
37 private ILogger logger = NullLogger.Instance;
39 private List<string> assemblies;
41 /// <summary>
42 /// Initializes a new instance of the <see cref="DefaultControllerFactory"/> class.
43 /// </summary>
44 public DefaultControllerFactory()
48 /// <summary>
49 /// Initializes a new instance of the <see cref="DefaultControllerFactory"/> class.
50 /// </summary>
51 /// <param name="tree">The tree.</param>
52 public DefaultControllerFactory(IControllerTree tree) : base(tree)
56 #region IInitializable implementation
58 /// <summary>
59 /// Invoked by the framework in order to initialize the state
60 /// </summary>
61 public override void Initialize()
63 base.Initialize();
65 if (assemblies != null)
67 foreach(String assembly in assemblies)
69 Inspect(assembly);
73 assemblies = null;
76 #endregion
78 /// <summary>
79 /// Invoked by the framework in order to give a chance to
80 /// obtain other services
81 /// </summary>
82 /// <param name="provider">The service proviver</param>
83 public override void Service(IServiceProvider provider)
85 base.Service(provider);
87 ILoggerFactory loggerFactory = (ILoggerFactory) provider.GetService(typeof(ILoggerFactory));
89 if (loggerFactory != null)
91 logger = loggerFactory.Create(typeof(AbstractControllerFactory));
94 IMonoRailConfiguration config = (IMonoRailConfiguration) provider.GetService(typeof(IMonoRailConfiguration));
96 if (config != null)
98 assemblies = config.ControllersConfig.Assemblies;
100 if (assemblies.Count == 0)
102 throw new System.Configuration.ConfigurationErrorsException("No assembly was informed on the configuration file. " +
103 "Unfortunatelly this cannot be inferred (we tried)");
108 /// <summary>
109 /// Loads the assembly and inspect its public types.
110 /// </summary>
111 /// <param name="assemblyFileName"></param>
112 public void Inspect(String assemblyFileName)
114 if (logger.IsDebugEnabled)
116 logger.DebugFormat("Inspecting assembly '{0}'", assemblyFileName);
119 Assembly assembly = Assembly.Load( assemblyFileName );
121 Inspect(assembly);
124 /// <summary>
125 /// Inspect the assembly's public types.
126 /// </summary>
127 public void Inspect(Assembly assembly)
129 Type[] types = assembly.GetExportedTypes();
131 foreach(Type type in types)
133 if (!type.IsPublic || type.IsAbstract || type.IsInterface || type.IsValueType)
135 continue;
138 if (typeof(IController).IsAssignableFrom(type))
140 ControllerDescriptor contrDesc = ControllerInspectionUtil.Inspect(type);
142 RegisterController(contrDesc);
147 /// <summary>
148 /// Registers the controller.
149 /// </summary>
150 /// <param name="descriptor">The descriptor.</param>
151 private void RegisterController(ControllerDescriptor descriptor)
153 if (logger.IsDebugEnabled)
155 logger.DebugFormat("Registering controller descriptor for Area: '{0}' Name: '{1}'",
156 descriptor.Area, descriptor.Name);
159 Tree.AddController(descriptor.Area, descriptor.Name, descriptor.ControllerType);