5652N/A/*
5652N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
5652N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5652N/A *
5652N/A * This code is free software; you can redistribute it and/or modify it
5652N/A * under the terms of the GNU General Public License version 2 only, as
5652N/A * published by the Free Software Foundation.
5652N/A *
5652N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5652N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5652N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5652N/A * version 2 for more details (a copy is included in the LICENSE file that
5652N/A * accompanied this code).
5652N/A *
5652N/A * You should have received a copy of the GNU General Public License version
5652N/A * 2 along with this work; if not, write to the Free Software Foundation,
5652N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5652N/A *
5652N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5652N/A * or visit www.oracle.com if you need additional information or have any
5652N/A * questions.
5652N/A */
5652N/A
5652N/Aimport java.io.File;
5652N/Aimport java.io.IOException;
5652N/Aimport java.io.RandomAccessFile;
5652N/Aimport java.util.Objects;
5652N/A
5652N/Aimport sun.management.jdp.JdpJmxPacket;
5652N/Aimport sun.management.jdp.JdpException;
5652N/A
5652N/Apublic class JdpDoSomething {
5652N/A
5652N/A private static final String lockFileName = "JdpDoSomething.lck";
5652N/A private static final boolean verbose=false;
5652N/A
5652N/A public static boolean getVerbose(){
5652N/A return verbose;
5652N/A }
5652N/A
5652N/A public static void printJdpPacket(JdpJmxPacket p) {
5652N/A if (getVerbose()) {
5652N/A try {
5652N/A RandomAccessFile f = new RandomAccessFile("out.dmp", "rw");
5652N/A f.write(p.getPacketData());
5652N/A f.close();
5652N/A } catch (IOException e) {
5652N/A System.out.println("Can't write a dump file: " + e);
5652N/A }
5652N/A
5652N/A System.out.println("Id: " + p.getId());
5652N/A System.out.println("Jmx: " + p.getJmxServiceUrl());
5652N/A System.out.println("Main: " + p.getMainClass());
5652N/A System.out.println("InstanceName: " + p.getInstanceName());
5652N/A
5652N/A System.out.flush();
5652N/A }
5652N/A }
5652N/A
5652N/A public static void compaireJdpPacketEx(JdpJmxPacket p1, JdpJmxPacket p2)
5652N/A throws JdpException {
5652N/A
5652N/A if (!Objects.equals(p1, p1)) {
5652N/A throw new JdpException("Packet mismatch error");
5652N/A }
5652N/A
5652N/A if (!Objects.equals(p1.getMainClass(), p2.getMainClass())) {
5652N/A throw new JdpException("Packet mismatch error (main class)");
5652N/A }
5652N/A
5652N/A if (!Objects.equals(p1.getInstanceName(), p2.getInstanceName())) {
5652N/A throw new JdpException("Packet mismatch error (instance name)");
5652N/A }
5652N/A }
5652N/A
5652N/A public static void doSomething() {
5652N/A try {
5652N/A File lockFile = new File(lockFileName);
5652N/A lockFile.createNewFile();
5652N/A
5652N/A while (lockFile.exists()) {
5652N/A long datetime = lockFile.lastModified();
5652N/A long epoch = System.currentTimeMillis() / 1000;
5652N/A
5652N/A // Don't allow test app to run more than an hour
5652N/A if (epoch - datetime > 3600) {
5652N/A System.err.println("Lock is too old. Aborting");
5652N/A return;
5652N/A }
5652N/A Thread.sleep(1);
5652N/A }
5652N/A
5652N/A } catch (Throwable e) {
5652N/A System.err.println("Something bad happens:" + e);
5652N/A }
5652N/A }
5652N/A
5652N/A public static void main(String args[]) throws Exception {
5652N/A System.err.println("main enter");
5652N/A doSomething();
5652N/A System.err.println("main exit");
5652N/A }
5652N/A}