blob: 994d1cd91512e542472641af4ec06f8f26e492c1 (
plain)
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
|
package fossee.xcos.on.web;
/**
* Servlet code for xcos-on-web Authors: Maverick and Adhitya Description: The
* following servlet creates a shell command to run Scilab without any
* GUI/simulation window. The diagram sent from client is stored in a
* usr-xxx.xcos file and is then simulated. The result is a .png file which is
* then sent back to the client.
*/
import java.io.*;
import javax.servlet.*;
import java.util.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.FilenameUtils;
public class SciExec extends GenericServlet {
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
InputStream is = null;
ByteArrayOutputStream baos = null;
List<String> commands = new ArrayList<String>();
/**
* Maverick and Srikant: Creating a shell command to run Scilab without
* any GUI so that the simulation window doesn't appear.
*/
commands.add("scilab-adv-cli");
commands.add("-noatomsautoload");
commands.add("-nogui");
commands.add("-nb");
commands.add("-e");
String imagePath = "";
String fileNameWithoutExt = "";
/**
* Maverick: Ignore the following block.
*
* commands.add("plot3d();xs2png(gcf(),'img2.png');exit();"); String
* data=request.getParameter("name"); pw.println(data);
* commands.add("echo "+data+" >
* /home/saarang/Softwares/apache-tomcat-8.0.36/webapps/sci/servlet/file.xcos");
* commands.add("driver('PNG');xinit('/home/saarang/apache-tomcat-8.0.36/webapps/worknogui/servlet/ans"+k+".png');loadXcosLibs();importXcosDiagram('/home/saarang/apache-tomcat-8.0.36/file.xcos');xcos_simulate(scs_m,4);mode(2);xend();quit();");
*/
ProcessBuilder pb = new ProcessBuilder(commands);
try {
InputStream inputStream = request.getInputStream();
/**
* Adhitya: Creating a temporary file in the directory containing
* tomcat. This is the .xcos file which will be executed by the
* Scilab engine.
*/
File xcosFile = File.createTempFile("usr-", ".xcos");
OutputStream outputStream = new FileOutputStream(xcosFile);
IOUtils.copy(inputStream, outputStream);
outputStream.close();
String tempPath = xcosFile.getAbsolutePath();
String parentPath = xcosFile.getParent();
fileNameWithoutExt = FilenameUtils.removeExtension(xcosFile.getName());
/**
* Maverick: The resultant .svg file which is produced as a result
* of simulation is stored in a different folder inside the
* respective webapp directory. The path of the folder is
* /path/to/webapp/servlet.
*
* @ToDo: Modify the following path.
*/
imagePath = parentPath + File.separator + fileNameWithoutExt + ".svg";
/**
* Maverick: Creating the command which is to be executed using
* terminal.
*/
commands.add("driver('SVG');xinit('" + imagePath + "');loadXcosLibs();importXcosDiagram('" + tempPath + "');xcos_simulate(scs_m,4);mode(2);xend();quit();");
/**
* Maverick: Reference:
* http://stackoverflow.com/questions/23086778/how-to-access-unix-shell-special-variables-using-java
* Starting a process to execute the created command.
*/
Process prs = pb.start();
is = prs.getInputStream();
byte[] b = new byte[1024];
int size = 0;
baos = new ByteArrayOutputStream();
while ((size = is.read(b)) != -1) {
baos.write(b, 0, size);
}
System.out.println(new String(baos.toByteArray()));
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
System.out.println("Exception");
} finally {
try {
if (is != null) {
is.close();
}
if (baos != null) {
baos.close();
}
} catch (Exception ex) {
}
}
/**
* Adhitya: Accessing the .svg file and sending it to the client.
*/
OutputStream out = response.getOutputStream();
FileInputStream in = new FileInputStream(imagePath);
byte[] buffer = new byte[4096];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.flush();
}
}
|