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
18 using System
.Collections
.Generic
;
19 using System
.Reflection
;
22 using Castle
.Core
.Logging
;
23 using Castle
.MonoRail
.Framework
.Configuration
;
24 using Castle
.MonoRail
.Framework
.Descriptors
;
25 using Castle
.MonoRail
.Framework
.Services
.Utils
;
28 /// Standard implementation of <see cref="IControllerFactory"/>.
29 /// It inspects assemblies looking for concrete classes
30 /// that extend <see cref="IController"/>.
32 public class DefaultControllerFactory
: AbstractControllerFactory
, IInitializable
35 /// The logger instance
37 private ILogger logger
= NullLogger
.Instance
;
39 private List
<string> assemblies
;
42 /// Initializes a new instance of the <see cref="DefaultControllerFactory"/> class.
44 public DefaultControllerFactory()
49 /// Initializes a new instance of the <see cref="DefaultControllerFactory"/> class.
51 /// <param name="tree">The tree.</param>
52 public DefaultControllerFactory(IControllerTree tree
) : base(tree
)
56 #region IInitializable implementation
59 /// Invoked by the framework in order to initialize the state
61 public override void Initialize()
65 if (assemblies
!= null)
67 foreach(String assembly
in assemblies
)
79 /// Invoked by the framework in order to give a chance to
80 /// obtain other services
82 /// <param name="provider">The service proviver</param>
83 public override void Service(IServiceProvider provider
)
85 base.Service(provider
);
87 ILoggerFactory loggerFactory
= (ILoggerFactory
) provider
.GetService(typeof(ILoggerFactory
));
89 if (loggerFactory
!= null)
91 logger
= loggerFactory
.Create(typeof(AbstractControllerFactory
));
94 IMonoRailConfiguration config
= (IMonoRailConfiguration
) provider
.GetService(typeof(IMonoRailConfiguration
));
98 assemblies
= config
.ControllersConfig
.Assemblies
;
100 if (assemblies
.Count
== 0)
102 throw new System
.Configuration
.ConfigurationErrorsException("No assembly was informed on the configuration file. " +
103 "Unfortunatelly this cannot be inferred (we tried)");
109 /// Loads the assembly and inspect its public types.
111 /// <param name="assemblyFileName"></param>
112 public void Inspect(String assemblyFileName
)
114 if (logger
.IsDebugEnabled
)
116 logger
.DebugFormat("Inspecting assembly '{0}'", assemblyFileName
);
119 Assembly assembly
= Assembly
.Load( assemblyFileName
);
125 /// Inspect the assembly's public types.
127 public void Inspect(Assembly assembly
)
129 Type
[] types
= assembly
.GetExportedTypes();
131 foreach(Type type
in types
)
133 if (!type
.IsPublic
|| type
.IsAbstract
|| type
.IsInterface
|| type
.IsValueType
)
138 if (typeof(IController
).IsAssignableFrom(type
))
140 ControllerDescriptor contrDesc
= ControllerInspectionUtil
.Inspect(type
);
142 RegisterController(contrDesc
);
148 /// Registers the controller.
150 /// <param name="descriptor">The descriptor.</param>
151 private void RegisterController(ControllerDescriptor descriptor
)
153 if (logger
.IsDebugEnabled
)
155 logger
.DebugFormat("Registering controller descriptor for Area: '{0}' Name: '{1}'",
156 descriptor
.Area
, descriptor
.Name
);
159 Tree
.AddController(descriptor
.Area
, descriptor
.Name
, descriptor
.ControllerType
);