Archive for August, 2002

Butterfly Code Generator

Saturday, August 31st, 2002

Noticed this on Java-Channel. Takes in XMI [such as outputted by ArgoUml etc] and uses a XSLT based template system to output Java. Definitely on the need to play with list as code generation has been a play-thing of mine [and many other Java developers] for a while now. I have a highly generic framework kicking its heels in my development directory. It works but my ’simple’ implementation has gotten way out of hand.

Anyway, check out Butterfly.

netBook resurrection

Friday, August 30th, 2002

I’ve spent most of the evening and night working on my Psion netBook. This baby is a PDA come laptop that makes a perfect book reader. Up until I bought an OS X-driven Apple Powerbook, I was using this as a full on laptop. Then it went into remission. For a while its only active service was to show me my resume/CV in interviews and as an alarm in the morning.

Now it’s back. My aim is to fill it full with Java specifications, code examples and javadoc. The screen is 11 inches I think, making it just enough to hold in one hand while on a train. It has an 8 hour battery life, so about a transatlantic flight, and good crisp clear colours. I even found some software tonight to read PDFs.

It takes a CF card, so I have an IBM gigabyte drive in there. I use a PNY media reader to push stuff on it as the Windows/netBook serial link is too annoyingly slow. Opera had a Psion release [unsure if it’s been updated since] and that suits my html reading just fine.

Add to that the fact it has Java on [which when it came out was pretty new] of JDK 1.1.8, which I can use through the JPE IDE, the piece of software that lured me into finally doing some open source coding [I hacked many new features on].

Sadly Psion have struggled to match the Palms and Windows-clones of this world. A shame because the various Psion machines were far and away the best available. By some strange quirk of fate, the netBook is still supported while all the more popular consumer PDAs from Psion are heading towards their graves.

I look forward to writing tips and articles on the netbook in the future. The perfect personal computer.

Ant tidbits

Friday, August 30th, 2002

So here I am reading the Ant book on Safari. Bound to be lots of new things as although I’ve used Ant a lot, and have happily written Tasks, there are large areas I’ve never gone near.

First tidbit: ant -projecthelp lists the targets and subtargets. Doh. Last time I make a target called “targets” which echoes out the top level targets :) ant -help being what I should have typed all this time.
Second tidbit: Ant reads build files in the following: First it scans all children of the <project> tag. It finds errors in their attributes or tag names. Then it goes into each child, depth first. So errors are found in the first <target> and its dependencies before the second <target>, unless the second <target> is a dependency.

Third tidbit: Environment variables. Ant really needs to see these and I’ve never found out how to do it. Well the book tells me: <property environment=”env”/> then any variable starting with ‘env.’ is taken from the system environment. For example, <property name=”xalan.home” value=”${env.XALAN_HOME}”/>. Happiness.

Fourth tidbit: Regexps. Ant has a pluggable regexp system, so it can handle both types of Jakarta regexp library as well as Suns. Always a nice touch. Now to somehow get it out of Ant and into Jakarta Commons so all can use the concept.

I’ll end it there. Halfway through the book and I’m feeling happy with Safari already.

Safari

Friday, August 30th, 2002

Just signed up to safari.oreilly.com, though the oreilly website failed to work and after a day or two of conversing with the safari tech support I got it to work by going via safari.informit.com. Very odd.

I like this idea. If I can get my old psion netBook setup again [not got the PC software installed and the IBM gigadrive is full of shit that my Mac OS 9 put on there] it would be nice to read. Need to check the small print to see if I’m allowed to put it on a PDA or if I have to have a constant net connection.

I’m also considering a PC next to the bed. I have an addiction with the infernal computer I think. If I can get a wireless version of a Sleekline machine, then I just might.

URLConnection

Wednesday, August 28th, 2002

Okay, I’m the last person in the world to find this out I bet.

Am messing around with a Html link checker for the fun of it, and because the only Java ones I found online were old. So there might be a new aspect.

Anyways. Found out that if you open a URLConnection to a site and check the headers, it is faster/better to get the InputStream and then close it, than to allow the URLConnection to garbage collect. I presume because either opening the URLConnection or getting the headers magically kicks off the InputStream underneath.

Obvious to some maybe, but to others like myself probably not. I need to stop playing with http and grab a copy of HttpClient.

Teaching Java

Tuesday, August 27th, 2002

There’s an article doing the rounds over at OReilly’s [the heroes of my computer youth] Java website. It’s about teaching Java and how “Hello World” programs are a fundamental flaw for introducing Java.

My personal favourite is a program which outputs the System properties. It allows me to show importing, packages, the main(String[]) an object ‘props’, usage of a standard Java class ‘Properties’, and the fact that Systemis imported automatically as it’s in java.lang. Then in the second line of actual code :) It shows invoking a method on an object, and gives me the opportunity to discuss a Stream, including the difference between the error and output streams.

It also proves a highly useful program to test Java is working on a machine.
package test;

import java.util.Properties;

public class ShowProps {

    static public void main(String[] args) {
        Properties props = System.getProperties();
        props.list(System.err);
    }

}

Quartz 2nd time around

Monday, August 26th, 2002

So I’m having a look at Quartz now in consideration of using it at production to handle when databases are polled, when events time out usw usw. Oh, and originally to replace jdring as the master timer for our web scraping.

I grabbed the download from the Quartz website. First odd thing, a legal note about containing Apache code, but none that I can see. I guess they repackaged, appropriated or copy and pasted, which is a nice benefit of the Apache licence. I think. Damn hard having a clue about licences sometimes.

Anyways.. Quartz looks pretty fancy and nice.Up until looking into Quartz I was using JDRing. A nice little package, but sorely limited. For example, you would set a job up by implementing a listener, but there was no way for the method thusly called to know which of 5 ‘cron’s specified called it. So I can’t have the same class handling my “scrape”, “check”, “notify” and “stop-checking” jobs.

So Quartz immediately passes muster. They’ve separated out the concept of a Job and a Trigger. To my mind this is akin to having a JobEvent for the JDRing listener. Quartz make good use of beans/reflection too. I always like an API that can balance itself nicely on top of such a thing.

Quartz can also hide its Factory pattern implementation behind JNDI. This is nice, though I wish I could get triggers and job details from the factory too. I think a sign of a good API is one in which there is only one time when you need to instantiate with a concrete class. After that bootstrapping you get everything you need from it as an interface type.

There are two main triggers available, though I’m sure you could write your own. A SimpleTrigger and a CronTrigger. Annoyingly there’s no melding of the two :) But I can quite easily see how I would set a CronTrigger to turn on at 6am on a Tuesday morning, then this would fire off a SimpleTrigger which would fire every 10 minutes for the next 2 hours. Then it would die. A much better setup than having them all in one big trigger I guess.

There are some unimplemented bits in the CronTrigger according to Javadoc. A bit annoying and can cause you to worry about the matureness of the library. Hopefully it simply shows that they are rarely used.

For some odd reason, CronTrigger needs to know about the JobDetail it’s being associated with, but SimpleTrigger doesn’t. You’d hope that’s the kind of thing that would vanish behind the scheduler.scheduleJob(detail, trigger) method call in the future.

I’ll have to see how it goes. The aim is to get a SimpleTrigger firing every 5 minutes, and a CronTrigger firing on the 5 minutes, for the same Job. Currently it complains saying I can’t have the same Job in the Scheduler twice. Which is just ick, but seems to be the way things go. My first complaint :) Could have sworn the documentation said I could have the same Job triggering multiple times.

Java Beans/Business Objects

Friday, August 23rd, 2002

So the things I was generating were business objects. I don’t plan for them to be anything special, just data holding beans. No logic methods. Think of them as Entity Beans with a XxxBeanHelper to handle the logic, much as you’d use a Stateless Session Bean.
Anyways, the ideas I’ve been pondering are on how to improve these guys. A while back I thought in terms of Primary and Secondary business objects. An idea that leaked into me from JDO. There are important business objects that you want to talk in terms of, such as a Company, PowerPlant or Person. And then there are secondary business objects, really just wrappers for connected data. Such as an Address, Country, or Currency. Obviously it depends on the system though, Currency could be pretty important in a banking application.

I realise now that there are also Container business objects, whose main role is to be a collection of another business object, but they have some special features.

Anyways. There would be interfaces for these different types. Primary and Secondary. They would fit the design process in that you would first draw up a Primary diagram,, and you’d assume you had secondary types available. So when deciding on a Person, I would just say, “Oh, and mailing address of type Address. “. Secondary types would make a good possibility for a company-wide pool of business object ideas.

The next idea is to stop null pointer deaths. Enforce the NULL pattern everywhere. Every business object’s attribute begins as a null pattern element. This means that for Address we have a city of type String and initial value [”"] and a country of type Country and initial value [new NullCountry()].

Then when an empty Address is returned to an AddressForm, the line:

address.getCountry().getISOCode() just returns an empty string. No NPE.

This is great for production, some users get some poor data, but generally things stay upright and they can move on or attempt to figure out what is causing it. For test it is bad as errors will slip through. So at test an overlay version of the Null pattern classes can be used which throws an Exception or prints loudly to the logging system. To make matters nicer, each business object would maintain its own NULL instance. So Address.country would really have an initial value of “Country.NULL”.

The idea may or may not work, but it’s kind of fun. I’ve used the Null pattern successfully all over the place, but never so fanatically as with these business objects. The generator is happily spewing them out too :)

Deciding on the API for ‘indexed-properties’ was interesting. [aka Maps]. I ended up deciding to offer up the values via the entrySet method. I’ve never used this a lot due to the pain of extending it, but will give it a shot in a system and see how nice it is. At the very worst it will force me to entrySet-ify some extensions of mine. Or move to Jakarta Commons Collections.

It would be nice to create an SQL statement for each business object, then have the system automatically zap out DAO’s per object. Whether I’m using EJB, simple Java or C#, that would be useful. Not so good if I use an OR mapping tool though :P

So the structure is currently looking like:

Primary –> Xxxx –> XxxxImpl ==> NullXxxx
With Xxxx.NULL = new NullXxxx.

I’ve not bothered with a Secondary interface at the moment. Primary has an ID, but I’m not sure what Secondary would have. Primary also has loosely-typed variables available through a map like interface. Another interesting experiment that moatas has been driving. It allows me to do something I’ve been talking about, adding temporary variables to a class. A nice way in which I can do hacks when I want to.

Hmm. Should be the subject of another blog. Creating systems with back doors, so that when you absolutely have to do it quick and hacky, there’s lots of possibilities. I’m sure there’s a large swell opposition from the do-it-right crowd :)

Code Generation

Friday, August 23rd, 2002

I’ve got the code generation system I put together last year out of the cupboard, demothballed it a bit and used it to zap out some javabean business objects. Probably the worst thing about the system is that the implementations of each component are kept deliberately simple. That and the lack of a nice TUI, so it took me a while to remember just how to pass the data and config into the CodeGen front end.

The whole thing is meant to be pluggable, so replacing the pretend-script GJT with Jelly or Velocity would be nice, adding an XMI interface to the MMScript/XML and partial JDBC/CSV inputs would be nice, and adding Ruby/C# output would be nice.

Currently the system is a little Java-centric, but not in too many places.

I’m still not convinced Code Generation is a good thing. After a couple of years of specialising in it, I decided that runtime based systems that worked off of a live model were a better way to go. However, I do still find code generating to be a nice way to speed up the development process or even to look after a region of very similar code. I did 90% of the work for the String Taglib with the code generator.

Open vs Closed

Thursday, August 22nd, 2002

Currently the Jakarta Commons mail list is awash with a debate over whether an XxxUtils class [containing just static methods] should have a private constructor and be final. The issue came about due to a user using StringUtils from Lang in Velocity [which requires instances to work upon] and the beta release of Lang having a private() in StringUtils.

However, at heart it represents a fundamental schism in programmer thinking. I like to view it as the VB-mindset vs the perl-mindset.On the one hand we have the privateers. The belief that a library should be kept tight and closed. Developers should only do what the library creators have given them permission to do and any attempt to do elsewise is not supported by the
library. This has some big positives:

  • It makes the API of a library smaller. Thus easier to learn., thus easier to change.

This is the VB-mindset. It’s not intended to be an insult :) The mindset works very well when the user is not trusted, and I don’t trust VB coders, [or anyone who I feel relies on their IDE knowledge and not their language knowledge].

The opposite are the publiceers. The belief that a library should not try to enforce walls and doors just because it is ‘good’, but that it should be done only when necessary [ie to protect a singleton]. This mindset believes that the user is liable for whatever they try to do and that if they do something outside the ’spec’ of the library, then it is up to the user to handle upgrade nightmares.

The major benefits of this approach are:

  • The library is more usable as it hasn’t blocked a host of options

This is the Perl-mindset. The user is trusted to deal with the shotgun, and as much power as possible is available to them, or rather, power is not denied to them simply because the library doesn’t want to go there.

Myself, I’m a publiceer for myself and a reluctant privateer if I’m dealing with newbies to Java. I’d like to have an idiomatic Java checkstyle which stopped people in my charge being able to use the do-while loop, or a non-simple for loop, or putting Iterators in for loops etc. Basically a dumbed down version of Java.

For the same reason, it would be very nice if as well as private/protected etc, Java had the idea of a published keyword, or javadoc tag. This would indicate that a method, be it protected or public, is supported and published and Javadoc could be created only for published methods. Build tools like Maven would create change-logs based only on the published methods.

That way, the publiceers can achieve the privateers desires. A system in which there is an obviously small API.
And the privateers can achieve what the publiceers desire. An API without blocked functionality.