jdk/src/jdk.dev/share/classes/com/sun/tools/hat/internal/server/QueryHandler.java
changeset 30677 59b772a18fac
parent 30658 3b1ad397517c
parent 30676 0545deee4403
child 30678 a8b7fd8ede97
equal deleted inserted replaced
30658:3b1ad397517c 30677:59b772a18fac
     1 /*
       
     2  * Copyright (c) 1997, 2013, 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.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 
       
    27 /*
       
    28  * The Original Code is HAT. The Initial Developer of the
       
    29  * Original Code is Bill Foote, with contributions from others
       
    30  * at JavaSoft/Sun.
       
    31  */
       
    32 
       
    33 package com.sun.tools.hat.internal.server;
       
    34 
       
    35 import java.io.PrintWriter;
       
    36 
       
    37 import com.sun.tools.hat.internal.model.*;
       
    38 import com.sun.tools.hat.internal.util.Misc;
       
    39 import java.io.StringWriter;
       
    40 
       
    41 import java.net.URLEncoder;
       
    42 import java.io.UnsupportedEncodingException;
       
    43 
       
    44 /**
       
    45  *
       
    46  * @author      Bill Foote
       
    47  */
       
    48 
       
    49 
       
    50 abstract class QueryHandler {
       
    51 
       
    52     protected String urlStart;
       
    53     protected String query;
       
    54     protected PrintWriter out;
       
    55     protected Snapshot snapshot;
       
    56 
       
    57     abstract void run();
       
    58 
       
    59 
       
    60     void setUrlStart(String s) {
       
    61         urlStart = s;
       
    62     }
       
    63 
       
    64     void setQuery(String s) {
       
    65         query = s;
       
    66     }
       
    67 
       
    68     void setOutput(PrintWriter o) {
       
    69         this.out = o;
       
    70     }
       
    71 
       
    72     void setSnapshot(Snapshot ss) {
       
    73         this.snapshot = ss;
       
    74     }
       
    75 
       
    76     protected String encodeForURL(String s) {
       
    77         try {
       
    78             s = URLEncoder.encode(s, "UTF-8");
       
    79         } catch (UnsupportedEncodingException ex) {
       
    80             // Should never happen
       
    81             ex.printStackTrace();
       
    82         }
       
    83         return s;
       
    84     }
       
    85 
       
    86     protected void startHtml(String title) {
       
    87         out.print("<html><title>");
       
    88         print(title);
       
    89         out.println("</title>");
       
    90         out.println("<body bgcolor=\"#ffffff\"><center><h1>");
       
    91         print(title);
       
    92         out.println("</h1></center>");
       
    93     }
       
    94 
       
    95     protected void endHtml() {
       
    96         out.println("</body></html>");
       
    97     }
       
    98 
       
    99     protected void error(String msg) {
       
   100         println(msg);
       
   101     }
       
   102 
       
   103     protected void printAnchorStart() {
       
   104         out.print("<a href=\"");
       
   105         out.print(urlStart);
       
   106     }
       
   107 
       
   108     protected void printThingAnchorTag(long id) {
       
   109         printAnchorStart();
       
   110         out.print("object/");
       
   111         printHex(id);
       
   112         out.print("\">");
       
   113     }
       
   114 
       
   115     protected void printObject(JavaObject obj) {
       
   116         printThing(obj);
       
   117     }
       
   118 
       
   119     protected void printThing(JavaThing thing) {
       
   120         if (thing == null) {
       
   121             out.print("null");
       
   122             return;
       
   123         }
       
   124         if (thing instanceof JavaHeapObject) {
       
   125             JavaHeapObject ho = (JavaHeapObject) thing;
       
   126             long id = ho.getId();
       
   127             if (id != -1L) {
       
   128                 if (ho.isNew())
       
   129                 out.println("<strong>");
       
   130                 printThingAnchorTag(id);
       
   131             }
       
   132             print(thing.toString());
       
   133             if (id != -1) {
       
   134                 if (ho.isNew())
       
   135                     out.println("[new]</strong>");
       
   136                 out.print(" (" + ho.getSize() + " bytes)");
       
   137                 out.println("</a>");
       
   138             }
       
   139         } else {
       
   140             print(thing.toString());
       
   141         }
       
   142     }
       
   143 
       
   144     protected void printRoot(Root root) {
       
   145         StackTrace st = root.getStackTrace();
       
   146         boolean traceAvailable = (st != null) && (st.getFrames().length != 0);
       
   147         if (traceAvailable) {
       
   148             printAnchorStart();
       
   149             out.print("rootStack/");
       
   150             printHex(root.getIndex());
       
   151             out.print("\">");
       
   152         }
       
   153         print(root.getDescription());
       
   154         if (traceAvailable) {
       
   155             out.print("</a>");
       
   156         }
       
   157     }
       
   158 
       
   159     protected void printClass(JavaClass clazz) {
       
   160         if (clazz == null) {
       
   161             out.println("null");
       
   162             return;
       
   163         }
       
   164         printAnchorStart();
       
   165         out.print("class/");
       
   166         print(encodeForURL(clazz));
       
   167         out.print("\">");
       
   168         print(clazz.toString());
       
   169         out.println("</a>");
       
   170     }
       
   171 
       
   172     protected String encodeForURL(JavaClass clazz) {
       
   173         if (clazz.getId() == -1) {
       
   174             return encodeForURL(clazz.getName());
       
   175         } else {
       
   176             return clazz.getIdString();
       
   177         }
       
   178     }
       
   179 
       
   180     protected void printField(JavaField field) {
       
   181         print(field.getName() + " (" + field.getSignature() + ")");
       
   182     }
       
   183 
       
   184     protected void printStatic(JavaStatic member) {
       
   185         JavaField f = member.getField();
       
   186         printField(f);
       
   187         out.print(" : ");
       
   188         if (f.hasId()) {
       
   189             JavaThing t = member.getValue();
       
   190             printThing(t);
       
   191         } else {
       
   192             print(member.getValue().toString());
       
   193         }
       
   194     }
       
   195 
       
   196     protected void printStackTrace(StackTrace trace) {
       
   197         StackFrame[] frames = trace.getFrames();
       
   198         for (int i = 0; i < frames.length; i++) {
       
   199             StackFrame f = frames[i];
       
   200             String clazz = f.getClassName();
       
   201             out.print("<font color=purple>");
       
   202             print(clazz);
       
   203             out.print("</font>");
       
   204             print("." + f.getMethodName() + "(" + f.getMethodSignature() + ")");
       
   205             out.print(" <bold>:</bold> ");
       
   206             print(f.getSourceFileName() + " line " + f.getLineNumber());
       
   207             out.println("<br>");
       
   208         }
       
   209     }
       
   210 
       
   211     protected void printException(Throwable t) {
       
   212         println(t.getMessage());
       
   213         out.println("<pre>");
       
   214         StringWriter sw = new StringWriter();
       
   215         t.printStackTrace(new PrintWriter(sw));
       
   216         print(sw.toString());
       
   217         out.println("</pre>");
       
   218     }
       
   219 
       
   220     protected void printHex(long addr) {
       
   221         if (snapshot.getIdentifierSize() == 4) {
       
   222             out.print(Misc.toHex((int)addr));
       
   223         } else {
       
   224             out.print(Misc.toHex(addr));
       
   225         }
       
   226     }
       
   227 
       
   228     protected long parseHex(String value) {
       
   229         return Misc.parseHex(value);
       
   230     }
       
   231 
       
   232     protected void print(String str) {
       
   233         out.print(Misc.encodeHtml(str));
       
   234     }
       
   235 
       
   236     protected void println(String str) {
       
   237         out.println(Misc.encodeHtml(str));
       
   238     }
       
   239 }