Skip to content

Commit 1287bf4

Browse files
author
zhourenjian
committed
Merge from branch 3.3 for Java2Script Hotspot server
1 parent 0f68398 commit 1287bf4

File tree

6 files changed

+478
-0
lines changed

6 files changed

+478
-0
lines changed

META-INF/MANIFEST.MF

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ Require-Bundle: org.eclipse.core.runtime,
1212
Eclipse-AutoStart: true
1313
Export-Package: net.sf.j2s.core.astvisitors,
1414
net.sf.j2s.core.compiler,
15+
net.sf.j2s.core.hotspot,
1516
net.sf.j2s.core

src/net/sf/j2s/core/CorePlugin.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package net.sf.j2s.core;
22

3+
import net.sf.j2s.core.hotspot.InnerHotspotServer;
4+
35
import org.eclipse.core.runtime.Plugin;
46
import org.osgi.framework.BundleContext;
57

@@ -23,6 +25,9 @@ public CorePlugin() {
2325
*/
2426
public void start(BundleContext context) throws Exception {
2527
super.start(context);
28+
if (!InnerHotspotServer.isServerStarted()) {
29+
InnerHotspotServer.getSingletonServer().startServer();
30+
}
2631
}
2732

2833
/**
@@ -31,6 +36,9 @@ public void start(BundleContext context) throws Exception {
3136
public void stop(BundleContext context) throws Exception {
3237
super.stop(context);
3338
plugin = null;
39+
if (InnerHotspotServer.isServerStarted()) {
40+
InnerHotspotServer.getSingletonServer().stopServer();
41+
}
3442
}
3543

3644
/**

src/net/sf/j2s/core/compiler/Java2ScriptCompiler.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import net.sf.j2s.core.astvisitors.SWTScriptVisitor;
2525
import net.sf.j2s.core.builder.SourceFile;
2626
import net.sf.j2s.core.builder.SourceFileProxy;
27+
import net.sf.j2s.core.hotspot.InnerHotspotServer;
28+
2729
import org.eclipse.core.resources.IContainer;
2830
import org.eclipse.core.resources.IProject;
2931
import org.eclipse.core.resources.IncrementalProjectBuilder;
@@ -405,6 +407,9 @@ public static void outputJavaScript(ASTScriptVisitor visitor, DependencyASTVisit
405407
throw new RuntimeException("Failed to create folder " + folderPath); //$NON-NLS-1$
406408
}
407409
}
410+
InnerHotspotServer.addCompiledItem(packageName + "." + elementName);
411+
} else {
412+
InnerHotspotServer.addCompiledItem(elementName);
408413
}
409414
File jsFile = new File(folderPath, elementName + ".js"); //$NON-NLS-1$
410415
try {
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
package net.sf.j2s.core.hotspot;
2+
3+
import java.io.BufferedInputStream;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.io.PrintStream;
7+
import java.net.Socket;
8+
import java.util.Date;
9+
import java.util.Vector;
10+
11+
public class HotspotWorker implements Runnable {
12+
final static int BUF_SIZE = 2048;
13+
14+
static final byte[] EOL = {(byte)'\r', (byte)'\n' };
15+
16+
/* buffer to use for requests */
17+
byte[] buf;
18+
/* Socket to client we're handling */
19+
private Socket s;
20+
21+
HotspotWorker() {
22+
buf = new byte[BUF_SIZE];
23+
s = null;
24+
}
25+
26+
synchronized void setSocket(Socket s) {
27+
this.s = s;
28+
notify();
29+
}
30+
31+
public synchronized void run() {
32+
while(true) {
33+
if (s == null) {
34+
/* nothing to do */
35+
try {
36+
wait();
37+
} catch (InterruptedException e) {
38+
/* should not happen */
39+
continue;
40+
}
41+
}
42+
try {
43+
handleClient();
44+
} catch (Exception e) {
45+
e.printStackTrace();
46+
}
47+
/* go back in wait queue if there's fewer
48+
* than numHandler connections.
49+
*/
50+
s = null;
51+
Vector pool = InnerHotspotServer.threads;
52+
synchronized (pool) {
53+
if (pool.size() >= 5) {
54+
/* too many threads, exit this one */
55+
return;
56+
} else {
57+
pool.addElement(this);
58+
}
59+
}
60+
}
61+
}
62+
63+
void handleClient() throws IOException {
64+
InputStream is = new BufferedInputStream(s.getInputStream());
65+
PrintStream ps = new PrintStream(s.getOutputStream());
66+
/* we will only block in read for this many milliseconds
67+
* before we fail with java.io.InterruptedIOException,
68+
* at which point we will abandon the connection.
69+
*/
70+
s.setSoTimeout(5000);
71+
s.setTcpNoDelay(true);
72+
/* zero out the buffer from last time */
73+
for (int i = 0; i < BUF_SIZE; i++) {
74+
buf[i] = 0;
75+
}
76+
try {
77+
/* We only support HTTP GET/HEAD, and don't
78+
* support any fancy HTTP options,
79+
* so we're only interested really in
80+
* the first line.
81+
*/
82+
int nread = 0, r = 0;
83+
84+
outerloop:
85+
while (nread < BUF_SIZE) {
86+
r = is.read(buf, nread, BUF_SIZE - nread);
87+
if (r == -1) {
88+
/* EOF */
89+
return;
90+
}
91+
int i = nread;
92+
nread += r;
93+
for (; i < nread; i++) {
94+
if (buf[i] == (byte)'\n' || buf[i] == (byte)'\r') {
95+
/* read one line */
96+
break outerloop;
97+
}
98+
}
99+
}
100+
101+
/* are we doing a GET or just a HEAD */
102+
boolean doingGet;
103+
/* beginning of file name */
104+
int index;
105+
if (buf[0] == (byte)'G' &&
106+
buf[1] == (byte)'E' &&
107+
buf[2] == (byte)'T' &&
108+
buf[3] == (byte)' ') {
109+
doingGet = true;
110+
index = 4;
111+
} else if (buf[0] == (byte)'H' &&
112+
buf[1] == (byte)'E' &&
113+
buf[2] == (byte)'A' &&
114+
buf[3] == (byte)'D' &&
115+
buf[4] == (byte)' ') {
116+
doingGet = false;
117+
index = 5;
118+
} else {
119+
/* we don't support this method */
120+
ps.print("HTTP/1.0 405 unsupported method type: ");
121+
ps.write(buf, 0, 5);
122+
ps.write(EOL);
123+
ps.flush();
124+
s.close();
125+
return;
126+
}
127+
128+
int i = 0;
129+
for (i = index; i < nread; i++) {
130+
if (buf[i] == (byte)' ') {
131+
break;
132+
}
133+
}
134+
String fname = new String(buf, 0, index, i-index);
135+
if (fname.startsWith("/") || fname.startsWith("\\")) {
136+
fname = fname.substring(1);
137+
}
138+
int idx = fname.indexOf('.');
139+
if (idx != -1) {
140+
fname = fname.substring(0, idx);
141+
}
142+
idx = fname.indexOf('?');
143+
if (idx != -1) {
144+
fname = fname.substring(idx + 1);
145+
}
146+
long sessionID = -1;
147+
try {
148+
sessionID = Long.parseLong(fname);
149+
} catch (NumberFormatException e) {
150+
}
151+
ps.print("HTTP/1.0 200 OK");
152+
ps.write(EOL);
153+
ps.print("Server: Java2Script Hotspot Sever");
154+
ps.write(EOL);
155+
ps.print("Date: " + (new Date()));
156+
ps.write(EOL);
157+
ps.print("Last Modified: " + (new Date()));
158+
ps.write(EOL);
159+
ps.print("Content-type: text/javascript");
160+
ps.write(EOL);
161+
if (doingGet) {
162+
sendLatestHotspot(sessionID, ps);
163+
}
164+
} finally {
165+
s.close();
166+
}
167+
}
168+
169+
void sendLatestHotspot(long session, PrintStream ps) throws IOException {
170+
ps.write(EOL);
171+
StringBuffer strBuf = new StringBuffer();
172+
strBuf.append("ClazzLoader.updateHotspot (");
173+
long time = 0;
174+
while (time < 5000) {
175+
String hotspotJS = InnerHotspotServer.getHotspotJavaScript(session);
176+
if (hotspotJS.length() != 0) {
177+
strBuf.append("\r\n");
178+
strBuf.append(hotspotJS);
179+
break;
180+
} else {
181+
Thread thread = new Thread(new Runnable() {
182+
public void run() {
183+
try {
184+
Thread.sleep(250);
185+
} catch (InterruptedException e) {
186+
e.printStackTrace();
187+
}
188+
}
189+
});
190+
thread.start();
191+
try {
192+
thread.join();
193+
} catch (InterruptedException e) {
194+
e.printStackTrace();
195+
}
196+
time += 250;
197+
}
198+
}
199+
strBuf.append("null);");
200+
ps.write(strBuf.toString().getBytes("utf-8"));
201+
}
202+
203+
}
204+

0 commit comments

Comments
 (0)