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
.Reflection
;
21 using Castle
.Core
.Logging
;
22 using Castle
.MonoRail
.Framework
.Configuration
;
23 using Castle
.MonoRail
.Framework
.Internal
;
24 using Castle
.MonoRail
.Framework
.Services
.Utils
;
27 /// Standard implementation of <see cref="IControllerFactory"/>.
28 /// It inspects assemblies looking for concrete classes
29 /// that extend <see cref="Controller"/>.
31 public class DefaultControllerFactory
: AbstractControllerFactory
, IInitializable
34 /// The logger instance
36 private ILogger logger
= NullLogger
.Instance
;
38 private string[] assemblies
;
41 /// Initializes a new instance of the <see cref="DefaultControllerFactory"/> class.
43 public DefaultControllerFactory()
47 #region IInitializable implementation
50 /// Invoked by the framework in order to initialize the state
52 public override void Initialize()
56 if (assemblies
!= null)
58 foreach(String assembly
in assemblies
)
70 /// Invoked by the framework in order to give a chance to
71 /// obtain other services
73 /// <param name="provider">The service proviver</param>
74 public override void Service(IServiceProvider provider
)
76 base.Service(provider
);
78 ILoggerFactory loggerFactory
= (ILoggerFactory
) provider
.GetService(typeof(ILoggerFactory
));
80 if (loggerFactory
!= null)
82 logger
= loggerFactory
.Create(typeof(AbstractControllerFactory
));
85 MonoRailConfiguration config
= (MonoRailConfiguration
) provider
.GetService(typeof(MonoRailConfiguration
));
89 assemblies
= config
.ControllersConfig
.Assemblies
;
91 if (assemblies
== null || assemblies
.Length
== 0)
93 throw new System
.Configuration
.ConfigurationErrorsException("No assembly was informed on the configuration file. " +
94 "Unfortunatelly this cannot be inferred (we tried)");
100 /// Loads the assembly and inspect its public types.
102 /// <param name="assemblyFileName"></param>
103 public void Inspect(String assemblyFileName
)
105 if (logger
.IsDebugEnabled
)
107 logger
.DebugFormat("Inspecting assembly '{0}'", assemblyFileName
);
110 Assembly assembly
= Assembly
.Load( assemblyFileName
);
116 /// Inspect the assembly's public types.
118 public void Inspect(Assembly assembly
)
120 Type
[] types
= assembly
.GetExportedTypes();
122 foreach(Type type
in types
)
124 if (!type
.IsPublic
|| type
.IsAbstract
|| type
.IsInterface
|| type
.IsValueType
)
129 if (typeof(Controller
).IsAssignableFrom(type
))
131 ControllerDescriptor contrDesc
= ControllerInspectionUtil
.Inspect(type
);
133 RegisterController(contrDesc
);
139 /// Registers the controller.
141 /// <param name="descriptor">The descriptor.</param>
142 private void RegisterController(ControllerDescriptor descriptor
)
144 if (logger
.IsDebugEnabled
)
146 logger
.DebugFormat("Registering controller descriptor for Area: '{0}' Name: '{1}'",
147 descriptor
.Area
, descriptor
.Name
);
150 Tree
.AddController(descriptor
.Area
, descriptor
.Name
, descriptor
.ControllerType
);