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