Skip to content

Commit ac2dc83

Browse files
author
zhourenjian
committed
Setup inner Java2Script hotspot server (Java2Script is going to support Java-like hotspot loading.)
1 parent 6cb0545 commit ac2dc83

File tree

3 files changed

+47
-27
lines changed

3 files changed

+47
-27
lines changed

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

Lines changed: 5 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

@@ -31,6 +33,9 @@ public void start(BundleContext context) throws Exception {
3133
public void stop(BundleContext context) throws Exception {
3234
super.stop(context);
3335
plugin = null;
36+
if (InnerHotspotServer.isServerStarted()) {
37+
InnerHotspotServer.getSingletonServer().stopServer();
38+
}
3439
}
3540

3641
/**

src/net/sf/j2s/core/hotspot/HotspotWorker.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,23 @@ void handleClient() throws IOException {
131131
break;
132132
}
133133
}
134-
// String fname = new String(buf, 0, index, i-index);
135-
// if (fname.startsWith(File.separator)) {
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-
// }
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+
}
146151
ps.print("HTTP/1.0 200 OK");
147152
ps.write(EOL);
148153
ps.print("Server: Java2Script Hotspot Sever");
@@ -154,20 +159,20 @@ void handleClient() throws IOException {
154159
ps.print("Content-type: text/javascript");
155160
ps.write(EOL);
156161
if (doingGet) {
157-
sendLatestHotspot(ps);
162+
sendLatestHotspot(sessionID, ps);
158163
}
159164
} finally {
160165
s.close();
161166
}
162167
}
163168

164-
void sendLatestHotspot(PrintStream ps) throws IOException {
169+
void sendLatestHotspot(long session, PrintStream ps) throws IOException {
165170
ps.write(EOL);
166171
StringBuffer strBuf = new StringBuffer();
167172
strBuf.append("ClazzLoader.updateHotspot (");
168173
long time = 0;
169174
while (time < 5000) {
170-
String hotspotJS = InnerHotspotServer.getHotspotJavaScript();
175+
String hotspotJS = InnerHotspotServer.getHotspotJavaScript(session);
171176
if (hotspotJS.length() != 0) {
172177
strBuf.append("\r\n");
173178
strBuf.append(hotspotJS);

src/net/sf/j2s/core/hotspot/InnerHotspotServer.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,28 @@ public class InnerHotspotServer {
2323

2424
private static int port = -1;
2525

26+
private static InnerHotspotServer server = null;
27+
2628
private ServerSocket ss;
2729

30+
private InnerHotspotServer() {
31+
// prevent ...
32+
}
33+
34+
public static InnerHotspotServer getSingletonServer() {
35+
if (server == null) {
36+
server = new InnerHotspotServer();
37+
}
38+
return server;
39+
}
40+
41+
public static boolean isServerStarted() {
42+
return serverStarted;
43+
}
44+
2845
public static int getHotspotPort() {
2946
if (port == -1) {
30-
return new InnerHotspotServer().startServer();
47+
return getSingletonServer().startServer();
3148
}
3249
return port;
3350
}
@@ -53,13 +70,14 @@ public static void addCompiledItem(String name) {
5370
}
5471
}
5572

56-
public static String getHotspotJavaScript() {
73+
public static String getHotspotJavaScript(long session) {
5774
StringBuffer buf = new StringBuffer();
5875
long now = new Date().getTime();
5976
synchronized (hotspotItems) {
6077
for (Iterator iterator = hotspotItems.iterator(); iterator.hasNext();) {
6178
Java2ScriptCompiledItem item = (Java2ScriptCompiledItem) iterator.next();
62-
if (item.getTime() >= now - 10000) { // 10 seconds delay!
79+
if ((session > 0 && item.getId() > session)
80+
|| (session <= 0 && item.getTime() >= now - 10000)) { // 10 seconds delay!
6381
buf.append(item.getTime());
6482
buf.append(", ");
6583
buf.append(item.getId());
@@ -68,7 +86,7 @@ public static String getHotspotJavaScript() {
6886
buf.append("\",\r\n");
6987
}
7088
}
71-
}
89+
}
7290
return buf.toString();
7391
}
7492

@@ -174,13 +192,5 @@ private int tryToGetAPort() throws Exception {
174192
}
175193
return port;
176194
}
177-
178-
public static void main(String[] args) {
179-
new InnerHotspotServer().startServer();
180-
new InnerHotspotServer().startServer();
181-
new InnerHotspotServer().startServer();
182-
new InnerHotspotServer().startServer();
183-
addCompiledItem("hello.world.SeeYou");
184-
}
185195
}
186196

0 commit comments

Comments
 (0)