2007-12-01 00:00:00 +00:00
|
|
|
/*
|
2010-07-22 00:01:31 +00:00
|
|
|
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
|
2007-12-01 00:00:00 +00:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2010-05-25 22:58:33 +00:00
|
|
|
* 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.
|
2007-12-01 00:00:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2010-07-22 00:01:31 +00:00
|
|
|
* A simple application used by unit tests. The first argument to this
|
|
|
|
* class is the name of a file to which a TCP port number can be written.
|
2007-12-01 00:00:00 +00:00
|
|
|
*
|
2010-07-22 00:01:31 +00:00
|
|
|
* By default, this class does nothing other than bind to a TCP port,
|
|
|
|
* write the TCP port number to a file, and wait for an incoming connection
|
|
|
|
* in order to complete the application shutdown protocol.
|
2007-12-01 00:00:00 +00:00
|
|
|
*/
|
|
|
|
import java.net.Socket;
|
|
|
|
import java.net.ServerSocket;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
|
|
|
public class SimpleApplication {
|
2010-07-22 00:01:31 +00:00
|
|
|
private static SimpleApplication myApp; // simple app or a subclass
|
|
|
|
private static String myAppName; // simple app name
|
|
|
|
private static int myPort; // coordination port #
|
|
|
|
private static ServerSocket mySS; // coordination socket
|
|
|
|
|
|
|
|
// protected so a subclass can extend it; not public so creation is
|
|
|
|
// limited.
|
|
|
|
protected SimpleApplication() {
|
|
|
|
// save simple app (or subclass) name for messages
|
|
|
|
myAppName = getClass().getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
// return the simple application (or a subclass)
|
|
|
|
final public static SimpleApplication getMyApp() {
|
|
|
|
return myApp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the simple application (for use by a subclass)
|
|
|
|
final public static void setMyApp(SimpleApplication _myApp) {
|
|
|
|
myApp = _myApp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// execute the application finish protocol
|
|
|
|
final public void doMyAppFinish(String[] args) throws Exception {
|
|
|
|
System.out.println("INFO: " + myAppName + " is waiting on port: " +
|
|
|
|
myPort);
|
|
|
|
System.out.flush();
|
|
|
|
|
|
|
|
// wait for test harness to connect
|
|
|
|
Socket s = mySS.accept();
|
|
|
|
s.close();
|
|
|
|
mySS.close();
|
|
|
|
|
|
|
|
System.out.println("INFO: " + myAppName + " is shutting down.");
|
|
|
|
System.out.flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
// execute the application start protocol
|
|
|
|
final public void doMyAppStart(String[] args) throws Exception {
|
|
|
|
if (args.length < 1) {
|
|
|
|
throw new RuntimeException("Usage: " + myAppName +
|
|
|
|
" port-file [arg(s)]");
|
|
|
|
}
|
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
// bind to a random port
|
2010-07-22 00:01:31 +00:00
|
|
|
mySS = new ServerSocket(0);
|
|
|
|
myPort = mySS.getLocalPort();
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
// Write the port number to the given file
|
|
|
|
File f = new File(args[0]);
|
|
|
|
FileOutputStream fos = new FileOutputStream(f);
|
2010-07-22 00:01:31 +00:00
|
|
|
fos.write( Integer.toString(myPort).getBytes("UTF-8") );
|
2007-12-01 00:00:00 +00:00
|
|
|
fos.close();
|
|
|
|
|
2010-07-22 00:01:31 +00:00
|
|
|
System.out.println("INFO: " + myAppName + " created socket on port: " +
|
|
|
|
myPort);
|
2007-12-01 00:00:00 +00:00
|
|
|
System.out.flush();
|
2010-07-22 00:01:31 +00:00
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2010-07-22 00:01:31 +00:00
|
|
|
// execute the app work (subclass can override this)
|
|
|
|
public void doMyAppWork(String[] args) throws Exception {
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
if (myApp == null) {
|
|
|
|
// create myApp since a subclass hasn't done so
|
|
|
|
myApp = new SimpleApplication();
|
|
|
|
}
|
|
|
|
|
|
|
|
myApp.doMyAppStart(args); // do the app start protocol
|
|
|
|
|
|
|
|
System.out.println("INFO: " + myAppName + " is calling doMyAppWork()");
|
|
|
|
System.out.flush();
|
|
|
|
myApp.doMyAppWork(args); // do the app work
|
|
|
|
System.out.println("INFO: " + myAppName + " returned from" +
|
|
|
|
" doMyAppWork()");
|
|
|
|
System.out.flush();
|
|
|
|
|
|
|
|
myApp.doMyAppFinish(args); // do the app finish protocol
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2010-07-22 00:01:31 +00:00
|
|
|
System.exit(0);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|