Category Archives: Uncategorized

Command Line Processing in Java

I've always found processing command line parameters in Java something of
a hassle. It's not hard, but it can be error prone and
having to write the same code over and over is annoying.

Here's a class I wrote that I find useful for this. Usage:

String usage = "java " + YourClass.class.getName() + " -a FirstParam -b
SecondParam";
CmdLineProcessor cmdLine = new CmdLineProcessor(args, usage);
cmdLine.setExpectedArgumentCount(4);
if (cmdLine.process()) {
	String firstParam = cmdLine.getArgument("-a");
	String secondParam = cmdLine.getArgument("-b");
	System.out.println("Parameters were " + firstParam + " and " +
secondParam);
}
// usage message is output if process() did not return true

The class:

public class CmdLineProcessor {
	private String[] args;
	private int expectedArgumentCount = 0;
	private String usageMessage;

	public CmdLineProcessor(String[] args, String usage) {
		this.args = args;
		setUsageMessage(usage);
	}

	public boolean process() {
		if ((args == null)
			|| (args.length != expectedArgumentCount)) {

			System.err.println(getUsageMessage());
			return false;
		}
		return true;
	}

	public String getArgument(String param)
			throws IllegalArgumentException {
		for (int i = 0; i < args.length; i++) {
			if (param.equals(args[i] )) {
				if (args.length > (i + 1)) {
					return args[i+1];
				}
			}
		}
		System.err.println(getUsageMessage());
		throw new IllegalArgumentException();
	}

	public int getExpectedArgumentCount() {
		return expectedArgumentCount;
	}

	public String getUsageMessage() {
		return usageMessage;
	}

	public void setExpectedArgumentCount(int i) {
		expectedArgumentCount = i;
	}

	public void setUsageMessage(String string) {
		usageMessage = string;
	}
}

Feel free to reuse it as you need.

Stuff I've been Doing

Obviously I haven't done much blogging lately. However, I have moved house and done a few trips to
various far-flung places around the country.

I flew back from Perth last week. The Perth<->Adelaide trip normally takes around 3 hours. We had (according to
the pilot) 300 kph tail winds, so we did it 2:15, which was great!

Another, unrelated note: a hand whisk taped to a piece of plastic attached to a hammer drill does NOT make
a suitible substitute for an electric beater. It will, however do a great job of painting the kitchen with cheesecake…

Tim Bray at Sun

Tim Bray is going to work at Sun. That is
the best bit of news about Sun I've heard for a long, time. Hopefully he will be able to express some of Sun's ideas in
a less confusing way than the mixed messages we currently get.

Someone (I'm hoping a few referers from this link) should point him at JavaBlogs
and tell him to add himself to it – since that's where all the cool kids get their news (in the Java world, anyway).

Pluto Documentation Updated

The Pluto (JSR168 RI) Website
has been updated. If you have any comments on what we can do better, either let me know,
enter them Bugzilla or send them to the mailing lists.

In particular if you notice any mistakes or things that could be more clear on the
Using the Pluto Portal page please let
me know. I'm quite keen for the Pluto documentation to more closely follow the “Ant” model of Apache documentation
(ie: lots of correct and understandable documentation) than the “Maven” model.

Java Search JSR?

I am wondering if there are any updates on the progress of the JSR proposed in
this
message? Small quote:


The Search Specification will be an API for composing and federating search queries to a
set of search providers, and aggregating returned results. The main
goal is to support search as a way of integrating enterprise
systems. Examples of use cases include a Java web application or
portal that offers combined Web, enterprise, and site search, or a front-end
Java customer support application that offers search of a variety of
back-end enterprise applications, as a way of locating all available
information on a particular customer.

Java Projects Someone Should Write

I often come up with “really good ideas” for programming projects (many may not actually be good ideas, but
who's counting..). Since I don't have time to do these projects, perhaps someone else will. Let me know if you finish any of
them….

Aggregator using the Rich Client Platform & Classifier4J

JMX console for Eclipse

Proper java launching exe's, which examine the hardware they run on and pass appropriate arguments to the VM. There
would be separate launchers for different configs, eg “javaserver.exe”, “javaclient.exe” etc.

A better build tool (and/or decent docs for Maven)

A taglib based Portal for Apache Pluto

An application infrastructure thing that downloads Jars from the Maven repositry as they are required (aside – that
repositry really should be mirrored)

Sync4J – a java library that will sync two directories by using various methods including email.
(yes, I know about RSync in Java, but the email thing is important to me).

A Velocity based XDoclet replacement that actually works. It should include
a translation tool to translate xdt XDoclet files into Velocity templates.

An XDoclet template for JSR168 Portlets

A web application framework that merges the web request<->response model and
messaging (possibly JMS): A request is sent down a message bus, and services
on the bus can subscribe to particular request types (think specific URLs).
The services modify the application data model, and when all services have
processed the request a response in generated, which is sent back up the bus
for processing by presentation services.

Dashboard in Java

A project like MS Application Blocks, but for J2EE (and don't suggest the
“Core J2EE Patterns” book. That book is good, but the code isn't alway
exactly right).

Something which exposes JMX attributes as Windows Performance Monitors
(think JDK 1.5 VM as well as app server monitoring).

AustDevBlogs.com.au: An Australian developer blogs aggregator.

A Java Servlet Filter version DOSEvasion

I'll update this post with more projects as I remember them.