private static IApplicationContext GetContextInternal(string virtualPath) { string virtualDirectory = WebUtils.GetVirtualDirectory(virtualPath); string contextName = virtualDirectory; if (0 == string.Compare(contextName, ("" + HttpRuntime.AppDomainAppVirtualPath).TrimEnd('/') + "/", true)) { contextName = DefaultRootContextName; } ILog s_weblog = LogManager.GetLogger(typeof(WebApplicationContext)); bool isLogDebugEnabled = s_weblog.IsDebugEnabled; lock (s_webContextCache) { if (isLogDebugEnabled) { s_weblog.Debug(string.Format("looking up web context '{0}' in WebContextCache", contextName)); } // first lookup in our own cache IApplicationContext context = (IApplicationContext)s_webContextCache[contextName]; if (context != null) { // found - nothing to do anymore if (isLogDebugEnabled) { s_weblog.Debug( string.Format("returning WebContextCache hit '{0}' for vpath '{1}' ", context, contextName)); } return(context); } // lookup ContextRegistry lock (ContextRegistry.SyncRoot) { if (isLogDebugEnabled) { s_weblog.Debug(string.Format("looking up web context '{0}' in ContextRegistry", contextName)); } if (ContextRegistry.IsContextRegistered(contextName)) { context = ContextRegistry.GetContext(contextName); } if (context == null) { // finally ask HttpConfigurationSystem for the requested context try { if (isLogDebugEnabled) { s_weblog.Debug( string.Format( "web context for vpath '{0}' not found. Force creation using filepath '{1}'", contextName, virtualPath)); } // assure context is resolved to the given virtualDirectory using (new HttpContextSwitch(virtualDirectory)) { context = (IApplicationContext)ConfigurationUtils.GetSection(ContextSectionName); } if (context != null) { if (isLogDebugEnabled) { s_weblog.Debug(string.Format("got context '{0}' for vpath '{1}'", context, contextName)); } } else { if (isLogDebugEnabled) { s_weblog.Debug(string.Format("no context defined for vpath '{0}'", contextName)); } } } catch (Exception ex) { if (s_weblog.IsErrorEnabled) { s_weblog.Error(string.Format("failed creating context '{0}', Stacktrace:\n{1}", contextName, new StackTrace()), ex); } throw; } } } // add it to the cache // Note: use 'contextName' not 'context.Name' here - the same context may be used for different paths! s_webContextCache.Add(contextName, context); if (isLogDebugEnabled) { s_weblog.Debug( string.Format("added context '{0}' to WebContextCache for vpath '{1}'", context, contextName)); } if (context != null) { // register this context and all ParentContexts by their name - parent contexts may be additionally created by the HttpRuntime IApplicationContext parentContext = context; while (parentContext != null) { if (!s_webContextCache.ContainsKey(parentContext.Name)) { s_webContextCache.Add(parentContext.Name, parentContext); if (isLogDebugEnabled) { s_weblog.Debug( string.Format("added parent context '{0}' to WebContextCache for vpath '{1}'", parentContext, parentContext.Name)); } } parentContext = parentContext.ParentContext; } } return(context); } // lock(s_webContextCache) }
public void SetUp() { ContextRegistry.Clear(); }
public void TearDown() { ContextRegistry.Clear(); }
public void SetUp() { ContextRegistry.Clear(); ResetConfigurationSystem(); }
/// <summary> /// This handler simulates calls to ContextRegistry during context creation /// </summary> private static object GetContextRecursive(object parent, object context, XmlNode section) { return(ContextRegistry.GetContext()); // this must fail! }
public void GetContextWithEmptyName() { Assert.Throws <ArgumentException>(() => ContextRegistry.GetContext("")); }
public void GetContextByNameNotRegisteredThrowsException() { Assert.Throws <ApplicationContextException>( () => ContextRegistry.GetContext("bingo"), "No context registered under name 'bingo'. Use the 'RegisterContext' method or the 'spring/context' section from your configuration file."); }
public void GetContextWithNullName() { Assert.Throws <ArgumentException>(() => ContextRegistry.GetContext(null)); }
/// <summary> /// Creates an <see cref="Spring.Context.IApplicationContext"/> instance /// using the context definitions supplied in a custom /// configuration section. /// </summary> /// <remarks> /// <p> /// This <see cref="Spring.Context.IApplicationContext"/> instance is /// also used to configure the <see cref="ContextRegistry"/>. /// </p> /// </remarks> /// <param name="parent"> /// The configuration settings in a corresponding parent /// configuration section. /// </param> /// <param name="configContext"> /// The configuration context when called from the ASP.NET /// configuration system. Otherwise, this parameter is reserved and /// is <see langword="null"/>. /// </param> /// <param name="section"> /// The <see cref="System.Xml.XmlNode"/> for the section. /// </param> /// <returns> /// An <see cref="Spring.Context.IApplicationContext"/> instance /// populated with the object definitions supplied in the configuration /// section. /// </returns> public object Create(object parent, object configContext, XmlNode section) { XmlElement contextElement = section as XmlElement; #region Sanity Checks if (contextElement == null) { throw ConfigurationUtils.CreateConfigurationException( "Context configuration section must be an XmlElement."); } // sanity check on parent if ((parent != null) && !(parent is IApplicationContext)) { throw ConfigurationUtils.CreateConfigurationException( String.Format("Parent context must be of type IApplicationContext, but was '{0}'", parent.GetType().FullName)); } #endregion // determine name of context to be created string contextName = GetContextName(configContext, contextElement); if (!StringUtils.HasLength(contextName)) { contextName = AbstractApplicationContext.DefaultRootContextName; } #region Instrumentation if (Log.IsDebugEnabled) { Log.Debug(string.Format("creating context '{0}'", contextName)); } #endregion IApplicationContext context = null; try { IApplicationContext parentContext = parent as IApplicationContext; // determine context type Type contextType = GetContextType(contextElement, parentContext); // determine case-sensitivity bool caseSensitive = GetCaseSensitivity(contextElement); // get resource-list string[] resources = GetResources(contextElement); // finally create the context instance context = InstantiateContext(parentContext, configContext, contextName, contextType, caseSensitive, resources); // and register with global context registry if (AutoRegisterWithContextRegistry) { ContextRegistry.RegisterContext(context); } // get and create child context definitions XmlNode[] childContexts = GetChildContexts(contextElement); CreateChildContexts(context, configContext, childContexts); if (Log.IsDebugEnabled) { Log.Debug(string.Format("context '{0}' created for name '{1}'", context, contextName)); } } catch (Exception ex) { if (!ConfigurationUtils.IsConfigurationException(ex)) { throw ConfigurationUtils.CreateConfigurationException( String.Format("Error creating context '{0}': {1}", contextName, ReflectionUtils.GetExplicitBaseException(ex).Message), ex); } throw; } return(context); }
public void GetContextByNameNotRegisteredThrowsException() { IApplicationContext context = ContextRegistry.GetContext("bingo"); }
public void GetContextWithEmptyName() { ContextRegistry.GetContext(""); }
public void GetContextWithNullName() { ContextRegistry.GetContext(null); }