-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
/
StreamTaskListener.java
217 lines (193 loc) · 7.78 KB
/
StreamTaskListener.java
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
* The MIT License
*
* Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.util;
import hudson.CloseProofOutputStream;
import hudson.model.TaskListener;
import hudson.remoting.RemoteOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.util.SystemProperties;
import org.kohsuke.stapler.framework.io.WriterOutputStream;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
// TODO: AbstractTaskListener is empty now, but there are dependencies on that e.g. Ruby Runtime - JENKINS-48116)
// The change needs API deprecation policy or external usages cleanup.
/**
* {@link TaskListener} that generates output into a single stream.
*
* <p>
* This object is remotable.
*
* @author Kohsuke Kawaguchi
*/
public class StreamTaskListener extends AbstractTaskListener implements TaskListener, Closeable {
@Nonnull
private PrintStream out;
@CheckForNull
private Charset charset;
/**
* @deprecated as of 1.349
* The caller should use {@link #StreamTaskListener(OutputStream, Charset)} to pass in
* the charset and output stream separately, so that this class can handle encoding correctly,
* or use {@link #fromStdout()} or {@link #fromStderr()}.
*/
@Deprecated
public StreamTaskListener(@Nonnull PrintStream out) {
this(out,null);
}
public StreamTaskListener(@Nonnull OutputStream out) {
this(out,null);
}
public StreamTaskListener(@Nonnull OutputStream out, @CheckForNull Charset charset) {
try {
if (charset == null)
this.out = (out instanceof PrintStream) ? (PrintStream)out : new PrintStream(out, false);
else
this.out = new PrintStream(out, false, charset.name());
this.charset = charset;
} catch (UnsupportedEncodingException e) {
// it's not very pretty to do this, but otherwise we'd have to touch too many call sites.
throw new Error(e);
}
}
public StreamTaskListener(@Nonnull File out) throws IOException {
this(out,null);
}
public StreamTaskListener(@Nonnull File out, @CheckForNull Charset charset) throws IOException {
// don't do buffering so that what's written to the listener
// gets reflected to the file immediately, which can then be
// served to the browser immediately
this(Files.newOutputStream(asPath(out)), charset);
}
private static Path asPath(@Nonnull File out) throws IOException {
try {
return out.toPath();
} catch (InvalidPathException e) {
throw new IOException(e);
}
}
/**
* Constructs a {@link StreamTaskListener} that sends the output to a specified file.
*
* @param out the file.
* @param append if {@code true}, then output will be written to the end of the file rather than the beginning.
* @param charset if non-{@code null} then the charset to use when writing.
* @throws IOException if the file could not be opened.
* @since 1.651
*/
public StreamTaskListener(@Nonnull File out, boolean append, @CheckForNull Charset charset) throws IOException {
// don't do buffering so that what's written to the listener
// gets reflected to the file immediately, which can then be
// served to the browser immediately
this(Files.newOutputStream(
asPath(out),
StandardOpenOption.CREATE, append ? StandardOpenOption.APPEND: StandardOpenOption.TRUNCATE_EXISTING
),
charset
);
}
public StreamTaskListener(@Nonnull Writer w) throws IOException {
this(new WriterOutputStream(w));
}
/**
* @deprecated as of 1.349
* Use {@link #NULL}
*/
@Deprecated
public StreamTaskListener() throws IOException {
this(new NullStream());
}
public static StreamTaskListener fromStdout() {
return new StreamTaskListener(System.out,Charset.defaultCharset());
}
public static StreamTaskListener fromStderr() {
return new StreamTaskListener(System.err,Charset.defaultCharset());
}
@Override
public PrintStream getLogger() {
return out;
}
@Override
public Charset getCharset() {
return charset != null ? charset : Charset.defaultCharset();
}
private void writeObject(ObjectOutputStream out) throws IOException {
out.writeObject(new RemoteOutputStream(new CloseProofOutputStream(this.out)));
out.writeObject(charset==null? null : charset.name());
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE, null, new Throwable("serializing here with AUTO_FLUSH=" + AUTO_FLUSH));
}
}
private static final String KEY_AUTO_FLUSH = StreamTaskListener.class.getName() + ".AUTO_FLUSH";
static {
SystemProperties.allowOnAgent(KEY_AUTO_FLUSH);
}
/**
* Restores eager remote flushing behavior.
* By default, remote tasks are expected to call {@link PrintStream#flush} before exiting.
* This flag will ensure that no output is lost from tasks which neglect to do so,
* at the expense of heavier Remoting traffic and reduced performance.
*/
private static /* not final */ boolean AUTO_FLUSH = SystemProperties.getBoolean(KEY_AUTO_FLUSH);
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
out = new PrintStream((OutputStream)in.readObject(), AUTO_FLUSH);
String name = (String)in.readObject();
charset = name==null ? null : Charset.forName(name);
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE, null, new Throwable("deserializing here with AUTO_FLUSH=" + AUTO_FLUSH));
}
}
@Override
public void close() throws IOException {
out.close();
}
/**
* Closes this listener and swallows any exceptions, if raised.
*
* @since 1.349
*/
public void closeQuietly() {
try {
close();
} catch (IOException e) {
LOGGER.log(Level.WARNING,"Failed to close",e);
}
}
private static final long serialVersionUID = 1L;
private static final Logger LOGGER = Logger.getLogger(StreamTaskListener.class.getName());
}