File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\util\StreamTaskListener.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.util;
25
26 import hudson.CloseProofOutputStream;
27 import hudson.console.ConsoleNote;
28 import hudson.console.HudsonExceptionNote;
29 import hudson.model.TaskListener;
30 import hudson.remoting.RemoteOutputStream;
31 import org.kohsuke.stapler.framework.io.WriterOutputStream;
32
33 import java.io.Closeable;
34 import java.io.File;
35 import java.io.FileOutputStream;
36 import java.io.IOException;
37 import java.io.ObjectInputStream;
38 import java.io.ObjectOutputStream;
39 import java.io.OutputStream;
40 import java.io.OutputStreamWriter;
41 import java.io.PrintStream;
42 import java.io.PrintWriter;
43 import java.io.Serializable;
44 import java.io.UnsupportedEncodingException;
45 import java.io.Writer;
46 import java.nio.charset.Charset;
47 import java.util.logging.Level;
48 import java.util.logging.Logger;
49
50 /**
51 * {@link TaskListener} that generates output into a single stream.
52 *
53 * <p>
54 * This object is remotable.
55 *
56 * @author Kohsuke Kawaguchi
57 */
58 public class StreamTaskListener extends AbstractTaskListener implements Serializable, Closeable {
59 private PrintStream out;
60 private Charset charset;
61
62 /**
63 * @deprecated as of 1.349
64 * The caller should use {@link #StreamTaskListener(OutputStream, Charset)} to pass in
65 * the charset and output stream separately, so that this class can handle encoding correctly,
66 * or use {@link #fromStdout()} or {@link #fromStderr()}.
67 */
68 public StreamTaskListener(PrintStream out) {
69 this(out,null);
70 }
71
72 public StreamTaskListener(OutputStream out) {
73 this(out,null);
74 }
75
76 public StreamTaskListener(OutputStream out, Charset charset) {
77 try {
78 if (charset == null)
79 this.out = (out instanceof PrintStream) ? (PrintStream)out : new PrintStream(out, false);
80 else
81 this.out = new PrintStream(out, false, charset.name());
82 this.charset = charset;
83 } catch (UnsupportedEncodingException e) {
84 // it's not very pretty to do this, but otherwise we'd have to touch too many call sites.
85 throw new Error(e);
86 }
87 }
88
89 public StreamTaskListener(File out) throws IOException {
90 this(out,null);
91 }
92
93 public StreamTaskListener(File out, Charset charset) throws IOException {
94 // don't do buffering so that what's written to the listener
95 // gets reflected to the file immediately, which can then be
96 // served to the browser immediately
97 this(new FileOutputStream(out),charset);
98 }
99
100 public StreamTaskListener(Writer w) throws IOException {
101 this(new WriterOutputStream(w));
102 }
103
104 /**
105 * @deprecated as of 1.349
106 * Use {@link #NULL}
107 */
108 public StreamTaskListener() throws IOException {
109 this(new NullStream());
110 }
111
112 public static StreamTaskListener fromStdout() {
113 return new StreamTaskListener(System.out,Charset.defaultCharset());
114 }
115
116 public static StreamTaskListener fromStderr() {
117 return new StreamTaskListener(System.err,Charset.defaultCharset());
118 }
119
120 public PrintStream getLogger() {
121 return out;
122 }
123
124 private PrintWriter _error(String prefix, String msg) {
125 out.print(prefix);
126 out.println(msg);
127
128 // the idiom in Hudson is to use the returned writer for writing stack trace,
129 // so put the marker here to indicate an exception. if the stack trace isn't actually written,
130 // HudsonExceptionNote.annotate recovers gracefully.
131 try {
132 annotate(new HudsonExceptionNote());
133 } catch (IOException e) {
134 // for signature compatibility, we have to swallow this error
135 }
136 return new PrintWriter(
137 charset!=null ? new OutputStreamWriter(out,charset) : new OutputStreamWriter(out),true);
138 }
139
140 public PrintWriter error(String msg) {
141 return _error("ERROR: ",msg);
142 }
143
144 public PrintWriter error(String format, Object... args) {
145 return error(String.format(format,args));
146 }
147
148 public PrintWriter fatalError(String msg) {
149 return _error("FATAL: ",msg);
150 }
151
152 public PrintWriter fatalError(String format, Object... args) {
153 return fatalError(String.format(format,args));
154 }
155
156 public void annotate(ConsoleNote ann) throws IOException {
157 ann.encodeTo(out);
158 }
159
160 private void writeObject(ObjectOutputStream out) throws IOException {
161 out.writeObject(new RemoteOutputStream(new CloseProofOutputStream(this.out)));
162 out.writeObject(charset==null? null : charset.name());
163 }
164
165 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
166 out = new PrintStream((OutputStream)in.readObject(),true);
167 String name = (String)in.readObject();
168 charset = name==null ? null : Charset.forName(name);
169 }
170
171 public void close() throws IOException {
172 out.close();
173 }
174
175 /**
176 * Closes this listener and swallows any exceptions, if raised.
177 *
178 * @since 1.349
179 */
180 public void closeQuietly() {
181 try {
182 close();
183 } catch (IOException e) {
184 LOGGER.log(Level.WARNING,"Failed to close",e);
185 }
186 }
187
188 private static final long serialVersionUID = 1L;
189
190 private static final Logger LOGGER = Logger.getLogger(StreamTaskListener.class.getName());
191 }
192