Sunday, March 13, 2011

install and configure Zend server Java Bridge on Linux

As far as I know, there are two active PHP-Java bridge projects. One is PHP/Java Bridge hosted on SourceForge. The other is Java bridge coming with Zend Server and Zend Server CE for linux and IBM i. It is clear that sourceForge PHP/Java bridge is based on SOAP web service. But, this make me feel it is not useful so much as it is not difficult for a PHP programmer to write SOAP client to call a SOAP service written in Java. I guess Zend Java bridge is implemented in client/server architecture too as its configuration parameters include a TCP/IP port number. I thought Zend Java Bridge is written based on JNI technology. However, it seems not true.

Anyway, the mystification of Zend Java Bridge attracts me to give it a shot. Here are simple steps about how to install and configure Zend Java Bridge.

1) Following post Installing Zend server CE on Fedora 14 and fix YUM update to install Zend Server CE on Fedora 14.

2) Running command yum search java-bridge to have output similar as below.

[root@jiaFedora14 yyjia]# yum search java-bridge
Loaded plugins: langpacks, presto, refresh-packagekit
Adding en_US to language list
updates/pkgtags | 56 kB 00:00
======================================================================== Matched: java-bridge =========================================================================
php-5.2-java-bridge-zend-server.x86_64 : Zend Java bridge
php-5.3-java-bridge-zend-server.x86_64 : Zend Java bridge
[root@jiaFedora14 yyjia]#


3) call command yum install php-5.3-java-bridge-zend-server to install Zend Java Bridge as I installed php 5.3 version on my machine.

4) call command /usr/local/zend/etc/rc.d/06jb stop and command /usr/local/zend/etc/rc.d/06jb start to start Zend Java Bridge if it is necessary. Or you can enable/disable Zend Java Bridge from Zend Server admin Web page.

5) compile following Java code into a .jar file, say jia.jar and add put it into directory /usr/local/zend/bin.

SystemPropertyList.java
package jia.blog.util.lang;

import java.util.Properties;

/**
 *
 * @author Yiyu Jia
 */
public class SystemPropertyList {

    public static Properties listSystemProperties(){
         return System.getProperties();
    }
}


6) open file /usr/local/zend/etc/watchdog-jb.ini and add jia.jar file into the CLASSPATH.

7) put below PHP code into directory, where Zend server executes php file. For example, /var/www/html.

javaInfo.php
<?php
$javaEnv = new Java("jia.blog.util.lang.SystemPropertyList");
$out = $javaEnv->listSystemProperties();
?>

List of JVM System Properties

<?php foreach($out as $key => $value) { ?> <?php } ?>
<?php echo $key; ?> <?php echo $value; ?>

8) open web browser and access php code, for example http://localhost/javaEnv.php. If you can see the list of Java system properties, it is done. Enjoy it. This Java system properties is very important for Java programmer to create java app running in Zend Java Bridge.

Note: it seems that Zend Java Bridge does not support Java class hot deployment. So, you may need to restart Java Bridge every time after you modified your java classes.

Thursday, March 10, 2011

Implementing Singleton pattern in Java and PHP shows different "static" modifers between Java and PHP

This is a quick note about the difference between Java "static" modifier and PHP "static" modifier. I investigated the different "static" modifiers in Java and PHP (static variable in PHP vs static variable in Java). Here is a quick notes that shows more difference. It is about "synchronized" and multi-threaded safe coding. Java programmer have to pay extra attention when static variables are used. However, this is not problem in PHP. It does not mean PHP is better than Java. It is just because PHP does not natively support multi-thread.

The following are two singleton pattern implementations in Java and PHP respectively. Listing both of them here can clearly show the difference between Java and PHP static variables.

Java Singleton pattern
/**
 * @author Yiyu Jia
 */
public class DBConnectionFactory {
   
    // static means there will be only one instance in one JVM.
    private static DBConnectionFactory m_connFactory = new DBConnectionFactory();

    // private means this class can not be created by "new" operator outside it.
    private DBConnectionFactory() {
        //connManagerList = new ConcurrentHashMap();
    }
    
    public static DBConnectionFactory getInstance() {

        return m_connFactory;

    }


}

To have better understand about Java memory management, singleton pattern, we can refer to this article and links inside it.



PHP singleton pattern
/**
  * @author Yiyu Jia
  */
class JiaLog
{ 
    private static $_instance;   
    private $logger;
    
    // declaring constructor to be private function.
    private function __construct() 
    {       
     $writer = new Zend_Log_Writer_Stream('path/to/logfile');     
     $this->logger = new Zend_Log($writer);     
    }

    //public static function is the way allowed to get instance
    public static function getJiaLog() 
    {
        if (!isset(self::$_instance)) {
            $c = __CLASS__;
            self::$_instance = new $c;
        }
        return self::$_instance;  
    }
    
   
    public function info($str)
    {
        $this->logger->log($str, Zend_Log::INFO);
    }   

    // Prevent users to clone the instance
    public function __clone()
    {
     trigger_error('Clone is not allowed.', E_USER_ERROR);
    }
}