package org.robotframework.rappio.agent;

import java.lang.instrument.Instrumentation;
import java.lang.management.ManagementFactory;
import java.util.Properties;
import java.util.logging.LogManager;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.varia.NullAppender;
import org.robotframework.rappio.remote.DaemonRemoteServer;

import org.robotframework.remoteserver.RemoteServer;
import org.robotframework.swing.SwingLibrary;
import sun.awt.AppContext;

public class TestJavaAgent {

	public static void premain(String agentArgument, Instrumentation instrumentation){
                int port = Integer.parseInt(agentArgument);
                Logger root = Logger.getRootLogger();
                root.removeAllAppenders();
                BasicConfigurator.configure();
                root.setLevel(Level.OFF);
                root.addAppender(new NullAppender());
                LogFactory.releaseAll();
                LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log",
                        "org.apache.commons.logging.impl.Log4JLogger");
                Properties p = new Properties();
                p.setProperty("org.eclipse.jetty.LEVEL", "WARN");
                org.eclipse.jetty.util.log.StdErrLog.setProperties(p);
                RemoteServer server = new DaemonRemoteServer();
                server.putLibrary("/RPC2", new SwingLibrary());
                server.setPort(port);
                server.setAllowStop(true);
                try {
                    server.start();
                } catch (Exception e) {
                    System.out.println("TODODOODODODODOODODODODO!!!");
                }
                AppContext.getAppContext();
	}

}

@@ -35,16 +35,73 @@
 agent;%0A%0A
+import java.io.OutputStream;%0Aimport java.io.PrintStream;%0A
 import j
@@ -664,16 +664,61 @@
 gent %7B%0A%0A
+        private static PrintStream out;%0A    %0A
 %09public 
@@ -793,24 +793,44 @@
 ation)%7B%0A    
+        noOutput();%0A
             
@@ -885,20 +885,16 @@
         
-    
 Logger r
@@ -931,28 +931,24 @@
             
- 
-   
 root.removeA
@@ -958,28 +958,24 @@
 ppenders();%0A
-    
             
@@ -1013,28 +1013,24 @@
             
-    
 root.setLeve
@@ -1051,28 +1051,24 @@
             
-  
-  
 root.addAppe
@@ -1089,28 +1089,24 @@
 ppender());%0A
-    
             
@@ -1134,36 +1134,32 @@
 ();%0A            
-    
 LogFactory.getFa
@@ -1233,20 +1233,16 @@
         
-  
-  
 %22org.apa
@@ -1293,20 +1293,16 @@
         
-    
 Properti
@@ -1338,20 +1338,16 @@
         
- 
-   
 p.setPro
@@ -1400,20 +1400,16 @@
         
-   
- 
 org.ecli
@@ -1467,20 +1467,16 @@
         
-    
 RemoteSe
@@ -1523,28 +1523,24 @@
             
- 
-   
 server.putLi
@@ -1571,26 +1571,25 @@
 Library());%0A
-  
+%0A
             
@@ -1580,34 +1580,32 @@
 );%0A%0A            
-  
 server.setPort(p
@@ -1606,28 +1606,24 @@
 Port(port);%0A
-    
             
@@ -1661,20 +1661,16 @@
         
-    
 try %7B%0A  
@@ -1675,36 +1675,32 @@
                 
-  
-  
 server.start();%0A
@@ -1695,28 +1695,24 @@
 er.start();%0A
-    
             
@@ -1735,20 +1735,16 @@
 on e) %7B%0A
-    
         
@@ -1818,18 +1818,10 @@
     
-  
-  %7D%0A    
+%7D%0A
     
@@ -1860,10 +1860,301 @@
 ();%0A
-%09%7D%0A
+            System.setOut(out);%0A%09%7D%0A%0A        private static void noOutput() %7B%0A            out = System.out;%0A            System.setOut(new PrintStream(new OutputStream() %7B%0A                 public void write(int b) %7B%0A                     //DO NOTHING%0A                 %7D%0A             %7D));%0A        %7D
 %0A%7D%0A

no output