Пример #1
0
		/// <summary>
		/// Extracts the services the controller uses from the context -- which ultimately 
		/// is a service provider.
		/// </summary>
		/// <param name="context">The context/service provider.</param>
		public void InitializeFieldsFromServiceProvider(IRailsEngineContext context)
		{
			serviceProvider = context;

			viewEngineManager = (IViewEngineManager) serviceProvider.GetService(typeof(IViewEngineManager));

			IControllerDescriptorProvider controllerDescriptorBuilder = (IControllerDescriptorProvider)
				serviceProvider.GetService( typeof(IControllerDescriptorProvider) );

			metaDescriptor = controllerDescriptorBuilder.BuildDescriptor(this);

			ILoggerFactory loggerFactory = (ILoggerFactory) context.GetService(typeof(ILoggerFactory));
			
			if (loggerFactory != null)
			{
				logger = loggerFactory.Create(GetType().Name);
			}
			
			this.context = context;

			Initialize();
		}
		/// <summary>
		/// Collects the filters.
		/// </summary>
		/// <param name="descriptor">The descriptor.</param>
		/// <param name="controllerType">Type of the controller.</param>
		private void CollectFilters(ControllerMetaDescriptor descriptor, Type controllerType)
		{
			descriptor.Filters = filterDescriptorProvider.CollectFilters(controllerType);

			Array.Sort(descriptor.Filters, FilterDescriptorComparer.Instance);
		}
		internal void InitializeControllerFieldsFromServiceProvider()
		{
			controller.InitializeFieldsFromServiceProvider(context);

			serviceProvider = context;

			metaDescriptor = controller.metaDescriptor;

			controller.viewEngineManager = viewEngineManager;

			ILoggerFactory loggerFactory = (ILoggerFactory) context.GetService(typeof(ILoggerFactory));

			if (loggerFactory != null)
			{
				controller.logger = loggerFactory.Create(controller.GetType().Name);
			}
		}
		/// <summary>
		/// Collects the dynamic action.
		/// </summary>
		/// <param name="descriptor">The descriptor.</param>
		/// <param name="controllerType">Type of the controller.</param>
		private void CollectDynamicAction(ControllerMetaDescriptor descriptor, Type controllerType)
		{
			object[] attributes = controllerType.GetCustomAttributes(typeof(DynamicActionProviderAttribute), true);

			if (attributes.Length != 0)
			{
				foreach(DynamicActionProviderAttribute attr in attributes)
				{
					descriptor.ActionProviders.Add(attr.ProviderType);
				}
			}
		}
		/// <summary>
		/// Collects the helpers.
		/// </summary>
		/// <param name="descriptor">The descriptor.</param>
		/// <param name="controllerType">Type of the controller.</param>
		private void CollectHelpers(ControllerMetaDescriptor descriptor, Type controllerType)
		{
			descriptor.Helpers = helperDescriptorProvider.CollectHelpers(controllerType);
		}
		/// <summary>
		/// Collects the default action.
		/// </summary>
		/// <param name="descriptor">The descriptor.</param>
		/// <param name="controllerType">Type of the controller.</param>
		private void CollectDefaultAction(ControllerMetaDescriptor descriptor, Type controllerType)
		{
			object[] attributes = controllerType.GetCustomAttributes(typeof(DefaultActionAttribute), true);

			if (attributes.Length != 0)
			{
				descriptor.DefaultAction = (DefaultActionAttribute) attributes[0];
			}
		}
		/// <summary>
		/// Collects the scaffolding.
		/// </summary>
		/// <param name="descriptor">The descriptor.</param>
		/// <param name="controllerType">Type of the controller.</param>
		private void CollectScaffolding(ControllerMetaDescriptor descriptor, Type controllerType)
		{
			object[] attributes = controllerType.GetCustomAttributes(typeof(ScaffoldingAttribute), false);

			if (attributes.Length != 0)
			{
				foreach(ScaffoldingAttribute scaffolding in attributes)
				{
					descriptor.Scaffoldings.Add(scaffolding);
				}
			}
		}
		/// <summary>
		/// Collects the class level attributes.
		/// </summary>
		/// <param name="controllerType">Type of the controller.</param>
		/// <param name="descriptor">The descriptor.</param>
		private void CollectClassLevelAttributes(Type controllerType, ControllerMetaDescriptor descriptor)
		{
			CollectHelpers(descriptor, controllerType);
			CollectResources(descriptor, controllerType);
			CollectFilters(descriptor, controllerType);
			CollectLayout(descriptor, controllerType);
			CollectRescues(descriptor, controllerType);
			CollectDefaultAction(descriptor, controllerType);
			CollectScaffolding(descriptor, controllerType);
			CollectDynamicAction(descriptor, controllerType);
		}
		/// <summary>
		/// Collects the action attributes.
		/// </summary>
		/// <param name="method">The method.</param>
		/// <param name="descriptor">The descriptor.</param>
		private void CollectActionAttributes(MethodInfo method, ControllerMetaDescriptor descriptor)
		{
			if (logger.IsDebugEnabled)
			{
				logger.DebugFormat("Collection attributes for action {0}", method.Name);
			}

			ActionMetaDescriptor actionDescriptor = descriptor.GetAction(method);

			CollectResources(actionDescriptor, method);
			CollectSkipFilter(actionDescriptor, method);
			CollectRescues(actionDescriptor, method);
			CollectAccessibleThrough(actionDescriptor, method);
			CollectSkipRescue(actionDescriptor, method);
			CollectLayout(actionDescriptor, method);
			CollectCacheConfigures(actionDescriptor, method);
			CollectTransformFilter(actionDescriptor, method);
			
			if (method.IsDefined(typeof(AjaxActionAttribute), true))
			{
				descriptor.AjaxActions.Add(method);
			}
		}
		/// <summary>
		/// Collects the action level attributes.
		/// </summary>
		/// <param name="descriptor">The descriptor.</param>
		private void CollectActionLevelAttributes(ControllerMetaDescriptor descriptor)
		{
			foreach(object action in descriptor.Actions.Values)
			{
				if (action is IList)
				{
					foreach(MethodInfo overloadedAction in (action as IList))
					{
						CollectActionAttributes(overloadedAction, descriptor);
					}

					continue;
				}

				CollectActionAttributes(action as MethodInfo, descriptor);
			}
		}
		/// <summary>
		/// Collects the actions.
		/// </summary>
		/// <param name="controllerType">Type of the controller.</param>
		/// <param name="desc">The desc.</param>
		private void CollectActions(Type controllerType, ControllerMetaDescriptor desc)
		{
			// HACK: GetRealControllerType is a workaround for DYNPROXY-14 bug
			// see: http://support.castleproject.org/browse/DYNPROXY-14
			controllerType = GetRealControllerType(controllerType);

			MethodInfo[] methods = controllerType.GetMethods(BindingFlags.Public | BindingFlags.Instance);

			foreach(MethodInfo method in methods)
			{
				Type declaringType = method.DeclaringType;

				if (declaringType == typeof(Object) || 
					declaringType == typeof(Controller) || 
					declaringType == typeof(SmartDispatcherController))
				{
					continue;
				}

				if (desc.Actions.Contains(method.Name))
				{
					ArrayList list = desc.Actions[method.Name] as ArrayList;

					if (list == null)
					{
						list = new ArrayList();
						list.Add(desc.Actions[method.Name]);

						desc.Actions[method.Name] = list;
					}

					list.Add(method);
				}
				else
				{
					desc.Actions[method.Name] = method;
				}
			}
		}
		/// <summary>
		/// Builds the <see cref="ControllerMetaDescriptor"/> for the specified controller type
		/// </summary>
		/// <param name="controllerType">Type of the controller.</param>
		/// <returns></returns>
		private ControllerMetaDescriptor InternalBuildDescriptor(Type controllerType)
		{
			if (logger.IsDebugEnabled)
			{
				logger.DebugFormat("Building controller descriptor for {0}", controllerType);
			}

			ControllerMetaDescriptor descriptor = new ControllerMetaDescriptor();

			CollectClassLevelAttributes(controllerType, descriptor);
			
			CollectActions(controllerType, descriptor);

			CollectActionLevelAttributes(descriptor);
			
			return descriptor;
		}