RequestHandler.java revision 3170:dc017a37aac5
1/*
2 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.tools.sjavac.server;
27
28import static com.sun.tools.sjavac.server.SjavacServer.LINE_TYPE_RC;
29import static com.sun.tools.sjavac.server.SjavacServer.LINE_TYPE_STDERR;
30import static com.sun.tools.sjavac.server.SjavacServer.LINE_TYPE_STDOUT;
31
32import java.io.BufferedReader;
33import java.io.InputStreamReader;
34import java.io.PrintWriter;
35import java.io.StringWriter;
36import java.io.Writer;
37import java.net.Socket;
38
39import com.sun.tools.sjavac.AutoFlushWriter;
40import com.sun.tools.sjavac.Log;
41
42
43/**
44 * A RequestHandler handles requests performed over a socket. Specifically it
45 *  - Reads the command string specifying which method is to be invoked
46 *  - Reads the appropriate arguments
47 *  - Delegates the actual invocation to the given sjavac implementation
48 *  - Writes the result back to the socket output stream
49 *
50 * None of the work performed by this class is really bound by the CPU. It
51 * should be completely fine to have a large number of RequestHandlers active.
52 * To limit the number of concurrent compilations, use PooledSjavac.
53 *
54 *  <p><b>This is NOT part of any supported API.
55 *  If you write code that depends on this, you do so at your own risk.
56 *  This code and its internal interfaces are subject to change or
57 *  deletion without notice.</b>
58 */
59public class RequestHandler implements Runnable {
60
61    private final Socket socket;
62    private final Sjavac sjavac;
63
64    public RequestHandler(Socket socket, Sjavac sjavac) {
65        this.socket = socket;
66        this.sjavac = sjavac;
67    }
68
69    @Override
70    public void run() {
71        try (BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
72             PrintWriter out = new PrintWriter(socket.getOutputStream(), true)) {
73
74            // Read argument array
75            int n = Integer.parseInt(in.readLine());
76            String[] args = new String[n];
77            for (int i = 0; i < n; i++) {
78                args[i] = in.readLine();
79            }
80
81            // Perform compilation
82            Writer stdout = new LinePrefixFilterWriter(new AutoFlushWriter(out), LINE_TYPE_STDOUT + ":");
83            Writer stderr = new LinePrefixFilterWriter(new AutoFlushWriter(out), LINE_TYPE_STDERR + ":");
84            int rc = sjavac.compile(args, stdout, stderr);
85            stdout.flush();
86            stderr.flush();
87
88            // Send return code back to client
89            out.println(LINE_TYPE_RC + ":" + rc);
90
91        } catch (Exception ex) {
92            // Not much to be done at this point. The client side request
93            // code will most likely throw an IOException and the
94            // compilation will fail.
95            StringWriter sw = new StringWriter();
96            ex.printStackTrace(new PrintWriter(sw));
97            Log.error(sw.toString());
98        }
99    }
100}
101