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
19 using Castle
.Core
.Logging
;
20 using Castle
.MonoRail
.Framework
.ViewComponents
;
23 /// Base implementation for <see cref="IViewComponentFactory"/>
25 public abstract class AbstractViewComponentFactory
: IInitializable
, IServiceEnabledComponent
, IViewComponentFactory
28 /// The logger instance
30 private ILogger logger
= NullLogger
.Instance
;
33 /// Initializes a new instance of the <see cref="AbstractViewComponentFactory"/> class.
35 protected AbstractViewComponentFactory()
39 #region IInitializable implementation
42 /// Invoked by the framework in order to initialize the state
44 public virtual void Initialize()
46 AddBuiltInComponents();
51 #region IServiceEnabledComponent implementation
54 /// Invoked by the framework in order to give a chance to
55 /// obtain other services
57 /// <param name="provider">The service proviver</param>
58 public virtual void Service(IServiceProvider provider
)
60 ILoggerFactory loggerFactory
= (ILoggerFactory
) provider
.GetService(typeof(ILoggerFactory
));
62 if (loggerFactory
!= null)
64 logger
= loggerFactory
.Create(typeof(AbstractViewComponentFactory
));
71 /// Creates an instance of the requested <see cref="ViewComponent"/>
73 /// <param name="name">The view component's name</param>
74 /// <returns>The view component instance</returns>
75 public virtual ViewComponent
Create(String name
)
77 Type viewCompType
= ResolveType(name
);
81 return (ViewComponent
) Activator
.CreateInstance(viewCompType
);
85 logger
.Error("Could not create ViewComponent instance", ex
);
92 /// Gets the registry.
94 /// <value>The registry.</value>
95 public IViewComponentRegistry Registry
97 get { return GetViewComponentRegistry(); }
101 /// Resolves the type.
103 /// <param name="name">The name.</param>
104 /// <returns></returns>
105 protected Type
ResolveType(string name
)
107 if (logger
.IsDebugEnabled
)
109 logger
.DebugFormat("Creating view component '{0}'", name
);
112 Type viewCompType
= GetViewComponentRegistry().GetViewComponent(name
);
118 /// Releases a ViewComponent instance
121 /// Not currently used
123 /// <param name="instance"></param>
124 public virtual void Release(ViewComponent instance
)
126 if (logger
.IsDebugEnabled
)
128 logger
.Debug("Releasing view component instance " + instance
);
133 /// Implementors should return a reference to
134 /// the current view engine.
136 public abstract IViewEngine ViewEngine { get; set; }
139 /// Registers viewcomponents provided by default.
140 /// <seealso cref="CaptureFor"/>
141 /// <seealso cref="SecurityComponent"/>
143 protected virtual void AddBuiltInComponents()
145 RegisterComponent("CaptureFor", typeof(CaptureFor
));
146 RegisterComponent("SecurityComponent", typeof(SecurityComponent
));
147 RegisterComponent("ChildContentComponent", typeof(ChildContentComponent
));
148 // RegisterComponent("UpdatePage", typeof(UpdatePage));
149 // RegisterComponent("UpdatePageTag", typeof(UpdatePageTag));
150 RegisterComponent("AuthenticatedContent", typeof(AuthenticatedContent
));
151 RegisterComponent("DiggStylePagination", typeof(DiggStylePagination
));
152 RegisterComponent("SelectStylePagination", typeof(SelectStylePagination
));
153 RegisterComponent("ColumnRenderer", typeof(ColumnRenderer
));
154 RegisterComponent("SiteMapComponent", typeof(SiteMapComponent
));
155 RegisterComponent("TreeMakerComponent", typeof(TreeMakerComponent
));
159 /// Registers a view component type.
161 /// <param name="name">The view components's name</param>
162 /// <param name="type">The view component's which must extend <see cref="ViewComponent"/></param>
163 protected void RegisterComponent(String name
, Type type
)
165 if (logger
.IsDebugEnabled
)
167 logger
.DebugFormat("Registering ViewComponent {0} Type {1} ", name
, type
);
169 GetViewComponentRegistry().AddViewComponent(name
, type
);
173 /// Gets the view component registry.
175 /// <returns></returns>
176 protected virtual IViewComponentRegistry
GetViewComponentRegistry()
178 throw new NotImplementedException();