Exemplo n.º 1
0
        /// <summary>
        /// Reads the attribute <c>customsession</c>
        /// from <see cref="MonoRailConfiguration"/> and
        /// instantiate it based on the type name provided.
        /// </summary>
        /// <exception cref="ConfigurationException">
        /// If the typename was not provided or the type
        /// could not be instantiated/found
        /// </exception>
        /// <param name="configuration"></param>
        public override void Init(MonoRailConfiguration configuration)
        {
            XmlAttribute customSessionAtt =
                configuration.ConfigSection.Attributes["customsession"];

            if (customSessionAtt == null || customSessionAtt.Value.Length == 0)
            {
                throw new ConfigurationException("The CustomSessionExtension requires that " +
                                                 "the type that implements ICustomSessionFactory be specified through the " +
                                                 "'customsession' attribute on 'monoRail' configuration node");
            }

            Type customSessType = MonoRailConfiguration.GetType(customSessionAtt.Value);

            if (customSessType == null)
            {
                throw new ConfigurationException("The Type for the custom session could not be loaded. " +
                                                 customSessionAtt.Value);
            }

            try
            {
                customSession = (ICustomSessionFactory)Activator.CreateInstance(customSessType);
            }
            catch (InvalidCastException)
            {
                throw new ConfigurationException("The Type for the custom session must " +
                                                 "implement ICustomSessionFactory. " + customSessionAtt.Value);
            }
        }
        private void InstallExceptionHandler(XmlNode node, String typeName)
        {
            IExceptionHandler handler = null;

            Type handlerType = MonoRailConfiguration.GetType(typeName);

            if (handlerType == null)
            {
                throw new ConfigurationException("The Type for the custom session could not be loaded. " +
                                                 typeName);
            }

            try
            {
                handler = (IExceptionHandler)Activator.CreateInstance(handlerType);
            }
            catch (InvalidCastException)
            {
                throw new ConfigurationException("The Type for the custom session must " +
                                                 "implement ICustomSessionFactory. " + typeName);
            }

            IConfigurableHandler configurableHandler = handler as IConfigurableHandler;

            if (configurableHandler != null)
            {
                configurableHandler.Configure(node);
            }

            handler.Initialize();

            if (firstHandler == null)
            {
                firstHandler = handler;
            }
            else
            {
                IExceptionHandler navHandler = firstHandler;

                while (navHandler != null)
                {
                    if (navHandler.Next == null)
                    {
                        navHandler.Next = handler;
                        break;
                    }

                    navHandler = navHandler.Next;
                }
            }
        }