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
20 using Castle
.Core
.Logging
;
21 using Castle
.MonoRail
.Framework
;
22 using Castle
.MonoRail
.Framework
.Controllers
;
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"/>
30 public abstract class AbstractControllerFactory
: IServiceEnabledComponent
, IInitializable
, IControllerFactory
33 /// The controller tree. A binary tree that contains
34 /// all controllers registered
36 private IControllerTree tree
;
39 /// The logger instance
41 private ILogger logger
= NullLogger
.Instance
;
44 /// Initializes an <c>AbstractControllerFactory</c> instance
46 public AbstractControllerFactory()
50 #region IInitializable implementation
53 /// Invoked by the framework in order to initialize the state
55 public virtual void Initialize()
57 AddBuiltInControllers();
62 #region IServiceEnabledComponent implementation
65 /// Invoked by the framework in order to give a chance to
66 /// obtain other services
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
));
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>.
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
);
100 /// Implementors should perform their logic
101 /// to release the <see cref="Controller"/> instance
102 /// and its resources.
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();
118 /// <value>The tree.</value>
119 public IControllerTree Tree
125 /// Register built-in controller that serve static files
127 protected void AddBuiltInControllers()
129 if (logger
.IsDebugEnabled
)
131 logger
.Debug("Registering built-in controllers");
134 Tree
.AddController("MonoRail", "Files", typeof(FilesController
));
138 /// Creates the controller instance.
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
);
154 logger
.ErrorFormat("Controller not found. Area '{0}' Name '{1}'", area
, name
);
156 throw new ControllerNotFoundException(area
, name
);
161 return (Controller
) Activator
.CreateInstance(type
);
165 logger
.Error("Could not create controller instance. Activation failed.", ex
);