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
19 using Castle
.Core
.Logging
;
22 /// Coordinates the creation of new <see cref="MonoRailHttpHandler"/>
23 /// and uses the configuration to obtain the correct factories
26 public class MonoRailHttpHandlerFactory
: IHttpHandlerFactory
28 private ILoggerFactory loggerFactory
;
31 /// Initializes a new instance of the <see cref="MonoRailHttpHandlerFactory"/> class.
33 public MonoRailHttpHandlerFactory()
38 /// Returns an instance of a class that implements
39 /// the <see cref="T:System.Web.IHttpHandler"></see> interface.
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>
46 /// A new <see cref="T:System.Web.IHttpHandler"></see> object that processes the request.
48 public virtual IHttpHandler
GetHandler(HttpContext context
,
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
);
71 /// Enables a factory to reuse an existing handler instance.
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
);
97 mrHandler
= provider
.ObtainMonoRailHttpHandler(mrContext
);
100 if (mrHandler
== null)
102 ILogger logger
= CreateLogger(typeof(MonoRailHttpHandler
).FullName
, mrContext
);
104 mrHandler
= new MonoRailHttpHandler(logger
);
110 private IMonoRailHttpHandlerProvider
ObtainMonoRailHandlerProvider(IRailsEngineContext mrContext
)
112 return (IMonoRailHttpHandlerProvider
) mrContext
.GetService(typeof(IMonoRailHttpHandlerProvider
));
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
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
);