Wednesday, December 21, 2005

 

Experiences porting from PHP to Java

Recently, I've been occupied porting an app to Java. The original app was written by myself in PHP, and is running with no problems at all. The problem is that the platform it's on isn't supported 24/7, while PHP isn't supported in the main hosting environment. Hence I had to use Java and rewrite the app.

Now, I had the advantage of knowing the app inside and out, although my Java experience (<> 5 years). Additionally, I'm using Oracle for the port, which I've never needed to use before.

I suppose it's inevitable that I'll need to learn Java at some stage, but that doesn't mean that I have to like it :) Can't say that I like Oracle much either at the moment, although I can see benefits over MySQL when it's used in anger.

Problem number 1 for me is of course getting used to datatyping. I use types in PHP too, but of course I only do this when I want to force something to be an int, or something. I find it really boring having to do something like this:

int i=1;
String s = "1";
if (new Integer(i).equals(new Integer(s).intValue())){...


while in PHP, I'd just do this:

if (i==s){

OK, I know that they're different types, but who cares? Most of the time, this data's coming from a text source anyway, like form input, and any implicit cast from int to string is what is intended anyway (like comparing an int counter in a 'for' loop with a string).

Anyway, I do like a lot of things about Java, don't get me wrong, but there are some things that annoy me.

Another example: I'm using a PreparedStatement type to get my SQL ready. Just seemed to be the 'Java' way of doing things. So, being unfamiliar with the type, I just started coding according to how I expected it to work. When it didn't work, I had loads of grief debugging the SQL. Problems:
  1. With an SQL statement like insert into foo(columnA, columnB) values (?,?), I decided that the first ? should be index 0, the next ? should be index 1, etc. Wrong. When I'd sorted that error, which was admittedly quick and easy to sort...
  2. I'd put the ?s in quotes, like this: '?', without realising that that's what setString() does.
  3. You can't see the SQL that's produced and actually sent to the database! At all! toString(), as is too common in Java, doesn't provide any useful information, there's no 'getSQL()' method, no 'getLastStatement()' kind of method in a Connection type, etc. You can't get it in debug mode in Eclipse either. Arrgh! I wish I'd just either hard-coded the SQL, or used a kind of pseudo statement-template now, so I had some means of getting back the actual SQL (so I could execute it manually to test it).
On the other hand, a bit of persistence with another problem has resulted in some pretty useful code. I needed to shell to execute remote commands with SSH and use SCP. I tried jsch, but there is no documentation, bugs in the code (putting a password in a user auth object made no difference - it was never used...) and incomplete examples. So, I decided that I'd just try to capture everything that would normally be sent to stdout so I could parse it for human-readable errors and other messages.

Here's a basic version of the code:

r = Runtime.getRuntime();
p = r.exec(cmd);

InputStream in = p.getInputStream();
InputStream error = p.getErrorStream();

StreamEater seIn = new StreamEater(in);
StreamEater seEr = new StreamEater(error);

ht.put("error", seIn.getString());
ht.put("output", seEr.getString());


What I discovered is that you need to empty both the InputStream and the ErrorStream before the command will return. And all sorts of stuff can be put in the error stream, like information about the host you're connecting to. What's more, you can't just empty the error stream then empty the input stream in sequence, because this isn't always going to work. What you need is a class that just takes a stream as input and reads everything from it, then supply some way of getting the resulting string (I used a string buffer).

This is better than my existing PHP solution. exec() in PHP only returns the last line of output. Passthru() does, but it doesn't return anything, just sends output to stdout (browser, typically). Of course, having written this, I've looked in the PHP online manual and found proc_open(), which seems to support everything I need. It was introduced in PHP 4.3.0. *Sigh*

Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?