Search in sources :

Example 1 with LineNumberReader

use of java.io.LineNumberReader in project jetty.project by eclipse.

the class JettyStopTask method execute.

/** 
     * @see org.apache.tools.ant.Task#execute()
     */
public void execute() throws BuildException {
    try {
        Socket s = new Socket(InetAddress.getByName("127.0.0.1"), stopPort);
        if (stopWait > 0)
            s.setSoTimeout(stopWait * 1000);
        try {
            OutputStream out = s.getOutputStream();
            out.write((stopKey + "\r\nstop\r\n").getBytes());
            out.flush();
            if (stopWait > 0) {
                TaskLog.log("Waiting" + (stopWait > 0 ? (" " + stopWait + "sec") : "") + " for jetty to stop");
                LineNumberReader lin = new LineNumberReader(new InputStreamReader(s.getInputStream()));
                String response = lin.readLine();
                if ("Stopped".equals(response))
                    System.err.println("Stopped");
            }
        } finally {
            s.close();
        }
    } catch (ConnectException e) {
        TaskLog.log("Jetty not running!");
    } catch (Exception e) {
        TaskLog.log(e.getMessage());
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) OutputStream(java.io.OutputStream) Socket(java.net.Socket) ConnectException(java.net.ConnectException) BuildException(org.apache.tools.ant.BuildException) LineNumberReader(java.io.LineNumberReader) ConnectException(java.net.ConnectException)

Example 2 with LineNumberReader

use of java.io.LineNumberReader in project jetty.project by eclipse.

the class HttpServerTestBase method testPipeline.

@Test
public void testPipeline() throws Exception {
    configureServer(new HelloWorldHandler());
    //for (int pipeline=1;pipeline<32;pipeline++)
    for (int pipeline = 1; pipeline < 32; pipeline++) {
        try (Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort())) {
            client.setSoTimeout(5000);
            OutputStream os = client.getOutputStream();
            String request = "";
            for (int i = 1; i < pipeline; i++) request += "GET /data?writes=1&block=16&id=" + i + " HTTP/1.1\r\n" + "host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\r\n" + "user-agent: testharness/1.0 (blah foo/bar)\r\n" + "accept-encoding: nothing\r\n" + "cookie: aaa=1234567890\r\n" + "\r\n";
            request += "GET /data?writes=1&block=16 HTTP/1.1\r\n" + "host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\r\n" + "user-agent: testharness/1.0 (blah foo/bar)\r\n" + "accept-encoding: nothing\r\n" + "cookie: aaa=bbbbbb\r\n" + "Connection: close\r\n" + "\r\n";
            os.write(request.getBytes());
            os.flush();
            LineNumberReader in = new LineNumberReader(new InputStreamReader(client.getInputStream()));
            String line = in.readLine();
            int count = 0;
            while (line != null) {
                if ("HTTP/1.1 200 OK".equals(line))
                    count++;
                line = in.readLine();
            }
            assertEquals(pipeline, count);
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) EndPoint(org.eclipse.jetty.io.EndPoint) Socket(java.net.Socket) LineNumberReader(java.io.LineNumberReader) Test(org.junit.Test)

Example 3 with LineNumberReader

use of java.io.LineNumberReader in project jetty.project by eclipse.

the class ShutdownMonitorTest method stop.

public void stop(String command, int port, String key, boolean check) throws Exception {
    System.out.printf("Attempting to send " + command + " to localhost:%d (%b)%n", port, check);
    try (Socket s = new Socket(InetAddress.getByName("127.0.0.1"), port)) {
        // send stop command
        try (OutputStream out = s.getOutputStream()) {
            out.write((key + "\r\n" + command + "\r\n").getBytes());
            out.flush();
            if (check) {
                // check for stop confirmation
                LineNumberReader lin = new LineNumberReader(new InputStreamReader(s.getInputStream()));
                String response;
                if ((response = lin.readLine()) != null)
                    assertEquals("Stopped", response);
                else
                    throw new IllegalStateException("No stop confirmation");
            }
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) OutputStream(java.io.OutputStream) Socket(java.net.Socket) LineNumberReader(java.io.LineNumberReader)

Example 4 with LineNumberReader

use of java.io.LineNumberReader in project jetty.project by eclipse.

the class Main method stop.

public void stop(String host, int port, String key, int timeout) {
    if (host == null || host.length() == 0) {
        host = "127.0.0.1";
    }
    try {
        if ((port <= 0) || (port > 65535)) {
            System.err.println("STOP.PORT property must be specified with a valid port number");
            usageExit(ERR_BAD_STOP_PROPS);
        }
        if (key == null) {
            key = "";
            System.err.println("STOP.KEY property must be specified");
            System.err.println("Using empty key");
        }
        try (Socket s = new Socket(InetAddress.getByName(host), port)) {
            if (timeout > 0) {
                s.setSoTimeout(timeout * 1000);
            }
            try (OutputStream out = s.getOutputStream()) {
                out.write((key + "\r\nstop\r\n").getBytes());
                out.flush();
                if (timeout > 0) {
                    StartLog.info("Waiting %,d seconds for jetty to stop%n", timeout);
                    LineNumberReader lin = new LineNumberReader(new InputStreamReader(s.getInputStream()));
                    String response;
                    while ((response = lin.readLine()) != null) {
                        StartLog.debug("Received \"%s\"", response);
                        if ("Stopped".equals(response)) {
                            StartLog.warn("Server reports itself as Stopped");
                        }
                    }
                }
            }
        }
    } catch (SocketTimeoutException e) {
        StartLog.warn("Timed out waiting for stop confirmation");
        System.exit(ERR_UNKNOWN);
    } catch (ConnectException e) {
        usageExit(e, ERR_NOT_STOPPED, jsvcStartArgs.isTestingModeEnabled());
    } catch (Exception e) {
        usageExit(e, ERR_UNKNOWN, jsvcStartArgs.isTestingModeEnabled());
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) InputStreamReader(java.io.InputStreamReader) OutputStream(java.io.OutputStream) Socket(java.net.Socket) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) LineNumberReader(java.io.LineNumberReader) ConnectException(java.net.ConnectException)

Example 5 with LineNumberReader

use of java.io.LineNumberReader in project elasticsearch by elastic.

the class Definition method addStructs.

/** adds classes from definition. returns hierarchy */
private Map<String, List<String>> addStructs() {
    final Map<String, List<String>> hierarchy = new HashMap<>();
    for (String file : DEFINITION_FILES) {
        int currentLine = -1;
        try {
            try (InputStream stream = Definition.class.getResourceAsStream(file);
                LineNumberReader reader = new LineNumberReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) {
                String line = null;
                while ((line = reader.readLine()) != null) {
                    currentLine = reader.getLineNumber();
                    line = line.trim();
                    if (line.length() == 0 || line.charAt(0) == '#') {
                        continue;
                    }
                    if (line.startsWith("class ")) {
                        String[] elements = line.split(" ");
                        assert elements[2].equals("->") : "Invalid struct definition [" + String.join(" ", elements) + "]";
                        if (elements.length == 7) {
                            hierarchy.put(elements[1], Arrays.asList(elements[5].split(",")));
                        } else {
                            assert elements.length == 5 : "Invalid struct definition [" + String.join(" ", elements) + "]";
                        }
                        String className = elements[1];
                        String javaPeer = elements[3];
                        final Class<?> javaClazz;
                        switch(javaPeer) {
                            case "void":
                                javaClazz = void.class;
                                break;
                            case "boolean":
                                javaClazz = boolean.class;
                                break;
                            case "byte":
                                javaClazz = byte.class;
                                break;
                            case "short":
                                javaClazz = short.class;
                                break;
                            case "char":
                                javaClazz = char.class;
                                break;
                            case "int":
                                javaClazz = int.class;
                                break;
                            case "long":
                                javaClazz = long.class;
                                break;
                            case "float":
                                javaClazz = float.class;
                                break;
                            case "double":
                                javaClazz = double.class;
                                break;
                            default:
                                javaClazz = Class.forName(javaPeer);
                                break;
                        }
                        addStruct(className, javaClazz);
                    }
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("error in " + file + ", line: " + currentLine, e);
        }
    }
    return hierarchy;
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) List(java.util.List) LineNumberReader(java.io.LineNumberReader)

Aggregations

LineNumberReader (java.io.LineNumberReader)401 IOException (java.io.IOException)203 InputStreamReader (java.io.InputStreamReader)167 FileReader (java.io.FileReader)102 File (java.io.File)79 InputStream (java.io.InputStream)64 StringReader (java.io.StringReader)64 ArrayList (java.util.ArrayList)54 FileInputStream (java.io.FileInputStream)33 BufferedReader (java.io.BufferedReader)27 PrintWriter (java.io.PrintWriter)25 HashMap (java.util.HashMap)22 FileNotFoundException (java.io.FileNotFoundException)20 BufferedWriter (java.io.BufferedWriter)16 FileWriter (java.io.FileWriter)16 Pattern (java.util.regex.Pattern)16 Test (org.junit.jupiter.api.Test)16 ByteArrayInputStream (java.io.ByteArrayInputStream)15 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 Reader (java.io.Reader)14