langtools/src/jdk.compiler/share/classes/com/sun/tools/sjavac/comp/JavacServiceImpl.java
changeset 26110 6ce251a87137
parent 26086 cfcea23d2d19
parent 26109 0430c63da650
child 26111 915a71858aef
equal deleted inserted replaced
26086:cfcea23d2d19 26110:6ce251a87137
     1 /*
       
     2  * Copyright (c) 2014, 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.sjavac.comp;
       
    27 
       
    28 import java.io.File;
       
    29 import java.io.PrintWriter;
       
    30 import java.io.StringWriter;
       
    31 import java.net.URI;
       
    32 import java.util.Arrays;
       
    33 import java.util.List;
       
    34 import java.util.Set;
       
    35 
       
    36 import javax.tools.JavaCompiler.CompilationTask;
       
    37 import javax.tools.JavaFileObject;
       
    38 import javax.tools.StandardJavaFileManager;
       
    39 
       
    40 import com.sun.tools.javac.api.JavacTaskImpl;
       
    41 import com.sun.tools.javac.api.JavacTool;
       
    42 import com.sun.tools.javac.util.Context;
       
    43 import com.sun.tools.javac.util.ListBuffer;
       
    44 import com.sun.tools.sjavac.Util;
       
    45 import com.sun.tools.sjavac.server.CompilationResult;
       
    46 import com.sun.tools.sjavac.server.JavacServer;
       
    47 import com.sun.tools.sjavac.server.JavacService;
       
    48 import com.sun.tools.sjavac.server.SysInfo;
       
    49 
       
    50 public class JavacServiceImpl implements JavacService {
       
    51 
       
    52     JavacServer javacServer;
       
    53     private ThreadLocal<Boolean> forcedExit;
       
    54 
       
    55     public JavacServiceImpl(JavacServer javacServer) {
       
    56         this.javacServer = javacServer;
       
    57 
       
    58     }
       
    59 
       
    60     public void logError(String msg) {
       
    61 //        stderr.println(msg);
       
    62         forcedExit.set(true);
       
    63     }
       
    64 
       
    65     @Override
       
    66     public SysInfo getSysInfo() {
       
    67         return new SysInfo(Runtime.getRuntime().availableProcessors(),
       
    68                            Runtime.getRuntime().maxMemory());
       
    69     }
       
    70 
       
    71     @Override
       
    72     public CompilationResult compile(String protocolId,
       
    73                                      String invocationId,
       
    74                                      String[] args,
       
    75                                      List<File> explicitSources,
       
    76                                      Set<URI> sourcesToCompile,
       
    77                                      Set<URI> visibleSources) {
       
    78 
       
    79         JavacTool compiler = JavacTool.create();
       
    80         StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
       
    81         SmartFileManager smartFileManager = new SmartFileManager(fileManager);
       
    82         Context context = new Context();
       
    83         ResolveWithDeps.preRegister(context);
       
    84         AttrWithDeps.preRegister(context);
       
    85         JavaCompilerWithDeps.preRegister(context, this);
       
    86 
       
    87         // Now setup the actual compilation....
       
    88         CompilationResult compilationResult = new CompilationResult(0);
       
    89 
       
    90         // First deal with explicit source files on cmdline and in at file.
       
    91         ListBuffer<JavaFileObject> compilationUnits = new ListBuffer<>();
       
    92         for (JavaFileObject i : fileManager.getJavaFileObjectsFromFiles(explicitSources)) {
       
    93             compilationUnits.append(i);
       
    94         }
       
    95         // Now deal with sources supplied as source_to_compile.
       
    96         ListBuffer<File> sourcesToCompileFiles = new ListBuffer<>();
       
    97         for (URI u : sourcesToCompile) {
       
    98             sourcesToCompileFiles.append(new File(u));
       
    99         }
       
   100         for (JavaFileObject i : fileManager.getJavaFileObjectsFromFiles(sourcesToCompileFiles)) {
       
   101             compilationUnits.append(i);
       
   102         }
       
   103         // Log the options to be used.
       
   104         StringBuilder options = new StringBuilder();
       
   105         for (String s : args) {
       
   106             options.append(">").append(s).append("< ");
       
   107         }
       
   108         javacServer.log(protocolId+" <"+invocationId+"> options "+options.toString());
       
   109 
       
   110         forcedExit.set(false);
       
   111         // Create a new logger.
       
   112         StringWriter stdoutLog = new StringWriter();
       
   113         StringWriter stderrLog = new StringWriter();
       
   114         PrintWriter stdout = new PrintWriter(stdoutLog);
       
   115         PrintWriter stderr = new PrintWriter(stderrLog);
       
   116         com.sun.tools.javac.main.Main.Result rc = com.sun.tools.javac.main.Main.Result.OK;
       
   117         try {
       
   118             if (compilationUnits.size() > 0) {
       
   119                 smartFileManager.setVisibleSources(visibleSources);
       
   120                 smartFileManager.cleanArtifacts();
       
   121                 smartFileManager.setLog(stdout);
       
   122 
       
   123 
       
   124                 // Do the compilation!
       
   125                 CompilationTask task = compiler.getTask(stderr, smartFileManager, null, Arrays.asList(args), null, compilationUnits, context);
       
   126                 rc = ((JavacTaskImpl) task).doCall();
       
   127                 smartFileManager.flush();
       
   128             }
       
   129         } catch (Exception e) {
       
   130             stderr.println(e.getMessage());
       
   131             forcedExit.set(true);
       
   132         }
       
   133 
       
   134         compilationResult.packageArtifacts = smartFileManager.getPackageArtifacts();
       
   135 
       
   136         Dependencies deps = Dependencies.instance(context);
       
   137         compilationResult.packageDependencies = deps.getDependencies();
       
   138         compilationResult.packagePubapis = deps.getPubapis();
       
   139 
       
   140         compilationResult.stdout = stdoutLog.toString();
       
   141         compilationResult.stderr = stderrLog.toString();
       
   142         compilationResult.returnCode = rc.exitCode == 0 && forcedExit.get() ? -1 : rc.exitCode;
       
   143 
       
   144         return compilationResult;
       
   145     }
       
   146 }