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

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

mercurial