1 /*
   2  * Copyright (c) 2012, 2013, 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 import java.io.File;
  25 import java.io.IOException;
  26 import java.io.RandomAccessFile;
  27 import java.util.Objects;
  28 
  29 import sun.management.jdp.JdpJmxPacket;
  30 import sun.management.jdp.JdpException;
  31 
  32 public class JdpDoSomething {
  33 
  34     private static final String lockFileName = "JdpDoSomething.lck";
  35     private static final boolean verbose = false;
  36 
  37     public static boolean getVerbose(){
  38         return verbose;
  39     }
  40 
  41     public static void printJdpPacket(JdpJmxPacket p) {
  42         if (getVerbose()) {
  43             try {
  44                 RandomAccessFile f = new RandomAccessFile("out.dmp", "rw");
  45                 f.write(p.getPacketData());
  46                 f.close();
  47             } catch (IOException e) {
  48                 System.out.println("Can't write a dump file: " + e);
  49             }
  50 
  51             System.out.println("Id: " + p.getId());
  52             System.out.println("Jmx: " + p.getJmxServiceUrl());
  53             System.out.println("Main: " + p.getMainClass());
  54             System.out.println("InstanceName: " + p.getInstanceName());
  55             System.out.println("ProccessId: " + p.getProcessId());
  56             System.out.println("BroadcastInterval: " + p.getBroadcastInterval());
  57             System.out.println("Rmi Hostname: " + p.getRmiHostname());
  58 
  59             System.out.flush();
  60         }
  61     }
  62 
  63     public static void compaireJdpPacketEx(JdpJmxPacket p1, JdpJmxPacket p2)
  64     throws JdpException {
  65 
  66         if (!Objects.equals(p1, p1)) {
  67             throw new JdpException("Packet mismatch error");
  68         }
  69 
  70         if (!Objects.equals(p1.getMainClass(), p2.getMainClass())) {
  71             throw new JdpException("Packet mismatch error (main class)");
  72         }
  73 
  74         if (!Objects.equals(p1.getInstanceName(), p2.getInstanceName())) {
  75             throw new JdpException("Packet mismatch error (instance name)");
  76         }
  77     }
  78 
  79     public static void doSomething() {
  80         try {
  81             File lockFile = new File(lockFileName);
  82             lockFile.createNewFile();
  83 
  84             while (lockFile.exists()) {
  85                 long datetime = lockFile.lastModified();
  86                 long epoch = System.currentTimeMillis() / 1000;
  87 
  88                 // Don't allow test app to run more than an hour
  89                 if (epoch - datetime > 3600) {
  90                     System.err.println("Lock is too old. Aborting");
  91                     return;
  92                 }
  93                 Thread.sleep(1);
  94             }
  95 
  96         } catch (Throwable e) {
  97             System.err.println("Something bad happens:" + e);
  98         }
  99     }
 100 
 101     public static void main(String args[]) throws Exception {
 102         System.err.println("main enter");
 103         doSomething();
 104         System.err.println("main exit");
 105     }
 106 }