Oct 16 2005

Speak with a Lisp

I took Scheme programming in college. Scheme is a dialect of Lisp. In my object-oriented mind, Scheme is weird although you will hear that is very powerful. Here is a snippet of code I found online that I wanted to share that shows how ‘weird’ Scheme, and Lisp for that matter, is:

(this (is what)
	(lisp code
		(looks)
		(like (more (or less)))))

When you program in Scheme, you end up counting brackets.


Oct 16 2005

Windows Hack

Microsoft XP Professional comes with a few handy Visual Basic script files. In your C:\WINDOWS\SYSTEM32 directory you will find PRNJOBS.VBS and PRNMNGR.VBS amongst other script files. As you can gather from the name of these files, they related to printer functionality. Cay Horstmann, my former Computer Science instructor, strongly encouraged his students to always explore and learn from the files in any Open Source software and/or Operating System we use. With that same hacker spirit in mind, feel free to see how Microsoft hacks Visual Basic by opening up these scripts in your favorite text editor. Who knows, you might to learn something new. Oh, by the way, another of my Computer Science instructors was famed science fiction and popular science author Rudy Rucker and he taught me to keep a blog! Haha. Maybe I should have named this entry as ‘Windows Hack and Name Dropping.’


Oct 13 2005

Jakarta BeanUtils PropertyUtils

I recently had to work with the Jakarta BeanUtils project and found it really useful. I think of it as BeanUseful. The BeanUtils project allows you to easily manipulate a JavaBean object using the static methods from the PropertyUtils class. Here is a simple example of how to update a property value of a JavaBean. Imagine I have a simple Greeting JavaBean class with a single property message.

Greeting object = new Greeting();
object.setMessage("Hello");
// ...
String greeting = (String)PropertyUtils
   .getSimpleProperty(object, "message");
PropertyUtils.setSimpleProperty(
   object, "message", greeting+", World!");

Of course, object is an instance of a JavaBean with a property named message.

PropertyUtils can also access an indexed method. Here is how you define an indexed property for a JavaBean:

public Item getItem(int index);
public void setItem(int index, Item item);

And here is how you would access an index property using PropertyUtils:

int index = ...;
String name = "item[" + index + "]";
Item item = (Item)PropertyUtils
   .getIndexedProperty(object, name);

In similar fashion, you can access a property that is mapped. Here is an example of how you define a mapped property in a JavaBean:

public Object getValue(String key);
public void setValue(String key, Object item);

And here is how you would access a mapped property using PropertyUtils:

PropertyUtils.setMappedProperty(
   object, "value("+key+")", value);

The BeanUtils project and Java Reflection in general, is best used in a dynamic setting when you do not know the type of a bean at compile time; that is the strength of Java Reflection, which BeanUtils builds on.


Oct 10 2005

Page Redirect

I totally forgot how to redirect to another page using PHP. After trying to remember I had to look it up in the PHP documentation. So if you want to redirect to another page using PHP you write to the header using this bit of code:

header('Location: index.html');

I opted to use PHP solution instead of the HTML meta refresh tag. You can do the same thing by placing this tag in your html output.

<meta
  http-equiv='refresh'
  content='0;URL=http://www.juixe.com'>

Of course, you can redirect to another page in just about in every language, including JavaScript as this snippet of JavaScript code demonstrates it:

document.location.href='index.html'

Oct 9 2005

WebSphere BloatWare

Why is it that IBM’s WebSphere 5.1.1 fixpack almost as large as the WebSphere 5.1 installation bundle itself? The WebSphere 5.1 installation bundle (base51wi.zip) is 285MB while the 5.1.1 fixpack (was51_fp1_win.zip) comes in at whopping 228MB. So by upgrading just one minor version I practically doubled WebSphere disk space footprint. I don’t think this is just a WebSphere issue but an IBM thing. I had been using IBM’s Rational Application Developer (RAD) 6.0.0.1 sometime back. So when I had to upgrade RAD with some interim fix my friend, and IBM employee, suggested that I better leave the upgrade running overnight.


Oct 9 2005

JavaBeans

JavaBeans are so simple that they might be hard for some people to grasp. I was asked by a college classmate to describe JavaBeans. What I saw in her face was that she did not understand the big fuss over JavaBeans. As much as I tried, she just didn’t get the context and the need for a specific name to such a Plain Old Java Object.

Technically speaking, a JavaBean is a Plain Old Java Object (POJO) which you defined properties for via getters and setters. JavaBeans can be used as third-party components that can be pluggable into a system. Using Java Reflection a system can analyze the available properties for a JavaBean. I have personally used JavaBeans in both InstalledAnywhere and InstalledShield when building custom actions and panels.

To define a JavaBean, the class must be public with the default constructor also publicly available. The constructor is set to public so that when using Reflection you can create an instance without knowing ahead of time the class name, such as:

Class beanClass = Class.forName(classNameStr);
Object bean = beanClass.newInstance();

You define properties by naming convention, using getter and setter methods. For example, for a property named ‘salary’ the JavaBean would define a getSalary and setSalary methods. If the property is a boolean, instead of a get method you define an is method, such as isManager. The parameter type of the set method must be the same as the return type for the get method. It is possible to have a read only property, just provide the getMethod not the setMethod. This is the essentials of JavaBeans but of course you can read the gory details by looking up the JavaBeans Specification.