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.
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.
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.
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"); |
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 |
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"); |
JavaException | java.lang.Exception | A wrapped exception class. The original exception can be retrieved with $exception->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种不同模式下运行:
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=627890