Added non-generic registration interface to IKernel and IWindsor to accommodate dynam...
[castle.git] / MonoRail / Castle.MonoRail.Framework / Services / AbstractControllerFactory.cs
blob00ca9132ba402708909132ceaba3f84144c0c1ad
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 Castle.Core;
19 using Castle.Core.Logging;
20 using Castle.MonoRail.Framework;
22 /// <summary>
23 /// Base implementation of <see cref="IControllerFactory"/>
24 /// using the <see cref="DefaultControllerTree"/> to build an hierarchy
25 /// of controllers and the areas they belong to.
26 /// <seealso cref="DefaultControllerTree"/>
27 /// </summary>
28 public abstract class AbstractControllerFactory : IServiceEnabledComponent, IInitializable, IControllerFactory
30 /// <summary>
31 /// The controller tree. A binary tree that contains
32 /// all controllers registered
33 /// </summary>
34 private IControllerTree tree;
36 /// <summary>
37 /// The logger instance
38 /// </summary>
39 private ILogger logger = NullLogger.Instance;
41 /// <summary>
42 /// Initializes an <c>AbstractControllerFactory</c> instance
43 /// </summary>
44 protected AbstractControllerFactory()
48 /// <summary>
49 /// Initializes a new instance of the <see cref="AbstractControllerFactory"/> class.
50 /// </summary>
51 /// <param name="tree">The tree.</param>
52 protected AbstractControllerFactory(IControllerTree tree)
54 this.tree = tree;
57 #region IInitializable implementation
59 /// <summary>
60 /// Invoked by the framework in order to initialize the state
61 /// </summary>
62 public virtual void Initialize()
64 AddBuiltInControllers();
67 #endregion
69 #region IServiceEnabledComponent implementation
71 /// <summary>
72 /// Invoked by the framework in order to give a chance to
73 /// obtain other services
74 /// </summary>
75 /// <param name="provider">The service proviver</param>
76 public virtual void Service(IServiceProvider provider)
78 ILoggerFactory loggerFactory = (ILoggerFactory) provider.GetService(typeof(ILoggerFactory));
80 if (loggerFactory != null)
82 logger = loggerFactory.Create(typeof(AbstractControllerFactory));
85 tree = (IControllerTree) provider.GetService(typeof(IControllerTree));
88 #endregion
90 /// <summary>
91 /// Implementors should perform their logic to
92 /// return a instance of <see cref="IController"/>.
93 /// If the <see cref="IController"/> can not be found,
94 /// it should return <c>null</c>.
95 /// </summary>
96 /// <returns></returns>
97 public virtual IController CreateController(string area, string controller)
99 area = area ?? String.Empty;
101 return CreateControllerInstance(area, controller);
104 /// <summary>
105 /// Creates the controller.
106 /// </summary>
107 /// <param name="controllerType">Type of the controller.</param>
108 /// <returns></returns>
109 public IController CreateController(Type controllerType)
113 return (IController) Activator.CreateInstance(controllerType);
115 catch (Exception ex)
117 logger.Error("Could not create controller instance. Activation failed.", ex);
119 throw;
123 /// <summary>
124 /// Implementors should perform their logic
125 /// to release the <see cref="IController"/> instance
126 /// and its resources.
127 /// </summary>
128 /// <param name="controller"></param>
129 public virtual void Release(IController controller)
131 if (logger.IsDebugEnabled)
133 logger.Debug("Controller released: " + controller.GetType());
136 controller.Dispose();
139 /// <summary>
140 /// Gets the tree.
141 /// </summary>
142 /// <value>The tree.</value>
143 public IControllerTree Tree
145 get { return tree; }
146 set { tree = value; }
149 /// <summary>
150 /// Register built-in controller that serve static files
151 /// </summary>
152 protected void AddBuiltInControllers()
154 if (logger.IsDebugEnabled)
156 logger.Debug("Registering built-in controllers");
159 // Tree.AddController("MonoRail", "Files", typeof(FilesController));
162 /// <summary>
163 /// Creates the controller instance.
164 /// </summary>
165 /// <param name="area">The area.</param>
166 /// <param name="name">The name.</param>
167 /// <returns></returns>
168 protected virtual IController CreateControllerInstance(String area, String name)
170 if (logger.IsDebugEnabled)
172 logger.DebugFormat("Creating controller instance. Area '{0}' Name '{1}'", area, name);
175 Type type = Tree.GetController(area, name);
177 if (type == null)
179 logger.ErrorFormat("Controller not found. Area '{0}' Name '{1}'", area, name);
181 throw new ControllerNotFoundException(area, name);
184 return CreateController(type);