001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.compatibility.JDBCDriverTest
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021: /**
022: * <p>
023: * Ping the server, waiting till it comes up.
024: * </p>
025: *
026: * @author Rick
027: */package org.apache.derbyTesting.functionTests.tests.junitTests.compatibility;
028:
029: import org.apache.derby.drda.NetworkServerControl;
030:
031: public class Pinger {
032: /////////////////////////////////////////////////////////////
033: //
034: // CONSTANTS
035: //
036: /////////////////////////////////////////////////////////////
037:
038: public static final long SLEEP_TIME_MILLIS = 5000L;
039:
040: public static final int SUCCESS_EXIT = 0;
041: public static final int FAILURE_EXIT = 1;
042:
043: /////////////////////////////////////////////////////////////
044: //
045: // STATE
046: //
047: /////////////////////////////////////////////////////////////
048:
049: /////////////////////////////////////////////////////////////
050: //
051: // CONSTRUCTOR
052: //
053: /////////////////////////////////////////////////////////////
054:
055: public Pinger() {
056: }
057:
058: /////////////////////////////////////////////////////////////
059: //
060: // ENTRY POINT
061: //
062: /////////////////////////////////////////////////////////////
063:
064: public static void main(String[] args) throws Exception {
065: Pinger me = new Pinger();
066:
067: me.ping(5);
068: }
069:
070: /////////////////////////////////////////////////////////////
071: //
072: // MINIONS
073: //
074: /////////////////////////////////////////////////////////////
075:
076: private void println(String text) {
077: System.err.println(text);
078: System.err.flush();
079: }
080:
081: private void exit(int exitStatus) {
082: Runtime.getRuntime().exit(exitStatus);
083: }
084:
085: /////////////////////
086: //
087: // SERVER MANAGEMENT
088: //
089: /////////////////////
090:
091: /**
092: * <p>
093: * Checks to see that the server is up. If the server doesn't
094: * come up in a reasonable amount of time, brings down the VM.
095: * </p>
096: */
097: public void ping(int iterations) throws Exception {
098: ping(new NetworkServerControl(), iterations);
099: }
100:
101: private void ping(NetworkServerControl controller, int iterations)
102: throws Exception {
103: Exception finalException = null;
104:
105: for (int i = 0; i < iterations; i++) {
106: try {
107: controller.ping();
108:
109: return;
110: } catch (Exception e) {
111: finalException = e;
112: }
113:
114: Thread.sleep(SLEEP_TIME_MILLIS);
115: }
116:
117: println("Server did not come up: "
118: + finalException.getMessage());
119: exit(FAILURE_EXIT);
120: }
121:
122: }
|