1 // Copyright 2004-2008 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
;
19 using Castle
.Core
.Logging
;
20 using Castle
.MonoRail
.Framework
.Configuration
;
23 /// Default implementation of <see cref="IViewComponentFactory"/>
25 /// This implementation looks for concrete types that extend
26 /// <see cref="ViewComponent"/> in an assembly
29 public class DefaultViewComponentFactory
: AbstractViewComponentFactory
32 /// The logger instance
34 private ILogger logger
= NullLogger
.Instance
;
37 /// View engine instance used to initialize the <see cref="ViewComponent"/>
38 /// instance upon creation
40 private IViewEngine viewEngine
;
42 private String
[] assemblies
;
45 /// A dictionary of name to ViewComponent
47 private IViewComponentRegistry registry
;
50 /// Constructs a <c>DefaultViewComponentFactory</c>
52 public DefaultViewComponentFactory()
54 registry
= new DefaultViewComponentRegistry();
57 #region IInitializable implementation
60 /// Invoked by the framework in order to initialize the state
62 public override void Initialize()
66 if (assemblies
!= null)
68 foreach(String assembly
in assemblies
)
80 /// Gets the view component registry.
82 /// <returns></returns>
83 protected override IViewComponentRegistry
GetViewComponentRegistry()
89 /// Invoked by the framework in order to give a chance to
90 /// obtain other services
92 /// <param name="provider">The service proviver</param>
93 public override void Service(IServiceProvider provider
)
95 base.Service(provider
);
97 ILoggerFactory loggerFactory
= (ILoggerFactory
) provider
.GetService(typeof(ILoggerFactory
));
99 if (loggerFactory
!= null)
101 logger
= loggerFactory
.Create(typeof(DefaultViewComponentFactory
));
104 IMonoRailConfiguration config
= (IMonoRailConfiguration
) provider
.GetService(typeof(IMonoRailConfiguration
));
108 assemblies
= config
.ViewComponentsConfig
.Assemblies
;
110 if (assemblies
== null || assemblies
.Length
== 0)
112 // Convetion: uses the controller assemblies in this case
114 assemblies
= config
.ControllersConfig
.Assemblies
.ToArray();
120 /// Loads the assembly and inspect its public types.
122 /// <param name="assemblyFileName"></param>
123 public void Inspect(String assemblyFileName
)
125 if (logger
.IsDebugEnabled
)
127 logger
.DebugFormat("Inspecting assembly {0}", assemblyFileName
);
130 Assembly assembly
= Assembly
.Load(assemblyFileName
);
136 /// Inspect the assembly's public types.
138 public void Inspect(Assembly assembly
)
140 Type
[] types
= assembly
.GetExportedTypes();
142 foreach(Type type
in types
)
144 if (!type
.IsPublic
|| type
.IsAbstract
|| type
.IsInterface
|| type
.IsValueType
)
149 if (typeof(ViewComponent
).IsAssignableFrom(type
))
151 RegisterComponent(type
.Name
, type
);
157 /// Implementors should return a reference to
158 /// the current view engine.
161 public override IViewEngine ViewEngine
163 get { return viewEngine; }
164 set { viewEngine = value; }