Menu

[r489]: / trunk / php-java-bridge / tests.php5 / script_api.php  Maximize  Restore  History

Download this file

96 lines (89 with data), 3.0 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
<?php
/**
* This test checks the JSR223 script API. The test must be run three
* times, with the J2EE back end, the JavaBridgeRunner and standalone.
*
* In the J2EE environment the J2EE ContextRunner and ContextFactories
* must not interfere with the ContextRunner and ContextFactories
* created by the JavaBridgeRunner: check ContextRunner.runner, the
* keys from the ContextServers (the part after the @...) must be
* different, check if X_JAVABRIDGE_OVERRIDE_HOSTS works, so that PHP
* scripts started by the JavaBridgeRunner connect back to the
* JavaBridgeRunner and not to the default J2EE ContextRunner.
*
* When the JavaBridgeRunner is used, there must be exactly one
* JavaBridge runner with 6 ContextRunner and ContextFactories.
*
* In the standalone setup there must be 6 ContextRunner and
* ContextFactories and 5 CGIRunners.
*/
if (!extension_loaded('java')) {
if (!(require_once("http://127.0.0.1:8080/JavaBridge/java/Java.inc"))) {
echo "java extension not installed.";
exit(2);
}
}
/*
* Call the Java continuation from the PHP continuation. If that
* failed, start main().
*/
$IRunnable = new JavaClass("java.lang.Runnable");
java_context()->call(java_closure(new Runnable(), null, $IRunnable)) || main();
/**
* This class implements IRunnable. Its run method is called by each
* of the 5 Java threads. Each PHP thread writes $nr to the filename
* $name.out.
*/
class Runnable {
function run() {
$name = java_values(java_context()->getAttribute("name", 100)); // engine scope
$out = new Java("java.io.FileOutputStream", "$name.out", true);
$Thread = new JavaClass("java.lang.Thread");
$nr = java_values(java_context()->getAttribute("nr", 100));
echo "started thread: $nr\n";
for($i=0; $i<10; $i++) {
$out->write(ord("$nr"));
$Thread->yield();
}
$out->close();
}
}
/**
* Allocate a thread and an associated PHP continuation
*
* @arg nr The data will be passed to the PHP run method
* @arg name The current script name
* @return the PHP continuation.
*/
function createRunnable($nr, $name) {
global $IRunnable;
$r = new Java("php.java.script.PhpScriptEngine");
$r->eval(new Java("java.io.FileReader", $name));
$r->put("nr", $nr);
$r->put("name", $name);
$r->put("thread",new java("java.lang.Thread",$r->getInterface($IRunnable)));
return $r;
}
/**
* Creates and starts count PHP threads concurrently writing to
* SCRIPTNAME.out
*/
function main() {
$count = 5;
$argv = ($_SERVER['argv']);
$name = realpath($argv[0]);
$here = dirname($name);
java_require("$here/../modules/php-script.jar;$here/../modules/script-api.jar");
$runnables = array();
for($i=1; $i<=$count; $i++) {
$runnables[$i]=createRunnable($i, $name);
}
for($i=1; $i<=$count; $i++) {
$runnables[$i]->get("thread")->start();
}
for($i=1; $i<=$count; $i++) {
$runnables[$i]->get("thread")->join();
$runnables[$i]->release(); // release the PHP continuation
}
}
?>
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.