Wednesday, July 29, 2009

Accessing protected URL in Java

Come through an elegant code yesterday. Somehow the content needs to be accessed from the database server which is password protected. A simple URL query would fetch the content. I've tried Apache commons. I could managed to access the content via plain java.net classes works for me pretty smoothly.

here is how

Authenticator.setDefault(new MyAuthenticator("user","password"));

URL url = new URL("http://localhost:5300/database?_query='for $a in collection('test')/types return $a'");
InputStream resultStream =url.openStream();

And the simple inner class
class MyAuthenticator extends Authenticator {
private String username, password;

public MyAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}

// This method is called when a password-protected URL is accessed
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password.toCharArray());
}
}

Are there any better way?

Monday, July 27, 2009

Returning null from methods in Java

It's best not to return null from a methods that returns an String type. instead return just empty string "". This will actually avoid extra checking in the calling method. Return variables can be created and initialized with empty (""). If any results constructed in the method, can be assigned to this result variable. if nothing constructed, it might fall on the last instruction that simply return the initialized variable.

Another approach can be using an Static String constant when empty string need to be requited.

It's best not to return null from a method that returns anarray type. Always returning an array, even if the array has zero length, greatly improves the generality of algorithms. If you
anticipate that your methods will return zero-length arrays frequently, you might be concerned about the performance implications of allocating many such arrays. To solve that problem, simply allocate a single array, and always return the same one, for example:
<code>private static final int ZERO_LENGTH_ARRAY[] = new int[0];</code>
This array is immutable (it can't be changed), and can be shared throughout the application.

We can return null in case of returning an Object.

Sunday, June 14, 2009

Application launchers

Now it's season of thinking (at least for my known circle) for improving productivity in workplaces. It happened to me to read few chapters in a book by Neal ford, called "The Productive Programmer" A somehow very informative book on introducing various tools which can actually help in increasing the productivity.

I still love command line tools, pretty fast, low memory footprint. Over the years, i always try to create batch files are shell scripts for my routine work, like start various servers with different settings, invoking project build, folder syncing. Once i create them, i put all together in one folder and make the folder available in the PATH. That's all. The problem i was facing with this approach, Whenever i change my machine, it was hard for me to transfer them in to my new machine and make them work as the path changes.

So i decided to give a try to launchers. I tried several application launchers and i settled with Launchy, pretty decent tool then. Though i like it's simple UI, It doesn't completey solve my needs. When i looked at Executor, i was impressed and currently playing with it. At first glance, it's UI looks simple and skinnable(I'm not a fan of fancy skins, but it allows me to choose my colors), its feature list also impressive. Mostly I'll end up with Executor.

Another application launcher is ENSO is worth to give a try. it may be strong by features, for me it is not user friendly.

Until next time,
Arunachalam

Monday, May 25, 2009

Adobe Flex

I'd like to refresh my UI knowledge after a long gap of 2+ years. By the time i left eclipse based plug-in development, i tuned myself in core application program. I did few XML server side stuff and managed a team in Java Swings and finally built an adapter for webMethods Integration server. I was into little bit of XQJ1.0 spec implementation. little bored? yes. now i get some time. Somehow i turned my attention from core application to UI.

By the time, i looked UI, it tremendously grown and more stylish too. JSP, Struts, velocity, looks like vintage classic tools to develop UI. Now i could see this is the age of Rich Internet application (RIA). As i want to part of this modern phase, i started learning Adobe flex.

I'll write my experience with Flex soon.