Gow – Linux-ify your Windows batch programs

Gow is a very useful command line tool for Windows users. It enhances windows batch command line it by adding a lot of Unix/Linux known commands (ls, grep, cut, awk, …)

What a pity,  if it had “vi” it would have been really a must !

Gow website link.

Advertisements

Handling proxy in Java Applications


This post has been updated on June 2012. If you have read this post before or have downloaded the related resources, please reconsider this post.
There was a bug in the previous resources (mistyping in the source codes).

Static proxy selection
If you know your proxy settings (i.e. proxy host, proxy port, authentication …), this is quite an easy task. Just set in your Java program your proxy settings through system properties as follows

   System.setProperty("http.proxyHost", proyHost);
   System.setProperty("http.proxyPort", proyPort);
   System.setProperty("http.proxySet", "true");

Or

System.setProperty("https.proxyHost", proyHost);
System.setProperty("https.proxyPort", proyPort);
System.setProperty("https.proxySet", "true");

for HTTPS connections.

Note these same properties can be set on command line through

java -jar application.jar -Dhttp(s).proxyHost=myProxyHost -Dhttp(s).proxyPort=myProxyPort

Automatic proxy selection via proxy auto configuration “pac” script (.pac)
If proxy settings are set automatically via a proxy pac script (a script that is generally configured on web browsers like IE or else), one have to parse the auto configuration pac script to determine if a proxy is needed for a given targeted URL or not, and if so what is the proxy configuration to set ?

In this article (see in downloads section), I provide a “jar” that contains a utility Java class ProxyUtil that parses the auto-configuration proxy script and determines the adequate proxy settings for a given target URL as configured in the script.

Usage:
In your Java code, call

// (old post, deprecated) ProxyUtil.getProxy(URL, URL): IProxyInfo
ProxyUtil.getProxy(URL, URL): java.net.Proxy

which returns a java.net.Proxy instance that contains the proxy host and proxy port metadata for the input target URL (say “http://www.worpress.com” or else). Please refer to the Java documentation for more information about the java.net.Proxy class.

ProxyUtil.getProxy takes the “proxy pac script” URL and target URL as inputs. Once the proxy metadata obtained, one can set proxy parameters through System.setProperty(...) as explained earlier in this article.

Where can I find my proxy pac auto configuration script URL on Windows ?
On windows systems, the auto configuration pac URL is generally found (if any) in the windows registry under the path/key

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings

Under the variable

AutoConfigURL

To get the script URL, one can type on the Windows command line

reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v "AutoConfigURL"

downloads

references

Special thanks to M. Broutin who helped me a lot to develop this part (might even say that he has done almost every part of it !)

a Spring MVC template package (Spring 2.5, Maven 2)

Description

Spring MVC is a powerful MVC framework (Model-View-Controller) for web applications development.

Find here a template package (using Maven 2) thats configures a very simple and basic hello world web application based on Spring MVC  (2.5). This can be a starting point for a more complete web application using this framework.

Template Application architecture here.

Downloads: A light package that includes no libraries can be downloaded here.

Important notes

– This template was tested under Apache Tomcat 6.

– The “javax.servlet:servlet-api” is included as a provided dependency, since it is already embedded in the Tomcat 6 distribution.

Keywords: Spring MVC 2.5, Maven 2, Tomcat, Java J2EE

Reading and writing simple XML documents using w3c DOM library

Generating a new XML document

1. Obtain a new instance of an XML parser and a new document builder instance

DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();

2. Obtain a new DOM document from the former factory

Document document = builder.newDocument();

3. Generating the XML tree content

3.1. The root tag element (that I called for convenience “my-root”)

Element rootElement = document.createElement("my-root");
document.appendChild(rootElement);

3.2. A child tag element (that I called for convenience “my-child”), setting to it an attribute name “my-attribute” with a value “my-value”

Element documentElement = document.createElement("my-child");
documentElement.setAttribute("my-attribute", "my-value");
rootElement.appendChild(documentElement);

4. Saving the XML tree to a file

XMLSerializer serializer = new XMLSerializer();
serializer.setOutputByteStream(new FileOutputStream(file));
serializer.serialize(document);

Lists, Maps and hashCode

List’s are better suited when iterative operations are to be performed on your objects (use Iterators for this):

Iterator iterator = list.iterator();
while (iterator.hasNext()) {
Class object = iterator.next();
// do something
}

Map’s are better suited when search operations are to be performed on your objects, as Maps are (Key, Value) associative arrays:

map.get(key);

However, in order to make the Map’s search/fetch operations really efficient one must implement / override the int hashCode() method of Java Object’s, as Java uses this method to perform search / operations in Maps (see HashMaps, HashTables, …).

int hashCode(), what and how?
One might wander what is this method that, for Classes with several int‘s, String‘s, Object‘s, …etc attributes, returns a single int! If so, different Objects might have the same hashCode!

The answer is YES! Different objects might have the same hashCode. Even more, if your int hashCode() returns a different and unique hashCode for each different Object, you’re doing wrong and your code is not efficient at all! However, the hashCode must be the same if two objects are equal from the application point of view. This is why we generally advice people to override the boolean equals() method each time the int hashCode() is implemented.

The illustration is quite simple: as (Key, Value) pairs are added to the Map, Java will save a reference of the (Key, Value) pair in a (a bi-dimensional) table where the row index is determined by Key’s hashCode. So (Key, Value) of equal Key’s hashCode will find themselves on the same row and thus (Key, Value) pairs are grouped into subgroups (subgroups are the rows of the table) and searching / fetching a Value within a Map by it’s Key is reduced to a search on the row index containing the corresponding pair.

You understand now (I hope) why Key objects returning a unique hashCode value for each Object is not efficient since it will create a table with as many rows as Keys’s references … In the opposite way, returning the same hashCode for all objects will create a single row for all (Key, Value) pairs.

All the staff is to write a int hashCode() method that wisely spreads and divides your (Key, Value) pairs into subgroups of equal but not huge capacity (what do I define by huge? Only god knows!) … think of using “modulo” in computing your hashCode It surely will be of great help!

Hope this might help!

String tests in Java

NEVER test your input Strings this way:

public boolean equals(String arg) {
return arg.equals(MY_CONSTANT_STRING);
}

What happens if the input String “arg” is null ?! You’ll get a NullPointerException.
Rather than a cumbersome test of the input String nullity, implement your test this way (Java best practices) :

public boolean equals(String arg) {
return MY_CONSTANT_STRING.equals(arg);
}

Load a properties file on a BlackBerry RIM application

“Properties” class does not exist on the BlackBerry Java API. Find here an implementation of this class for your RIM applications.

Usage example:

   Class clazz = Class.forName(this.getClass().getName());
   InputStream stream = classs.getResourceAsStream("/file.properties");
   Properties properties = new Properties();
   properties.load(stream);
   // some code
   properties.getProperties();

Download link Properties.java.