PageRenderTime 52ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/System.ServiceModel/System.ServiceModel.Channels/SvcHttpHandlerFactory.cs

https://bitbucket.org/steenlund/mono-2.6.7-for-amiga
C# | 177 lines | 113 code | 27 blank | 37 comment | 30 complexity | 1745e58a3ccd5f98fa4f0840bf81fb0c MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, LGPL-2.1
  1. //
  2. // SvcHttpHandlerFactory.cs
  3. //
  4. // Author:
  5. // Ankit Jain <jankit@novell.com>
  6. // Atsushi Enomoto <atsushi@ximian.com>
  7. //
  8. // Copyright (C) 2006,2009 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.Collections.Generic;
  32. using System.Collections.Specialized;
  33. using System.IO;
  34. using System.Linq;
  35. using System.Reflection;
  36. using System.ServiceModel;
  37. using System.Web;
  38. using System.Web.Caching;
  39. using System.Web.Compilation;
  40. namespace System.ServiceModel.Channels {
  41. internal class SvcHttpHandlerFactory : IHttpHandlerFactory
  42. {
  43. static Dictionary<string, SvcHttpHandler> handlers = new Dictionary<string, SvcHttpHandler> ();
  44. string privateBinPath;
  45. Type service_type, factory_type;
  46. public SvcHttpHandlerFactory ()
  47. {
  48. ServiceHostingEnvironment.InAspNet = true;
  49. }
  50. public static SvcHttpHandler GetHandlerForListener (IChannelListener listener)
  51. {
  52. return handlers.Values.First (h => h.Host.ChannelDispatchers.Any (cd => cd.Listener == listener));
  53. }
  54. public IHttpHandler GetHandler (HttpContext context, string requestType, string url, string pathTranslated)
  55. {
  56. lock (handlers) {
  57. if (handlers.ContainsKey (url))
  58. return handlers [url];
  59. LoadTypeFromSvc (pathTranslated, url, context);
  60. if (service_type == null)
  61. throw new Exception (String.Format (
  62. "Could not find service for url : '{0}'", url));
  63. SvcHttpHandler handler = new SvcHttpHandler (service_type, factory_type, url);
  64. handlers [url] = handler;
  65. return handler;
  66. }
  67. }
  68. public void ReleaseHandler (IHttpHandler handler)
  69. {
  70. // do nothing
  71. }
  72. void LoadTypeFromSvc (string path, string url, HttpContext context)
  73. {
  74. if (CachingCompiler.GetTypeFromCache (path) != null)
  75. return;
  76. ServiceHostParser parser = new ServiceHostParser (path, url, context);
  77. parser.Parse ();
  78. if (parser.Program == null) {
  79. //FIXME: Not caching, as parser.TypeName could be
  80. //just typename or fully qualified name
  81. service_type = GetTypeFromBinAndConfig (parser.TypeName);
  82. /*CachingCompiler.InsertType (
  83. service_type, service_type.Assembly.Location, url,
  84. new CacheItemRemovedCallback (RemovedCallback));*/
  85. } else {
  86. service_type = CachingCompiler.CompileAndGetType (
  87. parser, url,
  88. new CacheItemRemovedCallback (RemovedCallback));
  89. }
  90. if (parser.Factory != null) {
  91. factory_type = GetTypeFromBinAndConfig (parser.Factory);
  92. /*CachingCompiler.InsertType (
  93. factory_type, factory_type.Assembly.Location, url,
  94. new CacheItemRemovedCallback (RemovedCallback));*/
  95. }
  96. }
  97. string PrivateBinPath {
  98. get {
  99. if (privateBinPath != null)
  100. return privateBinPath;
  101. AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
  102. privateBinPath = setup.PrivateBinPath;
  103. if (!Path.IsPathRooted (privateBinPath)) {
  104. string appbase = setup.ApplicationBase;
  105. if (appbase.StartsWith ("file://")) {
  106. appbase = appbase.Substring (7);
  107. if (Path.DirectorySeparatorChar != '/')
  108. appbase = appbase.Replace ('/', Path.DirectorySeparatorChar);
  109. }
  110. privateBinPath = Path.Combine (appbase, privateBinPath);
  111. }
  112. return privateBinPath;
  113. }
  114. }
  115. Type GetTypeFromBinAndConfig (string typeName)
  116. {
  117. string assname = null;
  118. int idx = typeName.IndexOf (',');
  119. if (idx > 0) {
  120. assname = typeName.Substring (idx + 1).Trim ();
  121. typeName = typeName.Substring (0, idx);
  122. }
  123. Type result = null;
  124. foreach (Assembly ass in BuildManager.GetReferencedAssemblies ()) {
  125. if (assname != null && ass.GetName ().Name != assname)
  126. continue;
  127. Type type = ass.GetType (typeName, false);
  128. if (type != null) {
  129. if (result != null)
  130. throw new HttpException (String.Format ("Type {0} is not unique.", typeName));
  131. result = type;
  132. }
  133. }
  134. if (result == null)
  135. throw new HttpException (String.Format ("Type {0} not found.", typeName));
  136. return result;
  137. }
  138. public static void RemovedCallback (string key, object value, CacheItemRemovedReason reason)
  139. {
  140. if (key.StartsWith (CachingCompiler.cacheTypePrefix)) {
  141. string path = key.Remove (0, CachingCompiler.cacheTypePrefix.Length);
  142. SvcHttpHandler handler;
  143. if (!handlers.TryGetValue (path, out handler))
  144. return;
  145. handler.Close ();
  146. handlers.Remove (path);
  147. }
  148. }
  149. }
  150. }