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