Home » openjdk-7 » com.sun.tools » javac » [javadoc | source]

    1   /*
    2    * Copyright (c) 2005, 2008, Oracle and/or its affiliates. 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.  Oracle designates this
    8    * particular file as subject to the "Classpath" exception as provided
    9    * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   22    * or visit www.oracle.com if you need additional information or have any
   23    * questions.
   24    */
   25   
   26   package com.sun.tools.javac;
   27   
   28   import java.io;
   29   import java.net;
   30   import java.util;
   31   import java.util.concurrent;
   32   import java.util.logging.Logger;
   33   import javax.tools;
   34   
   35   /**
   36    * Java Compiler Server.  Can be used to speed up a set of (small)
   37    * compilation tasks by caching jar files between compilations.
   38    *
   39    * <p><b>This is NOT part of any supported API.
   40    * If you write code that depends on this, you do so at your own
   41    * risk.  This code and its internal interfaces are subject to change
   42    * or deletion without notice.</b></p>
   43    *
   44    * @author Peter von der Ah&eacute;
   45    * @since 1.6
   46    */
   47   class Server implements Runnable {
   48       private final BufferedReader in;
   49       private final OutputStream out;
   50       private final boolean isSocket;
   51       private static final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
   52       private static Logger logger = Logger.getLogger("com.sun.tools.javac");
   53       static class CwdFileManager extends ForwardingJavaFileManager<JavaFileManager> {
   54           String cwd;
   55           CwdFileManager(JavaFileManager fileManager) {
   56               super(fileManager);
   57           }
   58           String getAbsoluteName(String name) {
   59               if (new File(name).isAbsolute()) {
   60                   return name;
   61               } else {
   62                   return new File(cwd,name).getPath();
   63               }
   64           }
   65   //      public JavaFileObject getFileForInput(String name)
   66   //          throws IOException
   67   //      {
   68   //          return super.getFileForInput(getAbsoluteName(name));
   69   //      }
   70       }
   71       // static CwdFileManager fm = new CwdFileManager(tool.getStandardFileManager());
   72       static StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
   73       static {
   74           // Use the same file manager for all compilations.  This will
   75           // cache jar files in the standard file manager.  Use
   76           // tool.getStandardFileManager().close() to release.
   77           // FIXME tool.setFileManager(fm);
   78           logger.setLevel(java.util.logging.Level.SEVERE);
   79       }
   80       private Server(BufferedReader in, OutputStream out, boolean isSocket) {
   81           this.in = in;
   82           this.out = out;
   83           this.isSocket = isSocket;
   84       }
   85       private Server(BufferedReader in, OutputStream out) {
   86           this(in, out, false);
   87       }
   88       private Server(Socket socket) throws IOException, UnsupportedEncodingException {
   89           this(new BufferedReader(new InputStreamReader(socket.getInputStream(), "utf-8")),
   90                socket.getOutputStream(),
   91                true);
   92       }
   93       public void run() {
   94           List<String> args = new ArrayList<String>();
   95           int res = -1;
   96           try {
   97               String line = null;
   98               try {
   99                   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   }

Home » openjdk-7 » com.sun.tools » javac » [javadoc | source]