* Copyright (c) 2011-2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
packagecom.sun.tools.sjavac.server;
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.io.PrintWriter;
importjava.io.FileNotFoundException;
importjava.net.URI;
importjava.util.HashSet;
importjava.util.Set;
importjava.util.HashMap;
importjava.util.Map;
importjava.net.InetAddress;
importjava.net.InetSocketAddress;
importjava.net.ServerSocket;
importjava.net.Socket;
importjava.net.SocketAddress;
importjava.util.ArrayList;
importjava.util.Random;
importcom.sun.tools.sjavac.Util;
importcom.sun.tools.sjavac.ProblemException;
importjava.io.*;
importjava.util.*;
/**
* The JavacServer class contains methods both to setup a server that responds to requests and methods to connect to this server.
*
* <p><b>This is NOT part of any supported API. If you write code that depends on this, you do so at your own risk. This code and its internal interfaces are
* subject to change or deletion without notice.</b></p>
*/
publicclassJavacServer{
// Responding to this tcp/ip port on localhost.
privatefinalServerSocketserverSocket;
// The secret cookie shared between server and client through the port file.
privatefinallongmyCookie;
// When the server was started.
privatelongserverStart;
// Accumulated build time for all requests, not counting idle time.
privatelongtotalBuildTime;
// The javac server specific log file.
PrintWritertheLog;
// The compiler pool that maintains the compiler threads.
CompilerPoolcompilerPool;
// For the client, all port files fetched, one per started javac server.
// Though usually only one javac server is started by a client.
* Sum up the total build time for this javac server.
*/
publicvoidaddBuildTime(longinc){
totalBuildTime+=inc;
}
/**
* Log this message.
*/
publicvoidlog(Stringmsg){
if(theLog!=null){
theLog.println(msg);
}else{
System.err.println(msg);
}
}
/**
* Make sure the log is flushed.
*/
publicvoidflushLog(){
if(theLog!=null){
theLog.flush();
}
}
/**
* Start a server using a settings string. Typically: "--startserver:portfile=/tmp/myserver,poolsize=3" and the string "portfile=/tmp/myserver,poolsize=3"
* is sent as the settings parameter. Returns 0 on success, -1 on failure.
// The port file is locked and the server port and cookie is written into it.
PortFileportFile=getPortFile(portfile);
JavacServers;
synchronized(portFile){
portFile.lock();
portFile.getValues();
if(portFile.containsPortInfo()){
err.println("Javac server not started because portfile exists!");
portFile.unlock();
return-1;
}
s=newJavacServer(poolsize,logfile);
portFile.setValues(s.getPort(),s.getCookie());
portFile.unlock();
}
// Run the server. Will delete the port file when shutting down.
// It will shut down automatically when no new requests have come in
// during the last 125 seconds.
s.run(portFile,err,keepalive);
// The run loop for the server has exited.
return0;
}catch(Exceptione){
e.printStackTrace(err);
return-1;
}
}
/**
* Dispatch a compilation request to a javac server.
*
* @param args are the command line args to javac and is allowed to contain source files, @file and other command line options to javac.
*
* The generated classes, h files and other artifacts from the javac invocation are stored by the javac server to disk.
*
* @param sources_to_compile The sources to compile.
*
* @param visibleSources If visible sources has a non zero size, then visible_sources are the only files in the file system that the javac server can see!
* (Sources to compile are always visible.) The visible sources are those supplied by the (filtered) -sourcepath
*
* @param visibleClasses If visible classes for a specific root/jar has a non zero size, then visible_classes are the only class files that the javac server
* can see, in that root/jar. It maps from a classpath root or a jar file to the set of visible classes for that root/jar.
*
* The server return meta data about the build in the following parameters.
* @param package_artifacts, map from package name to set of created artifacts for that package.
* @param package_dependencies, map from package name to set of packages that it depends upon.
* @param package_pubapis, map from package name to unique string identifying its pub api.
* Run the server thread until it exits. Either because of inactivity or because the port file has been deleted by someone else, or overtaken by some other