Haohappy
PHP5研究中心:http://blog.csdn.net/haohappy2004/
(三)為什麼要使用 PHP/Java Bridge?
PHP中的組件都是短暫,非持久化的。如果是複雜應用體系,我們需要提供中介層組件(像java beans/EJB)或者企業級的提供緩衝,串連池或商業邏輯給PHP組件產生的頁面。例如解析XML檔案就是一個比較耗資源的工作,需要緩衝;串連資料庫也是個比較耗資源的工作,所以需要能夠重用。標準的PHP XML和DB抽象層都是比較沒效率的,因為它們都不能通過一個中介層來實現緩衝和串連池。
即使是一些小任務,也可能需要用到Java Class或Java類庫,例如需要跨平台地產生Word,Excel或PDF文檔。
PHP,PHP/Java Bridge和php代碼可以打包成標準的J2EE檔案包格式,使用者可以方便地布置到一個J2EE應用伺服器或servlet引擎中去。使用者不需要安裝PHP,而且從他們的角度來說,他們看不到這些用JSP,servlet和PHP產生的頁面有什麼區別。由於Bridge允許PHP和J2EE間的session共用,開發人員可以一步步地把基於JSP的程式和PHP整合起來。
上面是說為什麼PHP需要Java。而對於Java程式員來說,PHP和PHP/Java Bridge也可能是有用的。 現在有許多基於JSP模板系統的技術如jakarta Struts及更新一代技術Java Server Faces。JSP和自訂標籤庫有很多缺陷,把它們整合在一起去建立一個物件導向的WEB Framework暴露了這些問題。即使JSF的作者也承認了這樣的系統是有嚴重缺陷的,並推薦像tapestry或facelets 這樣用Java類定義組件並通過他們的ID來綁定到XML/HTML模板中。PHP/Java Bridge version 3.0可以嵌入PHP代碼到JSF Framework中,這樣使用者介面設計師集中精力在設計HTML模板,而程式員可以用PHP建立原型,並使用已有的開發架構。現在不少大型網站就在前端使用PHP,而核心使用Java來構建系統。
PHP/Java Bridge添加了下面這些原始類型和函數到PHP中,以使PHP可以方便地訪問Java對象。在表格1中可以看到資料類型的分布情況。
new Java("CLASSNAME"): References and instanciates the class CLASSNAME. After script execution the referenced classes may be garbage collected.
new JavaClass("CLASSNAME"): References the class CLASSNAME without creating an instance. The object returned is the class object itself, not an object of the class. After script execution the referenced classes may be garbage collected. Example:
java_require("JAR1;JAR2"): Makes additional libraries available to the current script. JAR can either be a "http:", "ftp:", "file:" or a "jar:" location. On "Security Enhanced Linux" (please see the README) the location must be tagged with a lib_t security context.
java_context(): Makes the javax.script.ScriptContext available to the current script. Available since PHP/Java Bridge version 3.0.
java_closure(ENVIRONMENT, MAP, TYPE): Makes it possible to call PHP code from java. It closes over the PHP environment and packages it up as a java class. If the ENVIRONMENT is missing, the current environment is used. If MAP is missing, the PHP procedures must have the same name as the required procedures. If TYPE is missing, the generated class is "generic", i.e. the interface it implements is determined when the closure is applied.
The java_closure can also be used to emulate try/catch functionality in PHP4.
$session=java_session(): Creates or retrieves a session context. When the backend is running in a J2EE environment, the session is taken from the request object, otherwise it is taken from PHP.
$session=java_session(SESSIONNAME): Creates or retrieves the session SESSIONNAME. This primitive uses a "private" session store with the name SESSIONNAME.
The java_session primitive is meant for values which must survive the current script. If you want to cache data which is expensive to create, bind the data to a class.
JavaException: A java exception class. Available in PHP 5 and above only. Example:
foreach(COLLECTION): It is possible to iterate over values of java classes that implement java.util.Collection or java.util.Map. Available in PHP 5 and above only.
[index]: It is possible to access elements of java arrays or elements of java classes that implement the java.util.Map interface. Available in PHP 5 and above only.
java_instanceof(JAVA_OBJ, JAVA_CLASS): Tests if JAVA_OBJ is an instance of JAVA_CLASS.
java_last_exception_get(): Returns the last exception instance or null. Since PHP 5 you can use try/catch instead. java_last_exception_clear(): Clears the error condition. Since PHP 5 you can use try/catch instead.
Table 1. 資料類型映射表
|
PHP |
Java |
Description |
Example |
|
object |
java.lang.Object |
An opaque object handle. However, we guarantee that the first handle always starts with 1 and that the next handle is n+1 for all n < 1024 (useful if you work with the raw XML protocol, see the python and scheme examples). |
$buf=new java("java.io.ByteArrayOutputStream");
$outbuf=new java("java.io.PrintStream", $buf); |
|
null |
null |
NULL value |
$outbuf->println(null); |
|
exact number |
long |
64 bit integer |
$outbuf->println(100); |
|
boolean |
boolean |
boolean value |
$outbuf->println(true); |
|
inexact number |
double |
IEEE floating point |
$outbuf->println(3.14); |
|
string |
byte[] |
binary data, unconverted |
$bytes=$buf->toByteArray(); |
|
string |
java.lang.String |
An UTF-8 encoded string. Since PHP does not support unicode, all java.lang.String values are auto-converted into a byte[] (see above) using UTF-8 encoding. The encoding can be changed with the java_set_file_encoding() primitive. |
$string=$buf->toString(); |
|
array (as array) |
java.util.Collection or T[] |
PHP4 sends and receives arrays as values. PHP5 sends arrays as values and receives object handles which implement the new iterator and array interface. |
// pass a Collection to Vector
$ar=array(1, 2, 3);
$v=new java("java.util.Vector", $ar);
echo $v->capacity();
// pass T[] to asList()
$A=new JavaClass("java.util.Arrays");
$lst=$A->asList($ar);
echo $lst->size(); |
|
array (as hash) |
java.util.Map |
PHP4 sends and receives hashtables as values. PHP5 sends hashtables as values and receives object handles which implement the new iterator interface. |
$h=array("k"=>"v", "k2"=>"v2");
$m=new java("java.util.HashMap",$h);
echo $m->size(); |
|
JavaException |
java.lang.Exception |
A wrapped exception class. The original exception can be retrieved with $exception->getCause(); |
...
catch(JavaException $ex) {
echo $ex->getCause();
} |
本模組在Mandrake Linux 9.2,RedHat Enterprise 3, RedHat Fedora Core 1..4, FreeBSD 5.3, Solaris 9 (Sparc, 64 bit JVM) 和 Windows 下RedHat (Cygwin虛擬)各種平台均通過測試, 理論上說可以運行於所有*nix作業系統。
PHP/Java Bridge可以在4種不同模式下運行:
通過dl()函數調用。在這種模式下,當HTTP伺服器接收一個客戶請求並反映結束後,開始調用PHP/Java Bridge。速度很慢,因為為每次請求都要重新啟動Java虛擬機器,但不需要任何系統管理許可權。 通過HTTP伺服器內部啟動並執行Java進程持久性地啟用全域設定檔php.ini。這種模式下PHP/Java Bridge是在HTTP伺服器啟動後啟動,停止後停止。在開發測試過程中推薦使用這種模式。 通過外部的Java進程來持久性地啟用全域設定檔php.ini。推薦用於產品系統中(即系統正式運行時)。這種模式類似於RedHat Linux下RPM包的實現方式。這種模式下PHP/Java Bridge作為系統服務。 通過外部的應用伺服器或servlet引擎來啟用php.ini檔案。PHP可以作為純Java應用伺服器的CGI子組件運行,或者安裝為Apache/IIS模組。
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=627890