PageRenderTime 64ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/corlib/System/AppDomainSetup.cs

https://github.com/iainlane/mono
C# | 397 lines | 321 code | 43 blank | 33 comment | 33 complexity | 39b2a54d930cc2fe08fb85486b5fa5c8 MD5 | raw file
  1. //
  2. // System.AppDomainSetup.cs
  3. //
  4. // Authors:
  5. // Dietmar Maurer (dietmar@ximian.com)
  6. // Sebastien Pouliot <sebastien@ximian.com>
  7. //
  8. // (C) 2001 Ximian, Inc. http://www.ximian.com
  9. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  10. //
  11. // Known Problems:
  12. // Fix serialization compatibility with MS.NET.
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System.Collections.Generic;
  34. using System.IO;
  35. using System.Runtime.CompilerServices;
  36. using System.Runtime.InteropServices;
  37. using System.Security;
  38. using System.Runtime.Serialization.Formatters.Binary;
  39. #if(!MOONLIGHT)
  40. using System.Runtime.Hosting;
  41. using System.Security.Policy;
  42. #endif
  43. namespace System
  44. {
  45. [Serializable]
  46. [ClassInterface (ClassInterfaceType.None)]
  47. [ComVisible (true)]
  48. [StructLayout (LayoutKind.Sequential)]
  49. #if NET_2_1
  50. public sealed class AppDomainSetup
  51. #else
  52. public sealed class AppDomainSetup : IAppDomainSetup
  53. #endif
  54. {
  55. string application_base;
  56. string application_name;
  57. string cache_path;
  58. string configuration_file;
  59. string dynamic_base;
  60. string license_file;
  61. string private_bin_path;
  62. string private_bin_path_probe;
  63. string shadow_copy_directories;
  64. string shadow_copy_files;
  65. bool publisher_policy;
  66. private bool path_changed;
  67. private LoaderOptimization loader_optimization;
  68. bool disallow_binding_redirects;
  69. bool disallow_code_downloads;
  70. #if (!MOONLIGHT)
  71. private ActivationArguments _activationArguments;
  72. AppDomainInitializer domain_initializer;
  73. [NonSerialized]
  74. ApplicationTrust application_trust;
  75. string [] domain_initializer_args;
  76. #else
  77. object _activationArguments;
  78. object domain_initializer; // always null
  79. [NonSerialized]
  80. object application_trust; // dummy, always null
  81. object domain_initializer_args;
  82. #endif
  83. bool disallow_appbase_probe;
  84. byte [] configuration_bytes;
  85. byte [] serialized_non_primitives;
  86. public AppDomainSetup ()
  87. {
  88. }
  89. internal AppDomainSetup (AppDomainSetup setup)
  90. {
  91. application_base = setup.application_base;
  92. application_name = setup.application_name;
  93. cache_path = setup.cache_path;
  94. configuration_file = setup.configuration_file;
  95. dynamic_base = setup.dynamic_base;
  96. license_file = setup.license_file;
  97. private_bin_path = setup.private_bin_path;
  98. private_bin_path_probe = setup.private_bin_path_probe;
  99. shadow_copy_directories = setup.shadow_copy_directories;
  100. shadow_copy_files = setup.shadow_copy_files;
  101. publisher_policy = setup.publisher_policy;
  102. path_changed = setup.path_changed;
  103. loader_optimization = setup.loader_optimization;
  104. disallow_binding_redirects = setup.disallow_binding_redirects;
  105. disallow_code_downloads = setup.disallow_code_downloads;
  106. _activationArguments = setup._activationArguments;
  107. domain_initializer = setup.domain_initializer;
  108. application_trust = setup.application_trust;
  109. domain_initializer_args = setup.domain_initializer_args;
  110. disallow_appbase_probe = setup.disallow_appbase_probe;
  111. configuration_bytes = setup.configuration_bytes;
  112. }
  113. #if (!MOONLIGHT)
  114. public AppDomainSetup (ActivationArguments activationArguments)
  115. {
  116. _activationArguments = activationArguments;
  117. }
  118. public AppDomainSetup (ActivationContext activationContext)
  119. {
  120. _activationArguments = new ActivationArguments (activationContext);
  121. }
  122. #endif
  123. static string GetAppBase (string appBase)
  124. {
  125. if (appBase == null)
  126. return null;
  127. int len = appBase.Length;
  128. if (len >= 8 && appBase.ToLower ().StartsWith ("file://")) {
  129. appBase = appBase.Substring (7);
  130. if (Path.DirectorySeparatorChar != '/')
  131. appBase = appBase.Replace ('/', Path.DirectorySeparatorChar);
  132. if (Environment.IsRunningOnWindows) {
  133. // Under Windows prepend "//" to indicate it's a local file
  134. appBase = "//" + appBase;
  135. }
  136. } else {
  137. appBase = Path.GetFullPath (appBase);
  138. }
  139. return appBase;
  140. }
  141. public string ApplicationBase {
  142. get { return GetAppBase (application_base); }
  143. set { application_base = value; }
  144. }
  145. public string ApplicationName {
  146. get {
  147. return application_name;
  148. }
  149. set {
  150. application_name = value;
  151. }
  152. }
  153. #if !MOONLIGHT
  154. public string CachePath {
  155. get {
  156. return cache_path;
  157. }
  158. set {
  159. cache_path = value;
  160. }
  161. }
  162. public string ConfigurationFile {
  163. get {
  164. if (configuration_file == null)
  165. return null;
  166. if (Path.IsPathRooted(configuration_file))
  167. return configuration_file;
  168. if (ApplicationBase == null)
  169. throw new MemberAccessException("The ApplicationBase must be set before retrieving this property.");
  170. return Path.Combine(ApplicationBase, configuration_file);
  171. }
  172. set {
  173. configuration_file = value;
  174. }
  175. }
  176. public bool DisallowPublisherPolicy {
  177. get {
  178. return publisher_policy;
  179. }
  180. set {
  181. publisher_policy = value;
  182. }
  183. }
  184. public string DynamicBase {
  185. get {
  186. if (dynamic_base == null)
  187. return null;
  188. if (Path.IsPathRooted (dynamic_base))
  189. return dynamic_base;
  190. if (ApplicationBase == null)
  191. throw new MemberAccessException ("The ApplicationBase must be set before retrieving this property.");
  192. return Path.Combine (ApplicationBase, dynamic_base);
  193. }
  194. set {
  195. if (application_name == null)
  196. throw new MemberAccessException ("ApplicationName must be set before the DynamicBase can be set.");
  197. uint id = (uint) application_name.GetHashCode ();
  198. dynamic_base = Path.Combine (value, id.ToString("x"));
  199. }
  200. }
  201. public string LicenseFile {
  202. get {
  203. return license_file;
  204. }
  205. set {
  206. license_file = value;
  207. }
  208. }
  209. #endif
  210. [MonoLimitation ("In Mono this is controlled by the --share-code flag")]
  211. public LoaderOptimization LoaderOptimization {
  212. get {
  213. return loader_optimization;
  214. }
  215. set {
  216. loader_optimization = value;
  217. }
  218. }
  219. #if !MOONLIGHT
  220. public string PrivateBinPath {
  221. get {
  222. return private_bin_path;
  223. }
  224. set {
  225. private_bin_path = value;
  226. path_changed = true;
  227. }
  228. }
  229. public string PrivateBinPathProbe {
  230. get {
  231. return private_bin_path_probe;
  232. }
  233. set {
  234. private_bin_path_probe = value;
  235. path_changed = true;
  236. }
  237. }
  238. public string ShadowCopyDirectories {
  239. get {
  240. return shadow_copy_directories;
  241. }
  242. set {
  243. shadow_copy_directories = value;
  244. }
  245. }
  246. public string ShadowCopyFiles {
  247. get {
  248. return shadow_copy_files;
  249. }
  250. set {
  251. shadow_copy_files = value;
  252. }
  253. }
  254. public bool DisallowBindingRedirects {
  255. get {
  256. return disallow_binding_redirects;
  257. }
  258. set {
  259. disallow_binding_redirects = value;
  260. }
  261. }
  262. public bool DisallowCodeDownload {
  263. get {
  264. return disallow_code_downloads;
  265. }
  266. set {
  267. disallow_code_downloads = value;
  268. }
  269. }
  270. public ActivationArguments ActivationArguments {
  271. get {
  272. if (_activationArguments != null)
  273. return _activationArguments;
  274. DeserializeNonPrimitives ();
  275. return _activationArguments;
  276. }
  277. set { _activationArguments = value; }
  278. }
  279. [MonoLimitation ("it needs to be invoked within the created domain")]
  280. public AppDomainInitializer AppDomainInitializer {
  281. get {
  282. if (domain_initializer != null)
  283. return domain_initializer;
  284. DeserializeNonPrimitives ();
  285. return domain_initializer;
  286. }
  287. set { domain_initializer = value; }
  288. }
  289. [MonoLimitation ("it needs to be used to invoke the initializer within the created domain")]
  290. public string [] AppDomainInitializerArguments {
  291. get { return domain_initializer_args; }
  292. set { domain_initializer_args = value; }
  293. }
  294. [MonoNotSupported ("This property exists but not considered.")]
  295. public ApplicationTrust ApplicationTrust {
  296. get {
  297. if (application_trust != null)
  298. return application_trust;
  299. DeserializeNonPrimitives ();
  300. if (application_trust == null)
  301. application_trust = new ApplicationTrust ();
  302. return application_trust;
  303. }
  304. set { application_trust = value; }
  305. }
  306. [MonoNotSupported ("This property exists but not considered.")]
  307. public bool DisallowApplicationBaseProbing {
  308. get { return disallow_appbase_probe; }
  309. set { disallow_appbase_probe = value; }
  310. }
  311. [MonoNotSupported ("This method exists but not considered.")]
  312. public byte [] GetConfigurationBytes ()
  313. {
  314. return configuration_bytes != null ? configuration_bytes.Clone () as byte [] : null;
  315. }
  316. [MonoNotSupported ("This method exists but not considered.")]
  317. public void SetConfigurationBytes (byte [] value)
  318. {
  319. configuration_bytes = value;
  320. }
  321. private void DeserializeNonPrimitives ()
  322. {
  323. lock (this) {
  324. if (serialized_non_primitives == null)
  325. return;
  326. BinaryFormatter bf = new BinaryFormatter ();
  327. MemoryStream ms = new MemoryStream (serialized_non_primitives);
  328. object [] arr = (object []) bf.Deserialize (ms);
  329. _activationArguments = (ActivationArguments) arr [0];
  330. domain_initializer = (AppDomainInitializer) arr [1];
  331. application_trust = (ApplicationTrust) arr [2];
  332. serialized_non_primitives = null;
  333. }
  334. }
  335. internal void SerializeNonPrimitives ()
  336. {
  337. object [] arr = new object [3];
  338. arr [0] = _activationArguments;
  339. arr [1] = domain_initializer;
  340. arr [2] = application_trust;
  341. BinaryFormatter bf = new BinaryFormatter ();
  342. MemoryStream ms = new MemoryStream ();
  343. bf.Serialize (ms, arr);
  344. serialized_non_primitives = ms.ToArray ();
  345. }
  346. #endif // !NET_2_1
  347. #if NET_4_0 || MOONLIGHT || MOBILE
  348. [MonoTODO ("not implemented, does not throw because it's used in testing moonlight")]
  349. public void SetCompatibilitySwitches (IEnumerable<string> switches)
  350. {
  351. }
  352. #endif
  353. }
  354. }