Fixing an issue with output parameters that are of type IntPtr
[castle.git] / MonoRail / Castle.MonoRail.Framework / Services / DefaultViewComponentFactory.cs
blob39cd0f8b5881beb18f34552e794e543dadf075e9
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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
17 using System;
18 using System.Reflection;
19 using Castle.Core.Logging;
20 using Castle.MonoRail.Framework.Configuration;
22 /// <summary>
23 /// Default implementation of <see cref="IViewComponentFactory"/>
24 /// <para>
25 /// This implementation looks for concrete types that extend
26 /// <see cref="ViewComponent"/> in an assembly
27 /// </para>
28 /// </summary>
29 public class DefaultViewComponentFactory : AbstractViewComponentFactory
31 /// <summary>
32 /// The logger instance
33 /// </summary>
34 private ILogger logger = NullLogger.Instance;
36 /// <summary>
37 /// View engine instance used to initialize the <see cref="ViewComponent"/>
38 /// instance upon creation
39 /// </summary>
40 private IViewEngine viewEngine;
42 private String[] assemblies;
44 /// <summary>
45 /// A dictionary of name to ViewComponent
46 /// </summary>
47 private IViewComponentRegistry registry;
49 /// <summary>
50 /// Constructs a <c>DefaultViewComponentFactory</c>
51 /// </summary>
52 public DefaultViewComponentFactory()
54 registry = new DefaultViewComponentRegistry();
57 #region IInitializable implementation
59 /// <summary>
60 /// Invoked by the framework in order to initialize the state
61 /// </summary>
62 public override void Initialize()
64 base.Initialize();
66 if (assemblies != null)
68 foreach(String assembly in assemblies)
70 Inspect(assembly);
74 assemblies = null;
77 #endregion
79 /// <summary>
80 /// Gets the view component registry.
81 /// </summary>
82 /// <returns></returns>
83 protected override IViewComponentRegistry GetViewComponentRegistry()
85 return registry;
88 /// <summary>
89 /// Invoked by the framework in order to give a chance to
90 /// obtain other services
91 /// </summary>
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));
106 if (config != null)
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();
119 /// <summary>
120 /// Loads the assembly and inspect its public types.
121 /// </summary>
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);
132 Inspect(assembly);
135 /// <summary>
136 /// Inspect the assembly's public types.
137 /// </summary>
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)
146 continue;
149 if (typeof(ViewComponent).IsAssignableFrom(type))
151 RegisterComponent(type.Name, type);
156 /// <summary>
157 /// Implementors should return a reference to
158 /// the current view engine.
159 /// </summary>
160 /// <value></value>
161 public override IViewEngine ViewEngine
163 get { return viewEngine; }
164 set { viewEngine = value; }