0N/A/*
2362N/A * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A
245N/A/*
0N/A * The Original Code is HAT. The Initial Developer of the
0N/A * Original Code is Bill Foote, with contributions from others
245N/A * at JavaSoft/Sun.
0N/A */
0N/A
0N/Apackage com.sun.tools.hat.internal.server;
0N/A
0N/Aimport java.io.PrintWriter;
0N/A
0N/Aimport com.sun.tools.hat.internal.model.*;
0N/Aimport com.sun.tools.hat.internal.util.Misc;
0N/A
0N/Aimport java.net.URLEncoder;
0N/Aimport java.io.UnsupportedEncodingException;
0N/A
0N/A/**
0N/A *
0N/A * @author Bill Foote
0N/A */
0N/A
0N/A
0N/Aabstract class QueryHandler {
0N/A
0N/A protected String urlStart;
0N/A protected String query;
0N/A protected PrintWriter out;
0N/A protected Snapshot snapshot;
0N/A
0N/A abstract void run();
0N/A
0N/A
0N/A void setUrlStart(String s) {
0N/A urlStart = s;
0N/A }
0N/A
0N/A void setQuery(String s) {
0N/A query = s;
0N/A }
0N/A
0N/A void setOutput(PrintWriter o) {
0N/A this.out = o;
0N/A }
0N/A
0N/A void setSnapshot(Snapshot ss) {
0N/A this.snapshot = ss;
0N/A }
0N/A
0N/A protected String encodeForURL(String s) {
0N/A try {
0N/A s = URLEncoder.encode(s, "UTF-8");
0N/A } catch (UnsupportedEncodingException ex) {
0N/A // Should never happen
0N/A ex.printStackTrace();
0N/A }
0N/A return s;
0N/A }
0N/A
0N/A protected void startHtml(String title) {
0N/A out.print("<html><title>");
0N/A print(title);
0N/A out.println("</title>");
0N/A out.println("<body bgcolor=\"#ffffff\"><center><h1>");
0N/A print(title);
0N/A out.println("</h1></center>");
0N/A }
0N/A
0N/A protected void endHtml() {
0N/A out.println("</body></html>");
0N/A }
0N/A
0N/A protected void error(String msg) {
0N/A out.println(msg);
0N/A }
0N/A
0N/A protected void printAnchorStart() {
0N/A out.print("<a href=\"");
0N/A out.print(urlStart);
0N/A }
0N/A
0N/A protected void printThingAnchorTag(long id) {
0N/A printAnchorStart();
0N/A out.print("object/");
0N/A printHex(id);
0N/A out.print("\">");
0N/A }
0N/A
0N/A protected void printObject(JavaObject obj) {
0N/A printThing(obj);
0N/A }
0N/A
0N/A protected void printThing(JavaThing thing) {
0N/A if (thing == null) {
0N/A out.print("null");
0N/A return;
0N/A }
0N/A if (thing instanceof JavaHeapObject) {
0N/A JavaHeapObject ho = (JavaHeapObject) thing;
0N/A long id = ho.getId();
0N/A if (id != -1L) {
0N/A if (ho.isNew())
0N/A out.println("<strong>");
0N/A printThingAnchorTag(id);
0N/A }
0N/A print(thing.toString());
0N/A if (id != -1) {
0N/A if (ho.isNew())
0N/A out.println("[new]</strong>");
0N/A out.print(" (" + ho.getSize() + " bytes)");
0N/A out.println("</a>");
0N/A }
0N/A } else {
0N/A print(thing.toString());
0N/A }
0N/A }
0N/A
0N/A protected void printRoot(Root root) {
0N/A StackTrace st = root.getStackTrace();
0N/A boolean traceAvailable = (st != null) && (st.getFrames().length != 0);
0N/A if (traceAvailable) {
0N/A printAnchorStart();
0N/A out.print("rootStack/");
0N/A printHex(root.getIndex());
0N/A out.print("\">");
0N/A }
0N/A print(root.getDescription());
0N/A if (traceAvailable) {
0N/A out.print("</a>");
0N/A }
0N/A }
0N/A
0N/A protected void printClass(JavaClass clazz) {
0N/A if (clazz == null) {
0N/A out.println("null");
0N/A return;
0N/A }
0N/A String name = clazz.getName();
0N/A printAnchorStart();
0N/A out.print("class/");
0N/A print(encodeForURL(clazz));
0N/A out.print("\">");
0N/A print(clazz.toString());
0N/A out.println("</a>");
0N/A }
0N/A
0N/A protected String encodeForURL(JavaClass clazz) {
0N/A if (clazz.getId() == -1) {
0N/A return encodeForURL(clazz.getName());
0N/A } else {
0N/A return clazz.getIdString();
0N/A }
0N/A }
0N/A
0N/A protected void printField(JavaField field) {
0N/A print(field.getName() + " (" + field.getSignature() + ")");
0N/A }
0N/A
0N/A protected void printStatic(JavaStatic member) {
0N/A JavaField f = member.getField();
0N/A printField(f);
0N/A out.print(" : ");
0N/A if (f.hasId()) {
0N/A JavaThing t = member.getValue();
0N/A printThing(t);
0N/A } else {
0N/A print(member.getValue().toString());
0N/A }
0N/A }
0N/A
0N/A protected void printStackTrace(StackTrace trace) {
0N/A StackFrame[] frames = trace.getFrames();
0N/A for (int i = 0; i < frames.length; i++) {
0N/A StackFrame f = frames[i];
0N/A String clazz = f.getClassName();
0N/A out.print("<font color=purple>");
0N/A print(clazz);
0N/A out.print("</font>");
0N/A print("." + f.getMethodName() + "(" + f.getMethodSignature() + ")");
0N/A out.print(" <bold>:</bold> ");
0N/A print(f.getSourceFileName() + " line " + f.getLineNumber());
0N/A out.println("<br>");
0N/A }
0N/A }
0N/A
0N/A protected void printHex(long addr) {
0N/A if (snapshot.getIdentifierSize() == 4) {
0N/A out.print(Misc.toHex((int)addr));
0N/A } else {
0N/A out.print(Misc.toHex(addr));
0N/A }
0N/A }
0N/A
0N/A protected long parseHex(String value) {
0N/A return Misc.parseHex(value);
0N/A }
0N/A
0N/A protected void print(String str) {
0N/A out.print(Misc.encodeHtml(str));
0N/A }
0N/A}