1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
19 using Castle
.Core
.Logging
;
20 using Castle
.MonoRail
.Framework
;
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"/>
28 public abstract class AbstractControllerFactory
: IServiceEnabledComponent
, IInitializable
, IControllerFactory
31 /// The controller tree. A binary tree that contains
32 /// all controllers registered
34 private IControllerTree tree
;
37 /// The logger instance
39 private ILogger logger
= NullLogger
.Instance
;
42 /// Initializes an <c>AbstractControllerFactory</c> instance
44 protected AbstractControllerFactory()
49 /// Initializes a new instance of the <see cref="AbstractControllerFactory"/> class.
51 /// <param name="tree">The tree.</param>
52 protected AbstractControllerFactory(IControllerTree tree
)
57 #region IInitializable implementation
60 /// Invoked by the framework in order to initialize the state
62 public virtual void Initialize()
64 AddBuiltInControllers();
69 #region IServiceEnabledComponent implementation
72 /// Invoked by the framework in order to give a chance to
73 /// obtain other services
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
));
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>.
96 /// <returns></returns>
97 public virtual IController
CreateController(string area
, string controller
)
99 area
= area
?? String
.Empty
;
101 return CreateControllerInstance(area
, controller
);
105 /// Creates the controller.
107 /// <param name="controllerType">Type of the controller.</param>
108 /// <returns></returns>
109 public IController
CreateController(Type controllerType
)
113 return (IController
) Activator
.CreateInstance(controllerType
);
117 logger
.Error("Could not create controller instance. Activation failed.", ex
);
124 /// Implementors should perform their logic
125 /// to release the <see cref="IController"/> instance
126 /// and its resources.
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();
142 /// <value>The tree.</value>
143 public IControllerTree Tree
146 set { tree = value; }
150 /// Register built-in controller that serve static files
152 protected void AddBuiltInControllers()
154 if (logger
.IsDebugEnabled
)
156 logger
.Debug("Registering built-in controllers");
159 // Tree.AddController("MonoRail", "Files", typeof(FilesController));
163 /// Creates the controller instance.
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
);
179 logger
.ErrorFormat("Controller not found. Area '{0}' Name '{1}'", area
, name
);
181 throw new ControllerNotFoundException(area
, name
);
184 return CreateController(type
);