forked from micw/php-java-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestInstallation.java
More file actions
363 lines (344 loc) · 12.4 KB
/
TestInstallation.java
File metadata and controls
363 lines (344 loc) · 12.4 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*-*- mode: Java; tab-width:8 -*-*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLFrameHyperlinkEvent;
/**
* Used only for release tests
*/
public class TestInstallation implements Runnable {
// See Util.DEFAULT_CGI_LOCATIONS
static final String DEFAULT_CGI_LOCATIONS[] = new String[] {"/usr/bin/php-cgi", "c:/Program Files/PHP/php-cgi.exe"};
private static String socket;
private Process proc;
private static Process runner;
private static File base;
TestInstallation() {
}
TestInstallation(Process proc) {
this.proc = proc;
}
static class SimpleBrowser implements Runnable, HyperlinkListener {
private String port;
SimpleBrowser(String port) {
this.port = port;
}
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
JEditorPane pane = (JEditorPane) e.getSource();
if (e instanceof HTMLFrameHyperlinkEvent) {
HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent)e;
HTMLDocument doc = (HTMLDocument)pane.getDocument();
doc.processHTMLFrameHyperlinkEvent(evt);
} else {
try {
URL url = e.getURL();
if(url.getFile().endsWith("/"))
pane.setPage(e.getURL());
else {
JFrame frame = new JFrame(url.toExternalForm());
JEditorPane p = null;
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
p = new JEditorPane(url);
p.setEditable(false);
p.addHyperlinkListener(this);
JScrollPane scroll = new JScrollPane(p);
frame.getContentPane().add(scroll);
frame.setSize(800,600);
frame.setVisible(true);
}
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}
public void run() {
try {
URL url = new URL("http://127.0.0.1:"+port+"/server/tests.php5/");
JEditorPane p = null;
JFrame frame = new JFrame(url.toExternalForm());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p = new JEditorPane(url);
p.setEditable(false);
p.addHyperlinkListener(this);
JScrollPane scroll = new JScrollPane(p);
frame.getContentPane().add(scroll);
frame.setSize(800,600);
frame.setVisible(true);
} catch (IOException e) {
System.exit(0);
}
}
}
private static String findSocket() {
for(int i=8080; i<8080+200; i++) {
try {
ServerSocket s = new ServerSocket(i);
s.close();
return socket = String.valueOf(i);
} catch (Exception e) {/* ignore */}
}
return null;
}
private static final Class[] STRING_PARAM = new Class[]{String.class};
private static final Class[] EMPTY_PARAM = new Class[0];
private static final Object[] EMPTY_ARG = new Object[0];
private static final File winnt = new File("c:/winnt");
private static final File windows = new File("c:/windows");
private static final Map COMMON_ENVIRONMENT = getCommonEnvironment();
private static final HashMap processEnvironment = getProcessEnvironment();
private static String[] hashToStringArray(Map h)
throws NullPointerException {
Vector v = new Vector();
Iterator e = h.keySet().iterator();
while (e.hasNext()) {
String k = e.next().toString();
v.add(k + "=" + h.get(k));
}
String[] strArr = new String[v.size()];
v.copyInto(strArr);
return strArr;
}
private static HashMap getProcessEnvironment() {
HashMap defaultEnv = new HashMap();
String val = null;
// Bug in WINNT and WINXP.
// If SystemRoot is missing, php cannot access winsock.
if(winnt.isDirectory()) val="c:\\winnt";
else if(windows.isDirectory()) val = "c:\\windows";
try {
String s = System.getenv("SystemRoot");
if(s!=null) val=s;
} catch (Throwable t) {/*ignore*/}
try {
String s = System.getProperty("Windows.SystemRoot");
if(s!=null) val=s;
} catch (Throwable t) {/*ignore*/}
if(val!=null) defaultEnv.put("SystemRoot", val);
try {
Method m = System.class.getMethod("getenv", EMPTY_PARAM);
Map map = (Map) m.invoke(System.class, EMPTY_ARG);
defaultEnv.putAll(map);
} catch (Exception e) {
defaultEnv.putAll(COMMON_ENVIRONMENT);
}
return defaultEnv;
}
/**
* A map containing common environment values for JDK <= 1.4:
* "PATH", "LD_LIBRARY_PATH", "LD_ASSUME_KERNEL", "USER", "TMP", "TEMP", "HOME", "HOMEPATH", "LANG", "TZ", "OS"
* They can be set with e.g.: <code>java -DPATH="$PATH" -DHOME="$HOME" -jar JavaBridge.jar</code> or
* <code>java -DPATH="%PATH%" -jar JavaBridge.jar</code>.
*/
private static HashMap getCommonEnvironment() {
String entries[] = {
"PATH", "LD_LIBRARY_PATH", "LD_ASSUME_KERNEL", "USER", "TMP", "TEMP", "HOME", "HOMEPATH", "LANG", "TZ", "OS"
};
HashMap map = new HashMap(entries.length+10);
String val;
Method m = null;
try {m = System.class.getMethod("getenv", STRING_PARAM);} catch (Exception e) {/*ignore*/}
for(int i=0; i<entries.length; i++) {
val = null;
if (m!=null) {
try {
val = (String) m.invoke(System.class, (Object[])new String[]{entries[i]});
} catch (Exception e) {
m = null;
}
}
if(val==null) {
try { val = System.getProperty(entries[i]); } catch (Exception e) {/*ignore*/}
}
if(val!=null) map.put(entries[i], val);
}
return map;
}
private void readError() throws IOException {
int c;
InputStream in = proc.getErrorStream();
while((c=in.read())!=-1) System.err.write(c);
in.close();
}
private void startRunner() throws IOException {
String[] cmd = new String[] {(String.valueOf(new File(System.getProperty("java.home"), "bin"+File.separator+"java"))),
"-jar",
base+File.separator+"ext"+File.separator+"JavaBridge.jar",
"SERVLET_LOCAL:"+socket};
System.err.println("Starting a simple servlet engine: " + Arrays.asList(cmd));
Process p;
try {
p = runner = Runtime.getRuntime().exec(cmd);
} catch (java.io.IOException ex) {
throw new RuntimeException("Could not run "+Arrays.asList(cmd)+".", ex);
}
(new Thread(new TestInstallation(p))).start();
InputStream i = p.getInputStream();
int c;
while((c=i.read())!=-1) System.out.write(c);
System.out.flush();
System.err.flush();
}
/**{@inheritDoc}*/
public void run() {
try {
if(proc==null) startRunner();
else readError();
} catch (Exception e) {
e.printStackTrace();
}
}
/* Don't use Util or DynamicJavaBridgeClassLoader at this stage! */
private static final boolean checkGNUVM() {
try {
return "libgcj".equals(System.getProperty("gnu.classpath.vm.shortname"));
} catch (Throwable t) {
return false;
}
}
/**
*
* @param args
*/
public static void main(String[] args) {
try { // Hack for Unix: execute the standalone container using the default SUN VM
if(args.length==0 && (new File("/usr/java/default/bin/java")).exists() && checkGNUVM() && (System.getProperty("php.java.bridge.exec_sun_vm", "true").equals("true"))) {
Process p = Runtime.getRuntime().exec(new String[] {"/usr/java/default/bin/java", "-Dphp.java.bridge.exec_sun_vm=false", "-classpath", System.getProperty("java.class.path"), "TestInstallation"});
(new Thread() {
InputStream in;
public Thread init(InputStream in) { this.in = in; return this; }
public void run() { int c; try { while((c=in.read())!=-1) System.out.write(c); } catch (IOException e) { e.printStackTrace(); } }
}).init(p.getInputStream()).start();
(new Thread() {
InputStream in;
public Thread init(InputStream in) { this.in = in; return this; }
public void run() { int c; try { while((c=in.read())!=-1) System.err.write(c); } catch (IOException e) { e.printStackTrace(); } }
}).init(p.getErrorStream()).start();
if(p != null) System.exit(p.waitFor());
}
} catch (Throwable t) {/*ignore*/}
try {
start(args);
} catch (Throwable t) {
t.printStackTrace();
if(runner!=null) runner.destroy();
System.exit(1);
}
}
static void start(String[] args) throws Exception {
String socket = findSocket();
String os = null;
String separator = "/-+.,;: ";
try {
String val = System.getProperty("os.name").toLowerCase();
StringTokenizer t = new StringTokenizer(val, separator);
os = t.nextToken();
} catch (Throwable t) {/*ignore*/}
if(os==null) os="unknown";
File ext = null;
try {
ext = (args.length==0) ? new File(new File(System.getProperty("java.class.path")).getParentFile().getAbsoluteFile(), "ext") : new File(args[0], "ext");
ext = ext.getAbsoluteFile();
} catch (Throwable t) {
ext = (args.length==0) ? new File("ext") : new File(args[0], "ext");
ext = ext.getAbsoluteFile();
}
if(!ext.isDirectory()) ext.mkdirs();
base = ext.getParentFile();
File java = new File(base, "java");
if(!java.isDirectory()) java.mkdirs();
ClassLoader loader = TestInstallation.class.getClassLoader();
InputStream in = loader.getResourceAsStream("WEB-INF/lib/JavaBridge.jar");
extractFile(in, new File(ext, "JavaBridge.jar").getAbsoluteFile());
in.close();
in = loader.getResourceAsStream("WEB-INF/lib/php-script.jar");
extractFile(in, new File(ext, "php-script.jar").getAbsoluteFile());
in.close();
in = loader.getResourceAsStream("WEB-INF/lib/php-servlet.jar");
extractFile(in, new File(ext, "php-servlet.jar").getAbsoluteFile());
in.close();
in = loader.getResourceAsStream("WEB-INF/lib/script-api.jar");
extractFile(in, new File(ext, "script-api.jar").getAbsoluteFile());
in.close();
in = loader.getResourceAsStream("test.php");
extractFile(in, new File(base, "test.php").getAbsoluteFile());
in.close();
// start back end
(new Thread(new TestInstallation())).start();
int count = 20;
while(count-->0) {
Thread.sleep(500);
try {Socket s = new Socket("127.0.0.1", Integer.parseInt(socket)); if(s!=null) s.close(); break;} catch (IOException e) {/* ignore */}
}
if(count==0) throw new IOException("Could not start test servlet engine");
// Fetch the Java.inc file
URL url = new URL("http://127.0.0.1:"+socket+"/JavaBridge/java/Java.inc");
URLConnection conn = url.openConnection();
conn.connect();
in = conn.getInputStream();
extractFile(in, new File(java, "Java.inc").getAbsoluteFile());
in.close();
FileOutputStream o = new FileOutputStream(new File(base,"RESULT.html"));
String php = "php-cgi";
for(int i=0; i<DEFAULT_CGI_LOCATIONS.length; i++) {
File location = new File(DEFAULT_CGI_LOCATIONS[i]);
if(location.exists()) {php = location.getAbsolutePath(); break;}
}
// start front end
String[] cmd = new String[] {php, "-n", "-d","allow_url_include=On",String.valueOf(new File(base,"test.php"))};
System.err.println("Invoking php: " + Arrays.asList(cmd));
HashMap env = (HashMap) processEnvironment.clone();
env.put("SERVER_PORT", socket);
env.put("X_JAVABRIDGE_OVERRIDE_HOSTS", "h:127.0.0.1:"+socket+"//JavaBridge/test.phpjavabridge");
Process p;
try {
p = Runtime.getRuntime().exec(cmd, hashToStringArray(env));
} catch (java.io.IOException ex) {
throw new RuntimeException("Could not run PHP ("+Arrays.asList(cmd)+"), please check if php-cgi is in the path.", ex);
}
(new Thread(new TestInstallation(p))).start();
InputStream i = p.getInputStream();
int c;
while((c=i.read())!=-1) o.write(c);
i.close();
p.getOutputStream().close();
o.close();
//p.destroy(); if(runner!=null) runner.destroy();
System.out.flush();
System.err.flush();
System.out.println("\nNow check the " + new File(base, "RESULT.html."));
System.out.println("Read the INSTALL.J2EE and/or INSTALL.J2SE documents.");
try {
SwingUtilities.invokeAndWait(new SimpleBrowser(socket));
} catch (Throwable err) {System.exit(0);}
}
private static void extractFile(InputStream in, File target) throws IOException {
byte[] buf = new byte[8192];
FileOutputStream out = new FileOutputStream(target);
int c;
while((c = in.read(buf))!=-1) {
out.write(buf, 0, c);
}
out.close();
}
}