1/* 2 * Copyright (c) 2017, 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 24import java.net.SocketTimeoutException; 25import java.util.Map; 26 27public class JdpJmxRemoteDynamicPortTestCase extends JdpTestCase { 28 29 private int receivedJDPpackets = 0; 30 31 public JdpJmxRemoteDynamicPortTestCase(ClientConnection connection) { 32 super(connection); 33 } 34 35 @Override 36 protected String initialLogMessage() { 37 return "Starting test case JdpJmxRemoteDynamicPortTestCase"; 38 } 39 40 /** 41 * This method is executed after a correct Jdp packet (coming from this VM) has been received. 42 * 43 * @param payload A dictionary containing the Jdp packet data. 44 */ 45 protected void packetFromThisVMReceived(Map<String, String> payload) throws Exception { 46 receivedJDPpackets++; 47 final String jmxServiceurl = payload.get("JMX_SERVICE_URL"); 48 int lastcolon = jmxServiceurl.lastIndexOf(':'); 49 int nextslash = jmxServiceurl.indexOf('/', lastcolon); 50 int jmxRemotePort = Integer.parseInt(jmxServiceurl, lastcolon + 1, nextslash, 10); 51 52 log.fine("Received #" + String.valueOf(receivedJDPpackets) + 53 ", jmxStringUrl=" + jmxServiceurl + ", jmxRemotePort=" + jmxRemotePort); 54 55 if (0 == jmxRemotePort) { 56 throw new Exception("JmxRemotePort value is zero. Test case failed."); 57 } 58 59 log.fine("Test case passed"); 60 } 61 62 /** 63 * The socket should not timeout. 64 * It is set to wait for 10 times the defined pause between Jdp packet. See JdpOnTestCase.TIME_OUT_FACTOR. 65 */ 66 @Override 67 protected void onSocketTimeOut(SocketTimeoutException e) throws Exception { 68 String message = "Timed out waiting for JDP packet. Should arrive within " + 69 connection.pauseInSeconds + " seconds, but waited for " + 70 timeOut + " seconds."; 71 log.severe(message); 72 throw new Exception(message, e); 73 } 74 75 76 /** 77 * After receiving one Jdp packets the test should end. 78 */ 79 @Override 80 protected boolean shouldContinue() { 81 return receivedJDPpackets < 1; 82 } 83 84 /** 85 * To run this test manually you might need the following VM options: 86 * <p/> 87 * -Dcom.sun.management.jmxremote.authenticate=false 88 * -Dcom.sun.management.jmxremote.ssl=false 89 * -Dcom.sun.management.jmxremote.port=0 90 * -Dcom.sun.management.jmxremote=true 91 * -Dcom.sun.management.jmxremote.autodiscovery=true 92 * -Dcom.sun.management.jdp.pause=1 93 * -Dcom.sun.management.jdp.name=alex (or some other string to identify this VM) 94 * <p/> 95 * Recommended for nice output: 96 * -Djava.util.logging.SimpleFormatter.format="%1$tF %1$tT %4$-7s %5$s %n" 97 * 98 * @param args 99 * @throws Exception 100 */ 101 public static void main(String[] args) throws Exception { 102 JdpTestCase client = new JdpJmxRemoteDynamicPortTestCase(new ClientConnection()); 103 client.run(); 104 } 105 106} 107