From 595d235b43316ed2b091ccf027be2273aaf0323b Mon Sep 17 00:00:00 2001 From: kohsuke Date: Mon, 27 Aug 2007 03:41:38 +0000 Subject: [PATCH] added log splitting support. git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@4444 71c3de6d-444a-0410-be80-ed276b4c234a --- .../main/java/hudson/maven/MavenBuild.java | 14 +++ .../hudson/maven/MavenModuleSetBuild.java | 23 +++-- .../hudson/maven/SplittableBuildListener.java | 93 +++++++++++++++++++ 3 files changed, 120 insertions(+), 10 deletions(-) create mode 100644 core/src/main/java/hudson/maven/SplittableBuildListener.java diff --git a/core/src/main/java/hudson/maven/MavenBuild.java b/core/src/main/java/hudson/maven/MavenBuild.java index c82de11568..628ba1a860 100644 --- a/core/src/main/java/hudson/maven/MavenBuild.java +++ b/core/src/main/java/hudson/maven/MavenBuild.java @@ -23,7 +23,11 @@ import org.apache.maven.project.MavenProject; import java.io.File; import java.io.IOException; +import java.io.OutputStream; import java.io.Serializable; +import java.io.BufferedOutputStream; +import java.io.FileOutputStream; +import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; @@ -232,10 +236,19 @@ public class MavenBuild extends AbstractBuild { } class ProxyImpl2 extends ProxyImpl implements MavenBuildProxy2 { + private final SplittableBuildListener listener; long startTime; + private final OutputStream log; + + ProxyImpl2(SplittableBuildListener listener) throws FileNotFoundException { + this.listener = listener; + log = new BufferedOutputStream(new FileOutputStream(getLogFile())); + } + public void start() { onStartBuilding(); startTime = System.currentTimeMillis(); + listener.setSideOutputStream(log); } public void end() { @@ -244,6 +257,7 @@ public class MavenBuild extends AbstractBuild { onEndBuilding(); duration = System.currentTimeMillis()- startTime; try { + listener.setSideOutputStream(null); save(); } catch (IOException e) { e.printStackTrace(); diff --git a/core/src/main/java/hudson/maven/MavenModuleSetBuild.java b/core/src/main/java/hudson/maven/MavenModuleSetBuild.java index 297e7ade4d..16054dd429 100644 --- a/core/src/main/java/hudson/maven/MavenModuleSetBuild.java +++ b/core/src/main/java/hudson/maven/MavenModuleSetBuild.java @@ -278,14 +278,15 @@ public final class MavenModuleSetBuild extends AbstractBuild proxies = new HashMap(); for (MavenModule m : modules.values()) - proxies.put(m.getModuleName(),m.newBuild().new ProxyImpl2()); + proxies.put(m.getModuleName(),m.newBuild().new ProxyImpl2(slistener)); // run the complete build here Map envVars = getEnvVars(); - ProcessCache.MavenProcess process = MavenBuild.mavenProcessCache.get(launcher.getChannel(), listener, + ProcessCache.MavenProcess process = MavenBuild.mavenProcessCache.get(launcher.getChannel(), slistener, new MavenProcessFactory(project,launcher,envVars)); ArgumentListBuilder margs = new ArgumentListBuilder(); @@ -294,7 +295,7 @@ public final class MavenModuleSetBuild extends AbstractBuild buildProxy; + private final Map proxies; private final Map> reporters = new HashMap>(); private final Map> executedMojos = new HashMap>(); private long mojoStartTime; - public Builder(BuildListener listener,Map buildProxy, Collection modules, List goals, Map systemProps) { + public Builder(BuildListener listener,Map proxies, Collection modules, List goals, Map systemProps) { super(listener,goals,systemProps); - this.buildProxy = buildProxy; + this.proxies = proxies; for (MavenModule m : modules) reporters.put(m.getModuleName(),m.createReporters()); @@ -354,7 +355,8 @@ public final class MavenModuleSetBuild extends AbstractBuild()); mojoList.add(new ExecutedMojo(mojoInfo,System.currentTimeMillis()-mojoStartTime)); - MavenBuildProxy2 proxy = buildProxy.get(name); + MavenBuildProxy2 proxy = proxies.get(name); for (MavenReporter r : reporters.get(name)) if(!r.postExecute(proxy,project,mojoInfo,listener,exception)) throw new hudson.maven.agent.AbortException(r+" failed"); diff --git a/core/src/main/java/hudson/maven/SplittableBuildListener.java b/core/src/main/java/hudson/maven/SplittableBuildListener.java new file mode 100644 index 0000000000..20c38baa0c --- /dev/null +++ b/core/src/main/java/hudson/maven/SplittableBuildListener.java @@ -0,0 +1,93 @@ +package hudson.maven; + +import hudson.model.BuildListener; +import hudson.model.Result; +import hudson.model.StreamBuildListener; +import hudson.util.NullStream; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintStream; +import java.io.PrintWriter; +import java.io.Serializable; + +/** + * Delegating {@link BuildListener} that can have "side" {@link OutputStream} + * that gets log outputs. The side stream can be changed at runtime. + * + * @author Kohsuke Kawaguchi + * @since 1.133 + */ +final class SplittableBuildListener implements BuildListener, Serializable { + /** + * The actual {@link BuildListener} where the output goes. + */ + private final BuildListener core; + + private OutputStream side = new NullStream(); + + /** + * Constant {@link PrintStream} connected to both {@link #core} and {@link #side}. + * This is so that we can change the side stream without the client noticing it. + */ + private final PrintStream logger; + + public SplittableBuildListener(BuildListener core) { + this.core = core; + final OutputStream base = core.getLogger(); + logger = new PrintStream(new OutputStream() { + public void write(int b) throws IOException { + base.write(b); + side.write(b); + } + + public void write(byte b[], int off, int len) throws IOException { + base.write(b,off,len); + side.write(b,off,len); + } + + public void flush() throws IOException { + base.flush(); + side.flush(); + } + + public void close() throws IOException { + base.close(); + side.close(); + } + }); + } + + public void setSideOutputStream(OutputStream os) { + if(os==null) os = new NullStream(); + this.side = os; + } + + public void started() { + core.started(); + } + + public void finished(Result result) { + core.finished(result); + } + + public PrintStream getLogger() { + return logger; + } + + public PrintWriter error(String msg) { + core.error(msg); + return new PrintWriter(logger); + } + + public PrintWriter fatalError(String msg) { + core.fatalError(msg); + return new PrintWriter(logger); + } + + private Object writeReplace() { + return new StreamBuildListener(logger); + } + + private static final long serialVersionUID = 1L; +}