View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.pipeline.testFramework;
19  
20  import java.util.ArrayList;
21  import java.util.Collections;
22  import java.util.List;
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.commons.pipeline.Stage;
26  import org.apache.commons.pipeline.StageContext;
27  import org.apache.commons.pipeline.StageException;
28  import org.apache.commons.pipeline.validation.ConsumedTypes;
29  import org.apache.commons.pipeline.validation.ProducesConsumed;
30  
31  @ConsumedTypes(Object.class)
32  @ProducesConsumed
33  public class TestStage implements Stage {
34      private Log log = LogFactory.getLog(TestStage.class);
35      private int index;
36      private StageContext context;
37  
38      public List<Object> processedObjects = Collections.synchronizedList(new ArrayList<Object>());
39      public boolean initialized = false;
40      public boolean preprocessed = false;
41      public boolean postprocessed = false;
42      public boolean released = false;
43      
44      /**
45       * Construct a TestStage with a numeric index used to easily identify this stage. 
46       * The {@link getIndex} and {@link toString} methods use this index.
47       * @param index acts as an identification number
48       */
49      public TestStage(int index) {
50          this.index = index;
51      }
52      
53      public int getIndex() {
54          return this.index;
55      }
56  
57      public void init(StageContext context) {        
58          this.context = context;
59          this.initialized = true;
60      }
61      
62      public void preprocess() throws StageException {
63          this.preprocessed = true;
64      }
65  
66      public void process(Object obj) throws StageException {
67          log.info(this + " is processing object " + obj);
68          this.processedObjects.add(obj);
69          this.context.getDownstreamFeeder(this).feed(obj);
70      }
71      
72      public void postprocess() throws StageException {
73          this.postprocessed = true;
74      }
75  
76      public void release() {
77          this.released = true;
78      }
79  
80      public String toString() {
81          return "TEST STAGE (" + this.index + ")";
82      }
83  }