View Javadoc

1   /**
2    * 
3    */
4   package pt.digitalis.sampleApp.iss;
5   
6   import pt.digitalis.iss.IProcess;
7   import pt.digitalis.iss.ProcessResults;
8   
9   /**
10   * Abstract class that provides a base implementation of a WritingProcess for
11   * performance testing. This simply dumps a sequence of strings into a provided
12   * container. The container methods are only implemented in this base class
13   * implementations.
14   * 
15   * @author Pedro Viegas <a
16   *         href="mailto:pviegas@digitalis.pt">pviegas@digitalis.pt</a>
17   * @created Aug 5, 2007
18   * 
19   */
20  public abstract class BaseWriterProcess implements IProcess {
21  
22  	/** number of strings to output */
23  	private int totalInteractions;
24  
25  	/**
26  	 * the ID that will be added to the output for identification process
27  	 */
28  	protected int processID;
29  
30  	/**
31  	 * The processing end time
32  	 */
33  	private long endTime;
34  
35  	/**
36  	 * Initializes the required parameters
37  	 * 
38  	 * @param totalInteractions
39  	 *            number of strings to output
40  	 * @param processID
41  	 *            the ID that will be added to the output for identification
42  	 *            process
43  	 */
44  	public void init(int totalInteractions, int processID) {
45  		this.totalInteractions = totalInteractions;
46  		this.processID = processID;
47  	}
48  
49  	/**
50  	 * @see pt.digitalis.iss.IProcess#execute()
51  	 */
52  	public ProcessResults execute() {
53  
54  		int i = 0;
55  
56  		if (StartWritingProcess()) {
57  
58  			// Do the operation itself...
59  
60  			for (i = 0; i < totalInteractions; i++) {
61  				Write("[Process ID:"
62  						+ String.format("0009", processID)
63  						+ "] Outputing some unimportant testing string to the container: Interaction #"
64  						+ i);
65  			}
66  
67  			EndWritingProcess();
68  		}
69  
70  		ProcessResults result = new ProcessResults();
71  
72  		result.setResult("lines", Long.valueOf(i));
73  
74  		endTime = System.currentTimeMillis();
75  
76  		// The total lines added
77  		return result;
78  	}
79  
80  	/**
81  	 * Perform initialization operations
82  	 * 
83  	 * @return true if all initialization operations were completed sucessfullly
84  	 */
85  	abstract protected boolean StartWritingProcess();
86  
87  	/**
88  	 * Writes a line to the container
89  	 * 
90  	 * @param item
91  	 *            line to write to the container
92  	 */
93  	abstract protected void Write(String item);
94  
95  	/**
96  	 * Perform wrapup operations
97  	 */
98  	abstract protected void EndWritingProcess();
99  
100 	/**
101 	 * @return the end time
102 	 */
103 	public long getEndTime() {
104 		return endTime;
105 	}
106 }