History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/com/sun/tools/hat/internal/server/QueryHandler.java (1 of 18352 files). Show all

Unified · Split

java/pkg/com/sun/tools/hat/internal/server/QueryHandler.java+226−0
@@ -0,0 +1,226 @@
1+/*
2+ * Copyright (c) 1997, 2008, 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+
40+import java.net.URLEncoder;
41+import java.io.UnsupportedEncodingException;
42+
43+/**
44+ *
45+ * @author Bill Foote
46+ */
47+
48+
49+abstract class QueryHandler {
50+
51+ protected String urlStart;
52+ protected String query;
53+ protected PrintWriter out;
54+ protected Snapshot snapshot;
55+
56+ abstract void run();
57+
58+
59+ void setUrlStart(String s) {
60+ urlStart = s;
61+ }
62+
63+ void setQuery(String s) {
64+ query = s;
65+ }
66+
67+ void setOutput(PrintWriter o) {
68+ this.out = o;
69+ }
70+
71+ void setSnapshot(Snapshot ss) {
72+ this.snapshot = ss;
73+ }
74+
75+ protected String encodeForURL(String s) {
76+ try {
77+ s = URLEncoder.encode(s, "UTF-8");
78+ } catch (UnsupportedEncodingException ex) {
79+ // Should never happen
80+ ex.printStackTrace();
81+ }
82+ return s;
83+ }
84+
85+ protected void startHtml(String title) {
86+ out.print("<html><title>");
87+ print(title);
88+ out.println("</title>");
89+ out.println("<body bgcolor=\"#ffffff\"><center><h1>");
90+ print(title);
91+ out.println("</h1></center>");
92+ }
93+
94+ protected void endHtml() {
95+ out.println("</body></html>");
96+ }
97+
98+ protected void error(String msg) {
99+ out.println(msg);
100+ }
101+
102+ protected void printAnchorStart() {
103+ out.print("<a href=\"");
104+ out.print(urlStart);
105+ }
106+
107+ protected void printThingAnchorTag(long id) {
108+ printAnchorStart();
109+ out.print("object/");
110+ printHex(id);
111+ out.print("\">");
112+ }
113+
114+ protected void printObject(JavaObject obj) {
115+ printThing(obj);
116+ }
117+
118+ protected void printThing(JavaThing thing) {
119+ if (thing == null) {
120+ out.print("null");
121+ return;
122+ }
123+ if (thing instanceof JavaHeapObject) {
124+ JavaHeapObject ho = (JavaHeapObject) thing;
125+ long id = ho.getId();
126+ if (id != -1L) {
127+ if (ho.isNew())
128+ out.println("<strong>");
129+ printThingAnchorTag(id);
130+ }
131+ print(thing.toString());
132+ if (id != -1) {
133+ if (ho.isNew())
134+ out.println("[new]</strong>");
135+ out.print(" (" + ho.getSize() + " bytes)");
136+ out.println("</a>");
137+ }
138+ } else {
139+ print(thing.toString());
140+ }
141+ }
142+
143+ protected void printRoot(Root root) {
144+ StackTrace st = root.getStackTrace();
145+ boolean traceAvailable = (st != null) && (st.getFrames().length != 0);
146+ if (traceAvailable) {
147+ printAnchorStart();
148+ out.print("rootStack/");
149+ printHex(root.getIndex());
150+ out.print("\">");
151+ }
152+ print(root.getDescription());
153+ if (traceAvailable) {
154+ out.print("</a>");
155+ }
156+ }
157+
158+ protected void printClass(JavaClass clazz) {
159+ if (clazz == null) {
160+ out.println("null");
161+ return;
162+ }
163+ String name = clazz.getName();
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 printHex(long addr) {
212+ if (snapshot.getIdentifierSize() == 4) {
213+ out.print(Misc.toHex((int)addr));
214+ } else {
215+ out.print(Misc.toHex(addr));
216+ }
217+ }
218+
219+ protected long parseHex(String value) {
220+ return Misc.parseHex(value);
221+ }
222+
223+ protected void print(String str) {
224+ out.print(Misc.encodeHtml(str));
225+ }
226+}