Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d1eab65f4bb251205b2229a4ba5aac9180064d0c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*******************************************************************************
* Copyright (c) 2007 compeople AG and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* 	compeople AG (Stefan Liebig) - initial API and implementation
*   IBM Corporation - continuing development
*******************************************************************************/
package org.eclipse.equinox.internal.provisional.p2.artifact.repository.processing;

import java.io.IOException;
import java.io.OutputStream;
import org.eclipse.core.runtime.*;
import org.eclipse.equinox.internal.provisional.p2.artifact.repository.IArtifactDescriptor;
import org.eclipse.equinox.internal.provisional.p2.artifact.repository.IStateful;

/**
 * ProcessingSteps process the data written to them and pass the resultant data on
 * to a configured destination stream.  Steps may monitor (e.g., count) the data, compute information 
 * about the data (e.g., checksum or hash) or transform the data (e.g., unpack200).
 */
public abstract class ProcessingStep extends OutputStream implements IStateful {

	private OutputStream destination;
	private IProgressMonitor monitor;
	private IStatus status = Status.OK_STATUS;

	protected ProcessingStep() {
		super();
	}

	/**
	 * Initialize this processing step according to the information in the given 
	 * descriptor and context.  After initialization, this step is ready for linking 
	 * with other steps or output streams
	 * @param descriptor description of the step
	 * @param context the context in which the step is being used
	 */
	public void initialize(ProcessingStepDescriptor descriptor, IArtifactDescriptor context) {
		// nothing to do here!
	}

	/**
	 * Link this step with the given output stream and configure the step to use the given
	 * progress monitor.  After linking the step is ready to have data written to it.
	 * @param destination the stream into which to write the processed data
	 * @param monitor the progress monitor to use for reporting activity
	 */
	public void link(OutputStream destination, IProgressMonitor monitor) {
		this.destination = destination;
		this.monitor = monitor;
	}

	/**
	 * Process the given byte and pass the result on to the configured destination stream
	 * @param b the byte being written
	 */
	public void write(int b) throws IOException {
		// nothing to do here!
	}

	/** 
	 * Flush any unwritten data from this stream.
	 */
	public void flush() throws IOException {
		super.flush();
		if (destination != null)
			destination.flush();
	}

	/**
	 * Close this stream and, if the configured destination is a ProcessingStep, 
	 * close it as well.  Typically a chain of steps terminates in a conventional 
	 * output stream.  Implementors of this method should ensure they set the 
	 * status of the step.
	 */
	public void close() throws IOException {
		super.close();
		if (destination instanceof ProcessingStep)
			destination.close();
		monitor = null;
	}

	public IStatus getStatus() {
		return status;
	}

	public void setStatus(IStatus value) {
		if (value == null)
			value = Status.OK_STATUS;
		if (status != null && status.getSeverity() >= value.getSeverity())
			return;
		status = value;
	}

	/**
	 * Get the progress monitor. 
	 * @return the progress monitor; may be null
	 */
	protected IProgressMonitor getProgressMonitor() {
		return monitor;
	}

	/**
	 * Get the stream to write the processed data into.
	 * 
	 * @return output stream for processed data
	 */
	protected OutputStream getDestination() {
		return destination;
	}

	/**
	 * Return the status of this step.  The status will be <code>null</code> if the
	 * step has not yet executed. If the step has executed the returned status
	 * indicates the success or failure of the step.
	 * @param deep whether or not to aggregate the status of any linked steps
	 * @return the requested status 
	 */
	public IStatus getStatus(boolean deep) {
		return ProcessingStepHandler.getStatus(this, deep);
	}
}

Back to the top