1 /*
2  * Copyright (c) 2014, 2016, 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.client;
27 
28 import java.io.OutputStreamWriter;
29 import java.io.Writer;
30 
31 import com.sun.tools.javac.main.Main;
32 import com.sun.tools.javac.main.Main.Result;
33 import com.sun.tools.sjavac.AutoFlushWriter;
34 import com.sun.tools.sjavac.Log;
35 import com.sun.tools.sjavac.Util;
36 import com.sun.tools.sjavac.comp.SjavacImpl;
37 import com.sun.tools.sjavac.options.Options;
38 import com.sun.tools.sjavac.server.Sjavac;
39 
40 /**
41  *  <p><b>This is NOT part of any supported API.
42  *  If you write code that depends on this, you do so at your own risk.
43  *  This code and its internal interfaces are subject to change or
44  *  deletion without notice.</b>
45  */
46 public class ClientMain {
47 
run(String[] args)48     public static int run(String[] args) {
49         return run(args,
50                    new AutoFlushWriter(new OutputStreamWriter(System.out)),
51                    new AutoFlushWriter(new OutputStreamWriter(System.err)));
52     }
53 
run(String[] args, Writer out, Writer err)54     public static int run(String[] args, Writer out, Writer err) {
55 
56         Log.setLogForCurrentThread(new Log(out, err));
57 
58         Options options;
59         try {
60             options = Options.parseArgs(args);
61         } catch (IllegalArgumentException e) {
62             Log.error(e.getMessage());
63             return Result.CMDERR.exitCode;
64         }
65 
66         Log.setLogLevel(options.getLogLevel());
67 
68         Log.debug("==========================================================");
69         Log.debug("Launching sjavac client with the following parameters:");
70         Log.debug("    " + options.getStateArgsString());
71         Log.debug("==========================================================");
72 
73         // Prepare sjavac object
74         boolean useServer = options.getServerConf() != null;
75         Sjavac sjavac = useServer ? new SjavacClient(options) : new SjavacImpl();
76 
77         // Perform compilation
78         Result result = sjavac.compile(args);
79 
80         // If sjavac is running in the foreground we should shut it down at this point
81         if (!useServer) {
82             sjavac.shutdown();
83         }
84 
85         return result.exitCode;
86     }
87 }
88