package com.openshift.jenkins.plugins.util;

import hudson.model.TaskListener;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class QuietTaskListenerFactory {

    public static QuietTasklistener build(TaskListener listener) {
        return (QuietTasklistener) Proxy.newProxyInstance(
                QuietTaskListenerFactory.class.getClassLoader(),
                new Class[] { QuietTasklistener.class },
                new QuietTaskListenerIH(listener));
    }

    protected static class QuietTaskListenerIH implements InvocationHandler {

        private final TaskListener underlying;
        private final ByteArrayOutputStream logContent = new ByteArrayOutputStream();
        private final PrintStream out;

        protected QuietTaskListenerIH(TaskListener underlying) {
            this.underlying = underlying;
            PrintStream tmp = null;
            try {
                tmp = new PrintStream(logContent, false, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException("UTF8 not supported", e);
            }
            out = tmp;
        }

        /**
         * The goal is to intercept calls to the TaskListener's getLogger method
         * to return our own PrintStream. We implement QuietTasklistener, so we
         * must also intercept/satisfy __getLogOutput.
         */
        @Override
        public Object invoke(Object o, Method method, Object[] objects)
                throws Throwable {
            if ("getLogger".equals(method.getName())) {
                return out;
            }
            if ("__getLogOutput".equals(method.getName())) {
                out.flush();
                return logContent.toString("UTF-8");
            }
            // If neither signature matches, pass it on to the underlying
            // TaskListener
            return method.invoke(o, objects);
        }

    }

    public interface QuietTasklistener extends TaskListener {
        public String __getLogOutput();
    }

}

@@ -103,32 +103,148 @@
 ayOutputStream;%0A
+import java.io.Externalizable;%0Aimport java.io.IOException;%0Aimport java.io.ObjectInput;%0Aimport java.io.ObjectOutput;%0A
 import java.io.P
@@ -763,24 +763,21 @@
 %7D%0A%0A    p
-rotected
+ublic
  static 
@@ -830,16 +830,32 @@
 nHandler
+, Externalizable
  %7B%0A%0A    
@@ -866,22 +866,16 @@
 private 
-final 
 TaskList
@@ -907,22 +907,16 @@
 private 
-final 
 ByteArra
@@ -991,14 +991,8 @@
 ate 
-final 
 Prin
@@ -1003,16 +1003,84 @@
 eam out;
+%0A        %0A        public QuietTaskListenerIH() %7B%0A        %09%0A        %7D
 %0A%0A      
@@ -2248,24 +2248,633 @@
 %0A        %7D%0A%0A
+        @Override%0A        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException %7B%0A            underlying = (TaskListener)in.readObject();%0A            PrintStream tmp = null;%0A            try %7B%0A                tmp = new PrintStream(logContent, false, %22UTF-8%22);%0A            %7D catch (UnsupportedEncodingException e) %7B%0A                throw new RuntimeException(%22UTF8 not supported%22, e);%0A            %7D%0A            out = tmp;%0A%0A        %7D%0A%0A        @Override%0A        public void writeExternal(ObjectOutput out) throws IOException %7B%0A            out.writeObject(underlying);%0A%09%09%09%0A        %7D%0A%0A
     %7D%0A%0A    p

make proxy task listener fully externalizable to satisy new 2.138 reqs