Oct 11 2007

Run PHP Web Applications on the Java Platform

There has been a lot of commotion and even a book with having Ruby on Rails run on the Java VM. But looking past the JRuby hype it is clear that PHP has a several orders of magnitude more open source code, projects, and corporate backing than Ruby on Rails. The folks behind the Resin application server have the ubiquitous of PHP and have developed Quercus, a PHP 5 implementation written in Java.

Quercus is available with the latest Resin but it is also available for download as a WAR file which can be deployed on Resin or other application Servers such as GlassFish.

Getting Started

Get the latest version of Resin server, as of this writing the latest version is Resin 3.1.3. To start the server just run the <INSTALL DIRECTORY>\bin\httpd script from the command prompt. If you look around the Resin install directory you will find a php folder where the Quercus WAR file is already configured to handle any PHP file. To give the Quercus PHP engine a try, lets create a simple test. First lets create phptest directory in <INSTALL DIRECTORY>\webapps. In the phptest directory create a new file, index.php, and add the following text to it.

[source:php]
<html>
<body>
<?php phpinfo(); ?>
</body>
</html>
[/source]

The phpinfo method outputs a large amount of PHP configuration information. I usually print this information out to ensure that PHP is setup correctly. If you have already started the Resin server you can direct your browser to the following URL: http://localhost:8080/phptest/

You should be looking at a screen that looks like the following screen shot.

phpinfo

Mixing PHP with Java

Since Quercus is written in Java there are hooks to import and mix Java classes in your PHP files. Lets import a Java class and manipulate it in PHP. In the next sample piece of code I will import a Java HashMap class and manipulated it PHP. The import functionality is obviously not part of PHP but an extension made possible by Quercus. .

[source:php]
<html>
<body>
<?php
import java.util.HashMap;

$map = new HashMap();

// Add some name-value pairs
$map->put(‘california’, 1000);
$map->put(‘oregon’, 1200);

$total = 0;
// Iterate over keys and sum values
for($itr = $map->keySet()->iterator(); $itr->hasNext(); ) {
$key = $itr->next();
$total += $map->get($key);
}
print “Total $total”;

?>
</body>
</html>
[/source]

Once a Java class has been loaded it can be handled just like any other object in PHP. In addition having access to JDK classes, you can import your own Java code. You can import any class that is available in your web applications class loader.

Run PHP Applications

Using Quercus and Resin you can deploy full fledge PHP web applications such as WordPress or Joomla! To get a PHP application running on Resin with Quercus you just need to unzip the application into the <INSTALL DIRECTORY>\webapps directory. Since your PHP application would be running on top of Java you need to have the right JDBC dirver installed in the <INSTALL DIRECTORY>\lib directory. This is usually all you have to do to have a PHP application to run on JSP/Servlet container like Resin.

There is a list of PHP applications that are known to be running on Java via Quercus.

Quercus opens a lot of opportunities for PHP web development. Now you don’t really have to choose between languages or frameworks, develop in what you know is best for the task and resources at hand and interoperate between PHP, Java, Ruby, Groovy whatever not at the XML level but at the bytecode.

Technorati Tags: , , , , , , , , ,


Oct 11 2007

Mac OS X Screen Grab

I recently mentioned how to grab a screen shot of an application window on Windows XP. On Mac OS X you can also capture a screen shot of a window but its definitely is not as simple as Windows since it does not come with a Print Screen key. Mac OS X comes with a Grab utility (under Applications | Utilities | Grab) which an capture the whole screen, a selected window, or a selected rectangular area of your screen.

You can also grab a screen shot by using the Preview application. Open the Mac OS X Preview application and select the File | Grab | Window menu to capture a screen shot of a selected application window.

Technorati Tags: , , , , ,


Oct 1 2007

Migrating to Java 6

I work with a large code base consisting of over 4,000 classes, with a custom ORM persistence layer, layout managers, Rapid Application Development (RAD) User Interface (UI) builder. The code is written in Java 1.4.2 but we have been considering a move to Java 5 or even up to Java 6. We currently have been debating the need, real or imagined, to migrate to Java 5. As developers, we just want to play with something new and shinny but there are business consideration that come into play.

As an exercise I migrated our code to Java 5 and then up to Java 6 and ran into few compilation and runtime errors. Upgrading to Java 5 revealed compilation errors with XPathAPI. To fix the XPathAPI error I just needed to add xalan as a dependency or import com.sun.org.apache.xpath.internal.XPathAPI. I also had a compilation error because it seems that compareTo(Object) had been removed form BigDecimal and BigInteger. As you know, Java 5 introduced enum as a keyword and wouldn’t you know I got a ton of warnings about this but this can easily be refactored.

Our system had a few runtime problems when moving to Java 5. We have some objects serialized to disk and changing the version of Java throws an InvalidClassException when reading in these objects. I also encountered some runtime exceptions thrown because of FileLock/FileChannel where none was throw before in Java 1.4.2. The errors where thrown because we had a programming error, we tried to release a file lock after the FileOutputStream for that lock had been closed.

Moving up to Java 6 I encountered a ton of compilation errors because new abstract methods have been introduced into JDBC classes such as Connection, Wrapper, and ResultSet.

Technorati Tags: , , ,


Sep 30 2007

Top Technology Podcasts

In no particular order here is the list of of technology related podcasts that I frequently listen to.

  • Cranky Geeks – Not cranky enough, but John C. Dvorak and guest rant on latest technology news.
  • Diggnation – A weekly tech/web culture show based on the top stories on Digg.
  • Drunk and Retired – They are not as drunk as one would hope, but they do tend to go off on Rails, software development, and zombies.
  • Google Developer Podcast – Googlers talking about the latest Google APIs.
  • The Java Posse – The seminal Java podcast put out by Google, Sun, and Apple engineers, just hope they never have to pronounce your name.
  • .Net Rocks! – A weekly talk show for anyone interested in programming on the Microsoft .NET platform.
  • Railscasts – Free, and most importantly frequent, Ruby on Rails screencasts ranging from 5 to 10 minutes covering testing, migrations, controllers, and more.
  • WebDevRadio – This podcast covers web development news with the occasional interview with engineers working on projects with .NET, MySQL, PHP, etc.
  • Polymorphic Podcast – Insight into software development in the .NET platform along with interviews with industry luminaries.
  • Code Sermon – The podast somewhat preaches to the choir. This is a somewhat semi-weekly sermon on the virtues of software development best practices.
  • Killer Innovation – A podcast about creativity, innovation, and idea generation. This podcast will present ideas to think outside the box, or IDE.
  • Grails Podcast – Keeps you up to date about the latest Grails developments.
  • NetBeans Podcast – Hosted by Roman Strobl of Sun, this podcast has the occasional interview with NetBeans developers like Tor Norbye and Geertjan Wielenga.
  • Ask A Ninja – Every programmer needs a break between hacking sessions.
  • Rails Podcast – News and interviews about the Ruby language and the Rails framework.
  • Entrepreneurial Thought Leaders – This podcast is put out by the Stanford business school. It usually has business leader share their experience.
  • Floss Weekly – Free/Libre Open Source Software might be free, but it is not timely.
  • Late Night Cocoa Podcast – Usually has hacking cocoa and API discussions and Cocoa practitioners, currently on summer hiatus.
  • TWiT – Leo Laporte and gang talk technology, unless they are reminiscing about their TechTV days or how to monetize ‘netcasts’.
  • Scoble Show – Robert Scoble talks with geeks, technologists, and developers.
  • GigaOm Show – Om Malik and Joyce Kim talk with entrepreneurs in the valley and run down some of the latest tech news.

If you feel I missed any other developer noteworthy podcast please let me know in the comments.

Technorati Tags: , , , , , , , , , , , ,


Sep 27 2007

YourKit Java Profiler 7.0 Review

Like most developers, I like free software and open source software both for it’s freedom and it’s price. But once in a while there are some software you can’t avoid but to pay for. I was involved in a project recently where I had to profile some Java memory leak in a tight deadline. The code base for the project is compiled to Java 1.4 bytecode and the team uses Eclipse 3.1. I don’t know of any good profiler for Eclipse so without skipping a beat or thinking twice about it I downloaded a free 15-day trial version of YourKit Java Profiler.

It was just drop dead simple to manually enable YourKit to profile our JBoss 3.2.x based application even though we highly customized the start batch script. Once profiling is enabled, I was able to connect to our application from YourKit to have it start profiling, monitor, inspecting the leaky architecture in the application.

It took me just a few hours to get familiar with the YourKit screens and memory snapshots views. Within a day I was able to track down a ThreadLocal object that was keeping a HashMap instance that in turn collected a large hierarchy of objects in memory. Clearing the value of the ThreadLocal did the job.

YourKit Java Profiler 7.0

Again, I choose YourKit mostly because of the environment and JVM I was using. I do understand that NetBeans 5.5 has a nice profiler for Java, which I have not tried myself. I would have opted for that if I was already using NetBeans or if it was the first result when I googled ‘java profiler.’ Unfortunately I was not able to find what seemed like a good or reliable profiler for Eclipse. For this reason alone I recommended YouKit Java Profiler to my boss. Although to be completely honest, since YourKit worked so well for me I thought of just getting another trial version next time I am ever in a bind and need to profile a Java application.

Technorati Tags: , , , , , , , ,


Sep 21 2007

Windows Print Screen Key

Earlier this year I wrote about discovering the purpose of the F11 key on OS X. Today I rediscovered how to get a screen shot for a particular application window using the Print Screen key. If you hit the Print Screen you get a screen shot of the whole desktop. If you you hit Alt + Print Screen you get a nicely cropped screen shot of the current application window. Here are other favorite Windows key shortcuts. Hit Alt + Tab to see all icons for all opened applications. To change focus to a different application hit Tab again until the application of interest is highlighted while still holding down the Alt key. Hit Start + D to toggle between the cluttered/clean desktop. Hit Start + E to open a new Windows Explorer. Hit Alt + F4 to exit the current application. Ctrl + F4 usually closes the current application window (I have seen some applications that don’t observe this key combination). Knowing key shortcuts will make you a more productive developer.

Technorati Tags: , , , ,