/// <summary> /// Create a handler instance for the given URL. /// </summary> /// <param name="appContext">the application context corresponding to the current request</param> /// <param name="context">The <see cref="HttpContext"/> instance for this request.</param> /// <param name="requestType">The HTTP data transfer method (GET, POST, ...)</param> /// <param name="rawUrl">The requested <see cref="HttpRequest.RawUrl"/>.</param> /// <param name="physicalPath">The physical path of the requested resource.</param> /// <returns>A handler instance for the current request.</returns> protected override IHttpHandler CreateHandlerInstance(IConfigurableApplicationContext appContext, HttpContext context, string requestType, string rawUrl, string physicalPath) { IHttpHandler handler; string appRelativeVirtualPath = WebUtils.GetAppRelativePath(rawUrl); NamedObjectDefinition namedPageDefinition = FindWebObjectDefinition(appRelativeVirtualPath, appContext.ObjectFactory); if (namedPageDefinition != null) { // is this a nested call (HttpServerUtility.Transfer() or HttpServerUtility.Execute())? if (context.Handler != null) { // all deps can/must be resolved now handler = (IHttpHandler)appContext.GetObject(namedPageDefinition.Name, typeof(IHttpHandler), null); } else { // execution pipeline "entry-point" - create page instance only // and defer configuration to PreRequestHandlerExecute step handler = (IHttpHandler)appContext.CreateObject(namedPageDefinition.Name, typeof(IHttpHandler), null); WebSupportModule.ConfigureHandler(context, handler, appContext, namedPageDefinition.Name, true); } } else { handler = WebObjectUtils.CreateHandler(rawUrl); // let WebSupportModule handle configuration handler = WebSupportModule.ConfigureHandler(context, handler, appContext, rawUrl, false); } return(handler); }
/// <summary> /// Obtains a handler by mapping <paramref name="rawUrl"/> to the list of patterns in <paramref name="handlerMappings"/>. /// </summary> /// <param name="appContext">the application context corresponding to the current request</param> /// <param name="context">The <see cref="HttpContext"/> instance for this request.</param> /// <param name="requestType">The HTTP data transfer method (GET, POST, ...)</param> /// <param name="rawUrl">The requested <see cref="HttpRequest.RawUrl"/>.</param> /// <param name="physicalPath">The physical path of the requested resource.</param> /// <param name="handlerMappings"></param> /// <param name="handlerWithFactoryTable"></param> /// <returns>A handler instance for processing the current request.</returns> protected IHttpHandler MapHandlerInstance(IConfigurableApplicationContext appContext, HttpContext context, string requestType, string rawUrl, string physicalPath, HandlerMap handlerMappings, IDictionary handlerWithFactoryTable) { // resolve handler instance by mapping the url to the list of patterns HandlerMapEntry handlerMapEntry = handlerMappings.MapPath(rawUrl); if (handlerMapEntry == null) { throw new HttpException(404, HttpStatusCode.NotFound.ToString()); } object handlerObject = appContext.GetObject(handlerMapEntry.HandlerObjectName); if (handlerObject is IHttpHandler) { return((IHttpHandler)handlerObject); } else if (handlerObject is IHttpHandlerFactory) { // keep a reference to the issuing factory for later ReleaseHandler call IHttpHandlerFactory factory = (IHttpHandlerFactory)handlerObject; IHttpHandler handler = factory.GetHandler(context, requestType, rawUrl, physicalPath); lock (handlerWithFactoryTable.SyncRoot) { handlerWithFactoryTable.Add(handler, factory); } return(handler); } throw new HttpException((int)HttpStatusCode.NotFound, HttpStatusCode.NotFound.ToString()); }
protected override object GetObjectFromType(Type type) { var objectNameList = GetAppContextObjectNameListFromType(type); var nameList = objectNameList as IList <string> ?? objectNameList.ToList(); return(_applicationContext.GetObject( nameList.Count() == 1 ? nameList.First() : type.FullName)); }
/// <summary> /// Creates the message queue given its name in the application context. /// </summary> /// <param name="messageQueueObjectName">Name of the message queue object.</param> /// <returns> /// A MessageQueue instance configured via the application context /// </returns> public MessageQueue CreateMessageQueue(string messageQueueObjectName) { AssertUtils.ArgumentHasText(messageQueueObjectName, "DefaultMessageQueueObjectName"); IDictionary queues = LogicalThreadContext.GetData(QUEUE_DICTIONARY_SLOTNAME) as IDictionary; if (queues == null) { queues = new Hashtable(); LogicalThreadContext.SetData(QUEUE_DICTIONARY_SLOTNAME, queues); } if (!queues.Contains(messageQueueObjectName)) { MessageQueue mq = applicationContext.GetObject(messageQueueObjectName) as MessageQueue; queues.Add(messageQueueObjectName, mq); } return(queues[messageQueueObjectName] as MessageQueue); }
public static IFactoryObject GetFactoryObject(this IConfigurableApplicationContext applicationContext, string name) { // In Spring.NET the factory object itself of an object can be retrieved by prefixing its name // with an ampersand "&". var factoryObjectName = $"&{name}"; return((IFactoryObject)applicationContext.GetObject(factoryObjectName)); }
public static void ReplaceObjectDefinition(this IConfigurableApplicationContext applicationContext, string name, IObjectDefinition definition) { if (definition?.ObjectType != typeof(SurrogateFactoryObject)) { // Proceed here if the object definition will replace the object definition of the // SurrogateFactoryObject type. var factoryObject = applicationContext.GetFactoryObject(name); if (factoryObject is SurrogateFactoryObject && factoryObject.IsSingleton) { // If it happens that Sprint.NET decides to cache this singleton object, then // proceed to clear its state, like call counters. This is to make sure that // a new test starts with a clean mock/spy surrogate. var fakeObject = applicationContext.GetObject(name); FakeItEasy.Fake.ClearRecordedCalls(fakeObject); } } applicationContext.ObjectFactory.RemoveSingleton(name); applicationContext.RegisterObjectDefinition(name, definition); }
/// <summary> /// Injects protected fields using Field Injection. /// </summary> protected virtual void InjectProtectedVariables() { for (int i = 0; i < managedVariableNames.Length; i++) { string fieldName = managedVariableNames[i]; Object obj = null; try { FieldInfo field = GetType().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance); if (field != null) { BeforeProtectedVariableInjection(field); obj = applicationContext.GetObject(fieldName, field.FieldType); field.SetValue(this, obj); if (logger.IsDebugEnabled) { logger.Debug("Populated field: " + field); } } else { if (logger.IsWarnEnabled) { logger.Warn("No field with name '" + fieldName + "'"); } } } catch (NoSuchObjectDefinitionException) { if (logger.IsWarnEnabled) { logger.Warn("No object definition with name '" + fieldName + "'"); } } } }
protected virtual void CreateDefaultMetadataCache() { if (metadataCache == null) { if (applicationContext.ContainsObject(METADATA_CACHE_NAME)) { metadataCache = applicationContext.GetObject(METADATA_CACHE_NAME) as MessageQueueMetadataCache; } else { metadataCache = new MessageQueueMetadataCache(); if (ApplicationContext != null) { metadataCache.ApplicationContext = ApplicationContext; metadataCache.AfterPropertiesSet(); metadataCache.Initialize(); applicationContext.ObjectFactory.RegisterSingleton("__MessageQueueMetadataCache__", metadataCache); } else { #region Logging if (LOG.IsWarnEnabled) { LOG.Warn( "The ApplicationContext property has not been set, so the MessageQueueMetadataCache can not be automatically generated. " + "Please explictly set the MessageQueueMetadataCache using the property MetadataCache or set the ApplicationContext property. " + "This will only effect the use of MessageQueueTemplate when publishing to remote queues."); } #endregion } } } }
/// <summary> /// Retrieves an instance of the <see cref="System.Web.IHttpHandler"/> /// implementation for handling web service requests /// for both Spring and ASP.NET AJAX 1.0 web services. /// </summary> /// <param name="context">The current HTTP context.</param> /// <param name="requestType">The type of HTTP request (GET or POST).</param> /// <param name="url">The url of the web service.</param> /// <param name="pathTranslated">The physical application path for the web service.</param> /// <returns>The web service handler object.</returns> public override IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated) { string filename = VirtualPathUtility.ToAbsolute(context.Request.FilePath); string cacheKey = (string)webServiceData_GetCacheKey.Invoke(null, new object[] { filename }); object webServiceData = context.Cache.Get(cacheKey); if (webServiceData == null) { IConfigurableApplicationContext appContext = base.GetCheckedApplicationContext(url); string appRelativeVirtualPath = WebUtils.GetAppRelativePath(url); NamedObjectDefinition nod = FindWebObjectDefinition(appRelativeVirtualPath, appContext.ObjectFactory); if (nod != null) { Type serviceType = null; if (appContext.IsTypeMatch(nod.Name, typeof(WebServiceExporter))) { WebServiceExporter wse = (WebServiceExporter)appContext.GetObject(nod.Name); serviceType = wse.GetExportedType(); } else { serviceType = appContext.GetType(nod.Name); } object[] attrs = serviceType.GetCustomAttributes(typeof(ScriptServiceAttribute), false); if (attrs.Length > 0) { webServiceData = webServiceData_ctor.Invoke(new object[] { serviceType, false }); context.Cache.Insert(cacheKey, webServiceData); } } } return(this.scriptHandlerFactory.GetHandler(context, requestType, url, pathTranslated)); }
/// <summary> /// Retrieves instance of the page from Spring web application context. /// </summary> /// <param name="context">current HttpContext</param> /// <param name="requestType">type of HTTP request (GET, POST, etc.)</param> /// <param name="url">requested page URL</param> /// <param name="path">translated server path for the page</param> /// <returns>instance of the configured page object</returns> IHttpHandler IHttpHandlerFactory.GetHandler(HttpContext context, string requestType, string url, string path) { new AspNetHostingPermission(AspNetHostingPermissionLevel.Minimal).Demand(); IConfigurableApplicationContext appContext = WebApplicationContext.GetContext(url) as IConfigurableApplicationContext; if (appContext == null) { throw new InvalidOperationException( "Implementations of IApplicationContext must also implement IConfigurableApplicationContext"); } string appRelativeVirtualPath = WebUtils.GetAppRelativePath(url); AbstractHandlerFactory.NamedObjectDefinition nod = AbstractHandlerFactory.FindWebObjectDefinition(appRelativeVirtualPath, appContext.ObjectFactory); Type serviceType = null; if (nod != null) { #if !MONO if (appContext.IsTypeMatch(nod.Name, typeof(WebServiceExporter))) { WebServiceExporter wse = (WebServiceExporter)appContext.GetObject(nod.Name); serviceType = wse.GetExportedType(); } else { serviceType = appContext.GetType(nod.Name); // check if the type defines a Web Service object[] wsAttribute = serviceType.GetCustomAttributes(typeof(WebServiceAttribute), true); if (wsAttribute.Length == 0) { serviceType = null; } } #else serviceType = appContext.GetType(nod.Name); // check if the type defines a Web Service object[] wsAttribute = serviceType.GetCustomAttributes(typeof(WebServiceAttribute), true); if (wsAttribute.Length == 0) { serviceType = null; } #endif } if (serviceType == null) { serviceType = WebServiceParser.GetCompiledType(url, context); } #if !MONO_2_0 return((IHttpHandler)CoreGetHandler.Invoke(this, new object[] { serviceType, context, context.Request, context.Response })); #else // find if the BuildManager already contains a cached value of the service type var buildCacheField = typeof(BuildManager).GetField("buildCache", BindingFlags.Static | BindingFlags.NonPublic); var buildCache = (IDictionary)buildCacheField.GetValue(null); if (!buildCache.Contains(appRelativeVirtualPath)) { // create new fake BuildManagerCacheItem wich represent the target type var buildManagerCacheItemType = Type.GetType("System.Web.Compilation.BuildManagerCacheItem, System.Web"); var cacheItemCtor = buildManagerCacheItemType.GetConstructor(new Type[] { typeof(Assembly), typeof(BuildProvider), typeof(CompilerResults) }); var buildProvider = new FakeBuildProvider(serviceType); var cacheItem = cacheItemCtor.Invoke(new object[] { serviceType.Assembly, buildProvider, null }); // store it in the BuildManager buildCache [appRelativeVirtualPath] = cacheItem; } // now that the target type is in the cache, let the default process continue return(base.GetHandler(context, requestType, url, path)); #endif }
/// <summary> /// Obtains a handler by mapping <paramref name="rawUrl"/> to the list of patterns in <paramref name="handlerMappings"/>. /// </summary> /// <param name="appContext">the application context corresponding to the current request</param> /// <param name="context">The <see cref="HttpContext"/> instance for this request.</param> /// <param name="requestType">The HTTP data transfer method (GET, POST, ...)</param> /// <param name="rawUrl">The requested <see cref="HttpRequest.RawUrl"/>.</param> /// <param name="physicalPath">The physical path of the requested resource.</param> /// <param name="handlerMappings"></param> /// <param name="handlerWithFactoryTable"></param> /// <returns>A handler instance for processing the current request.</returns> protected IHttpHandler MapHandlerInstance(IConfigurableApplicationContext appContext, HttpContext context, string requestType, string rawUrl, string physicalPath, HandlerMap handlerMappings, IDictionary handlerWithFactoryTable) { // resolve handler instance by mapping the url to the list of patterns HandlerMapEntry handlerMapEntry = handlerMappings.MapPath(rawUrl); if (handlerMapEntry == null) { throw new HttpException(404, HttpStatusCode.NotFound.ToString()); } object handlerObject = appContext.GetObject(handlerMapEntry.HandlerObjectName); if (handlerObject is IHttpHandler) { return (IHttpHandler) handlerObject; } else if (handlerObject is IHttpHandlerFactory) { // keep a reference to the issuing factory for later ReleaseHandler call IHttpHandlerFactory factory = (IHttpHandlerFactory) handlerObject; IHttpHandler handler = factory.GetHandler(context, requestType, rawUrl, physicalPath); lock(handlerWithFactoryTable.SyncRoot) { handlerWithFactoryTable.Add(handler, factory); } return handler; } throw new HttpException((int)HttpStatusCode.NotFound, HttpStatusCode.NotFound.ToString()); }
/// <summary> /// Create a handler instance for the given URL. /// </summary> /// <param name="appContext">the application context corresponding to the current request</param> /// <param name="context">The <see cref="HttpContext"/> instance for this request.</param> /// <param name="requestType">The HTTP data transfer method (GET, POST, ...)</param> /// <param name="rawUrl">The requested <see cref="HttpRequest.RawUrl"/>.</param> /// <param name="physicalPath">The physical path of the requested resource.</param> /// <returns>A handler instance for the current request.</returns> protected override IHttpHandler CreateHandlerInstance(IConfigurableApplicationContext appContext, HttpContext context, string requestType, string rawUrl, string physicalPath) { IHttpHandler handler; string appRelativeVirtualPath = WebUtils.GetAppRelativePath(rawUrl); NamedObjectDefinition namedPageDefinition = FindWebObjectDefinition(appRelativeVirtualPath, appContext.ObjectFactory); if (namedPageDefinition != null) { // is this a nested call (HttpServerUtility.Transfer() or HttpServerUtility.Execute())? if (context.Handler != null) { // all deps can/must be resolved now handler = (IHttpHandler)appContext.GetObject(namedPageDefinition.Name, typeof(IHttpHandler), null); } else { // execution pipeline "entry-point" - create page instance only // and defer configuration to PreRequestHandlerExecute step handler = (IHttpHandler)appContext.CreateObject(namedPageDefinition.Name, typeof(IHttpHandler), null); WebSupportModule.ConfigureHandler(context, handler, appContext, namedPageDefinition.Name, true); } } else { handler = WebObjectUtils.CreateHandler(rawUrl); // let WebSupportModule handle configuration handler = WebSupportModule.ConfigureHandler(context, handler, appContext, rawUrl, false); } return handler; }
public IEnumerable <IHandler> GetAllHandlersFor(Type type) { IDictionary <string, object> objectsOfType = applicationContext.GetObjectsOfType(type); List <IHandler> handlers = new List <IHandler>(); foreach (KeyValuePair <string, object> dictionaryEntry in objectsOfType) { string objectName = (string)dictionaryEntry.Key; IObjectDefinition objectDefinition = applicationContext.ObjectFactory.GetObjectDefinition(objectName); handlers.Add(new DefaultHandler(objectDefinition.ObjectType, objectDefinition.ObjectType, () => applicationContext.GetObject(objectName))); } return(handlers); }