- Fixed MR-84
[castle.git] / MonoRail / Castle.MonoRail.Framework / Services / AbstractControllerFactory.cs
blob984af11dfb6198484929646c6b853f233a7755cf
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;
19 using Castle.Core;
20 using Castle.Core.Logging;
21 using Castle.MonoRail.Framework;
22 using Castle.MonoRail.Framework.Controllers;
24 /// <summary>
25 /// Base implementation of <see cref="IControllerFactory"/>
26 /// using the <see cref="DefaultControllerTree"/> to build an hierarchy
27 /// of controllers and the areas they belong to.
28 /// <seealso cref="DefaultControllerTree"/>
29 /// </summary>
30 public abstract class AbstractControllerFactory : IServiceEnabledComponent, IInitializable, IControllerFactory
32 /// <summary>
33 /// The controller tree. A binary tree that contains
34 /// all controllers registered
35 /// </summary>
36 private IControllerTree tree;
38 /// <summary>
39 /// The logger instance
40 /// </summary>
41 private ILogger logger = NullLogger.Instance;
43 /// <summary>
44 /// Initializes an <c>AbstractControllerFactory</c> instance
45 /// </summary>
46 public AbstractControllerFactory()
50 #region IInitializable implementation
52 /// <summary>
53 /// Invoked by the framework in order to initialize the state
54 /// </summary>
55 public virtual void Initialize()
57 AddBuiltInControllers();
60 #endregion
62 #region IServiceEnabledComponent implementation
64 /// <summary>
65 /// Invoked by the framework in order to give a chance to
66 /// obtain other services
67 /// </summary>
68 /// <param name="provider">The service proviver</param>
69 public virtual void Service(IServiceProvider provider)
71 ILoggerFactory loggerFactory = (ILoggerFactory) provider.GetService(typeof(ILoggerFactory));
73 if (loggerFactory != null)
75 logger = loggerFactory.Create(typeof(AbstractControllerFactory));
78 tree = (IControllerTree) provider.GetService(typeof(IControllerTree));
81 #endregion
83 /// <summary>
84 /// Implementors should perform their logic to
85 /// return a instance of <see cref="Controller"/>.
86 /// If the <see cref="Controller"/> can not be found,
87 /// it should return <c>null</c>.
88 /// </summary>
89 /// <param name="urlInfo"></param>
90 /// <returns></returns>
91 public virtual Controller CreateController(UrlInfo urlInfo)
93 String area = urlInfo.Area ?? String.Empty;
94 String name = urlInfo.Controller;
96 return CreateControllerInstance(area, name);
99 /// <summary>
100 /// Implementors should perform their logic
101 /// to release the <see cref="Controller"/> instance
102 /// and its resources.
103 /// </summary>
104 /// <param name="controller"></param>
105 public virtual void Release(Controller controller)
107 if (logger.IsDebugEnabled)
109 logger.Debug("Controller released: " + controller.GetType());
112 controller.Dispose();
115 /// <summary>
116 /// Gets the tree.
117 /// </summary>
118 /// <value>The tree.</value>
119 public IControllerTree Tree
121 get { return tree; }
124 /// <summary>
125 /// Register built-in controller that serve static files
126 /// </summary>
127 protected void AddBuiltInControllers()
129 if (logger.IsDebugEnabled)
131 logger.Debug("Registering built-in controllers");
134 Tree.AddController("MonoRail", "Files", typeof(FilesController));
137 /// <summary>
138 /// Creates the controller instance.
139 /// </summary>
140 /// <param name="area">The area.</param>
141 /// <param name="name">The name.</param>
142 /// <returns></returns>
143 protected virtual Controller CreateControllerInstance(String area, String name)
145 if (logger.IsDebugEnabled)
147 logger.DebugFormat("Creating controller instance. Area '{0}' Name '{1}'", area, name);
150 Type type = Tree.GetController(area, name);
152 if (type == null)
154 logger.ErrorFormat("Controller not found. Area '{0}' Name '{1}'", area, name);
156 throw new ControllerNotFoundException(area, name);
161 return (Controller) Activator.CreateInstance(type);
163 catch(Exception ex)
165 logger.Error("Could not create controller instance. Activation failed.", ex);
167 throw;