2006-12-05 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web / HttpApplicationFactory.cs
1 //
2 // System.Web.HttpApplicationFactory
3 //
4 // Author:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (c) 2002,2003 Ximian, Inc. (http://www.ximian.com)
8 // (c) Copyright 2004 Novell, Inc. (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29 using System;
30 using System.Collections;
31 using System.IO;
32 using System.Reflection;
33 using System.Web.UI;
34 using System.Web.SessionState;
35 using System.Web.Configuration;
36
37 #if !TARGET_J2EE
38 using System.Web.Compilation;
39 #else
40 using vmw.common;
41 #endif
42
43 #if NET_2_0 && !TARGET_J2EE
44 using System.CodeDom.Compiler;
45 #endif
46
47 namespace System.Web {
48         class HttpApplicationFactory {
49                 // Initialized in InitType
50 #if TARGET_J2EE
51                 static HttpApplicationFactory theFactory {
52                         get
53                         {
54                                 HttpApplicationFactory factory = (HttpApplicationFactory)AppDomain.CurrentDomain.GetData("HttpApplicationFactory");
55                                 if (factory == null) {
56                                         lock(typeof(HttpApplicationFactory)) {
57                                                 factory = (HttpApplicationFactory)AppDomain.CurrentDomain.GetData("HttpApplicationFactory");
58                                                 if (factory == null) {
59                                                         factory = new HttpApplicationFactory();
60                                                         System.Threading.Thread.Sleep(1);
61                                                         AppDomain.CurrentDomain.SetData("HttpApplicationFactory", factory);
62                                                 }
63                                         }
64                                 }
65                                 return factory;
66                         }
67                 }
68 #else
69                 static HttpApplicationFactory theFactory = new HttpApplicationFactory();
70 #endif
71
72                 MethodInfo session_end;
73                 bool needs_init = true;
74                 bool app_start_needed = true;
75                 Type app_type;
76                 HttpApplicationState app_state;
77                 Hashtable app_event_handlers;
78 #if !TARGET_JVM
79                 FileSystemWatcher app_file_watcher;
80                 FileSystemWatcher bin_watcher;
81                 FileSystemWatcher config_watcher;
82 #endif
83                 Stack available = new Stack ();
84                 Stack available_for_end = new Stack ();
85                 
86                 // Watch this thing out when getting an instance
87                 IHttpHandler custom_application;
88
89                 bool IsEventHandler (MethodInfo m)
90                 {
91                         int pos = m.Name.IndexOf ('_');
92                         if (pos == -1 || (m.Name.Length - 1) <= pos)
93                                 return false;
94
95                         if (m.ReturnType != typeof (void))
96                                 return false;
97
98                         ParameterInfo [] pi = m.GetParameters ();
99                         int length = pi.Length;
100                         if (length == 0)
101                                 return true;
102
103                         if (length != 2)
104                                 return false;
105
106                         if (pi [0].ParameterType != typeof (object) ||
107                             pi [1].ParameterType != typeof (EventArgs))
108                                 return false;
109                         
110                         return true;
111                 }
112
113                 void AddEvent (MethodInfo method, Hashtable appTypeEventHandlers)
114                 {
115                         string name = method.Name.Replace ("_On", "_");
116                         if (appTypeEventHandlers [name] == null) {
117                                 appTypeEventHandlers [name] = method;
118                                 return;
119                         }
120
121                         MethodInfo old_method = appTypeEventHandlers [name] as MethodInfo;
122                         ArrayList list;
123                         if (old_method != null){
124                                 list = new ArrayList (4);
125                                 list.Add (old_method);
126                                 appTypeEventHandlers [name] = list;
127                         } else 
128                                 list = appTypeEventHandlers [name] as ArrayList;
129
130                         list.Add (method);
131                 }
132                 
133                 Hashtable GetApplicationTypeEvents (Type type)
134                 {
135                         lock (this) {
136                                 if (app_event_handlers != null)
137                                         return app_event_handlers;
138
139                                 app_event_handlers = new Hashtable ();
140                                 BindingFlags flags = BindingFlags.Public    | BindingFlags.NonPublic | 
141                                                      BindingFlags.Instance  | BindingFlags.Static;
142
143                                 MethodInfo [] methods = type.GetMethods (flags);
144                                 foreach (MethodInfo m in methods) {
145                                         if (m.DeclaringType != typeof (HttpApplication) && IsEventHandler (m))
146                                                 AddEvent (m, app_event_handlers);
147                                 }
148                         }
149
150                         return app_event_handlers;
151                 }
152
153                 Hashtable GetApplicationTypeEvents (HttpApplication app)
154                 {
155                         lock (this) {
156                                 if (app_event_handlers != null)
157                                         return app_event_handlers;
158
159                                 return GetApplicationTypeEvents (app.GetType ());
160                         }
161                 }
162
163                 bool FireEvent (string method_name, object target, object [] args)
164                 {
165                         Hashtable possibleEvents = GetApplicationTypeEvents ((HttpApplication) target);
166                         MethodInfo method = possibleEvents [method_name] as MethodInfo;
167                         if (method == null)
168                                 return false;
169
170                         if (method.GetParameters ().Length == 0)
171                                 args = null;
172
173                         method.Invoke (target, args);
174
175                         return true;
176                 }
177
178                 HttpApplication FireOnAppStart (HttpContext context)
179                 {
180                         HttpApplication app = (HttpApplication) Activator.CreateInstance (app_type, true);
181                         context.ApplicationInstance = app;
182                         app.SetContext (context);
183                         object [] args = new object [] {app, EventArgs.Empty};
184                         FireEvent ("Application_Start", app, args);
185                         return app;
186                 }
187
188                 void FireOnAppEnd ()
189                 {
190                         if (app_type == null)
191                                 return; // we didn't even get an application
192
193                         HttpApplication app = (HttpApplication) Activator.CreateInstance (app_type, true);
194                         FireEvent ("Application_End", app, new object [] {new object (), EventArgs.Empty});
195                         app.Dispose ();
196                 }
197
198                 //
199                 // This is invoked by HttpRuntime.Dispose, when we unload an AppDomain
200                 // To reproduce this in action, touch "global.asax" while XSP is running.
201                 //
202                 public static void Dispose ()
203                 {
204                         theFactory.FireOnAppEnd ();
205                 }
206
207 #if !TARGET_JVM
208                 static FileSystemWatcher CreateWatcher (string file, FileSystemEventHandler hnd, RenamedEventHandler reh)
209                 {
210                         FileSystemWatcher watcher = new FileSystemWatcher ();
211
212                         watcher.Path = Path.GetFullPath (Path.GetDirectoryName (file));
213                         watcher.Filter = Path.GetFileName (file);
214
215                         watcher.Changed += hnd;
216                         watcher.Created += hnd;
217                         watcher.Deleted += hnd;
218                         watcher.Renamed += reh;
219
220                         watcher.EnableRaisingEvents = true;
221
222                         return watcher;
223                 }
224
225                 void OnAppFileRenamed (object sender, RenamedEventArgs args)
226                 {
227                         OnAppFileChanged (sender, args);
228                 }
229
230                 void OnAppFileChanged (object sender, FileSystemEventArgs args)
231                 {
232                         if (config_watcher != null)
233                                 config_watcher.EnableRaisingEvents = false;
234                         if (bin_watcher != null)
235                                 bin_watcher.EnableRaisingEvents = false;
236                         if (app_file_watcher != null)
237                                 app_file_watcher.EnableRaisingEvents = false;
238                         HttpRuntime.UnloadAppDomain ();
239                 }
240 #endif
241
242                 internal static void AttachEvents (HttpApplication app)
243                 {
244                         HttpApplicationFactory factory = theFactory;
245                         Hashtable possibleEvents = factory.GetApplicationTypeEvents (app);
246                         foreach (string key in possibleEvents.Keys) {
247                                 int pos = key.IndexOf ('_');
248                                 string moduleName = key.Substring (0, pos);
249                                 object target;
250                                 if (moduleName == "Application") {
251                                         target = app;
252                                 } else {
253                                         target = app.Modules [moduleName];
254                                         if (target == null)
255                                                 continue;
256                                 }
257
258                                 string eventName = key.Substring (pos + 1);
259                                 EventInfo evt = target.GetType ().GetEvent (eventName);
260                                 if (evt == null)
261                                         continue;
262
263                                 string usualName = moduleName + "_" + eventName;
264                                 object methodData = possibleEvents [usualName];
265                                 if (methodData != null && eventName == "End" && moduleName == "Session") {
266                                         lock (factory) {
267                                                 if (factory.session_end == null)
268                                                         factory.session_end = (MethodInfo) methodData;
269                                         }
270                                         continue;
271                                 }
272
273                                 if (methodData == null)
274                                         continue;
275
276                                 if (methodData is MethodInfo) {
277                                         factory.AddHandler (evt, target, app, (MethodInfo) methodData);
278                                         continue;
279                                 }
280
281                                 ArrayList list = (ArrayList) methodData;
282                                 foreach (MethodInfo method in list)
283                                         factory.AddHandler (evt, target, app, method);
284                         }
285                 }
286
287                 void AddHandler (EventInfo evt, object target, HttpApplication app, MethodInfo method)
288                 {
289                         int length = method.GetParameters ().Length;
290
291                         if (length == 0) {
292                                 NoParamsInvoker npi = new NoParamsInvoker (app, method.Name);
293                                 evt.AddEventHandler (target, npi.FakeDelegate);
294                         } else {
295                                 evt.AddEventHandler (target, Delegate.CreateDelegate (
296                                                         evt.EventHandlerType, app, method.Name));
297                         }
298                 }
299
300                 internal static void InvokeSessionEnd (object state)
301                 {
302                         HttpApplicationFactory factory = theFactory;
303                         MethodInfo method = null;
304                         HttpApplication app = null;
305                         lock (factory.available_for_end) {
306                                 method = factory.session_end;
307                                 if (method == null)
308                                         return;
309
310                                 app = GetApplicationForSessionEnd ();
311                         }
312
313                         app.SetSession ((HttpSessionState) state);
314                         try {
315                                 method.Invoke (app, new object [] {app, EventArgs.Empty});
316                         } catch (Exception) {
317                                 // Ignore
318                         }
319                         RecycleForSessionEnd (app);
320                 }
321
322                 static HttpStaticObjectsCollection MakeStaticCollection (ArrayList list)
323                 {
324                         if (list == null || list.Count == 0)
325                                 return null;
326
327                         HttpStaticObjectsCollection coll = new HttpStaticObjectsCollection ();
328                         foreach (ObjectTagBuilder tag in list) {
329                                 coll.Add (tag);
330                         }
331
332                         return coll;
333                 }
334                 
335                 internal static HttpApplicationState ApplicationState {
336 #if TARGET_J2EE
337                         get {
338                                 HttpApplicationFactory factory = theFactory;
339                                 if (factory.app_state == null)
340                                         factory.app_state = new HttpApplicationState (null, null);
341                                 return factory.app_state;
342                         }
343 #else
344                         get {
345                                 if (theFactory.app_state == null) {
346                                         HttpStaticObjectsCollection app = MakeStaticCollection (GlobalAsaxCompiler.ApplicationObjects);
347                                         HttpStaticObjectsCollection ses = MakeStaticCollection (GlobalAsaxCompiler.SessionObjects);
348
349                                         theFactory.app_state = new HttpApplicationState (app, ses);
350                                 }
351                                 return theFactory.app_state;
352                         }
353 #endif
354                 }
355
356                 public static void SetCustomApplication (IHttpHandler customApplication)
357                 {
358                         theFactory.custom_application = customApplication;
359                 }
360
361                 internal static Type AppType {
362                         get {
363                                 return theFactory.app_type;
364                         }
365                 }
366
367                 void InitType (HttpContext context)
368                 {
369                         lock (this) {
370                                 if (!needs_init)
371                                         return;
372                                 
373                                 string physical_app_path = context.Request.PhysicalApplicationPath;
374                                 string app_file;
375                                 
376                                 app_file = Path.Combine (physical_app_path, "Global.asax");
377                                 if (!File.Exists (app_file))
378                                         app_file = Path.Combine (physical_app_path, "global.asax");
379
380                         
381 #if NET_2_0
382                                 WebConfigurationManager.Init ();
383 #else
384                                 WebConfigurationSettings.Init (context);
385 #endif
386                 
387 #if NET_2_0 && !TARGET_J2EE
388                                 AppGlobalResourcesCompiler agrc = new AppGlobalResourcesCompiler();
389                                 agrc.Compile();
390                                 
391                                 // Todo: Process App_WebResources here
392                                 
393                                 // Todo: Generate profile properties assembly from Web.config here
394                                 
395                                 // Todo: Compile code from App_Code here
396                                 AppCodeCompiler acc = new AppCodeCompiler ();
397                                 acc.Compile ();
398 #endif
399
400                                 if (File.Exists (app_file)) {
401 #if TARGET_J2EE
402                                         app_type = System.Web.J2EE.PageMapper.GetObjectType(app_file);
403 #else
404                                         app_type = ApplicationFileParser.GetCompiledApplicationType (app_file, context);
405                                         if (app_type == null) {
406                                                 string msg = String.Format ("Error compiling application file ({0}).", app_file);
407                                                 throw new ApplicationException (msg);
408                                         }
409 #endif
410                                 } else {
411                                         app_type = typeof (System.Web.HttpApplication);
412                                         app_state = new HttpApplicationState ();
413                                 }
414
415 #if !TARGET_JVM
416                                 FileSystemEventHandler fseh = new FileSystemEventHandler (OnAppFileChanged);
417                                 RenamedEventHandler reh = new RenamedEventHandler (OnAppFileRenamed);
418                                 app_file_watcher = CreateWatcher (app_file, fseh, reh);
419
420                                 string config_file = Path.Combine (physical_app_path, "Web.config");
421                                 if (!File.Exists (config_file))
422                                         config_file = Path.Combine (physical_app_path, "web.config");
423                                 if (!File.Exists (config_file))
424                                         config_file = Path.Combine (physical_app_path, "Web.Config");
425
426                                 config_watcher = CreateWatcher (config_file, fseh, reh);
427 #endif
428                                 needs_init = false;
429
430                                 //
431                                 // Now init the settings
432                                 //
433
434                         }
435                 }
436                 
437                 //
438                 // Multiple-threads might hit this one on startup, and we have
439                 // to delay-initialize until we have the HttpContext
440                 //
441                 internal static HttpApplication GetApplication (HttpContext context)
442                 {
443                         HttpApplicationFactory factory = theFactory;
444                         HttpApplication app = null;
445                         if (factory.app_start_needed){
446                                 if (context == null)
447                                         return null;
448
449                                 factory.InitType (context);
450                                 lock (factory) {
451                                         if (factory.app_start_needed) {
452 #if !TARGET_JVM
453                                                 string bin = HttpRuntime.BinDirectory;
454                                                 if (Directory.Exists (bin))
455                                                         bin = Path.Combine (bin, "*.dll");
456
457                                                 FileSystemEventHandler fseh = new FileSystemEventHandler (factory.OnAppFileChanged);
458                                                 RenamedEventHandler reh = new RenamedEventHandler (factory.OnAppFileRenamed);
459                                                 // We watch bin or bin/*.dll if the directory exists
460                                                 factory.bin_watcher = CreateWatcher (bin, fseh, reh);
461 #endif
462                                                 app = factory.FireOnAppStart (context);
463                                                 factory.app_start_needed = false;
464                                                 return app;
465                                         }
466                                 }
467                         }
468
469                         lock (factory.available) {
470                                 if (factory.available.Count > 0) {
471                                         app = (HttpApplication) factory.available.Pop ();
472                                         app.RequestCompleted = false;
473                                         return app;
474                                 }
475                         }
476                         
477                         return (HttpApplication) Activator.CreateInstance (factory.app_type, true);
478                 }
479
480                 // The lock is in InvokeSessionEnd
481                 static HttpApplication GetApplicationForSessionEnd ()
482                 {
483                         HttpApplicationFactory factory = theFactory;
484                         if (factory.available_for_end.Count > 0)
485                                 return (HttpApplication) factory.available_for_end.Pop ();
486
487                         HttpApplication app = (HttpApplication) Activator.CreateInstance (factory.app_type, true);
488                         app.InitOnce (false);
489
490                         return app;
491                 }
492
493                 internal static void RecycleForSessionEnd (HttpApplication app)
494                 {
495                         HttpApplicationFactory factory = theFactory;
496                         lock (factory.available_for_end) {
497                                 if (factory.available_for_end.Count < 32)
498                                         factory.available_for_end.Push (app);
499                                 else
500                                         app.Dispose ();
501                         }
502                 }
503
504                 internal static void Recycle (HttpApplication app)
505                 {
506                         HttpApplicationFactory factory = theFactory;
507                         lock (factory.available) {
508                                 if (factory.available.Count < 32)
509                                         factory.available.Push (app);
510                                 else
511                                         app.Dispose ();
512                         }
513                 }
514
515                 internal static bool ContextAvailable {
516                         get { return theFactory != null && !theFactory.app_start_needed; }
517                 }
518         }
519 }
520