RequestHandler.java revision 2593:035b01d356ee
1/* 2 * Copyright (c) 2014, 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 java.io.File; 28import java.io.IOException; 29import java.io.ObjectInputStream; 30import java.io.ObjectOutputStream; 31import java.io.PrintWriter; 32import java.io.StringWriter; 33import java.net.Socket; 34import java.net.URI; 35import java.util.List; 36import java.util.Set; 37 38import com.sun.tools.sjavac.Log; 39 40/** 41 * A RequestHandler handles requests performed over a socket. Specifically it 42 * - Reads the command string specifying which method is to be invoked 43 * - Reads the appropriate arguments 44 * - Delegates the actual invocation to the given sjavac implementation 45 * - Writes the result back to the socket output stream 46 * 47 * None of the work performed by this class is really bound by the CPU. It 48 * should be completely fine to have a large number of RequestHandlers active. 49 * To limit the number of concurrent compilations, use PooledSjavac. 50 * 51 * <p><b>This is NOT part of any supported API. 52 * If you write code that depends on this, you do so at your own risk. 53 * This code and its internal interfaces are subject to change or 54 * deletion without notice.</b> 55 */ 56public class RequestHandler implements Runnable { 57 58 private final Socket socket; 59 private final Sjavac sjavac; 60 61 public RequestHandler(Socket socket, Sjavac sjavac) { 62 this.socket = socket; 63 this.sjavac = sjavac; 64 } 65 66 @Override 67 public void run() { 68 try (ObjectOutputStream oout = new ObjectOutputStream(socket.getOutputStream()); 69 ObjectInputStream oin = new ObjectInputStream(socket.getInputStream())) { 70 String id = (String) oin.readObject(); 71 String cmd = (String) oin.readObject(); 72 Log.info("Handling request, id: " + id + " cmd: " + cmd); 73 switch (cmd) { 74 case SjavacServer.CMD_SYS_INFO: handleSysInfoRequest(oin, oout); break; 75 case SjavacServer.CMD_COMPILE: handleCompileRequest(oin, oout); break; 76 default: Log.error("Unknown command: " + cmd); 77 } 78 } catch (Exception ex) { 79 // Not much to be done at this point. The client side request 80 // code will most likely throw an IOException and the 81 // compilation will fail. 82 StringWriter sw = new StringWriter(); 83 ex.printStackTrace(new PrintWriter(sw)); 84 Log.error(sw.toString()); 85 } 86 } 87 88 private void handleSysInfoRequest(ObjectInputStream oin, 89 ObjectOutputStream oout) throws IOException { 90 oout.writeObject(sjavac.getSysInfo()); 91 oout.flush(); 92 } 93 94 @SuppressWarnings("unchecked") 95 private void handleCompileRequest(ObjectInputStream oin, 96 ObjectOutputStream oout) throws IOException { 97 try { 98 // Read request arguments 99 String protocolId = (String) oin.readObject(); 100 String invocationId = (String) oin.readObject(); 101 String[] args = (String[]) oin.readObject(); 102 List<File> explicitSources = (List<File>) oin.readObject(); 103 Set<URI> sourcesToCompile = (Set<URI>) oin.readObject(); 104 Set<URI> visibleSources = (Set<URI>) oin.readObject(); 105 106 // Perform compilation 107 CompilationResult cr = sjavac.compile(protocolId, 108 invocationId, 109 args, 110 explicitSources, 111 sourcesToCompile, 112 visibleSources); 113 // Write request response 114 oout.writeObject(cr); 115 oout.flush(); 116 } catch (ClassNotFoundException cnfe) { 117 throw new IOException(cnfe); 118 } 119 } 120} 121