Menu

[r457]: / trunk / php-java-bridge / java / JavaProxy.php  Maximize  Restore  History

Download this file

378 lines (361 with data), 12.2 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php /*-*- mode: php; tab-width:4 -*-*/
/* java_Proxy.php -- contains the main interface
Copyright (C) 2006 Jost Boekemeier
This file is part of the PHP/Java Bridge.
This file ("the library") is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2, or (at
your option) any later version.
The library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with the PHP/Java Bridge; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this file statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
require_once("java/Client.php");
function __javaproxy_Client_getClient() {
static $client;
if(!isset($client)) {
$client=new java_Client();
}
return $client;
}
function java_last_exception_get() {
$client = __javaproxy_Client_getClient();
return $client->getProperty(0, "lastException");
}
function java_last_exception_clear() {
$client = __javaproxy_Client_getClient();
$client->setProperty(0, "lastException", null);
}
function java_values($object) {
$client = __javaproxy_Client_getClient();
return $client->invokeMethod(0, "getValues", array($object));
}
function java_reset() {
$client = __javaproxy_Client_getClient();
echo ("ERROR: Your script has called the privileged procedure \"java_reset()\" which resets the java back-end to its initial state. Therefore all java caches are gone.");
return $client->invokeMethod(0, "reset", array());
}
function java_inspect($object) {
$client = __javaproxy_Client_getClient();
return $client->invokeMethod(0, "inspect", array($object));
}
function java_set_file_encoding($enc) {
$client = __javaproxy_Client_getClient();
return $client->invokeMethod(0, "setFileEncoding", array($enc));
}
function java_instanceof($ob, $clazz) {
$client = __javaproxy_Client_getClient();
return $client->invokeMethod(0, "instanceOf", array($ob, $clazz));
}
function java_require($arg) {
$client = __javaproxy_Client_getClient();
return $client->invokeMethod(0, "updateJarLibraryPath",
array($arg, ini_get("extension_dir")));
}
function java_session_internal($args) {
$client = __javaproxy_Client_getClient();
if(!isset($args[1])) $args[1]=false;
if(!isset($args[2])) {
$sesion_max_lifetime=ini_get("session.gc_maxlifetime");
if(is_null($session_max_lifetime)) $session_max_lifetime=1440;
$args[2] = $session_max_lifetime;
}
return $client->getSession($args);
}
function java_session() {
return java_session_internal(func_get_args());
}
function java_servername() {
$client = __javaproxy_Client_getClient();
return $client->getServerName();
}
function java_context() {
$client = __javaproxy_Client_getClient();
return $client->getContext();
}
function java_closure_internal($args) {
$client = __javaproxy_Client_getClient();
$args[0] = isset($args[0]) ? $client->globalRef->add($args[0]) : 0;
return $client->invokeMethod(0, "makeClosure", $args);
}
function java_closure() {
return java_closure_internal(func_get_args());
}
function java_begin_document() {
$client = __javaproxy_Client_getClient();
$rc = $client->invokeMethod(0, "beginDocument", array());
$client->setAsyncHandler();
return $rc;
}
function java_end_document() {
$client = __javaproxy_Client_getClient();
$client->setDefaultHandler();
return $client->invokeMethod(0, "endDocument", array());
}
function java_create($args, $instance) {
$client = __javaproxy_Client_getClient();
$name = array_shift($args);
$rc = $client->createObject($name, $args, $instance);
return $rc;
}
class java_JavaProxy {
var $__serialID, $__java, $__client;
function java_JavaProxy($java){
$this->__client = __javaproxy_Client_getClient();
$this->__java=$java;
}
function __cast($type) {
return $this->__client->cast($this, $type);
}
function __sleep() {
$lifetime = ini_get("session.gc_maxlifetime");
if(!isset($lifetime)) $lifetime = 1440;
$args = array($this, $lifetime);
$this->__serialID = $this->__client->invokeMethod(0, "serialize", $args);
return array("__serialID");
}
function __wakeup() {
$lifetime = ini_get("session.gc_maxlifetime");
if(!isset($lifetime)) $lifetime = 1440;
if(!isset($this->__client)) $this->__client=__javaproxy_Client_getClient();
$args = array($this->__serialID, $lifetime);
$this->__java = $this->__client->invokeMethod(0, "deserialize", $args);
}
function __destruct() {
$this->__client->unref($this->__java);
}
function __get($key) {
return $this->__client->getProperty($this->__java, $key);
}
function __set($key, $val) {
$this->__client->setProperty($this->__java, $key, $val);
}
function __call($method, $args) {
return $this->__client->invokeMethod($this->__java, $method, $args);
}
function __toString() {
return $this->__client->invokeMethod(0,"ObjectToString",array($this));
}
}
class java_JavaProxyClass extends java_JavaProxy {
function java_JavaProxyClass($java){
parent::java_JavaProxy($java);
}
}
class java_objectIterator implements Iterator {
var $javaProxy;
var $phpMap, $__java;
var $hasNext;
function java_ObjectIterator($javaProxy) {
$this->proxy = $javaProxy;
$this->__client = $javaProxy->__client;
}
function rewind() {
$proxy = array($this->proxy);
$this->phpMap = $this->__client->invokeMethod(0, "getPhpMap", $proxy);
$this->__java = $this->phpMap->__java;
}
function valid() {
if(isset($this->hasNext)) return $this->hasNext;
return $this->hasNext =
$this->__client->invokeMethod($this->__java, "hasMore", array());
}
function next() {
return $this->hasNext =
$this->__client->invokeMethod($this->__java, "moveForward", array());
}
function key() {
return
$this->__client->invokeMethod($this->__java, "currentKey", array());
}
function current() {
return
$this->__client->invokeMethod($this->__java, "currentData", array());
}
}
class java_IteratorProxy extends java_JavaProxy implements IteratorAggregate {
function java_IteratorProxy($java) {
parent::java_JavaProxy($java);
}
function getIterator() {
return new java_ObjectIterator($this);
}
}
class java_ArrayProxy extends java_IteratorProxy implements ArrayAccess {
function java_ArrayProxy($java) {
parent::java_JavaProxy($java);
}
function offsetExists($idx) {
$ar = array($this, $idx);
return $this->__client->invokeMethod(0,"offsetExists", $ar);
}
function offsetGet($idx) {
$ar = array($this, $idx);
return $this->__client->invokeMethod(0,"offsetGet", $ar);
}
function offsetSet($idx, $val) {
$ar = array($this, $idx, $val);
return $this->__client->invokeMethod(0,"offsetSet", $ar);
}
function offsetUnset($idx) {
$ar = array($this, $idx);
return $this->__client->invokeMethod(0,"offsetUnset", $ar);
}
}
class java_ExceptionProxy extends java_JavaProxy {
function java_ExceptionProxy($java){
parent::java_JavaProxy($java);
}
function __toExceptionString($trace) {
$args = array($this, $trace);
return $this->__client->invokeMethod(0,"ObjectToString",$args);
}
}
/**
* This decorator overrides all magic methods and delegates to the
* proxy so that it may handle them or pass them on to the back-end.
* The delegate isn't passed via the constructor but allocated from
* the back-end, based on the type of the Java instance, see
* PROTOCOL.TXT: "p: char ([A]rray, [C]ollection, [O]bject,
* [E]xception)". See the getProxy() and create() methods in
* Client.php and writeObject() and getType() in Response.java.<p>
*
* The constructor is an exception. If it is called, the user has
* already allocated Java, so that $wrap is false and the proxy is
* returned and set into $__delegate.
* @see #java_InternalJava
*/
class Java extends java_JavaProxy implements IteratorAggregate, ArrayAccess {
var $__delegate;
function Java() {
$delegate = $this->__delegate = java_create(func_get_args(), true);
$this->__java = $delegate->__java;
$this->__client = $delegate->__client;
}
function __cast($type) {
return $this->__delegate->__cast($type);
}
function __sleep() {
$rc = $this->__delegate->__sleep();
$this->__serialID = $this->__delegate->__serialID;
return array("__delegate");
}
function __wakeup() {
$this->__delegate->__wakeup();
$this->__java = $this->__delegate->__java;
}
function __destruct() {
$this->__delegate = null;
$this->__client = null;
}
function __get($key) {
return $this->__delegate->__get($key);
}
function __set($key, $val) {
$this->__delegate->__set($key, $val);
}
function __call($method, $args) {
return $this->__delegate->__call($method, $args);
}
function __toString() {
return $this->__delegate->__toString();
}
function getIterator() {
return $this->__delegate->getIterator();
}
function offsetExists($idx) {
return $this->__delegate->offsetExists($idx);
}
function offsetGet($idx) {
return $this->__delegate->offsetGet($idx);
}
function offsetSet($idx, $val) {
return $this->__delegate->offsetSet($idx, $val);
}
function offsetUnset($idx) {
return $this->__delegate->offsetUnset($idx);
}
}
class java_class extends Java {
function java_class() {
$delegate = $this->__delegate = java_create(func_get_args(), false);
$this->__java = $delegate->__java;
$this->__client = $delegate->__client;
}
}
class JavaClass extends java_class{}
class java_InternalJava extends Java {
function java_InternalJava($proxy) {
$this->__delegate = $proxy;
$this->__java = $proxy->__java;
$this->__client = $proxy->__client;
}
}
/**
* A decorator pattern which overrides all magic methods.
*/
class java_exception extends Exception {
var $__serialID, $__java, $__client;
var $__delegate;
function java_exception() {
$delegate = $this->__delegate = java_create(func_get_args(), true);
$this->__java = $delegate->__java;
$this->__client = $delegate->__client;
}
function __cast($type) {
return $this->__delegate->__cast($type);
}
function __sleep() {
$rc = $this->__delegate->__sleep();
$this->__serialID = $this->__delegate->__serialID;
return $rc;
}
function __wakeup() {
$this->__delegate->__wakeup();
$this->__java = $this->__delegate->__java;
}
function __destruct() {
$this->__delegate = null;
$this->__client = null;
}
function __get($key) {
return $this->__delegate->__get($key);
}
function __set($key, $val) {
$this->__delegate->__set($key, $val);
}
function __call($method, $args) {
return $this->__delegate->__call($method, $args);
}
function __toString() {
return $this->__delegate->__toExceptionString($this->getTraceAsString());
}
}
class JavaException extends java_exception {}
class java_InternalException extends JavaException {
function java_InternalException($proxy) {
$this->__delegate = $proxy;
$this->__java = $proxy->__java;
$this->__client = $proxy->__client;
}
}
?>
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.