| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- //
- // System.Web.HttpApplicationFactory
- //
- // Author:
- // Patrik Torstensson ([email protected])
- // Gonzalo Paniagua Javier ([email protected])
- //
- // (c) 2002,2003 Ximian, Inc. (http://www.ximian.com)
- //
- using System;
- using System.Collections;
- using System.IO;
- using System.Reflection;
- using System.Web;
- using System.Web.Compilation;
- using System.Web.SessionState;
- namespace System.Web {
- class HttpApplicationFactory {
- private string _appFilename;
- private Type _appType;
- private bool _appInitialized;
- private bool _appFiredEnd;
- private Stack _appFreePublicList;
- private int _appFreePublicInstances;
- static private int _appMaxFreePublicInstances = 32;
- private HttpApplicationState _state;
- static Hashtable appEventNames;
- static IHttpHandler custApplication;
- static private HttpApplicationFactory s_Factory = new HttpApplicationFactory();
- static HttpApplicationFactory ()
- {
- appEventNames = new Hashtable ();
- appEventNames.Add ("Application_BeginRequest", null);
- appEventNames.Add ("Application_AuthenticateRequest", null);
- appEventNames.Add ("Application_AuthorizeRequest", null);
- appEventNames.Add ("Application_ResolveRequestCache", null);
- appEventNames.Add ("Application_AcquireRequestState", null);
- appEventNames.Add ("Application_PreRequestHandlerExecute", null);
- appEventNames.Add ("Application_PostRequestHandlerExecute", null);
- appEventNames.Add ("Application_ReleaseRequestState", null);
- appEventNames.Add ("Application_UpdateRequestCache", null);
- appEventNames.Add ("Application_EndRequest", null);
- appEventNames.Add ("Application_PreSendRequestHeaders", null);
- appEventNames.Add ("Application_PreSendRequestContent", null);
- appEventNames.Add ("Application_Disposed", null);
- appEventNames.Add ("Application_Error", null);
- }
-
- public HttpApplicationFactory() {
- _appInitialized = false;
- _appFiredEnd = false;
- _appFreePublicList = new Stack();
- _appFreePublicInstances = 0;
- }
- static private string GetAppFilename (HttpContext context)
- {
- string physicalAppPath = context.Request.PhysicalApplicationPath;
- string appFilePath = Path.Combine (physicalAppPath, "Global.asax");
- if (File.Exists (appFilePath))
- return appFilePath;
- return Path.Combine (physicalAppPath, "global.asax");
- }
- private void CompileApp(HttpContext context) {
- if (File.Exists(_appFilename)) {
- // Setup filemonitor for all filedepend also. CacheDependency?
- _appType = GlobalAsaxCompiler.CompileApplicationType (_appFilename, context);
- if (_appType == null) {
- string msg = String.Format ("Error compiling application file ({0}).", _appFilename);
- throw new ApplicationException (msg);
- }
- } else {
- _appType = typeof (System.Web.HttpApplication);
- _state = new HttpApplicationState ();
- }
- }
- static bool IsEventHandler (MethodInfo m)
- {
- if (m.ReturnType != typeof (void))
- return false;
- ParameterInfo [] pi = m.GetParameters ();
- if (pi.Length != 2)
- return false;
- if (pi [0].ParameterType != typeof (object) ||
- pi [1].ParameterType != typeof (EventArgs))
- return false;
-
- return appEventNames.ContainsKey (m.Name);
- }
- static void AddEvent (MethodInfo method, Hashtable appTypeEventHandlers)
- {
- string name = method.Name;
- ArrayList list;
- list = appTypeEventHandlers [name] as ArrayList;
- if (list == null) {
- list = new ArrayList ();
- appTypeEventHandlers [name] = list;
- }
- list.Add (method);
- }
-
- static Hashtable GetApplicationTypeEvents (HttpApplication app)
- {
- Type appType = app.GetType ();
- Hashtable appTypeEventHandlers = new Hashtable ();
- ArrayList evtMethods = new ArrayList ();
- BindingFlags flags = BindingFlags.Public |
- BindingFlags.NonPublic |
- BindingFlags.DeclaredOnly |
- BindingFlags.Instance |
- BindingFlags.Static;
- MethodInfo [] methods = appType.GetMethods (flags);
- foreach (MethodInfo m in methods) {
- if (IsEventHandler (m))
- AddEvent (m, appTypeEventHandlers);
- }
- Type baseType = appType.BaseType;
- if (baseType == typeof (HttpApplication))
- return appTypeEventHandlers;
- flags = BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;
- methods = appType.GetMethods (flags);
- foreach (MethodInfo m in methods) {
- if (IsEventHandler (m))
- AddEvent (m, appTypeEventHandlers);
- }
- return appTypeEventHandlers;
- }
- void FireEvents (string method_name, object state_context, object [] args)
- {
- /*
- if (methods == null || methods.Count == 0)
- return;
- foreach (MethodInfo method in methods)
- method.Invoke (this, args);
- */
- }
-
- void FireOnAppStart (HttpContext context)
- {
- FireEvents ("Application_OnStart", context, new object [] {this, EventArgs.Empty});
- }
- void FireOnAppEnd ()
- {
- FireEvents ("Application_OnEnd", null, new object [] {this, EventArgs.Empty});
- }
- void FireOnSessionStart (HttpSessionState state, object source, EventArgs args)
- {
- FireEvents ("Session_OnStart", state, new object [] {source, args});
- }
-
- void FireOnSessionEnd (HttpSessionState state, object source, EventArgs args)
- {
- FireEvents ("Session_OnEnd", state, new object [] {source, args});
- }
-
- private void InitializeFactory(HttpContext context) {
- // TODO: Should we be impersonating here? We are reading a app file.. security issue?
- _appFilename = GetAppFilename(context);
- CompileApp(context);
- FireOnAppStart(context);
- }
- private void Dispose() {
- ArrayList torelease = new ArrayList();
- lock (_appFreePublicList) {
- while (_appFreePublicList.Count > 0) {
- torelease.Add(_appFreePublicList.Pop());
- _appFreePublicInstances--;
- }
- }
- if (torelease.Count > 0) {
- foreach (Object obj in torelease) {
- ((HttpApplication) obj).Cleanup();
- }
- }
- if (!_appFiredEnd) {
- lock (this) {
- if (!_appFiredEnd) {
- FireOnAppEnd();
- _appFiredEnd = true;
- }
- }
- }
- }
- internal static IHttpHandler GetInstance(HttpContext context)
- {
- if (custApplication != null)
- return custApplication;
- if (!s_Factory._appInitialized) {
- lock (s_Factory) {
- if (!s_Factory._appInitialized) {
- s_Factory.InitializeFactory(context);
- s_Factory._appInitialized = true;
- }
- }
- }
- return s_Factory.GetPublicInstance(context);
- }
- internal static void RecycleInstance(HttpApplication app) {
- if (!s_Factory._appInitialized)
- throw new InvalidOperationException("Factory not intialized");
- s_Factory.RecyclePublicInstance(app);
- }
- internal static void AttachEvents (HttpApplication app)
- {
- Type appType = app.GetType ();
- Hashtable appTypeEventHandlers = GetApplicationTypeEvents (app);
- foreach (string key in appTypeEventHandlers.Keys) {
- if (key == "Application_OnStart" || key == "Application_OnEnd" ||
- key == "Session_OnStart" || key == "Session_OnEnd")
- continue;
- int pos = key.IndexOf ('_');
- if (pos == -1 || key.Length <= pos + 1)
- continue;
- EventInfo evt = appType.GetEvent (key.Substring (pos + 1));
- if (evt == null)
- continue;
-
- ArrayList list = appTypeEventHandlers [key] as ArrayList;
- if (list == null || list.Count == 0)
- continue;
- foreach (MethodInfo method in list)
- evt.AddEventHandler (app, Delegate.CreateDelegate (typeof (EventHandler), method));
- }
- }
- private IHttpHandler GetPublicInstance(HttpContext context) {
- HttpApplication app = null;
- lock (_appFreePublicList) {
- if (_appFreePublicInstances > 0) {
- app = (HttpApplication) _appFreePublicList.Pop();
- _appFreePublicInstances--;
- }
- }
- if (app == null) {
- // Create non-public object
- app = (HttpApplication) HttpRuntime.CreateInternalObject(_appType);
- app.Startup(context, HttpApplicationFactory.ApplicationState);
- }
- return (IHttpHandler) app;
- }
- internal void RecyclePublicInstance(HttpApplication app) {
- lock (_appFreePublicList) {
- if (_appFreePublicInstances < _appMaxFreePublicInstances) {
- _appFreePublicList.Push(app);
- _appFreePublicInstances++;
- app = null;
- }
- }
-
- if (app != null) {
- app.Cleanup();
- }
- }
- static internal HttpApplicationState ApplicationState {
- get {
- if (null == s_Factory._state) {
- s_Factory._state = new HttpApplicationState();
- }
- return s_Factory._state;
- }
- }
- internal static void EndApplication() {
- s_Factory.Dispose();
- }
- internal static void StartSession(HttpSessionState state, object source, EventArgs args) {
- s_Factory.FireOnSessionStart(state, source, args);
- }
- static void EndSession(HttpSessionState state, object source, EventArgs args) {
- s_Factory.FireOnSessionEnd(state, source, args);
- }
- public static void SetCustomApplication (IHttpHandler customApplication)
- {
- custApplication = customApplication;
- }
- }
- }
|