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

Sat, 01 Dec 2007 00:00:00 +0000

author
duke
date
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1
9a66ca7c79fa
child 104
5e89c4ca637c
permissions
-rw-r--r--

Initial load

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

mercurial