Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/tools/common/SimpleApplication.java
38838 views
/*1* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* A simple application used by unit tests. The first argument to this25* class is the name of a file to which a TCP port number can be written.26*27* By default, this class does nothing other than bind to a TCP port,28* write the TCP port number to a file, and wait for an incoming connection29* in order to complete the application shutdown protocol.30*/31import java.net.Socket;32import java.net.ServerSocket;33import java.io.File;34import java.io.FileOutputStream;3536public class SimpleApplication {37private static SimpleApplication myApp; // simple app or a subclass38private static String myAppName; // simple app name39private static int myPort; // coordination port #40private static ServerSocket mySS; // coordination socket4142// protected so a subclass can extend it; not public so creation is43// limited.44protected SimpleApplication() {45// save simple app (or subclass) name for messages46myAppName = getClass().getName();47}4849// return the simple application (or a subclass)50final public static SimpleApplication getMyApp() {51return myApp;52}5354// set the simple application (for use by a subclass)55final public static void setMyApp(SimpleApplication _myApp) {56myApp = _myApp;57}5859// execute the application finish protocol60final public void doMyAppFinish(String[] args) throws Exception {61System.out.println("INFO: " + myAppName + " is waiting on port: " +62myPort);63System.out.flush();6465// wait for test harness to connect66Socket s = mySS.accept();67s.close();68mySS.close();6970System.out.println("INFO: " + myAppName + " is shutting down.");71System.out.flush();72}7374// execute the application start protocol75final public void doMyAppStart(String[] args) throws Exception {76if (args.length < 1) {77throw new RuntimeException("Usage: " + myAppName +78" port-file [arg(s)]");79}8081// bind to a random port82mySS = new ServerSocket(0);83myPort = mySS.getLocalPort();8485// Write the port number to the given file86File f = new File(args[0]);87FileOutputStream fos = new FileOutputStream(f);88fos.write( Integer.toString(myPort).getBytes("UTF-8") );89fos.close();9091System.out.println("INFO: " + myAppName + " created socket on port: " +92myPort);93System.out.flush();94}9596// execute the app work (subclass can override this)97public void doMyAppWork(String[] args) throws Exception {98}99100public static void main(String[] args) throws Exception {101if (myApp == null) {102// create myApp since a subclass hasn't done so103myApp = new SimpleApplication();104}105106myApp.doMyAppStart(args); // do the app start protocol107108System.out.println("INFO: " + myAppName + " is calling doMyAppWork()");109System.out.flush();110myApp.doMyAppWork(args); // do the app work111System.out.println("INFO: " + myAppName + " returned from" +112" doMyAppWork()");113System.out.flush();114115myApp.doMyAppFinish(args); // do the app finish protocol116117System.exit(0);118}119}120121122