1 /*
   2  * Copyright (c) 2004, 2011, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.tools.jps;
  27 
  28 import java.util.*;
  29 import java.net.*;
  30 import sun.jvmstat.monitor.*;
  31 
  32 /**
  33  * Application to provide a listing of monitorable java processes.
  34  *
  35  * @author Brian Doherty
  36  * @since 1.5
  37  */
  38 public class Jps {
  39 
  40     private static Arguments arguments;
  41 
  42     public static void main(String[] args) {
  43         try {
  44             arguments = new Arguments(args);
  45         } catch (IllegalArgumentException e) {
  46             System.err.println(e.getMessage());
  47             Arguments.printUsage(System.err);
  48             return;
  49         }
  50 
  51         if (arguments.isHelp()) {
  52             Arguments.printUsage(System.out);
  53             System.exit(0);
  54         }
  55 
  56         try {
  57             HostIdentifier hostId = arguments.hostId();
  58             MonitoredHost monitoredHost =
  59                     MonitoredHost.getMonitoredHost(hostId);
  60 
  61             // get the set active JVMs on the specified host.
  62             Set jvms = monitoredHost.activeVms();
  63 
  64             for (Iterator j = jvms.iterator(); j.hasNext(); /* empty */ ) {
  65                 StringBuilder output = new StringBuilder();
  66                 Throwable lastError = null;
  67 
  68                 int lvmid = ((Integer)j.next()).intValue();
  69 
  70                 output.append(String.valueOf(lvmid));
  71 
  72                 if (arguments.isQuiet()) {
  73                     System.out.println(output);
  74                     continue;
  75                 }
  76 
  77                 MonitoredVm vm = null;
  78                 String vmidString = "//" + lvmid + "?mode=r";
  79 
  80                 String errorString = null;
  81 
  82                 try {
  83                     // Note: The VM associated with the current VM id may
  84                     // no longer be running so these queries may fail. We
  85                     // already added the VM id to the output stream above.
  86                     // If one of the queries fails, then we try to add a
  87                     // reasonable message to indicate that the requested
  88                     // info is not available.
  89 
  90                     errorString = " -- process information unavailable";
  91                     VmIdentifier id = new VmIdentifier(vmidString);
  92                     vm = monitoredHost.getMonitoredVm(id, 0);
  93 
  94                     // leading blank not needed here because of the
  95                     // next append() call
  96                     errorString = "-- main class information unavailable";
  97                     output.append(" ");
  98                     output.append(MonitoredVmUtil.mainClass(vm,
  99                             arguments.showLongPaths()));
 100 
 101                     if (arguments.showMainArgs()) {
 102                         errorString = " -- main args information unavailable";
 103                         String mainArgs = MonitoredVmUtil.mainArgs(vm);
 104                         if (mainArgs != null && mainArgs.length() > 0) {
 105                             output.append(" ").append(mainArgs);
 106                         }
 107                     }
 108                     if (arguments.showVmArgs()) {
 109                         errorString = " -- jvm args information unavailable";
 110                         String jvmArgs = MonitoredVmUtil.jvmArgs(vm);
 111                         if (jvmArgs != null && jvmArgs.length() > 0) {
 112                           output.append(" ").append(jvmArgs);
 113                         }
 114                     }
 115                     if (arguments.showVmFlags()) {
 116                         errorString = " -- jvm flags information unavailable";
 117                         String jvmFlags = MonitoredVmUtil.jvmFlags(vm);
 118                         if (jvmFlags != null && jvmFlags.length() > 0) {
 119                             output.append(" ").append(jvmFlags);
 120                         }
 121                     }
 122 
 123                     errorString = " -- detach failed";
 124                     monitoredHost.detach(vm);
 125 
 126                     System.out.println(output);
 127 
 128                     errorString = null;
 129                 } catch (URISyntaxException e) {
 130                     // unexpected as vmidString is based on a validated hostid
 131                     lastError = e;
 132                     assert false;
 133                 } catch (Exception e) {
 134                     lastError = e;
 135                 } finally {
 136                     if (errorString != null) {
 137                         /*
 138                          * we ignore most exceptions, as there are race
 139                          * conditions where a JVM in 'jvms' may terminate
 140                          * before we get a chance to list its information.
 141                          * Other errors, such as access and I/O exceptions
 142                          * should stop us from iterating over the complete set.
 143                          */
 144                         output.append(errorString);
 145                         if (arguments.isDebug()) {
 146                             if ((lastError != null)
 147                                     && (lastError.getMessage() != null)) {
 148                                 output.append("\n\t");
 149                                 output.append(lastError.getMessage());
 150                             }
 151                         }
 152                         System.out.println(output);
 153                         if (arguments.printStackTrace()) {
 154                             lastError.printStackTrace();
 155                         }
 156                         continue;
 157                     }
 158                 }
 159             }
 160         } catch (MonitorException e) {
 161             if (e.getMessage() != null) {
 162                 System.err.println(e.getMessage());
 163             } else {
 164                 Throwable cause = e.getCause();
 165                 if ((cause != null) && (cause.getMessage() != null)) {
 166                     System.err.println(cause.getMessage());
 167                 } else {
 168                     e.printStackTrace();
 169                 }
 170             }
 171         }
 172     }
 173 }