Skip to content

Commit

Permalink
Merge pull request #13 from jglick/SimpleBuildWrapper-JENKINS-27207
Browse files Browse the repository at this point in the history
[FIXED JENKINS-27207] Use SimpleBuildWrapper to record timestamps
  • Loading branch information
StevenGBrown committed Jun 17, 2015
2 parents 0cef7ad + b8aa367 commit a19858b
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 63 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.520</version> <!-- earliest version that requires Java 6 -->
<version>1.609.1</version>
</parent>

<licenses>
Expand Down
Expand Up @@ -27,7 +27,6 @@
import hudson.Extension;
import hudson.Launcher;
import hudson.console.LineTransformationOutputStream;
import hudson.model.BuildListener;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.plugins.timestamper.io.TimestampsWriter;
Expand All @@ -44,6 +43,15 @@
import org.kohsuke.stapler.DataBoundConstructor;

import com.google.common.base.Optional;
import hudson.EnvVars;
import hudson.FilePath;
import hudson.console.ConsoleLogFilter;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.plugins.timestamper.io.TimestamperPaths;
import java.io.File;
import java.io.Serializable;
import jenkins.tasks.SimpleBuildWrapper;

/**
* Build wrapper that decorates the build's logger to record time-stamps as each
Expand All @@ -53,7 +61,7 @@
*
* @author Steven G. Brown
*/
public final class TimestamperBuildWrapper extends BuildWrapper {
public final class TimestamperBuildWrapper extends SimpleBuildWrapper {

private static final Logger LOGGER = Logger
.getLogger(TimestamperBuildWrapper.class.getName());
Expand All @@ -68,39 +76,43 @@ public TimestamperBuildWrapper() {
/**
* {@inheritDoc}
*/
@SuppressWarnings("rawtypes")
@Override
public Environment setUp(AbstractBuild build, Launcher launcher,
BuildListener listener) throws IOException, InterruptedException {
// Jenkins requires this method to be overridden.
return new Environment() {
// No tear down behaviour or additional environment variables.
};
@Override public void setUp(Context context, Run<?, ?> build, FilePath workspace, Launcher launcher, TaskListener listener, EnvVars initialEnvironment) throws IOException, InterruptedException {
// nothing to do
}

/**
* {@inheritDoc}
*/
@SuppressWarnings("rawtypes")
@Override
public OutputStream decorateLogger(AbstractBuild build, OutputStream logger) {
if (Boolean.getBoolean(TimestampNote.getSystemProperty())) {
return new TimestampNotesOutputStream(logger);
}
Optional<MessageDigest> digest = Optional.absent();
try {
digest = Optional.of(MessageDigest.getInstance("SHA-1"));
} catch (NoSuchAlgorithmException ex) {
LOGGER.log(Level.WARNING, ex.getMessage(), ex);
}
try {
TimestampsWriter timestampsWriter = new TimestampsWriter(build, digest);
logger = new TimestamperOutputStream(build, logger, timestampsWriter);
} catch (IOException ex) {
LOGGER.log(Level.WARNING, ex.getMessage(), ex);
}
return logger;
@Override public ConsoleLogFilter createLoggerDecorator(Run<?,?> build) {
return new ConsoleLogFilterImpl(build);
}
private static class ConsoleLogFilterImpl extends ConsoleLogFilter implements Serializable {
private static final long serialVersionUID = 1;
private final File timestampsFile;
private final long buildStartTime;
private final boolean useTimestampNotes;
ConsoleLogFilterImpl(Run<?,?> build) {
this.timestampsFile = TimestamperPaths.timestampsFile(build);
this.buildStartTime = build.getTimeInMillis();
useTimestampNotes = !(build instanceof AbstractBuild) || Boolean.getBoolean(TimestampNote.getSystemProperty());
}
@SuppressWarnings("rawtypes")
@Override public OutputStream decorateLogger(AbstractBuild _ignore, OutputStream logger) throws IOException, InterruptedException {
if (useTimestampNotes) {
return new TimestampNotesOutputStream(logger);
}
Optional<MessageDigest> digest = Optional.absent();
try {
digest = Optional.of(MessageDigest.getInstance("SHA-1"));
} catch (NoSuchAlgorithmException ex) {
LOGGER.log(Level.WARNING, ex.getMessage(), ex);
}
try {
TimestampsWriter timestampsWriter = new TimestampsWriter(timestampsFile, buildStartTime, digest);
logger = new TimestamperOutputStream(logger, timestampsWriter);
} catch (IOException ex) {
LOGGER.log(Level.WARNING, ex.getMessage(), ex);
}
return logger;
}
}

/**
* Output stream that writes each line to the provided delegate output stream
Expand Down
Expand Up @@ -24,7 +24,6 @@
package hudson.plugins.timestamper;

import static com.google.common.base.Preconditions.checkNotNull;
import hudson.model.Run;
import hudson.plugins.timestamper.io.TimestampsWriter;

import java.io.IOException;
Expand All @@ -43,11 +42,6 @@ final class TimestamperOutputStream extends OutputStream {
private static final Logger LOGGER = Logger
.getLogger(TimestamperOutputStream.class.getName());

/**
* The build in progress.
*/
private final Run<?, ?> build;

/**
* The delegate output stream.
*/
Expand Down Expand Up @@ -77,17 +71,13 @@ final class TimestamperOutputStream extends OutputStream {
/**
* Create a new {@link TimestamperOutputStream}.
*
* @param build
* the build in progress
* @param delegate
* the delegate output stream
* @param timestampsWriter
* will be used by this output stream to write the time-stamps and
* closed when the {@link #close()} method is called
*/
TimestamperOutputStream(Run<?, ?> build, OutputStream delegate,
TimestampsWriter timestampsWriter) {
this.build = checkNotNull(build);
TimestamperOutputStream(OutputStream delegate, TimestampsWriter timestampsWriter) {
this.delegate = checkNotNull(delegate);
this.timestampsWriter = checkNotNull(timestampsWriter);
}
Expand Down Expand Up @@ -137,7 +127,7 @@ private void writeTimestamps(byte[] b, int off, int len) {
} catch (IOException ex) {
writeError = true;
LOGGER.log(Level.WARNING,
"Error writing timestamps for " + build.getFullDisplayName(), ex);
"Error writing timestamps", ex);
}
}
}
Expand Down
Expand Up @@ -32,9 +32,9 @@
*
* @author Steven G. Brown
*/
class TimestamperPaths {
public class TimestamperPaths {

static File timestampsFile(Run<?, ?> build) {
public static File timestampsFile(Run<?, ?> build) {
File timestamperDir = timestamperDir(build);
return new File(timestamperDir, "timestamps");
}
Expand Down
Expand Up @@ -88,8 +88,12 @@ public TimestampsWriter(Run<?, ?> build) throws IOException {
*/
public TimestampsWriter(Run<?, ?> build, Optional<MessageDigest> digest)
throws IOException {
this.timestampsFile = TimestamperPaths.timestampsFile(build);
this.buildStartTime = build.getTimeInMillis();
this(TimestamperPaths.timestampsFile(build), build.getTimeInMillis(), digest);
}

public TimestampsWriter(File timestampsFile, long buildStartTime, Optional<MessageDigest> digest) throws IOException {
this.timestampsFile = timestampsFile;
this.buildStartTime = buildStartTime;
this.previousCurrentTimeMillis = buildStartTime;
this.timestampsDigest = checkNotNull(digest);

Expand Down
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License
Copyright 2015 Jesse Glick.
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.
-->

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core"/>
Expand Up @@ -31,17 +31,13 @@
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import hudson.model.AbstractBuild;
import hudson.plugins.timestamper.io.TimestampsWriter;

import java.io.IOException;
import java.io.OutputStream;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

/**
* Unit test for the TimestamperOutputStream class.
Expand All @@ -52,13 +48,6 @@ public class TimestamperOutputStreamTest {

private static final char NEWLINE = 0x0A;

/**
*/
@Rule
public TemporaryFolder folder = new TemporaryFolder();

private AbstractBuild<?, ?> build;

private OutputStream delegate;

private TimestampsWriter writer;
Expand All @@ -73,11 +62,9 @@ public class TimestamperOutputStreamTest {
*/
@Before
public void setUp() {
build = mock(AbstractBuild.class);
when(build.getRootDir()).thenReturn(folder.getRoot());
delegate = mock(OutputStream.class);
writer = mock(TimestampsWriter.class);
timestamperOutputStream = new TimestamperOutputStream(build, delegate,
timestamperOutputStream = new TimestamperOutputStream(delegate,
writer);
data = new byte[] { 'a', (byte) NEWLINE };
dataTwoLines = new byte[] { 'a', (byte) NEWLINE, 'b', (byte) NEWLINE };
Expand Down

0 comments on commit a19858b

Please sign in to comment.