Go to the documentation of this file.00001 #region License
00002
00003
00004
00005
00006
00007
00008
00009 #endregion
00010
00011 using System.Diagnostics;
00012 using System.Runtime.CompilerServices;
00013 using System;
00014 using System.Configuration;
00015 using System.Web.Configuration;
00016 using System.Security;
00017 using N2.Configuration;
00018 using N2.Engine;
00019 using N2.Engine.MediumTrust;
00020 using System.IO;
00021
00022 namespace N2
00023 {
00027 public class Context
00028 {
00029 #region Initialization Methods
00030
00031
00032 [MethodImpl(MethodImplOptions.Synchronized)]
00033 public static IEngine Initialize(bool forceRecreate)
00034 {
00035 if (Singleton<IEngine>.Instance == null || forceRecreate)
00036 {
00037 Debug.WriteLine("Constructing engine " + DateTime.Now);
00038 Singleton<IEngine>.Instance = CreateEngineInstance();
00039 Debug.WriteLine("Initializing engine " + DateTime.Now);
00040 Singleton<IEngine>.Instance.Initialize();
00041 }
00042 return Singleton<IEngine>.Instance;
00043 }
00044
00045 [Obsolete("Use Context.Replace(IEngine)", false)]
00046 public static void Initialize(IEngine engine)
00047 {
00048 Singleton<IEngine>.Instance = engine;
00049 }
00050
00054 public static void Replace(IEngine engine)
00055 {
00056 Singleton<IEngine>.Instance = engine;
00057 }
00058
00059 private static System.Configuration.Configuration GetConfiguration()
00060 {
00061 try
00062 {
00063 return System.Web.Hosting.HostingEnvironment.IsHosted
00064 ? WebConfigurationManager.OpenWebConfiguration("~")
00065 : ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
00066 }
00067 catch (Exception ex)
00068 {
00069 Trace.TraceWarning("Error reading configuration. This has happened when running a web site project in a virtual directory (reason unknown). " + ex);
00070 return null;
00071 }
00072 }
00073
00076 public static IEngine CreateEngineInstance()
00077 {
00078 try
00079 {
00080 var config = ConfigurationManager.GetSection("n2/engine") as EngineSection;
00081 if (config != null && !string.IsNullOrEmpty(config.EngineType))
00082 {
00083 Type engineType = Type.GetType(config.EngineType);
00084 if(engineType == null)
00085 throw new ConfigurationErrorsException("The type '" + engineType + "' could not be found. Please check the configuration at /configuration/n2/engine[@engineType] or check for missing assemblies.");
00086 if(!typeof(IEngine).IsAssignableFrom(engineType))
00087 throw new ConfigurationErrorsException("The type '" + engineType + "' doesn't implement 'N2.Engines.IEngine' and cannot be configured in /configuration/n2/engine[@engineType] for that purpose.");
00088 return Activator.CreateInstance(engineType) as IEngine;
00089 }
00090
00091 return new ContentEngine();
00092 }
00093 catch (SecurityException ex)
00094 {
00095 Trace.TraceInformation("Caught SecurityException, reverting to MediumTrustEngine. " + ex);
00096 return new ContentEngine(new MediumTrustServiceContainer(), EventBroker.Instance, new ContainerConfigurer());
00097 }
00098 catch (Exception ex)
00099 {
00100 if (ex.GetType().Name != "ComponentActivatorException")
00101 throw;
00102
00103 Trace.TraceInformation("Caught ComponentActivatorException, reverting to MediumTrustEngine. " + ex);
00104 return new ContentEngine(new MediumTrustServiceContainer(), EventBroker.Instance, new ContainerConfigurer());
00105 }
00106 }
00107
00108 #endregion
00109
00110 #region Properties: Persister, Definitions, Integrity, UrlParser, CurrentPage
00111
00113 public static IEngine Current
00114 {
00115 get
00116 {
00117 if (Singleton<IEngine>.Instance == null)
00118 {
00119 Initialize(false);
00120 }
00121 return Singleton<IEngine>.Instance;
00122 }
00123 }
00124
00126 public static Persistence.IPersister Persister
00127 {
00128 get { return Current.Persister; }
00129 }
00130
00132 public static Definitions.IDefinitionManager Definitions
00133 {
00134 get { return Current.Definitions; }
00135 }
00136
00138 public static Integrity.IIntegrityManager IntegrityManager
00139 {
00140 get { return Current.IntegrityManager; }
00141 }
00142
00144 public static Security.ISecurityManager SecurityManager
00145 {
00146 get { return Current.SecurityManager; }
00147 }
00148
00150 public static Web.IUrlParser UrlParser
00151 {
00152 get { return Current.UrlParser; }
00153 }
00154
00156 public static ContentItem CurrentPage
00157 {
00158 get { return Current.UrlParser.CurrentPage; }
00159 }
00160 #endregion
00161 }
00162 }