RequestHandler.java revision 3012:adba44f6b471
1210502Ssyrinx/*
2330449Seadler * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
3330449Seadler * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4210502Ssyrinx *
5210502Ssyrinx * This code is free software; you can redistribute it and/or modify it
6310901Sngie * under the terms of the GNU General Public License version 2 only, as
7210502Ssyrinx * published by the Free Software Foundation.  Oracle designates this
8210502Ssyrinx * particular file as subject to the "Classpath" exception as provided
9210502Ssyrinx * by Oracle in the LICENSE file that accompanied this code.
10210502Ssyrinx *
11210502Ssyrinx * This code is distributed in the hope that it will be useful, but WITHOUT
12210502Ssyrinx * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13210502Ssyrinx * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14210502Ssyrinx * version 2 for more details (a copy is included in the LICENSE file that
15210502Ssyrinx * accompanied this code).
16210502Ssyrinx *
17210502Ssyrinx * You should have received a copy of the GNU General Public License version
18210502Ssyrinx * 2 along with this work; if not, write to the Free Software Foundation,
19210502Ssyrinx * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20210502Ssyrinx *
21210502Ssyrinx * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22210502Ssyrinx * or visit www.oracle.com if you need additional information or have any
23210502Ssyrinx * questions.
24210502Ssyrinx */
25210502Ssyrinxpackage com.sun.tools.sjavac.server;
26210502Ssyrinx
27210502Ssyrinximport java.io.IOException;
28210502Ssyrinximport java.io.ObjectInputStream;
29210502Ssyrinximport java.io.ObjectOutputStream;
30210502Ssyrinximport java.io.PrintWriter;
31210502Ssyrinximport java.io.StringWriter;
32210502Ssyrinximport java.net.Socket;
33210502Ssyrinx
34210502Ssyrinximport com.sun.tools.sjavac.Log;
35210502Ssyrinx
36210502Ssyrinx/**
37210502Ssyrinx * A RequestHandler handles requests performed over a socket. Specifically it
38210502Ssyrinx *  - Reads the command string specifying which method is to be invoked
39210502Ssyrinx *  - Reads the appropriate arguments
40210502Ssyrinx *  - Delegates the actual invocation to the given sjavac implementation
41210502Ssyrinx *  - Writes the result back to the socket output stream
42210502Ssyrinx *
43210502Ssyrinx * None of the work performed by this class is really bound by the CPU. It
44210502Ssyrinx * should be completely fine to have a large number of RequestHandlers active.
45210502Ssyrinx * To limit the number of concurrent compilations, use PooledSjavac.
46210502Ssyrinx *
47210502Ssyrinx *  <p><b>This is NOT part of any supported API.
48210502Ssyrinx *  If you write code that depends on this, you do so at your own risk.
49210502Ssyrinx *  This code and its internal interfaces are subject to change or
50210502Ssyrinx *  deletion without notice.</b>
51210502Ssyrinx */
52210502Ssyrinxpublic class RequestHandler implements Runnable {
53210502Ssyrinx
54210502Ssyrinx    private final Socket socket;
55210502Ssyrinx    private final Sjavac sjavac;
56210502Ssyrinx
57210502Ssyrinx    public RequestHandler(Socket socket, Sjavac sjavac) {
58210502Ssyrinx        this.socket = socket;
59210502Ssyrinx        this.sjavac = sjavac;
60210502Ssyrinx    }
61210502Ssyrinx
62210502Ssyrinx    @Override
63210502Ssyrinx    public void run() {
64210502Ssyrinx        try (ObjectOutputStream oout = new ObjectOutputStream(socket.getOutputStream());
65210502Ssyrinx             ObjectInputStream oin = new ObjectInputStream(socket.getInputStream())) {
66210502Ssyrinx            String id = (String) oin.readObject();
67210502Ssyrinx            String cmd = (String) oin.readObject();
68210502Ssyrinx            Log.info("Handling request, id: " + id + " cmd: " + cmd);
69210502Ssyrinx            switch (cmd) {
70210502Ssyrinx            case SjavacServer.CMD_COMPILE: handleCompileRequest(oin, oout); break;
71210502Ssyrinx            default: Log.error("Unknown command: " + cmd);
72210502Ssyrinx            }
73210502Ssyrinx        } catch (Exception ex) {
74210502Ssyrinx            // Not much to be done at this point. The client side request
75210502Ssyrinx            // code will most likely throw an IOException and the
76210502Ssyrinx            // compilation will fail.
77210502Ssyrinx            StringWriter sw = new StringWriter();
78210502Ssyrinx            ex.printStackTrace(new PrintWriter(sw));
79210502Ssyrinx            Log.error(sw.toString());
80210502Ssyrinx        }
81210502Ssyrinx    }
82210502Ssyrinx
83210502Ssyrinx    private void handleCompileRequest(ObjectInputStream oin,
84210502Ssyrinx                                      ObjectOutputStream oout) throws IOException {
85210502Ssyrinx        try {
86210502Ssyrinx            // Read request arguments
87210502Ssyrinx            String[] args = (String[]) oin.readObject();
88210502Ssyrinx
89210502Ssyrinx            // Perform compilation
90210502Ssyrinx            CompilationResult cr = sjavac.compile(args);
91210502Ssyrinx
92210502Ssyrinx            // Write request response
93210502Ssyrinx            oout.writeObject(cr);
94210502Ssyrinx            oout.flush();
95210502Ssyrinx        } catch (ClassNotFoundException cnfe) {
96210502Ssyrinx            throw new IOException(cnfe);
97210502Ssyrinx        }
98210502Ssyrinx    }
99210502Ssyrinx}
100210502Ssyrinx