forked from micw/php-java-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhpScriptEngine.java
More file actions
149 lines (129 loc) · 5.11 KB
/
Copy pathPhpScriptEngine.java
File metadata and controls
149 lines (129 loc) · 5.11 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
/*-*- 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.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringReader;
import java.io.Writer;
import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptException;
import php.java.bridge.Util;
import php.java.bridge.http.IContext;
/**
* This class implements the ScriptEngine.<p>
* Example:<p>
* <code>
* ScriptEngine e = (new ScriptEngineManager()).getEngineByName("php");<br>
* try { e.eval(<?php foo() ?>"); } catch (ScriptException e) { ... }<br>
* </code>
* @author jostb
*
*/
public class PhpScriptEngine extends AbstractPhpScriptEngine {
/**
* Create a new ScriptEngine with a default context.
*/
public PhpScriptEngine() {
super(new PhpScriptEngineFactory());
}
/**
* Create a new ScriptEngine from a factory.
* @param factory The factory
* @see #getFactory()
*/
public PhpScriptEngine(PhpScriptEngineFactory factory) {
super(factory);
}
/**
* Create a new ScriptEngine with bindings.
* @param n the bindings
*/
public PhpScriptEngine(Bindings n) {
this();
setBindings(n, ScriptContext.ENGINE_SCOPE);
}
protected Reader getLocalReader(Reader reader,boolean embedJavaInc) throws IOException {
/* header: <? require_once("http://localhost:<ourPort>/JavaBridge/java/Java.inc"); ?> */
ByteArrayOutputStream out = new ByteArrayOutputStream();
Writer w = new OutputStreamWriter(out);
try {
Reader localReader = null;
char[] buf = new char[Util.BUF_SIZE];
int c;
String stdHeader = embedJavaInc ? null : ((IContext)getContext()).getRedirecturl("/JavaBridge");
localReader = new StringReader(getStandardHeader(stdHeader));
while((c=localReader.read(buf))>0) w.write(buf, 0, c);
localReader.close(); localReader=null;
/* the script: */
while((c=reader.read(buf))>0) w.write(buf, 0, c);
w.close(); w = null;
/* now evaluate our script */
localReader = new InputStreamReader(new ByteArrayInputStream(out.toByteArray()));
return localReader;
} finally {
if (w!=null) try {w.close();} catch (IOException e) {/*ignore*/}
}
}
protected Object doEvalPhp(Reader reader, ScriptContext context) throws ScriptException {
if((continuation != null) || (reader == null) ) release();
if(reader==null) return null;
setNewContextFactory();
Reader localReader = null;
try {
localReader = getLocalReader(reader, false);
this.script = doEval(localReader, context);
} catch (Exception e) {
Util.printStackTrace(e);
if (e instanceof RuntimeException) throw (RuntimeException)e;
if (e instanceof ScriptException) throw (ScriptException)e;
throw new ScriptException(e);
} finally {
if(localReader!=null) try { localReader.close(); } catch (IOException e) {/*ignore*/}
// release the engine, so that any error reported by the script can trigger a Java exception
release();
}
return resultProxy;
}
protected Object doEvalCompiledPhp(Reader reader, ScriptContext context) throws ScriptException {
if((continuation != null) || (reader == null) ) release();
if(reader==null) return null;
setNewContextFactory();
try {
this.script = doEval(reader, context);
} catch (Exception e) {
Util.printStackTrace(e);
if (e instanceof RuntimeException) throw (RuntimeException)e;
if (e instanceof ScriptException) throw (ScriptException)e;
throw new ScriptException(e);
} finally {
// release the engine, so that any error reported by the script can trigger a Java exception
release();
}
return resultProxy;
}
}