jdk/src/share/classes/com/sun/tools/hat/internal/oql/OQLEngine.java
author ohair
Wed, 30 Apr 2008 17:34:41 -0700
changeset 468 642c8c0be52e
parent 2 90ce3da70b43
child 715 f16baef3a20e
permissions -rw-r--r--
6695553: Cleanup GPLv2+SPL legal notices in hat sources Summary: Just correcting the legal notices on the HAT sources. Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
468
642c8c0be52e 6695553: Cleanup GPLv2+SPL legal notices in hat sources
ohair
parents: 2
diff changeset
     2
 * Copyright 1997-2007 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * The Original Code is HAT. The Initial Developer of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * Original Code is Bill Foote, with contributions from others
468
642c8c0be52e 6695553: Cleanup GPLv2+SPL legal notices in hat sources
ohair
parents: 2
diff changeset
    30
 * at JavaSoft/Sun.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
package com.sun.tools.hat.internal.oql;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import com.sun.tools.hat.internal.model.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This is Object Query Language Interpreter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class OQLEngine {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            // Do we have javax.script support?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            // create ScriptEngineManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            Class<?> managerClass = Class.forName("javax.script.ScriptEngineManager");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            Object manager = managerClass.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            // create JavaScript engine
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            Method getEngineMethod = managerClass.getMethod("getEngineByName",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                                new Class[] { String.class });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            Object jse = getEngineMethod.invoke(manager, new Object[] {"js"});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            oqlSupported = (jse != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            oqlSupported = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // check OQL is supported or not before creating OQLEngine
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public static boolean isOQLSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        return oqlSupported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public OQLEngine(Snapshot snapshot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (!isOQLSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            throw new UnsupportedOperationException("OQL not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        init(snapshot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
       Query is of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
          select &lt;java script code to select&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
          [ from [instanceof] &lt;class name&gt; [&lt;identifier&gt;]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            [ where &lt;java script boolean expression&gt; ]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
          ]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public synchronized void executeQuery(String query, ObjectVisitor visitor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                                          throws OQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        debugPrint("query : " + query);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        StringTokenizer st = new StringTokenizer(query);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (st.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            String first = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            if (! first.equals("select") ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                // Query does not start with 'select' keyword.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                // Just treat it as plain JavaScript and eval it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    Object res = evalScript(query);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    visitor.visit(res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    throw new OQLException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            throw new OQLException("query syntax error: no 'select' clause");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        String selectExpr = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        boolean seenFrom = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        while (st.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            String tok = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            if (tok.equals("from")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                seenFrom = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            selectExpr += " " + tok;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (selectExpr.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            throw new OQLException("query syntax error: 'select' expression can not be empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        String className = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        boolean isInstanceOf = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        String whereExpr =  null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        String identifier = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (seenFrom) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            if (st.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                String tmp = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                if (tmp.equals("instanceof")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    isInstanceOf = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    if (! st.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                        throw new OQLException("no class name after 'instanceof'");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    className = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    className = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                throw new OQLException("query syntax error: class name must follow 'from'");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            if (st.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                identifier = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                if (identifier.equals("where")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    throw new OQLException("query syntax error: identifier should follow class name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                if (st.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    String tmp = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    if (! tmp.equals("where")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                        throw new OQLException("query syntax error: 'where' clause expected after 'from' clause");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    whereExpr = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    while (st.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                        whereExpr += " " + st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    if (whereExpr.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                        throw new OQLException("query syntax error: 'where' clause cannot have empty expression");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                throw new OQLException("query syntax error: identifier should follow class name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        executeQuery(new OQLQuery(selectExpr, isInstanceOf, className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                                  identifier, whereExpr), visitor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    private void executeQuery(OQLQuery q, ObjectVisitor visitor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                              throws OQLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        JavaClass clazz = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (q.className != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            clazz = snapshot.findClass(q.className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            if (clazz == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                throw new OQLException(q.className + " is not found!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        StringBuffer buf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        buf.append("function __select__(");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (q.identifier != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            buf.append(q.identifier);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        buf.append(") { return ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        buf.append(q.selectExpr.replace('\n', ' '));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        buf.append("; }");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        String selectCode = buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        debugPrint(selectCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        String whereCode = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (q.whereExpr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            buf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            buf.append("function __where__(");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            buf.append(q.identifier);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            buf.append(") { return ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            buf.append(q.whereExpr.replace('\n', ' '));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            buf.append("; }");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            whereCode = buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        debugPrint(whereCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        // compile select expression and where condition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            evalMethod.invoke(engine, new Object[] { selectCode });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            if (whereCode != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                evalMethod.invoke(engine, new Object[] { whereCode });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            if (q.className != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                Enumeration objects = clazz.getInstances(q.isInstanceOf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                while (objects.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    JavaHeapObject obj = (JavaHeapObject) objects.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    Object[] args = new Object[] { wrapJavaObject(obj) };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    boolean b = (whereCode == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    if (!b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                        Object res = call("__where__", args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                        if (res instanceof Boolean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                            b = ((Boolean)res).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                        } else if (res instanceof Number) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                            b = ((Number)res).intValue() != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                            b = (res != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    if (b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                        Object select = call("__select__", args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                        if (visitor.visit(select)) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                // simple "select <expr>" query
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                Object select = call("__select__", new Object[] {});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                visitor.visit(select);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            throw new OQLException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    public Object evalScript(String script) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        return evalMethod.invoke(engine, new Object[] { script });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    public Object wrapJavaObject(JavaHeapObject obj) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        return call("wrapJavaObject", new Object[] { obj });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    public Object toHtml(Object obj) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        return call("toHtml", new Object[] { obj });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    public Object call(String func, Object[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        return invokeMethod.invoke(engine, new Object[] { func, args });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    private static void debugPrint(String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if (debug) System.out.println(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    private void init(Snapshot snapshot) throws RuntimeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        this.snapshot = snapshot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            // create ScriptEngineManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            Class<?> managerClass = Class.forName("javax.script.ScriptEngineManager");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            Object manager = managerClass.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            // create JavaScript engine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            Method getEngineMethod = managerClass.getMethod("getEngineByName",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                                new Class[] { String.class });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            engine = getEngineMethod.invoke(manager, new Object[] {"js"});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            // initialize engine with init file (hat.js)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            InputStream strm = getInitStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            Class<?> engineClass = Class.forName("javax.script.ScriptEngine");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            evalMethod = engineClass.getMethod("eval",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                                new Class[] { Reader.class });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            evalMethod.invoke(engine, new Object[] {new InputStreamReader(strm)});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            // initialize ScriptEngine.eval(String) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            // Invocable.invokeFunction(String, Object[]) methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            Class<?> invocableClass = Class.forName("javax.script.Invocable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            evalMethod = engineClass.getMethod("eval",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                                  new Class[] { String.class });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            invokeMethod = invocableClass.getMethod("invokeFunction",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                                  new Class[] { String.class, Object[].class });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            // initialize ScriptEngine.put(String, Object) method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            Method putMethod = engineClass.getMethod("put",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                                  new Class[] { String.class, Object.class });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            // call ScriptEngine.put to initialize built-in heap object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            putMethod.invoke(engine, new Object[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                        "heap", call("wrapHeapSnapshot", new Object[] { snapshot })
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            if (debug) e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    private InputStream getInitStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        return getClass().getResourceAsStream("/com/sun/tools/hat/resources/hat.js");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    private Object engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    private Method evalMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    private Method invokeMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    private Snapshot snapshot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    private static boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    private static boolean oqlSupported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
}