/// <summary>
 /// Looks up a component in the pipeline
 /// </summary>
 /// <param name="stage">The stage the component is in</param>
 /// <param name="index">The 0-based index inside the stage</param>
 /// <returns>The component, or null if it was not found</returns>
 public IBaseComponent GetComponent(ppStage stage, int index)
 {
     foreach (PStage st in _pipeline.Stages)
     {
         if (st.Id == stage.StageID)
         {
             IEnumerator enumerator = st.GetComponentEnumerator();
             while (enumerator.MoveNext())
             {
                 if (index-- == 0)
                 {
                     return((IBaseComponent)enumerator.Current);
                 }
             }
         }
     }
     return(null);
 }
        //
        // Protected Methods
        //

        /// <summary>
        /// Finds a stage in the pipeline
        /// </summary>
        /// <param name="stage">Stage definition</param>
        /// <returns>The stage, if found, or a new stage if necessary</returns>
        protected PStage FindStage(ppStage stage)
        {
            PStage theStage = null;

            foreach (PStage pstage in _pipeline.Stages)
            {
                if (pstage.Id == stage.StageID)
                {
                    theStage = pstage;
                    break;
                }
            }
            if (theStage == null)
            {
                theStage = new PStage(stage.StageName, stage.ExecuteMethod, stage.StageID, _pipeline);
                _pipeline.Stages.Add(theStage);
            }
            return(theStage);
        }
        /// <summary>
        /// Adds a component to the specified stage
        /// </summary>
        /// <param name="component">Component to add to the stage</param>
        /// <param name="stage">Stage to add it to</param>
        public void AddComponent(IBaseComponent component, ppStage stage)
        {
            if (component == null)
            {
                throw new ArgumentNullException("component");
            }
            if (stage == null)
            {
                throw new ArgumentNullException("stage");
            }

            if (stage.IsReceiveStage != _isReceivePipeline)
            {
                throw new ArgumentException("Invalid Stage", "stage");
            }

            PStage theStage = FindStage(stage);

            theStage.AddComponent(component);
        }
        /// <summary>
        /// Applies the loaded configuration to a component
        /// </summary>
        /// <param name="stageId">The stage the component is in</param>
        /// <param name="name">The component name</param>
        /// <param name="index">The index of the component within the pipeline</param>
        /// <param name="reader">The per-instance configuration</param>
        private void ApplyComponentConfig(Guid stageId, string name, int index, XmlReader reader)
        {
            ppStage             stage     = ppStage.LookupStage(stageId);
            IPersistPropertyBag component = GetComponent(stage, index)
                                            as IPersistPropertyBag;

            if (component != null)
            {
                String compName = component.GetType().FullName;
                if (compName != name)
                {
                    throw new InvalidOperationException(String.Format(
                                                            "Component in stage '{0}', index {1} is '{2}', expected '{3}'",
                                                            stage.StageName, index, compName, name));
                }

                IPropertyBag bag = new InstConfigPropertyBag(reader);
                component.Load(bag, 1);
            }
        }