RequestHandler.java revision 3022:5ba1a29a0eb0
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 */ 25package com.sun.tools.sjavac.server; 26 27import static com.sun.tools.sjavac.server.SjavacServer.LINE_TYPE_RC; 28import static com.sun.tools.sjavac.server.SjavacServer.LINE_TYPE_STDERR; 29import static com.sun.tools.sjavac.server.SjavacServer.LINE_TYPE_STDOUT; 30 31import java.io.BufferedReader; 32import java.io.InputStreamReader; 33import java.io.PrintWriter; 34import java.io.StringWriter; 35import java.io.Writer; 36import java.net.Socket; 37 38import com.sun.tools.sjavac.AutoFlushWriter; 39import com.sun.tools.sjavac.Log; 40 41 42/** 43 * A RequestHandler handles requests performed over a socket. Specifically it 44 * - Reads the command string specifying which method is to be invoked 45 * - Reads the appropriate arguments 46 * - Delegates the actual invocation to the given sjavac implementation 47 * - Writes the result back to the socket output stream 48 * 49 * None of the work performed by this class is really bound by the CPU. It 50 * should be completely fine to have a large number of RequestHandlers active. 51 * To limit the number of concurrent compilations, use PooledSjavac. 52 * 53 * <p><b>This is NOT part of any supported API. 54 * If you write code that depends on this, you do so at your own risk. 55 * This code and its internal interfaces are subject to change or 56 * deletion without notice.</b> 57 */ 58public class RequestHandler implements Runnable { 59 60 private final Socket socket; 61 private final Sjavac sjavac; 62 63 public RequestHandler(Socket socket, Sjavac sjavac) { 64 this.socket = socket; 65 this.sjavac = sjavac; 66 } 67 68 @Override 69 public void run() { 70 try (BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 71 PrintWriter out = new PrintWriter(socket.getOutputStream(), true)) { 72 73 // Read argument array 74 int n = Integer.parseInt(in.readLine()); 75 String[] args = new String[n]; 76 for (int i = 0; i < n; i++) { 77 args[i] = in.readLine(); 78 } 79 80 // Perform compilation 81 Writer stdout = new LinePrefixFilterWriter(new AutoFlushWriter(out), LINE_TYPE_STDOUT + ":"); 82 Writer stderr = new LinePrefixFilterWriter(new AutoFlushWriter(out), LINE_TYPE_STDERR + ":"); 83 int rc = sjavac.compile(args, stdout, stderr); 84 stdout.flush(); 85 stderr.flush(); 86 87 // Send return code back to client 88 out.println(LINE_TYPE_RC + ":" + rc); 89 90 } catch (Exception ex) { 91 // Not much to be done at this point. The client side request 92 // code will most likely throw an IOException and the 93 // compilation will fail. 94 StringWriter sw = new StringWriter(); 95 ex.printStackTrace(new PrintWriter(sw)); 96 Log.error(sw.toString()); 97 } 98 } 99} 100