001 /*
002 * Copyright 2005-2008 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package com.sun.tools.javac;
027
028 import java.io.*;
029 import java.net.*;
030 import java.util.*;
031 import java.util.concurrent.*;
032 import java.util.logging.Logger;
033 import javax.tools.*;
034
035 /**
036 * Java Compiler Server. Can be used to speed up a set of (small)
037 * compilation tasks by caching jar files between compilations.
038 *
039 * <p><b>This is NOT part of any API supported by Sun Microsystems.
040 * If you write code that depends on this, you do so at your own
041 * risk. This code and its internal interfaces are subject to change
042 * or deletion without notice.</b></p>
043 *
044 * @author Peter von der Ahé
045 * @since 1.6
046 */
047 class Server implements Runnable {
048 private final BufferedReader in;
049 private final OutputStream out;
050 private final boolean isSocket;
051 private static final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
052 private static Logger logger = Logger.getLogger("com.sun.tools.javac");
053 static class CwdFileManager extends ForwardingJavaFileManager<JavaFileManager> {
054 String cwd;
055 CwdFileManager(JavaFileManager fileManager) {
056 super(fileManager);
057 }
058 String getAbsoluteName(String name) {
059 if (new File(name).isAbsolute()) {
060 return name;
061 } else {
062 return new File(cwd,name).getPath();
063 }
064 }
065 // public JavaFileObject getFileForInput(String name)
066 // throws IOException
067 // {
068 // return super.getFileForInput(getAbsoluteName(name));
069 // }
070 }
071 // static CwdFileManager fm = new CwdFileManager(tool.getStandardFileManager());
072 static StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
073 static {
074 // Use the same file manager for all compilations. This will
075 // cache jar files in the standard file manager. Use
076 // tool.getStandardFileManager().close() to release.
077 // FIXME tool.setFileManager(fm);
078 logger.setLevel(java.util.logging.Level.SEVERE);
079 }
080 private Server(BufferedReader in, OutputStream out, boolean isSocket) {
081 this.in = in;
082 this.out = out;
083 this.isSocket = isSocket;
084 }
085 private Server(BufferedReader in, OutputStream out) {
086 this(in, out, false);
087 }
088 private Server(Socket socket) throws IOException, UnsupportedEncodingException {
089 this(new BufferedReader(new InputStreamReader(socket.getInputStream(), "utf-8")),
090 socket.getOutputStream(),
091 true);
092 }
093 public void run() {
094 List<String> args = new ArrayList<String>();
095 int res = -1;
096 try {
097 String line = null;
098 try {
099 line = in.readLine();
100 } catch (IOException e) {
101 System.err.println(e.getLocalizedMessage());
102 System.exit(0);
103 line = null;
104 }
105 // fm.cwd=null;
106 String cwd = null;
107 while (line != null) {
108 if (line.startsWith("PWD:")) {
109 cwd = line.substring(4);
110 } else if (line.equals("END")) {
111 break;
112 } else if (!"-XDstdout".equals(line)) {
113 args.add(line);
114 }
115 try {
116 line = in.readLine();
117 } catch (IOException e) {
118 System.err.println(e.getLocalizedMessage());
119 System.exit(0);
120 line = null;
121 }
122 }
123 Iterable<File> path = cwd == null ? null : Arrays.<File>asList(new File(cwd));
124 // try { in.close(); } catch (IOException e) {}
125 long msec = System.currentTimeMillis();
126 try {
127 synchronized (tool) {
128 for (StandardLocation location : StandardLocation.values())
129 fm.setLocation(location, path);
130 res = compile(out, fm, args);
131 // FIXME res = tool.run((InputStream)null, null, out, args.toArray(new String[args.size()]));
132 }
133 } catch (Throwable ex) {
134 logger.log(java.util.logging.Level.SEVERE, args.toString(), ex);
135 PrintWriter p = new PrintWriter(out, true);
136 ex.printStackTrace(p);
137 p.flush();
138 }
139 if (res >= 3) {
140 logger.severe(String.format("problem: %s", args));
141 } else {
142 logger.info(String.format("success: %s", args));
143 }
144 // res = compile(args.toArray(new String[args.size()]), out);
145 msec -= System.currentTimeMillis();
146 logger.info(String.format("Real time: %sms", -msec));
147 } finally {
148 if (!isSocket) {
149 try { in.close(); } catch (IOException e) {}
150 }
151 try {
152 out.write(String.format("EXIT: %s%n", res).getBytes());
153 } catch (IOException ex) {
154 logger.log(java.util.logging.Level.SEVERE, args.toString(), ex);
155 }
156 try {
157 out.flush();
158 out.close();
159 } catch (IOException ex) {
160 logger.log(java.util.logging.Level.SEVERE, args.toString(), ex);
161 }
162 logger.info(String.format("EXIT: %s", res));
163 }
164 }
165 public static void main(String... args) throws FileNotFoundException {
166 if (args.length == 2) {
167 for (;;) {
168 throw new UnsupportedOperationException("TODO");
169 // BufferedReader in = new BufferedReader(new FileReader(args[0]));
170 // PrintWriter out = new PrintWriter(args[1]);
171 // new Server(in, out).run();
172 // System.out.flush();
173 // System.err.flush();
174 }
175 } else {
176 ExecutorService pool = Executors.newCachedThreadPool();
177 try
178 {
179 ServerSocket socket = new ServerSocket(0xcafe, -1, null);
180 for (;;) {
181 pool.execute(new Server(socket.accept()));
182 }
183 }
184 catch (IOException e) {
185 System.err.format("Error: %s%n", e.getLocalizedMessage());
186 pool.shutdown();
187 }
188 }
189 }
190
191 private int compile(OutputStream out, StandardJavaFileManager fm, List<String> args) {
192 // FIXME parse args and use getTask
193 // System.err.println("Running " + args);
194 return tool.run(null, null, out, args.toArray(new String[args.size()]));
195 }
196 }