Skip to content

Commit 30e4c14

Browse files
committed
refactory code and add angular js
1 parent bd65838 commit 30e4c14

File tree

4 files changed

+125
-77
lines changed

4 files changed

+125
-77
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.codeboy.dshell.webshell;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.io.InputStreamReader;
7+
import java.util.List;
8+
9+
import com.codeboy.dshell.common.DistributedNode;
10+
import com.sun.net.httpserver.HttpExchange;
11+
import com.sun.net.httpserver.HttpHandler;
12+
//http://localhot:8898/distributedshell
13+
14+
public class AMMainPageHandler implements HttpHandler {
15+
private List<DistributedNode> nodes;
16+
private String URL_TEMPLATE = "$distributed-shell-url";
17+
private DistributedNode masterNode;
18+
19+
public AMMainPageHandler(DistributedNode masterNode,
20+
List<DistributedNode> nodes) {
21+
this.nodes = nodes;
22+
this.masterNode = masterNode;
23+
}
24+
25+
/**
26+
* First version is hardcoded, it is fine now
27+
*/
28+
@Override
29+
public void handle(HttpExchange t) throws IOException {
30+
String htmlTemplate = readTemplateHTML("/webshell.html");
31+
String response = htmlTemplate.replace(URL_TEMPLATE,
32+
masterNode.getHttpBaseURL()
33+
+ WebShellServer.AM_REST_URL_EXEX_CMD);
34+
WebShellServer.sendResponse(t, response);
35+
}
36+
37+
private String readTemplateHTML(String htmlTemplateResource)
38+
throws IOException {
39+
InputStream stream = WebShellServer.class
40+
.getResourceAsStream(htmlTemplateResource);
41+
InputStreamReader is = new InputStreamReader(stream);
42+
StringBuilder sb = new StringBuilder();
43+
BufferedReader br = new BufferedReader(is);
44+
String read = br.readLine();
45+
46+
while (read != null) {
47+
// System.out.println(read);
48+
//${user.name}
49+
read = read.replace("${shell.promp}", getBashPrompt()) ;
50+
read = read.replace("${welcome.msg}",getWelComeMessage()) ;
51+
sb.append(read).append("\n");
52+
read = br.readLine();
53+
}
54+
55+
return sb.toString();
56+
}
57+
58+
private CharSequence getWelComeMessage() {
59+
// return "Welcome to yarn distributed shell:\n"+
60+
// "SHELL = "+System.getenv("SHELL")+"\n"+
61+
// "------------------------------------------------------------------\n" +
62+
return getBashPrompt() ;
63+
}
64+
65+
public static String getBashPrompt() {
66+
return "["+WebShellServer.userName+"]$";
67+
}
68+
}

src/main/java/com/codeboy/dshell/webshell/CommandExecutor.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66
import java.io.InputStreamReader;
77
import java.io.Reader;
88
import java.io.StringWriter;
9+
import java.io.UnsupportedEncodingException;
910
import java.io.Writer;
11+
import java.net.URLDecoder;
1012

1113
public class CommandExecutor {
1214

13-
public static String executeCommand(String command, boolean waitForResponse) {
15+
public static String executeCommand(String command, boolean waitForResponse) throws UnsupportedEncodingException {
1416

1517
String response = "";
16-
18+
command = URLDecoder.decode(command,"UTF-8") ;
1719
ProcessBuilder pb = new ProcessBuilder("bash", "-c", command);
1820
pb.redirectErrorStream(true);
1921

@@ -50,7 +52,7 @@ public static String executeCommand(String command, boolean waitForResponse) {
5052
+ e.getMessage());
5153
}
5254

53-
return response;
55+
return response+"\n" +AMMainPageHandler.getBashPrompt();
5456
}
5557

5658
/*

src/main/java/com/codeboy/dshell/webshell/WebShellServer.java

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class WebShellServer {
3636
private Boolean started = false;
3737

3838
List<DistributedNode> nodes = null;
39+
static final String userName = System.getProperty("user.name");
3940

4041
private MasterNode masterNode;
4142

@@ -78,6 +79,7 @@ public void handle(HttpExchange t) throws IOException {
7879
server.createContext(REST_URL_KILLME, new HttpHandler() {
7980
@Override
8081
public void handle(HttpExchange t) throws IOException {
82+
System.out.println("Got a command of killme! exit now!");
8183
System.exit(0);
8284
}
8385
});
@@ -108,8 +110,7 @@ public static void main(String args[]) throws IOException {
108110
String mainPageURLPath = System.getProperty("mainPageURLPath");
109111
String host = System.getProperty("host");
110112
String port = System.getProperty("port");
111-
String userName = System.getProperty("user.name");
112-
if (mainPageURLPath == null || mainPageURLPath.isEmpty()) {
113+
if (mainPageURLPath == null || mainPageURLPath.isEmpty()) {
113114
mainPageURLPath = "/distributedshell";
114115
}
115116
if (host == null || host.isEmpty()) {
@@ -131,49 +132,6 @@ public static void main(String args[]) throws IOException {
131132
* @author zhaoyong
132133
*
133134
*/
134-
// http://localhot:8898/distributedshell
135-
class AMMainPageHandler implements HttpHandler {
136-
private List<DistributedNode> nodes;
137-
private String URL_TEMPLATE = "$distributed-shell-url";
138-
private DistributedNode masterNode;
139-
140-
public AMMainPageHandler(DistributedNode masterNode,
141-
List<DistributedNode> nodes) {
142-
this.nodes = nodes;
143-
this.masterNode = masterNode;
144-
}
145-
146-
/**
147-
* First version is hardcoded, it is fine now
148-
*/
149-
@Override
150-
public void handle(HttpExchange t) throws IOException {
151-
String htmlTemplate = readTemplateHTML("/webshell.html");
152-
String response = htmlTemplate.replace(URL_TEMPLATE,
153-
masterNode.getHttpBaseURL()
154-
+ WebShellServer.AM_REST_URL_EXEX_CMD);
155-
WebShellServer.sendResponse(t, response);
156-
}
157-
158-
private String readTemplateHTML(String htmlTemplateResource)
159-
throws IOException {
160-
InputStream stream = WebShellServer.class
161-
.getResourceAsStream(htmlTemplateResource);
162-
InputStreamReader is = new InputStreamReader(stream);
163-
StringBuilder sb = new StringBuilder();
164-
BufferedReader br = new BufferedReader(is);
165-
String read = br.readLine();
166-
167-
while (read != null) {
168-
// System.out.println(read);
169-
sb.append(read).append("\n");
170-
read = br.readLine();
171-
}
172-
173-
return sb.toString();
174-
}
175-
}
176-
177135
// http://localhot:8898/rest/execmd?cmd=ls
178136
class AMRESTAPIHandler implements HttpHandler {
179137
private List<DistributedNode> nodes;

src/main/resources/webshell.html

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,78 @@
11

22
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
3-
<html>
3+
<html ng-app="dsApp">
44
<head>
55
<title></title>
66
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7+
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.1/angular.min.js"></script>
78
<script language="javascript">
8-
var xmlhttp;
9-
function init() {
10-
// put more code here in case you are concerned about browsers that do not provide XMLHttpRequest object directly
11-
xmlhttp = new XMLHttpRequest();
12-
}
13-
14-
function executeCommand() {
15-
var url = "$distributed-shell-url?cmd=" + shell_command.value;
16-
xmlhttp.open('GET',url,true);
17-
xmlhttp.send(null);
18-
xmlhttp.onreadystatechange = function() {
199

20-
var resultTextNode = document.getElementById("shell_result");
21-
if (xmlhttp.readyState == 4) {
22-
if ( xmlhttp.status == 200) {
23-
//var result = eval( "(" + xmlhttp.responseText + ")");
24-
var result = xmlhttp.responseText ;
10+
var initShell = "${welcome.msg}" ;
11+
12+
var myDSApp = angular.module('dsApp', []);
13+
myDSApp.controller('ShellCtrl', function ($scope, $http){
14+
$scope.shell_content=initShell;
15+
});
16+
17+
18+
19+
//do it in agular http later
20+
//todo add color in the shell
21+
function executeCommand() {
22+
var url = "$distributed-shell-url?cmd=" + shell_command.value;
23+
var xmlhttp = new XMLHttpRequest();
2524

26-
document.getElementById("shell_result").value=result;
27-
}
28-
}
29-
25+
xmlhttp.open('GET',url,true);
26+
xmlhttp.send(null);
27+
28+
xmlhttp.onreadystatechange = function() {
29+
30+
var resultTextNode = document.getElementById("shell_result");
31+
if (xmlhttp.readyState == 4) {
32+
if ( xmlhttp.status == 200) {
33+
//var result = eval( "(" + xmlhttp.responseText + ")");
34+
var result = xmlhttp.responseText ;
35+
36+
document.getElementById("shell_result").value= resultTextNode.value+"\n"+result;
37+
}
38+
}
3039

31-
};
32-
}
40+
41+
};
42+
}
43+
3344
</script>
3445
</head>
35-
<body onload="init()">
46+
<body ng-controller="ShellCtrl">
3647
<h3>
3748
Hadoop Yarn Distributed Shell
3849
</h1>
3950
<table>
4051

4152
<tr>
42-
<td>Input Shell Command Here : <input type="button"
43-
value="Kill me" onclick="killme()"></input>
44-
</td>
53+
<td>Input Shell Command Here : </td>
4554
<td></td>
4655
</tr>
4756
<tr>
48-
<td><input type="text" id="shell_command" size="100"> </input>
57+
<td><input type="text" id="shell_command" size="60"> </input>
4958
</td>
5059
<td>
5160
<button type="button" onclick="executeCommand()">Run</button>
5261
</td>
5362
</tr>
5463
<tr>
55-
<td><textarea id="shell_result" style="width:400px;height:400px;"> </textarea></td>
64+
<td>
65+
<textarea id="shell_result" cols="20" rows="1000" style="width:400px;height:400px; text-align:left">
66+
{{shell_content}}
67+
</textarea>
68+
</td>
69+
<td></td>
70+
</tr>
71+
72+
<tr>
73+
<td><input type="button"
74+
value="Kill me" onclick="killme()"></input> </td>
75+
<td></td>
5676
</tr>
5777
</table>
5878
</body>

0 commit comments

Comments
 (0)