<commit_before>package org.robotframework.rappio.agent;

import java.awt.Window;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.lang.instrument.Instrumentation;
import java.net.Socket;
import java.util.Map;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;

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;
import sun.awt.SunToolkit;

public class RappioJavaAgent {

	private static final String LOCALHOST = "127.0.0.1";
	private static final int DEFAULT_RAPPIO_PORT = 8181;
	private static PrintStream out = System.out;

	public static void premain(String agentArgument, Instrumentation instrumentation){
            try {
                noOutput();
                RemoteServer server = new DaemonRemoteServer();
                server.putLibrary("/RPC2", new SwingLibrary());
                server.putLibrary("/rappioservices", new RappioServicesLibrary());
                server.setPort(0);
                server.setAllowStop(true);
                server.start();
                if(AppContext.getAppContext() == null){
                    SunToolkit.createNewAppContext();
                }
                Integer actualPort = server.getLocalPort();
                notifyPort(actualPort, getRappioPort(agentArgument));
            } catch (Exception e) {
                    System.err.println("Error starting remote server");
                    e.printStackTrace();
            }finally{
                    System.setOut(out);
            }
	}
        
        private static void notifyPort(final Integer portToNotify, final Integer serverPort) {
            Timer timer = new Timer("RobotJavaAgentPortNotifyingTimer", true);
            timer.schedule(new TimerTask() {

                @Override
                public void run() {
                    try {
                        while(Window.getWindows().length == 0) {
                            Thread.sleep(100);
                        }
                        Socket echoSocket = new Socket(LOCALHOST, serverPort);
                        PrintWriter outToServer = new PrintWriter(echoSocket.getOutputStream(), true);
                        outToServer.write(portToNotify.toString()+":"+getName());
                        outToServer.close();
                        echoSocket.close();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            }, 100);
        }
        
        private static String getName() {
            String name = System.getProperty("sun.java.command");
            if(name != null)
                return name;
            for(final Map.Entry<String, String> entry : System.getenv().entrySet())
            {
              if(entry.getKey().startsWith("JAVA_MAIN_CLASS"))
                return entry.getValue();
            }<commit_msg>port notifier cleanup<commit_after>package org.robotframework.rappio.agent;

import java.awt.Window;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.lang.instrument.Instrumentation;
import java.net.Socket;
import java.util.Map;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;

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;
import sun.awt.SunToolkit;

public class RappioJavaAgent {

	private static final String LOCALHOST = "127.0.0.1";
	private static final int DEFAULT_RAPPIO_PORT = 8181;
	private static PrintStream out = System.out;

	public static void premain(String agentArgument, Instrumentation instrumentation){
            try {
                noOutput();
                RemoteServer server = new DaemonRemoteServer();
                server.putLibrary("/RPC2", new SwingLibrary());
                server.putLibrary("/rappioservices", new RappioServicesLibrary());
                server.setPort(0);
                server.setAllowStop(true);
                server.start();
                if(AppContext.getAppContext() == null){
                    SunToolkit.createNewAppContext();
                }
                Integer actualPort = server.getLocalPort();
                notifyPort(actualPort, getRappioPort(agentArgument));
            } catch (Exception e) {
                    System.err.println("Error starting remote server");
                    e.printStackTrace();
            }finally{
                    System.setOut(out);
            }
	}
        
        private static void notifyPort(final Integer portToNotify, final Integer serverPort) {
            Timer timer = new Timer("RobotJavaAgentPortNotifyingTimer", true);
            timer.schedule(new TimerTask() {

                @Override
                public void run() {
                    try {
                        waitForSwingComponent();
                        reportPort();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
                
                private void waitForSwingComponent() throws InterruptedException {
                    while(Window.getWindows().length == 0) {
                        Thread.sleep(100);
                    }
                }
                
                private void reportPort() throws IOException {
                    Socket echoSocket = new Socket(LOCALHOST, serverPort);
                    PrintWriter outToServer = new PrintWriter(echoSocket.getOutputStream(), true);
                    outToServer.write(portToNotify.toString()+":"+getName());
                    outToServer.close();
                    echoSocket.close();
                }

            }, 100);
        }
        
        private static String getName() {
            String name = System.getProperty("sun.java.command");
            if(name != null)
                return name;
            for(final Map.Entry<String, String> entry : System.getenv().entrySet())
            {
              if(entry.getKey().startsWith("JAVA_MAIN_CLASS"))
                return entry.getValue();
            }
{'fraction_non_alphanumeric': 0.09490333919156414, 'fraction_numerical': 0.006736965436438196, 'mean_word_length': 1.8741582491582491, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7150949', 'n_tokens_mistral': 1767, 'n_tokens_neox': 1659, 'n_words': 344}