jdk/src/share/classes/com/sun/tools/hat/internal/server/QueryHandler.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 468 642c8c0be52e
child 5506 202f599c92aa
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 468
diff changeset
     2
 * Copyright 1997-2008 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
468
642c8c0be52e 6695553: Cleanup GPLv2+SPL legal notices in hat sources
ohair
parents: 2
diff changeset
    27
/*
2
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.server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.PrintWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.tools.hat.internal.model.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import com.sun.tools.hat.internal.util.Misc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.net.URLEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.UnsupportedEncodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author      Bill Foote
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
abstract class QueryHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    protected String urlStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    protected String query;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    protected PrintWriter out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    protected Snapshot snapshot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    abstract void run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    void setUrlStart(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        urlStart = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    void setQuery(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        query = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    void setOutput(PrintWriter o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        this.out = o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    void setSnapshot(Snapshot ss) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        this.snapshot = ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    protected String encodeForURL(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            s = URLEncoder.encode(s, "UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        } catch (UnsupportedEncodingException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            // Should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    protected void startHtml(String title) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        out.print("<html><title>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        print(title);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        out.println("</title>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        out.println("<body bgcolor=\"#ffffff\"><center><h1>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        print(title);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        out.println("</h1></center>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    protected void endHtml() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        out.println("</body></html>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    protected void error(String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        out.println(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    protected void printAnchorStart() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        out.print("<a href=\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        out.print(urlStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    protected void printThingAnchorTag(long id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        printAnchorStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        out.print("object/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        printHex(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        out.print("\">");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    protected void printObject(JavaObject obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        printThing(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    protected void printThing(JavaThing thing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (thing == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            out.print("null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (thing instanceof JavaHeapObject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            JavaHeapObject ho = (JavaHeapObject) thing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            long id = ho.getId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (id != -1L) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                if (ho.isNew())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                out.println("<strong>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                printThingAnchorTag(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            print(thing.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if (id != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                if (ho.isNew())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    out.println("[new]</strong>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                out.print(" (" + ho.getSize() + " bytes)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                out.println("</a>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            print(thing.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    protected void printRoot(Root root) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        StackTrace st = root.getStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        boolean traceAvailable = (st != null) && (st.getFrames().length != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (traceAvailable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            printAnchorStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            out.print("rootStack/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            printHex(root.getIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            out.print("\">");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        print(root.getDescription());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (traceAvailable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            out.print("</a>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    protected void printClass(JavaClass clazz) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (clazz == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            out.println("null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        String name = clazz.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        printAnchorStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        out.print("class/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        print(encodeForURL(clazz));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        out.print("\">");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        print(clazz.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        out.println("</a>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    protected String encodeForURL(JavaClass clazz) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (clazz.getId() == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            return encodeForURL(clazz.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            return clazz.getIdString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    protected void printField(JavaField field) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        print(field.getName() + " (" + field.getSignature() + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    protected void printStatic(JavaStatic member) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        JavaField f = member.getField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        printField(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        out.print(" : ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (f.hasId()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            JavaThing t = member.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            printThing(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            print(member.getValue().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    protected void printStackTrace(StackTrace trace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        StackFrame[] frames = trace.getFrames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        for (int i = 0; i < frames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            StackFrame f = frames[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            String clazz = f.getClassName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            out.print("<font color=purple>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            print(clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            out.print("</font>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            print("." + f.getMethodName() + "(" + f.getMethodSignature() + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            out.print(" <bold>:</bold> ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            print(f.getSourceFileName() + " line " + f.getLineNumber());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            out.println("<br>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    protected void printHex(long addr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (snapshot.getIdentifierSize() == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            out.print(Misc.toHex((int)addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            out.print(Misc.toHex(addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    protected long parseHex(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return Misc.parseHex(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    protected void print(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        out.print(Misc.encodeHtml(str));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
}