Thursday, September 10, 2009

JVM Bind Exception Port Already In Use (Windows)

a) To find the application that is using the port in windows use this command from the prompt: netstat -a -n -o

b) The next step is to kill that application or process that is using that port. Use the taskkill command from the prompt: taskkill /F  /PID 1056 where 1056 is the process id.

Friday, July 31, 2009

Friday, July 17, 2009 sheds some really good insights on XML schema namespace managing and versioning, and strategies to adopt.

Thursday, July 16, 2009

Normative Architecture

A normative architecture is one based on codes and communications.

a) Where naming patterns of objects can be applied.
b) XML Schemas can be used within an organization so that different systems can be adaptive to change(s) within an organization.
c) A central normative repository in an project would also be very usefull in terms of applying security, and modularising products.
d) In my mind even certain apsects of the frameworks, if it is backed by a database design would allow it to scale and also be descriptive. One can argue that frameworks can also reflect/extend or other ways and capture this information, however there would be more control over what can be added and prevented when a framework si backed by db schema.

Just reading up on architecture ... and gathering some points.

Thursday, July 9, 2009

CodeNaut: Tabbed Command Window (Windows)

CodeNaut: Tabbed Command Window (Windows)

Tabbed Command Window (Windows)

For regular desktop development where you need to have multiple command windows open I found this utility very usefull.

a) Name your command windows and set default locations, and open them as a tab.
b) Set wallpapers for command windows.
c) Copy and paste text. 

I wish you could highlight specific words in the command window that will be a cool feature to have.

Tuesday, May 5, 2009

Publish DB Documentation using Schema Spy

You can create database documentation and relationship diagrams in a jiffy using
"Schema SPY". Schema Spy allows you to generate the metadata about the database and all sorts
of statistics that are helpfull at a glance. It supports multiple databases to generate the schema documentation of course if you are a java developer it should be more simpler, to get this configuration working.

You may optionally need a graphical library to draw the relationships called graphviz.

Other dependencies are to have the driver jar files in the class path and execute the schema spy jar from the command line.

Thursday, April 30, 2009

Multiple Faces-Config.xml in your JSF application.

Modular JSF code.

Facelets are a great templating tool for JSF and can also give performance benefits when you properly structure the way you load the pages.

If you want to modularize your web application by packing the backing beans in separate jar files, the JSF specification allows you to do that. All you have to do is pack the beans in a separate jar

file and in the META-INF folder you put your faces-config.xml file. According to the JSF specification all the jar files that contain a faces-config.xml are also automatically loaded.

You can also pack the jsp files that are specific to the module in the same jar. The trick you define a custom class that implements "com.sun.facelets.impl.ResourceResolver" interface and in your web.xml file add the facelet parameter to tell your web app to use the new custom resource resolver.


The resource resolver has one single method to implement and here is a snapshot of that code.

public URL resolveUrl(String path) {
try {
if(null != path && path.contains("/CUSTOM_INCLUDE/")){
path = path.substring(path.indexOf("/CUSTOM_INCLUDE/") + 20);
URL url = THISCLASS.class.getClassLoader().getResource(path);
return url;
// Default ResourceResolver
return Resource.getResourceUrl(FacesContext.getCurrentInstance(), path);
} catch (IOException e) {
throw new FacesException(e);

Though this mechanism works for the jsp pages and the java files as separate sub modules, if the code depends on CSS or java script then that does not get included properly. A new framework is available on called "weblets" that looks much better than the above approach.

Debugging MySql queries using My-Sql proxy.

MySql introduced a cool new utility called MySql proxy which allows you to route the queries to the database via a proxy.

The proxy utility has a lot of features and one of the very basic features is to log the queries that are routed through it and log the time taken for the query execution. This helps when you want to tune the application.

Download the utility and unzip it to a location, configure the proxy port as mentioned in your datasource and configure the original database port in your proxy. It comes bundled with a few sample scripts supported by a programming language called "lua" (btw just heard about this ). Dont worry about the new programming language, the sample scripts are good enough to get you started.

Another surprise bonus was if you have hibernate /other applications in your web layer you can see how many queries that are run against the database.

Debugging Hibernate SQL queries in MY-SQL

Have you ever wondered what kind of queries that hibernate generates? Now you can with a cool new utility called mysql-proxy. This utility allows you to find out the queries that are hitting the database, debug them,

Thursday, March 5, 2009

Encoding URL's to access a resource over HTTP in Java

In java the URI class is more flexible to construct much more flexible URL's and also supports encoding.

URI uri = new URI("http","localhost","/temp/XYZ.xsd",null);
URL url = uri.toURL();
URLConnection urlConnection = url.openConnection();

Quote from the sun documentation "The recommended way to manage the encoding and decoding of URLs is to use URI, and to convert between these two classes using toURI() and URI.toURL()."  I did see this only on the 1.5 version not on 1.4 version of the doc. 

Setup apache to serve xml files.

Apache by default is configured to serve html files, if you want to setup Apache to serve other files then you have to edit the "httpd.conf" file. This file is usually located in your apache installation folder conf directory, open the file using notepad search for the following tag "<IfModule mime_module>" within this tag you have to tell apache to serve other content types, you have to add the new type that you want to publish with the following values

    AddType application/xhtml+xml .xhtml .xml

    AddEncoding xhtml xml

The first variable defines the type of media and the second tell the encoding that you want to use, this will instruct the browser to handle these media types respectively. For a list of registered media types see here.

Thursday, January 29, 2009

Validating XML against XSD in Java

To validate XML documents against XSD in java...

You can use the DocumentBuilderFactory or SAXParserFactory from

        String JAXP_SCHEMA_SOURCE = "";
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        String schemaSource = "Path to your XSD";
        Source schemaSourceSrc = new StreamSource(schemaSource);        // has various constructors to pass xsd as a file,url,stream and so on ...
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = schemaFactory.newSchema(schemaSourceSrc);
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(" your xml file ");

This will validate your xml file against the xsd. Many examples in the web show similar snippets...  More details are on the link below from sun


Note: If your XSD document contains regular expressions or data type restrictions then they are also validated.

XML Schema Regular Expressions

The regular expressions used in the XML Schema are of a different flavour other than the ones used regularly in Java. Not all pattern matching techniques apply directly in XML Schema.

For example to validate the decimal places I use the following pattern "\-{0,1}[0-9]*(\.{1}[0-9]{1,2})?" without the codes, what this pattern says is that the negative symbol is optional and if provided only one character of the negative symbol should be given. The asterisk denotes that any number of numerals may be provided, followed by a opening bracket and a pattern and closing bracket followed by a "question" mark symbol. The question mark symbol means that the pattern in the brackets is optional. In effect this pattern can accept whole numbers and optional decimal numbers.

Once you have defined that the node type is decimal and applied that above pattern. Then you can write a simple java class that reads the XML document and also applies the XSD to validate that XML document. And if the XML document contains invalid characters for that node it throws an excpetion saying that the value is not supported by that pattern.

Calculate Date for a given week in an year.

How to calculate the date for a given week number in an year.

For E.G. If you have the week number "23" to transalate that to a date like "July-17-2009" I used this technique.

Take the week number multiply that by 7 to get the number of days since the beginning of the year and then construct the date.

In MySql - there is a date function called "makedate" which takes the year and the number of days as an parameter

"makedate(year(logintime),week(logintime) * 7)" This will return a date like "2009-07-21"

In DB2 to do the same thing.

"(DATE(((week_iso(a.logintime)-1)* 7)+ 2) + (year(a.logintime) -1) years)"

Take precuation to assume your week beginning with respect to your business logic, some businesses the week begins on sunday and some for monday and so on...

In MySql there are system variables to set the default start of week...

Wednesday, January 14, 2009

How to analyze queries in my-sql and configure an index.

If you have a query in My-Sql and if it is running slow you can always add the explain keyword in front of the SQL to analyze it.

For example executing the following query "explain select * from employee where employee_id='001'; " would give this output.

Note the column where it says "possible_keys" and "keys" in those columns if those columns are showing up as null, then this means that there are no indexes being used.

The next step would be is to create an index and execute the explain query again. If you see the index that you just created then this means the query would use the index and run faster. There are a lot of tradeoffs on when to create indexes so use it with caution... This cool article explains this in depth...

Tuesday, January 13, 2009

Eclipse Scrapbook

In eclipse you can write few lines of codes without having to write the entire class declaration and main method to execute the few lines. This feature is called scrapbook if you have a java enabled project create a new file with the extension as ".jpage".

The next step would be is to write a few lines of code in that page...

  1. String message="Hello %1$s";
  2. message = message.format(message,"Jane Doe");
  3. System.out.println(message);

The step after that would be is to select the lines of code you want to execute and access the right click menu

Select the execute option and the just the selected lines of code alone would be executed and outputted in the console. I use this feature to quickly test a few lines of code…

Java String formatting with "printf" like commands

There is a cool new way to create String templates in java and use the String.format() static method to substitute values dynamically.

For example the code snippet

  1. String message="Hello %1$s";
  2. message = message.format(message,"Jane Doe");
  3. System.out.println(message);

The output is:

> Hello Jane Doe

This gives you an idea of how to create string templates and replace them by using printf like syntax. Here is the sun link that will give you more on this style of formatting. in our project we are using this on the message properties and substituting values for more meaningfull validation messages.

Here is one more example with multiple indexes.

  1. String x = "test who let the stars out '%2$s' ";
  2. String y = "me";
  3. String z = "you";
  4. String message = String.format(x,new String[]{y,z});
  5. System.out.println(message);

The index "2" in the line number 1 refers to the second array element in the string array that is passed to the format utility in line number 4.

Much more complicated example are available in the sun link. I tried the above examples in Java 1.5.

Web development with Firefox.

I am listing some of the Firefox addons that I have found to be very useful for web development.


  1. Firebug – Allows you to inspect the browser DOM, edit CSS on the fly and to find more information about what style is being applied to a particular table and to identify nested tables simply hover over them to find out all the details! Awesome!!!
  2. Screengrab – With this you can highlight a portion of the webpage and save it to clipboard or a file, simply cool!! I use it a lot!!!
  3. MeasureIt -  This tool will allow you to measure the width and height in pixels of anything/any area in your webpage
  4. ColorZilla  - You can select the color from an area and it will give you hex codes and RGB colors … highly useful!!!
  5. WebDeveloper -  Awesome tool you can do most of the stuff that you do with firebug with this.


I also like fireftp for ftp work and Pencil for developing mockups; but the idea that you can do most of the work from inside your browser without having to manage too many applications is really cool!