Shaokang's Blog

A simple JSP Java emulator was built to demonstrate the solution code behavior of the DSC 30 final project, where I served as a TA. The entire program was deployed to azure, containing code for handling different threads to run the solution code in the cloud instantly and render the running result on https://myplayer.azurewebsites.net

The Servlet part of managing, rendering, and connecting applications and front-end:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package website;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
* Servlet implementation class hello
*/
public class hello extends HttpServlet {
private static final long serialVersionUID = 1L;

private static final HashMap<String, Process> runner = new HashMap<String, Process>();
private static final HashMap<String, Long> runtime = new HashMap<String, Long>();

/**
* @see HttpServlet#HttpServlet()
*/
public hello() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
HttpSession session = request.getSession();
try {
if (runner.containsKey(session.getId())) {
// continue
String code = request.getParameter("code");
if (code.isEmpty()) {
response.setContentType("application/json");
PrintWriter out = response.getWriter();
out.print("");
out.flush();
} else if (code.trim().compareTo("13") == 0) {
if (runner.containsKey(session.getId())) {
runner.get(session.getId()).destroy();
}
runner.remove(session.getId());
runtime.remove(session.getId());
response.setContentType("application/json");
PrintWriter out = response.getWriter();
out.print("Exit, please close browser.");
out.flush();
} else {
Process p = runner.get(session.getId());
OutputStream os = p.getOutputStream();
InputStream in = p.getInputStream();
os.write((code + System.lineSeparator()).getBytes());
os.flush();
BufferedReader dis = new BufferedReader(new InputStreamReader(in));
String disr = "";
while (true) {
String newLine = dis.readLine();
if (newLine.trim().equalsIgnoreCase("end")) {
break;
}
disr += newLine + "\n";
}

response.setContentType("application/json");
PrintWriter out = response.getWriter();
out.print(disr);
out.flush();

runtime.put(session.getId(), session.getLastAccessedTime());
}
} else {
String javaHome = System.getProperty("java.home");
String javaBin = javaHome + File.separator + "bin" + File.separator + "java";
File f = new File(MyPlayer.class.getProtectionDomain().getCodeSource().getLocation().getPath());
String className = MyPlayer.class.getName();
List<String> command = new LinkedList<String>();
command.add(javaBin);
command.add("-cp");
command.add(f.getAbsolutePath());
command.add(className);
ProcessBuilder builder = new ProcessBuilder(command);

Process p = builder.start();
OutputStream os = p.getOutputStream();
InputStream in = p.getInputStream();
BufferedReader dis = new BufferedReader(new InputStreamReader(in));
String disr = "";
while (true) {
String newLine = dis.readLine();
if (newLine.trim().equalsIgnoreCase("end")) {
break;
}
disr += newLine + "\n";
}

response.setContentType("application/json");
PrintWriter out = response.getWriter();
out.print(disr);
out.flush();

runner.put(session.getId(), p);
runtime.put(session.getId(), session.getLastAccessedTime());
}
} catch (Exception e) {
if (runner.containsKey(session.getId())) {
runner.get(session.getId()).destroy();
}
runner.remove(session.getId());
runtime.remove(session.getId());
response.setContentType("application/json");
PrintWriter out = response.getWriter();
e.printStackTrace(out);
out.flush();
}

// remove all long running job
for (String a : runtime.keySet()) {
if ((session.getLastAccessedTime() - runtime.get(a)) >= 180000) {// 3 minutes
if (runner.containsKey(a)) {
runner.get(a).destroy();
}
runner.remove(a);
runtime.remove(a);
}
}
}

public void destroy() {
for (String a : runner.keySet()) {
runner.get(a).destroy();
runner.remove(a);
runtime.remove(a);
}
}

}

 Comments