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.server;
27 
28 import java.io.FileWriter;
29 import java.io.FilterOutputStream;
30 import java.io.FilterWriter;
31 import java.io.IOException;
32 import java.io.PrintStream;
33 import java.lang.Thread.UncaughtExceptionHandler;
34 
35 import com.sun.tools.javac.main.Main;
36 import com.sun.tools.javac.main.Main.Result;
37 import com.sun.tools.sjavac.Log;
38 import com.sun.tools.sjavac.Log.Level;
39 import com.sun.tools.sjavac.server.log.LazyInitFileLog;
40 import com.sun.tools.sjavac.server.log.LoggingOutputStream;
41 
42 import static com.sun.tools.sjavac.Log.Level.ERROR;
43 import static com.sun.tools.sjavac.Log.Level.INFO;
44 
45 /**
46  *  <p><b>This is NOT part of any supported API.
47  *  If you write code that depends on this, you do so at your own risk.
48  *  This code and its internal interfaces are subject to change or
49  *  deletion without notice.</b>
50  */
51 public class ServerMain {
52 
53     // For logging server internal (non request specific) errors.
54     private static LazyInitFileLog errorLog;
55 
run(String[] args)56     public static int run(String[] args) {
57 
58         // Under normal operation, all logging messages generated server-side
59         // are due to compilation requests. These logging messages should
60         // be relayed back to the requesting client rather than written to the
61         // server log. The only messages that should be written to the server
62         // log (in production mode) should be errors,
63         Log.setLogForCurrentThread(errorLog = new LazyInitFileLog("server.log"));
64         Log.setLogLevel(ERROR); // should be set to ERROR.
65 
66         // Make sure no exceptions go under the radar
67         Thread.setDefaultUncaughtExceptionHandler((t, e) -> {
68             Log.setLogForCurrentThread(errorLog);
69             Log.error(e);
70         });
71 
72         // Inevitably someone will try to print messages using System.{out,err}.
73         // Make sure this output also ends up in the log.
74         System.setOut(new PrintStream(new LoggingOutputStream(System.out, INFO, "[stdout] ")));
75         System.setErr(new PrintStream(new LoggingOutputStream(System.err, ERROR, "[stderr] ")));
76 
77         // Any options other than --startserver?
78         if (args.length > 1) {
79             Log.error("When spawning a background server, only a single --startserver argument is allowed.");
80             return Result.CMDERR.exitCode;
81         }
82 
83         int exitCode;
84         try {
85             SjavacServer server = new SjavacServer(args[0]);
86             exitCode = server.startServer();
87         } catch (IOException | InterruptedException ex) {
88             ex.printStackTrace();
89             exitCode = Result.ERROR.exitCode;
90         }
91 
92         return exitCode;
93     }
94 
getErrorLog()95     public static LazyInitFileLog getErrorLog() {
96         return errorLog;
97     }
98 }
99