Fix the build.
[castle.git] / MonoRail / Castle.MonoRail.Framework / MonoRailHttpHandlerFactory.cs
blob4d0235e34bb0e8a6f187a4712b70e78a59c9ae10
1 // Copyright 2004-2007 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
17 using System;
18 using System.Web;
19 using Castle.Core.Logging;
21 /// <summary>
22 /// Coordinates the creation of new <see cref="MonoRailHttpHandler"/>
23 /// and uses the configuration to obtain the correct factories
24 /// instances.
25 /// </summary>
26 public class MonoRailHttpHandlerFactory : IHttpHandlerFactory
28 private ILoggerFactory loggerFactory;
30 /// <summary>
31 /// Initializes a new instance of the <see cref="MonoRailHttpHandlerFactory"/> class.
32 /// </summary>
33 public MonoRailHttpHandlerFactory()
37 /// <summary>
38 /// Returns an instance of a class that implements
39 /// the <see cref="T:System.Web.IHttpHandler"></see> interface.
40 /// </summary>
41 /// <param name="context">An instance of the <see cref="T:System.Web.HttpContext"></see> class that provides references to intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param>
42 /// <param name="requestType">The HTTP data transfer method (GET or POST) that the client uses.</param>
43 /// <param name="url">The <see cref="P:System.Web.HttpRequest.RawUrl"></see> of the requested resource.</param>
44 /// <param name="pathTranslated">The <see cref="P:System.Web.HttpRequest.PhysicalApplicationPath"></see> to the requested resource.</param>
45 /// <returns>
46 /// A new <see cref="T:System.Web.IHttpHandler"></see> object that processes the request.
47 /// </returns>
48 public virtual IHttpHandler GetHandler(HttpContext context,
49 String requestType,
50 String url, String pathTranslated)
52 if (!EngineContextModule.Initialized)
54 throw new RailsException("Looks like you forgot to register the http module " +
55 typeof(EngineContextModule).FullName + "\r\nAdd '<add name=\"monorail\" type=\"Castle.MonoRail.Framework.EngineContextModule, Castle.MonoRail.Framework\" />' " +
56 "to the <httpModules> section on your web.config");
59 IRailsEngineContext mrContext = EngineContextModule.ObtainRailsEngineContext(context);
61 if (mrContext == null)
63 throw new RailsException("IRailsEngineContext is null. Looks like the " +
64 "EngineContextModule has not run for this request.");
67 return ObtainMonoRailHandler(mrContext);
70 /// <summary>
71 /// Enables a factory to reuse an existing handler instance.
72 /// </summary>
73 /// <param name="handler">The <see cref="T:System.Web.IHttpHandler"></see> object to reuse.</param>
74 public virtual void ReleaseHandler(IHttpHandler handler)
76 HttpContext httpContext = HttpContext.Current;
78 if (httpContext != null)
80 IRailsEngineContext mrContext = EngineContextModule.ObtainRailsEngineContext(HttpContext.Current);
82 if (mrContext != null)
84 IMonoRailHttpHandlerProvider provider = ObtainMonoRailHandlerProvider(mrContext);
85 if (provider != null) provider.ReleaseHandler(handler);
90 private IHttpHandler ObtainMonoRailHandler(IRailsEngineContext mrContext)
92 IHttpHandler mrHandler = null;
93 IMonoRailHttpHandlerProvider provider = ObtainMonoRailHandlerProvider(mrContext);
95 if (provider != null)
97 mrHandler = provider.ObtainMonoRailHttpHandler(mrContext);
100 if (mrHandler == null)
102 ILogger logger = CreateLogger(typeof(MonoRailHttpHandler).FullName, mrContext);
104 mrHandler = new MonoRailHttpHandler(logger);
107 return mrHandler;
110 private IMonoRailHttpHandlerProvider ObtainMonoRailHandlerProvider(IRailsEngineContext mrContext)
112 return (IMonoRailHttpHandlerProvider) mrContext.GetService(typeof(IMonoRailHttpHandlerProvider));
115 /// <summary>
116 /// This might be subject to race conditions, but
117 /// I'd rather take the risk - which in the end
118 /// means just replacing the instance - than
119 /// creating locks that will affect every single request
120 /// </summary>
121 /// <param name="name">Logger name</param>
122 /// <param name="provider">Service provider</param>
123 /// <returns>Logger instance</returns>
124 private ILogger CreateLogger(String name, IServiceProvider provider)
126 if (loggerFactory == null)
128 loggerFactory = (ILoggerFactory) provider.GetService(typeof(ILoggerFactory));
130 if (loggerFactory == null)
132 loggerFactory = new NullLogFactory();
136 return loggerFactory.Create(name);