- Fixed MR-84
[castle.git] / MonoRail / Castle.MonoRail.Framework / Services / DefaultControllerFactory.cs
blob5b0e43bd081bdf3243bff756dd826610f981aaa8
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.Reflection;
20 using Castle.Core;
21 using Castle.Core.Logging;
22 using Castle.MonoRail.Framework.Configuration;
23 using Castle.MonoRail.Framework.Internal;
24 using Castle.MonoRail.Framework.Services.Utils;
26 /// <summary>
27 /// Standard implementation of <see cref="IControllerFactory"/>.
28 /// It inspects assemblies looking for concrete classes
29 /// that extend <see cref="Controller"/>.
30 /// </summary>
31 public class DefaultControllerFactory : AbstractControllerFactory, IInitializable
33 /// <summary>
34 /// The logger instance
35 /// </summary>
36 private ILogger logger = NullLogger.Instance;
38 private string[] assemblies;
40 /// <summary>
41 /// Initializes a new instance of the <see cref="DefaultControllerFactory"/> class.
42 /// </summary>
43 public DefaultControllerFactory()
47 #region IInitializable implementation
49 /// <summary>
50 /// Invoked by the framework in order to initialize the state
51 /// </summary>
52 public override void Initialize()
54 base.Initialize();
56 if (assemblies != null)
58 foreach(String assembly in assemblies)
60 Inspect(assembly);
64 assemblies = null;
67 #endregion
69 /// <summary>
70 /// Invoked by the framework in order to give a chance to
71 /// obtain other services
72 /// </summary>
73 /// <param name="provider">The service proviver</param>
74 public override void Service(IServiceProvider provider)
76 base.Service(provider);
78 ILoggerFactory loggerFactory = (ILoggerFactory) provider.GetService(typeof(ILoggerFactory));
80 if (loggerFactory != null)
82 logger = loggerFactory.Create(typeof(AbstractControllerFactory));
85 MonoRailConfiguration config = (MonoRailConfiguration) provider.GetService(typeof(MonoRailConfiguration));
87 if (config != null)
89 assemblies = config.ControllersConfig.Assemblies;
91 if (assemblies == null || assemblies.Length == 0)
93 throw new System.Configuration.ConfigurationErrorsException("No assembly was informed on the configuration file. " +
94 "Unfortunatelly this cannot be inferred (we tried)");
99 /// <summary>
100 /// Loads the assembly and inspect its public types.
101 /// </summary>
102 /// <param name="assemblyFileName"></param>
103 public void Inspect(String assemblyFileName)
105 if (logger.IsDebugEnabled)
107 logger.DebugFormat("Inspecting assembly '{0}'", assemblyFileName);
110 Assembly assembly = Assembly.Load( assemblyFileName );
112 Inspect(assembly);
115 /// <summary>
116 /// Inspect the assembly's public types.
117 /// </summary>
118 public void Inspect(Assembly assembly)
120 Type[] types = assembly.GetExportedTypes();
122 foreach(Type type in types)
124 if (!type.IsPublic || type.IsAbstract || type.IsInterface || type.IsValueType)
126 continue;
129 if (typeof(Controller).IsAssignableFrom(type))
131 ControllerDescriptor contrDesc = ControllerInspectionUtil.Inspect(type);
133 RegisterController(contrDesc);
138 /// <summary>
139 /// Registers the controller.
140 /// </summary>
141 /// <param name="descriptor">The descriptor.</param>
142 private void RegisterController(ControllerDescriptor descriptor)
144 if (logger.IsDebugEnabled)
146 logger.DebugFormat("Registering controller descriptor for Area: '{0}' Name: '{1}'",
147 descriptor.Area, descriptor.Name);
150 Tree.AddController(descriptor.Area, descriptor.Name, descriptor.ControllerType);