forked from micw/php-java-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaBridgeScriptRunner.java
More file actions
192 lines (182 loc) · 6.76 KB
/
Copy pathJavaBridgeScriptRunner.java
File metadata and controls
192 lines (182 loc) · 6.76 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*-*- mode: Java; tab-width:8 -*-*/
package php.java.script;
/*
* Copyright (C) 2003-2007 Jost Boekemeier
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import php.java.bridge.JavaBridgeRunner;
import php.java.bridge.Util;
import php.java.bridge.http.HttpRequest;
import php.java.bridge.http.HttpResponse;
/**
* This is the main entry point for the PHP/Java Bridge library.
* Example:<br>
* public MyClass { <br>
* public static void main(String s[]) {<br>
* JavaBridgeRunner runner = JavaBridgeRunner.getInstance();<br>
* // connect to port 9267 and send protocol requests ... <br>
* runner.destroy();<br>
* }<br>
* }<br>
* @author jostb
* @see php.java.script.PhpScriptContext
*/
public class JavaBridgeScriptRunner extends JavaBridgeRunner {
private ScriptEngineManager m = new ScriptEngineManager();
protected JavaBridgeScriptRunner() throws IOException {
super();
}
public JavaBridgeScriptRunner(String serverPort, boolean isSecure) throws IOException {
super(serverPort, isSecure);
}
public JavaBridgeScriptRunner(String serverPort) throws IOException {
super(serverPort);
}
/**
* Return a instance.
* @param serverPort The server port name
* @param isSecure use https instead of http
* @return a standalone runner
* @throws IOException
*/
public static synchronized JavaBridgeRunner getRequiredInstance(String serverPort, boolean isSecure) throws IOException {
if(runner!=null) return runner;
runner = new JavaBridgeScriptRunner(serverPort, isSecure);
return runner;
}
/**
* Return a instance.
* @param serverPort The server port name
* @param isSecure use https instead of http
* @return a standalone runner
*/
public static synchronized JavaBridgeRunner getInstance(String serverPort, boolean isSecure) {
if(runner!=null) return runner;
try {
runner = new JavaBridgeScriptRunner(serverPort, isSecure);
} catch (IOException e) {
Util.printStackTrace(e);
}
return runner;
}
/**
* Return a instance.
* @param serverPort The server port name
* @return a standalone runner
* @throws IOException
*/
public static synchronized JavaBridgeRunner getRequiredInstance(String serverPort) throws IOException {
if(runner!=null) return runner;
runner = new JavaBridgeScriptRunner(serverPort);
return runner;
}
/**
* Return a instance.
* @param serverPort The server port name
* @return a standalone runner
*/
public static synchronized JavaBridgeRunner getInstance(String serverPort) {
if(runner!=null) return runner;
try {
runner = new JavaBridgeScriptRunner(serverPort);
} catch (IOException e) {
Util.printStackTrace(e);
}
return runner;
}
/**
* Return a instance.
* @return a standalone runner
* @throws IOException
*/
public static synchronized JavaBridgeRunner getRequiredInstance() throws IOException {
if(runner!=null) return runner;
runner = new JavaBridgeScriptRunner();
return runner;
}
/**
* Evaluate the script engine. The engine is searched through the discovery mechanism. Add the "php-script.jar" or some other
* JSR223 script engine to the java ext dirs (usually /usr/share/java/ext or /usr/java/packages/lib/ext) and start the HTTP server:
* java -jar JavaBridge.jar HTTP_LOCAL:8080. Browse to http://localhost:8080/test.php.
* @param f The full name as a file
* @param params The request parameter
* @param length The length of the file
* @param req The HTTP request object
* @param res The HTTP response object
* @return true if the runner could evaluate the script, false otherwise.
* @throws IOException
*/
protected boolean handleScriptContent(String name, String params, File f, int length, HttpRequest req, HttpResponse res) throws IOException {
if("show".equals(params))
return false;
int extIdx = name.lastIndexOf('.');
if(extIdx == -1) return false;
String ext = name.substring(extIdx+1);
try {
if("php".equals(ext))
ext="phtml"; // we don't want bug reports from "quercus" users
ScriptEngine engine = m.getEngineByExtension(ext);
if(engine==null) return false;
ByteArrayOutputStream xout = new ByteArrayOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(xout, Util.UTF8));
ScriptContext ctx = engine.getContext();
ctx = new PhpJavaBridgeRunnerScriptContext(ctx, this);
ctx.setWriter(writer);
ctx.setErrorWriter(writer);
if (isSecure) engine.setContext(new PhpSecureScriptContext(ctx));
StringBuffer buf = new StringBuffer("/");
buf.append(name);
if(params!=null) {
buf.append("?");
buf.append(params);
}
FileReader r = null;;
try {
engine.eval(r = new FileReader(f));
} catch (Throwable e1) {
e1.printStackTrace(writer);
Util.printStackTrace(e1);
} finally {
try { engine.eval((Reader)null); } catch (Exception e1) {/*ignore*/};
if(r!=null) try { r.close(); } catch (Exception e1) {/*ignore*/};
}
res.addHeader("Content-Type", "text/html; charset=UTF-8");
res.setContentLength(xout.size());
OutputStream out = res.getOutputStream();
writer.close();
xout.writeTo(out);
} catch (Exception e) {
Util.printStackTrace(e);
return false;
}
return true;
}
}