1 /*
   2  * Copyright (c) 2004, 2007, 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 
  25 package sun.jvm.hotspot.utilities.soql;
  26 
  27 import java.util.*;
  28 import sun.jvm.hotspot.debugger.*;
  29 import sun.jvm.hotspot.oops.*;
  30 import sun.jvm.hotspot.runtime.*;
  31 
  32 public class JSJavaVM extends DefaultScriptObject {
  33     private static final int FIELD_ADDRESS_SIZE = 0;
  34     private static final int FIELD_BUILD_INFO   = 1;
  35     private static final int FIELD_CPU          = 2;
  36     private static final int FIELD_FLAGS        = 3;
  37     private static final int FIELD_HEAP         = 4;
  38     private static final int FIELD_OS           = 5;
  39     private static final int FIELD_SYS_PROPS    = 6;
  40     private static final int FIELD_THREADS      = 7;
  41     private static final int FIELD_TYPE         = 8;
  42     private static final int FIELD_VERSION      = 9;
  43     private static final int FIELD_CLASS_PATH   = 10;
  44     private static final int FIELD_BOOT_CLASS_PATH  = 11;
  45     private static final int FIELD_USER_DIR     = 12;
  46     private static final int FIELD_UNDEFINED    = -1;
  47 
  48     public JSJavaVM(JSJavaFactory factory) {
  49         this.factory = factory;
  50         this.vm = VM.getVM();
  51     }
  52 
  53     public Object get(String name) {
  54         int fieldID = getFieldID(name);
  55         switch (fieldID) {
  56         case FIELD_ADDRESS_SIZE:
  57             return new Long(getVMBit());
  58         case FIELD_BUILD_INFO:
  59             return vm.getVMInternalInfo();
  60         case FIELD_CPU:
  61             return vm.getCPU();
  62         case FIELD_FLAGS:
  63             return getFlags();
  64         case FIELD_HEAP:
  65             return getHeap();
  66         case FIELD_OS:
  67             return vm.getOS();
  68         case FIELD_SYS_PROPS:
  69             return getSysProps();
  70         case FIELD_THREADS:
  71             return getThreads();
  72         case FIELD_TYPE:
  73             return getType();
  74         case FIELD_VERSION:
  75             return vm.getVMRelease();
  76         case FIELD_CLASS_PATH:
  77             return getClassPath();
  78         case FIELD_BOOT_CLASS_PATH:
  79             return getBootClassPath();
  80         case FIELD_USER_DIR:
  81             return getUserDir();
  82         case FIELD_UNDEFINED:
  83         default:
  84             return super.get(name);
  85         }
  86     }
  87 
  88     public Object[] getIds() {
  89         Object[] superIds = super.getIds();
  90         Object[] tmp = fields.keySet().toArray();
  91         Object[] res = new Object[superIds.length + tmp.length];
  92         System.arraycopy(tmp, 0, res, 0, tmp.length);
  93         System.arraycopy(superIds, 0, res, tmp.length, superIds.length);
  94         return res;
  95     }
  96 
  97     public boolean has(String name) {
  98         if (getFieldID(name) != FIELD_UNDEFINED) {
  99             return true;
 100         } else {
 101             return super.has(name);
 102         }
 103     }
 104 
 105     public void put(String name, Object value) {
 106         if (getFieldID(name) == FIELD_UNDEFINED) {
 107             super.put(name, value);
 108         }
 109     }
 110 
 111     public String toString() {
 112         StringBuffer buf = new StringBuffer();
 113         buf.append("Java Hotspot ");
 114         buf.append(getType());
 115         buf.append(' ');
 116         buf.append(getVMBit());
 117         buf.append(" bit VM (build ");
 118         buf.append(vm.getVMRelease());
 119         buf.append(")");
 120         return buf.toString();
 121     }
 122 
 123     //-- Internals only below this point
 124     private static Map fields = new HashMap();
 125     private static void addField(String name, int fieldId) {
 126         fields.put(name, new Integer(fieldId));
 127     }
 128 
 129     private static int getFieldID(String name) {
 130         Integer res = (Integer) fields.get(name);
 131         return (res != null)? res.intValue() : FIELD_UNDEFINED;
 132     }
 133 
 134     static {
 135         addField("addressSize", FIELD_ADDRESS_SIZE);
 136         addField("buildInfo", FIELD_BUILD_INFO);
 137         addField("cpu", FIELD_CPU);
 138         addField("flags", FIELD_FLAGS);
 139         addField("heap", FIELD_HEAP);
 140         addField("os", FIELD_OS);
 141         addField("sysProps", FIELD_SYS_PROPS);
 142         addField("threads", FIELD_THREADS);
 143         addField("type", FIELD_TYPE);
 144         addField("version", FIELD_VERSION);
 145         addField("classPath", FIELD_CLASS_PATH);
 146         addField("bootClassPath", FIELD_BOOT_CLASS_PATH);
 147         addField("userDir", FIELD_USER_DIR);
 148     }
 149 
 150     private long getVMBit() {
 151         // address size in bits
 152         return vm.getAddressSize() * 8;
 153     }
 154 
 155     private synchronized JSMap getFlags() {
 156         if (flagsCache == null) {
 157             VM.Flag[] flags = vm.getCommandLineFlags();
 158             Map map = new HashMap();
 159             if (flags != null) {
 160                 for (int f = 0; f < flags.length; f++) {
 161                     VM.Flag flag = flags[f];
 162                     map.put(flag.getName(), flag.getValue());
 163                 }
 164             }
 165             flagsCache = factory.newJSMap(map);
 166         }
 167         return flagsCache;
 168     }
 169 
 170     private synchronized JSJavaHeap getHeap() {
 171         if (heapCache == null) {
 172             heapCache = factory.newJSJavaHeap();
 173         }
 174         return heapCache;
 175     }
 176 
 177     private synchronized JSMap getSysProps() {
 178         if (sysPropsCache == null) {
 179             Properties props = vm.getSystemProperties();
 180             Map map = new HashMap();
 181             if (props != null) {
 182                 Enumeration e = props.propertyNames();
 183                 while (e.hasMoreElements()) {
 184                     String key = (String) e.nextElement();
 185                     map.put(key, props.getProperty(key));
 186                 }
 187             }
 188             sysPropsCache = factory.newJSMap(map);
 189         }
 190         return sysPropsCache;
 191     }
 192 
 193     private synchronized JSList getThreads() {
 194         if (threadsCache == null) {
 195             List threads = new ArrayList(0);
 196             threadsCache = factory.newJSList(threads);
 197             JavaThread jthread = vm.getThreads().first();
 198             while (jthread != null) {
 199                 threads.add(jthread);
 200                 jthread = jthread.next();
 201             }
 202         }
 203         return threadsCache;
 204     }
 205 
 206     private String getType() {
 207         if (vm.isClientCompiler()) {
 208             return "Client";
 209         } else if (vm.isServerCompiler()) {
 210             return "Server";
 211         } else {
 212             return "Core";
 213         }
 214     }
 215 
 216     private String getClassPath() {
 217         return vm.getSystemProperty("java.class.path");
 218     }
 219 
 220     private String getBootClassPath() {
 221         return vm.getSystemProperty("sun.boot.class.path");
 222     }
 223 
 224     private String getUserDir() {
 225         return vm.getSystemProperty("user.dir");
 226     }
 227 
 228     private JSMap      flagsCache;
 229     private JSJavaHeap heapCache;
 230     private JSMap      sysPropsCache;
 231     private JSList     threadsCache;
 232     private final JSJavaFactory factory;
 233     private final VM vm;
 234 }