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 /** |
|
36 * |
|
37 * @author Bill Foote |
|
38 */ |
|
39 |
|
40 |
|
41 import java.net.Socket; |
|
42 import java.net.ServerSocket; |
|
43 import java.net.InetAddress; |
|
44 |
|
45 import java.io.InputStream; |
|
46 import java.io.BufferedInputStream; |
|
47 import java.io.IOException; |
|
48 import java.io.Writer; |
|
49 import java.io.BufferedWriter; |
|
50 import java.io.PrintWriter; |
|
51 import java.io.OutputStream; |
|
52 import java.io.OutputStreamWriter; |
|
53 import java.io.BufferedOutputStream; |
|
54 |
|
55 import com.sun.tools.hat.internal.model.Snapshot; |
|
56 import com.sun.tools.hat.internal.oql.OQLEngine; |
|
57 |
|
58 public class QueryListener implements Runnable { |
|
59 |
|
60 |
|
61 private Snapshot snapshot; |
|
62 private OQLEngine engine; |
|
63 private int port; |
|
64 |
|
65 public QueryListener(int port) { |
|
66 this.port = port; |
|
67 this.snapshot = null; // Client will setModel when it's ready |
|
68 this.engine = null; // created when snapshot is set |
|
69 } |
|
70 |
|
71 public void setModel(Snapshot ss) { |
|
72 this.snapshot = ss; |
|
73 if (OQLEngine.isOQLSupported()) { |
|
74 this.engine = new OQLEngine(ss); |
|
75 } |
|
76 } |
|
77 |
|
78 public void run() { |
|
79 try { |
|
80 waitForRequests(); |
|
81 } catch (IOException ex) { |
|
82 ex.printStackTrace(); |
|
83 System.exit(1); |
|
84 } |
|
85 } |
|
86 |
|
87 private void waitForRequests() throws IOException { |
|
88 ServerSocket ss = new ServerSocket(port); |
|
89 Thread last = null; |
|
90 for (;;) { |
|
91 Socket s = ss.accept(); |
|
92 Thread t = new Thread(new HttpReader(s, snapshot, engine)); |
|
93 if (snapshot == null) { |
|
94 t.setPriority(Thread.NORM_PRIORITY+1); |
|
95 } else { |
|
96 t.setPriority(Thread.NORM_PRIORITY-1); |
|
97 if (last != null) { |
|
98 try { |
|
99 last.setPriority(Thread.NORM_PRIORITY-2); |
|
100 } catch (Throwable ignored) { |
|
101 } |
|
102 // If the thread is no longer alive, we'll get a |
|
103 // NullPointerException |
|
104 } |
|
105 } |
|
106 t.start(); |
|
107 last = t; |
|
108 } |
|
109 } |
|
110 |
|
111 } |
|