Skip to content

Commit

Permalink
Refactoring: output stream creation moved to separate method
Browse files Browse the repository at this point in the history
  • Loading branch information
t-dan committed Oct 15, 2018
1 parent 4ae4169 commit 196fd52
Showing 1 changed file with 35 additions and 20 deletions.
55 changes: 35 additions & 20 deletions main/src/net/sourceforge/cruisecontrol/builders/WriterBuilder.java
Expand Up @@ -119,31 +119,13 @@ public class WriterBuilder extends Builder {
@Override
public Element build(Map<String, String> properties, Progress progress)
throws CruiseControlException {

final long startTime = System.currentTimeMillis();
final Element status = new Element("writer");
OutputStream out = null;
java.io.File f;

// Resolve properties in the settings. Fail. if they cannot be resolved
final String fname = Util.parsePropertiesInString(properties, this.file.getAbsolutePath(), true);
f = new java.io.File(fname);

try {
// The output file must not exist
if (!this.overwrite && f.exists()) {
throw new IOException("File " + f + " exists but overwrite=false");
}
// gzip compression is set on
if (this.gzip) {
if (!f.getName().endsWith(".gz")) {
f = new java.io.File(f.getAbsolutePath() + ".gz");
}
out = new GZIPOutputStream(new FileOutputStream(f, this.append));
// not-compressed file is required
} else {
out = new FileOutputStream(f, this.append);
}

out = getOutputStream(properties);
final StreamConsumer consumer = getStreamConsumer(out, encoding);
// Pass content to the consumer
for (Content content : this.messages) {
Expand Down Expand Up @@ -331,6 +313,39 @@ protected Content addContent(Content obj) {
return obj;
}

/**
* Creates instance of {@link OutputStream} used to consume data generated by the writer object.
*
* @param properties the built properties as passed to {@link #build(Map, Progress)}
* @return {@link OutputStream} object
* @throws CruiseControlException
*/
protected OutputStream getOutputStream(final Map<String, String> properties) throws CruiseControlException {
// Resolve properties in the settings. Fail. if they cannot be resolved
final String fname = Util.parsePropertiesInString(properties, this.file.getAbsolutePath(), true);
java.io.File f = new java.io.File(fname);

try {
// The output file must not exist
if (!this.overwrite && f.exists()) {
throw new IOException("File " + fname + " exists but overwrite=false");
}
// gzip compression is set on
if (this.gzip) {
if (!f.getName().endsWith(".gz")) {
f = new java.io.File(f.getAbsolutePath() + ".gz");
}
return new GZIPOutputStream(new FileOutputStream(f, this.append));
// not-compressed file is required
} else {
return new FileOutputStream(f, this.append);
}

} catch (Exception e) {
throw new CruiseControlException("Failed to build output for " + f.getAbsolutePath(), e);
}
}

/**
* Creates instance of {@link StreamConsumer} passing data into the given {@link OutputStream}
* instance. Note that <code>null</code> line must be passed to the consume to close the underlying
Expand Down

0 comments on commit 196fd52

Please sign in to comment.