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