src/share/classes/com/sun/tools/javac/Server.java

Tue, 25 May 2010 15:54:51 -0700

author
ohair
date
Tue, 25 May 2010 15:54:51 -0700
changeset 554
9d9f26857129
parent 117
24a47c3062fe
child 581
f2fdd52e4e87
permissions
-rw-r--r--

6943119: Rebrand source copyright notices
Reviewed-by: darcy

duke@1 1 /*
ohair@554 2 * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac;
duke@1 27
duke@1 28 import java.io.*;
duke@1 29 import java.net.*;
duke@1 30 import java.util.*;
duke@1 31 import java.util.concurrent.*;
duke@1 32 import java.util.logging.Logger;
duke@1 33 import javax.tools.*;
duke@1 34
duke@1 35 /**
duke@1 36 * Java Compiler Server. Can be used to speed up a set of (small)
duke@1 37 * compilation tasks by caching jar files between compilations.
duke@1 38 *
duke@1 39 * <p><b>This is NOT part of any API supported by Sun Microsystems.
duke@1 40 * If you write code that depends on this, you do so at your own
duke@1 41 * risk. This code and its internal interfaces are subject to change
duke@1 42 * or deletion without notice.</b></p>
duke@1 43 *
duke@1 44 * @author Peter von der Ah&eacute;
duke@1 45 * @since 1.6
duke@1 46 */
duke@1 47 class Server implements Runnable {
duke@1 48 private final BufferedReader in;
duke@1 49 private final OutputStream out;
duke@1 50 private final boolean isSocket;
duke@1 51 private static final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
duke@1 52 private static Logger logger = Logger.getLogger("com.sun.tools.javac");
duke@1 53 static class CwdFileManager extends ForwardingJavaFileManager<JavaFileManager> {
duke@1 54 String cwd;
duke@1 55 CwdFileManager(JavaFileManager fileManager) {
duke@1 56 super(fileManager);
duke@1 57 }
duke@1 58 String getAbsoluteName(String name) {
duke@1 59 if (new File(name).isAbsolute()) {
duke@1 60 return name;
duke@1 61 } else {
duke@1 62 return new File(cwd,name).getPath();
duke@1 63 }
duke@1 64 }
duke@1 65 // public JavaFileObject getFileForInput(String name)
duke@1 66 // throws IOException
duke@1 67 // {
duke@1 68 // return super.getFileForInput(getAbsoluteName(name));
duke@1 69 // }
duke@1 70 }
duke@1 71 // static CwdFileManager fm = new CwdFileManager(tool.getStandardFileManager());
duke@1 72 static StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
duke@1 73 static {
duke@1 74 // Use the same file manager for all compilations. This will
duke@1 75 // cache jar files in the standard file manager. Use
duke@1 76 // tool.getStandardFileManager().close() to release.
duke@1 77 // FIXME tool.setFileManager(fm);
duke@1 78 logger.setLevel(java.util.logging.Level.SEVERE);
duke@1 79 }
duke@1 80 private Server(BufferedReader in, OutputStream out, boolean isSocket) {
duke@1 81 this.in = in;
duke@1 82 this.out = out;
duke@1 83 this.isSocket = isSocket;
duke@1 84 }
duke@1 85 private Server(BufferedReader in, OutputStream out) {
duke@1 86 this(in, out, false);
duke@1 87 }
duke@1 88 private Server(Socket socket) throws IOException, UnsupportedEncodingException {
duke@1 89 this(new BufferedReader(new InputStreamReader(socket.getInputStream(), "utf-8")),
duke@1 90 socket.getOutputStream(),
duke@1 91 true);
duke@1 92 }
duke@1 93 public void run() {
duke@1 94 List<String> args = new ArrayList<String>();
duke@1 95 int res = -1;
duke@1 96 try {
duke@1 97 String line = null;
duke@1 98 try {
duke@1 99 line = in.readLine();
duke@1 100 } catch (IOException e) {
duke@1 101 System.err.println(e.getLocalizedMessage());
duke@1 102 System.exit(0);
duke@1 103 line = null;
duke@1 104 }
duke@1 105 // fm.cwd=null;
duke@1 106 String cwd = null;
duke@1 107 while (line != null) {
duke@1 108 if (line.startsWith("PWD:")) {
duke@1 109 cwd = line.substring(4);
duke@1 110 } else if (line.equals("END")) {
duke@1 111 break;
duke@1 112 } else if (!"-XDstdout".equals(line)) {
duke@1 113 args.add(line);
duke@1 114 }
duke@1 115 try {
duke@1 116 line = in.readLine();
duke@1 117 } catch (IOException e) {
duke@1 118 System.err.println(e.getLocalizedMessage());
duke@1 119 System.exit(0);
duke@1 120 line = null;
duke@1 121 }
duke@1 122 }
duke@1 123 Iterable<File> path = cwd == null ? null : Arrays.<File>asList(new File(cwd));
duke@1 124 // try { in.close(); } catch (IOException e) {}
duke@1 125 long msec = System.currentTimeMillis();
duke@1 126 try {
duke@1 127 synchronized (tool) {
duke@1 128 for (StandardLocation location : StandardLocation.values())
duke@1 129 fm.setLocation(location, path);
duke@1 130 res = compile(out, fm, args);
duke@1 131 // FIXME res = tool.run((InputStream)null, null, out, args.toArray(new String[args.size()]));
duke@1 132 }
duke@1 133 } catch (Throwable ex) {
duke@1 134 logger.log(java.util.logging.Level.SEVERE, args.toString(), ex);
duke@1 135 PrintWriter p = new PrintWriter(out, true);
duke@1 136 ex.printStackTrace(p);
duke@1 137 p.flush();
duke@1 138 }
duke@1 139 if (res >= 3) {
duke@1 140 logger.severe(String.format("problem: %s", args));
duke@1 141 } else {
duke@1 142 logger.info(String.format("success: %s", args));
duke@1 143 }
duke@1 144 // res = compile(args.toArray(new String[args.size()]), out);
duke@1 145 msec -= System.currentTimeMillis();
duke@1 146 logger.info(String.format("Real time: %sms", -msec));
duke@1 147 } finally {
duke@1 148 if (!isSocket) {
duke@1 149 try { in.close(); } catch (IOException e) {}
duke@1 150 }
duke@1 151 try {
duke@1 152 out.write(String.format("EXIT: %s%n", res).getBytes());
duke@1 153 } catch (IOException ex) {
duke@1 154 logger.log(java.util.logging.Level.SEVERE, args.toString(), ex);
duke@1 155 }
duke@1 156 try {
duke@1 157 out.flush();
duke@1 158 out.close();
duke@1 159 } catch (IOException ex) {
duke@1 160 logger.log(java.util.logging.Level.SEVERE, args.toString(), ex);
duke@1 161 }
duke@1 162 logger.info(String.format("EXIT: %s", res));
duke@1 163 }
duke@1 164 }
duke@1 165 public static void main(String... args) throws FileNotFoundException {
duke@1 166 if (args.length == 2) {
duke@1 167 for (;;) {
duke@1 168 throw new UnsupportedOperationException("TODO");
duke@1 169 // BufferedReader in = new BufferedReader(new FileReader(args[0]));
duke@1 170 // PrintWriter out = new PrintWriter(args[1]);
duke@1 171 // new Server(in, out).run();
duke@1 172 // System.out.flush();
duke@1 173 // System.err.flush();
duke@1 174 }
duke@1 175 } else {
duke@1 176 ExecutorService pool = Executors.newCachedThreadPool();
duke@1 177 try
duke@1 178 {
duke@1 179 ServerSocket socket = new ServerSocket(0xcafe, -1, null);
duke@1 180 for (;;) {
duke@1 181 pool.execute(new Server(socket.accept()));
duke@1 182 }
duke@1 183 }
duke@1 184 catch (IOException e) {
duke@1 185 System.err.format("Error: %s%n", e.getLocalizedMessage());
duke@1 186 pool.shutdown();
duke@1 187 }
duke@1 188 }
duke@1 189 }
duke@1 190
duke@1 191 private int compile(OutputStream out, StandardJavaFileManager fm, List<String> args) {
duke@1 192 // FIXME parse args and use getTask
duke@1 193 // System.err.println("Running " + args);
duke@1 194 return tool.run(null, null, out, args.toArray(new String[args.size()]));
duke@1 195 }
duke@1 196 }

mercurial