1 /* |
|
2 * Copyright (c) 2005, 2010, 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. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 /* |
|
25 * A simple application used by unit tests. The first argument to this |
|
26 * class is the name of a file to which a TCP port number can be written. |
|
27 * |
|
28 * By default, this class does nothing other than bind to a TCP port, |
|
29 * write the TCP port number to a file, and wait for an incoming connection |
|
30 * in order to complete the application shutdown protocol. |
|
31 */ |
|
32 import java.net.Socket; |
|
33 import java.net.ServerSocket; |
|
34 import java.io.File; |
|
35 import java.io.FileOutputStream; |
|
36 |
|
37 public class SimpleApplication { |
|
38 private static SimpleApplication myApp; // simple app or a subclass |
|
39 private static String myAppName; // simple app name |
|
40 private static int myPort; // coordination port # |
|
41 private static ServerSocket mySS; // coordination socket |
|
42 |
|
43 // protected so a subclass can extend it; not public so creation is |
|
44 // limited. |
|
45 protected SimpleApplication() { |
|
46 // save simple app (or subclass) name for messages |
|
47 myAppName = getClass().getName(); |
|
48 } |
|
49 |
|
50 // return the simple application (or a subclass) |
|
51 final public static SimpleApplication getMyApp() { |
|
52 return myApp; |
|
53 } |
|
54 |
|
55 // set the simple application (for use by a subclass) |
|
56 final public static void setMyApp(SimpleApplication _myApp) { |
|
57 myApp = _myApp; |
|
58 } |
|
59 |
|
60 // execute the application finish protocol |
|
61 final public void doMyAppFinish(String[] args) throws Exception { |
|
62 System.out.println("INFO: " + myAppName + " is waiting on port: " + |
|
63 myPort); |
|
64 System.out.flush(); |
|
65 |
|
66 // wait for test harness to connect |
|
67 Socket s = mySS.accept(); |
|
68 s.close(); |
|
69 mySS.close(); |
|
70 |
|
71 System.out.println("INFO: " + myAppName + " is shutting down."); |
|
72 System.out.flush(); |
|
73 } |
|
74 |
|
75 // execute the application start protocol |
|
76 final public void doMyAppStart(String[] args) throws Exception { |
|
77 if (args.length < 1) { |
|
78 throw new RuntimeException("Usage: " + myAppName + |
|
79 " port-file [arg(s)]"); |
|
80 } |
|
81 |
|
82 // bind to a random port |
|
83 mySS = new ServerSocket(0); |
|
84 myPort = mySS.getLocalPort(); |
|
85 |
|
86 // Write the port number to the given file |
|
87 File f = new File(args[0]); |
|
88 FileOutputStream fos = new FileOutputStream(f); |
|
89 fos.write( Integer.toString(myPort).getBytes("UTF-8") ); |
|
90 fos.close(); |
|
91 |
|
92 System.out.println("INFO: " + myAppName + " created socket on port: " + |
|
93 myPort); |
|
94 System.out.flush(); |
|
95 } |
|
96 |
|
97 // execute the app work (subclass can override this) |
|
98 public void doMyAppWork(String[] args) throws Exception { |
|
99 } |
|
100 |
|
101 public static void main(String[] args) throws Exception { |
|
102 if (myApp == null) { |
|
103 // create myApp since a subclass hasn't done so |
|
104 myApp = new SimpleApplication(); |
|
105 } |
|
106 |
|
107 myApp.doMyAppStart(args); // do the app start protocol |
|
108 |
|
109 System.out.println("INFO: " + myAppName + " is calling doMyAppWork()"); |
|
110 System.out.flush(); |
|
111 myApp.doMyAppWork(args); // do the app work |
|
112 System.out.println("INFO: " + myAppName + " returned from" + |
|
113 " doMyAppWork()"); |
|
114 System.out.flush(); |
|
115 |
|
116 myApp.doMyAppFinish(args); // do the app finish protocol |
|
117 |
|
118 System.exit(0); |
|
119 } |
|
120 } |
|