Wednesday, September 19, 2007

Fun programming

There are some programmer like me who code for money but there are few who just love programming. I would debate the wastage of such good programming brains for these useless programs, but i love them and I admire them. Have a look at 99 bottles of beer my favorite is this one elegantly written in perl, and have a look at this one in Java (satire).

By the way if you have some free time try this           

Developer tools for browser

Firefox developer tools: Web developer and Firebug
Internet Explorer Tools: IE Developer Toolbar

I think these are must for webapp developer, very useful in debugging. Web developer for Firefox is really Firefox version of IE Developer Toolbar. I specially like the Inspect feature for those nasty UI issues.

Multiple Profiles using firefox

Ever faced this situation, you are logged into your gmail, yahoo, orkut, hotmail etc. One of your friends came up to you and want to check his/her gmail,yahoo,orkut etc. You need to log out, I know its quite irritating. But thats where multiple profile feature of firefox come to rescue, you can use following command.

firefox -no-remote -p

create a new profile for your friend and you don't need to logout.
This is also useful when you are developing web application and need to loging as multiple users to complete one usecase. I generally update my desktop firefox shortcut with above.

Saturday, August 18, 2007

weirdo NULL handling in SQL

Conisder following SQL statement
SELECT * FROM sometable WHERE num <> 1;
If you expected this to return rows where value of num is null, then you are wrong. Similarly
SELECT * FROM sometable WHERE LENGTH(my_col) < 20
above will not return rows with my_col having null values.

This is bcoz SQL don't consider null as a value and since null is not a value it cannot be compared with a value. This is the diffrence between Null and either 0 (zero) or an empty string (a string value with a length of zero, represented in SQL as '' ). While null indicates the absence of any value, the empty string and numerical zero both represent actual values. So be careful while you are writing "less than" or "not equal to" queries on nullable column.

Got some more time read this on wikipedia.

javax.net.ssl.SSLHandshakeException

If you see collowing exception, while accessing HTTPS URL from java code.
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: No trusted certificate found
Then most likely the URL you are accessing doesn't have certificate signed by a CA, and you need to install this certificate manually. Use keytool to import the certificate to a keystore (lets name this as mykeystore and you put this in /etc/keystoers dir) and then start your java application with following parameter -Djavax.net.ssl.trustStore=/etc/keystores/mykeystore.

Hibernate: Computed property value

Many times you want to have computed value from db as a property value of an entity. For simple computation, where you additional information from DB is not needed, you use java code which is fine. But if you need information from DB, it means additional queries. Hibernate provides a powerful feature known as as derived properties

For example consider there is are table called Order and OrderItem; an Order has multiple OrderItem(s) and OrderItem has a field called price. Now if you want orderPrice property in Order Entity such that
orderPrice = sum of all OrderItem.price*OrderItem.qty where OrderItem.orderId=<my order id>.
This can be done simply by defining a property in hbm file like this .

< property name="orderPrice"
formula="( SELECT SUM (OI.QTY*OI.PRICE) FROM ORDER_ITEM
OI OI.ORDER_ID = ID )"/>
These properties are by definition read-only, and the value is computed at load time. You declare the computation as a SQL expression, this translates to a SELECT clause subquery in the SQL query that loads an instance. Make sure you use SQL names (TABLE and column names) and not the Java names (Object and property names). also don't forget to put the formula inside "(" and ")".

Ant javac: Unknow source in stacktrace

I,Nidhi and Kush at mChek were debugging some code and got following exception

Exception in thread ... java.lang.ClassCastException
at SearchCustomerAction.search(Unknown Source)
Some googling helped and we found the reason. It was obvious that Java compiler didn't generate the debugging information. But why? I thought I had not provided any extra javac parameters, and by default javac should generates debugging information (it actually generates line number and source file information by default, local variable information is not generated by default). So wats wrong?

Actually we had used Ant for our build and javac Ant task by default doesn't generate debugging information. So the debug flag need to be turned "on" explicitly, something like this:
<!-- build.xml -->
<project name="someProjectName" default="build" basedir=".">
<target name="build">
<javac srcdir="." destdir="." debug="on"/>
</target>
</project>
For more information see javac and ant javac task.

Saturday, August 04, 2007

AJAX response: HTML, XML, JSON

Its fun to have the partially refreshing dynamically modifiable page using AJAX, both for application developer and for application user. But as a application developer parsing the partial response from server and dynamically modifying the DOM can become very complicated, parsing the response itself can be very tedious. Though the 'X' in AJAX stands for XML but your response need not be XML, it could be any text format, for our discussion we consider following three text response types

  • HTML/plaintext
  • XML
  • JSON
If you just need to modify one section of page and can use the response as is, then its no-brainer, you simply use HTML or plain text response. e.g. if you build expand collapse control where you want to display some information in a div/span you just get processed HTML from server and use it as follows in you onreadystatechange event handler:
var expandAreaDiv= document.getElementById("expandAreaDiv");
expandAreaDiv.innerHTML=text;
But life is not this simple always :-). As you application matures need arise to modify multiple section of the page with one response. One response and multiple modification means you can no longer use the response as is, you have to parse the response. By default we tend to use XML as the only option; after all 'X' in AJAX stands for XML :-)
Take a simple example, if you performed update of some entity using AJAX than probably you need to do following
if success
update the status field
show the success message
else
show the failure message
So bare minimum you need three separate informations from server in one response
boolean value indicating success/failure
String value containing the message response
String value containing the new Status
So you decide to have XML like this

<myResponse>
<isSuccess></isSuccess>
<newStatus></newStatus>
<message></message>
<myResponse>

and in javascript you parse the response to get values into variables and then use it..
var response= req.responseXML.getElementsByTagName('myResponse');
var isSuccess = getNodeValue(response,'isSuccess');
var newStatus= getNodeValue(response,'newStatus');
var message= getNodeValue(response,'message');
function getNodeValue(obj,tag){
return obj.getElementsByTagName(tag)[0].firstChild.nodeValue;
}
and as your XML becomes complex parsing becomes more complex and you start looking for some parsing tools. This is where i like JSON, which stands for JavaScript Object Notation. Your JSON response would look like this:
{
"isSuccess" = true,
"message" = "Successfully updated the Status",
"newStatus = "Active"
}

and here is you response parsing code in JS:
var response = eval( '('+req.responseText + ')' );
and voila you can acess response.isSuccess, response.newStatus, response.newStatus. All parsing is done for you just by eval().

Be careful that you take care of JSON special character " and control characters like CR NL in the generated response.
e.g. "message" = "Successfully
updated the Status",
will not work. JSON is not as readable as XML, but its more readable for computer :-) . Though I could not find any powerful JSON editor but this one is useful.

Finally, as far as security is concerned JSON by itself is just a data format (like XML) so it is secure, but the eval function is not, so if someone could inject malicious script into your JSON, that would get executed on to the client. So make sure you validate the data before you put into JSON response (this you should do anyway if you need your application to be secure).

Friday, August 03, 2007

html checkbox submitting unchecked value

If you have used checkbox you would know that the checkbox value get submitted with form only if checkbox is checked, this is required by w3 html spec . What if you want to know if a checkbox was already checked when page was rendered and now unchecked by user. Specially if you are using a framework like struts and want to set a value in ActionForm based on checkbox value. Try submitting the form using Java script


<form name=myForm action="something.do" >
<input type=checkbox name=mycheckbox value=test />
...
Your normal form code here
...
<div id="hiddenArea"/>
<input type=button name='submitMyForm' value='submitMyForm' onclick='submitMyForm()'>
</form>

function submitMyForm(){
var hiddenArea = document.getElementById("hiddenArea");
var myCbx = document.myForm.mycheckbox;
if(!mCbx.checked){
var hiddenStr = "<input type=hidden name=mycheckbox value=notest />";
hiddenArea.innerHTML =hiddenStr;
}
document.myForm.submit;
}

Array: new vs. java.lang.reflect.Array.newInstance

Since i was writing about array I thought this is also worth mentioning.
There are two ways of creating array
String str[] = new String[4];
String str[] = (String[]) java.lang.reflect.Array.newInstance(String, 4);

Both are same, you use second one where you can't use the first i.e. you don't know the type of array in advance.

Lets discuss Array Typecasting

You would think this code should work:
Object[] objs = new Object[2];
objs[0]="Hello";
objs[1]="World";
String[] strs = (Str[]) Objs;
// print the array..

Code compiles fine, but when you run it, you get java.lang.ClassCastException. And you start wondering why? String is subclass of Object so its not narrow conversion then why do i get ClassCastException. I tried to find answer on net but could not find, so below is just my own logic (disclaimer :-) )

String extends Object fine, but String[] doesn't extend Object[] both of them extend Object, in short they are not in hierarchy so you get ClassCastException. Lets look into more detail

Lets try System.out.println(strs.getClass()); which prints --> [Ljava.lang.String
and System.out.println(obj.getClass()); prints --> [Ljava.lang.Object
only common thing is [L, what is this?

Class Type that defines an Object consist of two things ClassType and ComponentType. For all array objects class name is same i.e. [L, but component types differs. Which implies Java Arrays are special type of classes whoes Class Type is represented by [L. If you had some way of setting the componentType, typecast would have worked. But it is pretty obvious that setComponentType can't be exposed.

Now finally I think we should discuss the solution to above problem. There are two solutions but none which could avoid an additional array creation.

Solution 1:
String[] strs = new String[objs.length];
System.arraycopy(objs,0,strs,0, objs.length);

Solution 2:
If you are using List you can use Object[] List.toArray(Object[])
//assuming you have done List list = Arrays.asList(objs); for our current example.
String[] strs = new String[2];
strs = (String[])list.toArray(strs);

Now if you look at solution 2 and wonder why typecasting worked here, it worked because ComponentType of the Array returned by toArray method has ComponentType same as the componentType of the Array passed to it. The argument is just used to get the comonentType
i.e. this would also work
strs = (String[])list.toArray(new String[0]);

Sunday, July 29, 2007

Primary Key vs. Unique Key

There are two differences between Primary key and Unique Key:

  • First and a trivial difference is that Unique Key would permit null value (one null value per cloum in the unique key constraint), while Primary key would not allow for null value.
  • Second and the major difference that you should know is that the Unique Key indexes are non-clustered while Primary key is clustered index. This difference has following corollaries

    • There could be only one PK per table emanating from the fact that there could be only one clustered index on a table.
    • The PK affects how the data is physically stored but a unique key doesn't.
Now the question is what is a clustered index and why there can be only one clustered index on a table. Clustered index is a index with leaf node containing actual data and not pointer to the actual data while for Unique Key index, leaf node contains pointer to the actual data storage.

HTTP Head method

HTTP HEAD request is similar to HTTP GET request except that the Server will not send and response body in response to the request. e.g. if you make a HEAD request to resource /image/test.jpg, server will send only the response status to the client (200 if the image exists) the actual image will not be transmitted to the client. This is useful in cases where you are referring an external resource and not sure if a resource exists on server or not. Or you want to know if you have access to certain resource on server, or ever if you just want to know when the resource on the server was last modified.

The HEAD request can be very useful specially with AJAX calls.

Reversing words in a String

This I think is by far most popular string Interview question, be it on Java/C/C++ or C#
Question : Write procedure to reverse the words in a String, assume words are separated by blank-spaces e.g. given a string "Writing and debugging software", the output of the procedure should be "software debugging and Writing"

Here is the code which i had written recently in Java, i think the code and algorithm is self-explanatory, but if you need help understanding the code please let me know.

If you have a big string and don't like the idea of O(n) space complexity and want to do it in O(1) space, than instead of putting words in word and to_ret arrays, you just store the array indexes and as soon as you find a blank space shift the original array up by the last word length and add the word to appropriate position in the string. This is just a hint i'll leave the implementation to you.. I can send you the code, if needed.


public class Reverse {
public static String reverse(String str) {
char[] chars = str.toCharArray();
char[] to_ret = new char[chars.length];
char[] word = new char[chars.length];
int l = 0;
int last = chars.length - 1;
for (int i = 0; i < chars.length; i++) {
if (chars[i] == ' ') {
for (; l > 0; l--) {
to_ret[last] = word[l - 1];
last--;
}
to_ret[last] = ' ';
last--;
l = 0;
} else {
word[l] = chars[i];
l++;
}
}
//add the last word if any
if (l != 0) {
for (; l > 0; l--) {
to_ret[last] = word[l - 1];
last--;
}
}
return new String(to_ret);
}

public static void main(String[] args) {
String str = "This is a beautiful World";
System.out.println(str);
System.out.println(reverse(str));
}
}

Printing session attributes and request attributes in JSP for debugging

I generally like to use following JSP which I call req_session_attribute_printer.jsp for debugging purpose. I just include this jsp, where ever i want to debug, i have found this very useful over time for simple debugging.


<%--
req_session_attribute_printer.jsp
Author:<a href="mailto:get.anurag at gmail do com">Anurag Kumar Jain</a>
Date: July 25, 2007
--%>
<%@ page language="java" contentType="text/html; "%>
<script type="text/javascript">
function showHideAttributes(spanId){
if(document.getElementById(spanId).style.display=='block'){
document.getElementById(spanId).style.display='none';
}else if(document.getElementById(spanId).style.display=='none'){
document.getElementById(spanId).style.display='block';

}
}
</script>
<a href="#" onclick="showHideAttributes('debugSpan')"> Attributes</a>
<span id='debugSpan' style='display:none'>
Session Attributes:
<% java.util.Enumeration salist = session.getAttributeNames();
while(salist.hasMoreElements()){
String name=(String)salist.nextElement();
Object value = session.getAttribute(name);%>
<%=" "+name+"="+value %>
<% }%>
Request Attributes:
<% java.util.Enumeration ralist = request.getAttributeNames();
while(ralist.hasMoreElements()){
String name=(String)ralist.nextElement();
Object value = session.getAttribute(name);%>
<%=" "+name+"="+value %>
<% }%>
</span>



This is specially very helpful for debugging in environments other than dev, where you have less control.

IE Caching issue with AJAX HTTP GET request

There is one very annoying issue with IE using AJAX when you use a HTTP-GET request (I haven't faced this problem with HTTP-POST request). I tried following but nothing seemed to work:

response.setHeader("Cache-Control","no-cache"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", 0); //prevents caching at the proxy server


So we need to fool IE to consider the request as new request everytime, and you can fool IE by adding current timestamp at the end of your url. You can do following in your Javascript code:

url= url+"?currDate="+ new Date(); if your url is simple like /test/something.do
OR
url= url+"&currDate="+ new Date(); if your url contains request parameters and looks something like /test/something.do?id=1234

Hibernate Reverse engineering tool

May time you want to reverse engineer the Hibernate definitions from already existing scheme. Hibernate tools with Ecliplse plutgin is a very useful for this purpose, more information here on hibernate.org.

Creating your own ajax loading image

I found this useful for creating images for my AJAX loading ICONs http://www.ajaxload.info/

Enabling Expression Language (EL) in JSP

You can do it in two ways:

  • Enable it for whole application : Put following in web.xml

    <jsp-config>
    <jsp-property-group>
    <url-pattern>*.jsp</url-pattern>
    <el-ignored>false</el-ignored>
    </jsp-property-group>
    </jsp-config>

  • Enable it for just one JSP page : put following at top of your JSP Page
    <%@ page isELIgnored="false" %>
Make sure when you use the first option you use 2.4 schema or above. i. e. your root tag in web .xml looks something like this.

< web-app id="WebApp" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">

make sure you are using Jasper jars for JSP 2.0 or above.

Saturday, July 28, 2007

AndroMDA: Generating create/drop schema script with Maven 2

First of all I would recommend anyone who is using the anroMDA for first time to read
${application_root}/readme.txt file carefully. This helps understanding the generated schema.

If you read readme.txt, you will notice two tasks

  • mvn -f core/pom.xml andromdapp:schema -Dtasks=create --> generates the DDL create code and subsequently tells the database to create the schema for the entities

  • mvn -f core/pom.xml andromdapp:schema -Dtasks=drop --> generates the DDL drop code and subsequently tells the database to drop the schema for the entities

  • mvn -f core/pom.xml andromdapp:schema -Dtasks=drop,create
But wait, these target don't seem to work for me if i use maven-2 (mvn) with AndroMDA 3.2.
add following to the plugin configuration in core/pom.xml
<executescripts>false</executescripts>
<tasks>create</tasks>
<tasks>drop</tasks>
and now when you run the above targets, the create and drop DDLs are generated in the path specified by createOutputPath and dropOutputPath property.

The default location for generated scripts is ${application_root}/app/target/

Remote and Local EJB with AndroMDA

@andromda.ejb.viewType "both" isn't supported, that's the reason. If you want to use remote at all, then set it to "remote", most application servers (like JBoss) treat remote interfaces as local when they're in the same JVM anyway.

Understanding Java Security and permission

This is a must read for any java programmer, combined with this article on JavaWorld

Monday, June 04, 2007

Installing AndroMDA plugin for Maven 2

  1. Click here to download the the AndroMDA plugin installer.
  2. Unzip the contents of the installer into your Maven repository at C:\Documents and Settings\your user name\.m2\repository.
  3. Verify that the following directory was created:
    C:\Documents and Settings\your user name\.m2\repository\org\andromda\maven\plugins\andromda-maven-plugin
  4. Create a temporary directory, e.g. C:\andromda-temp.
  5. Create a file called pom.xml in this directory with the following content.

  6. <project>

    <modelVersion>4.0.0</modelVersion>
    <groupId>samples.test</groupId>
    <artifactId>test</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <name>test</name>

    <build>
    <defaultGoal>compile</defaultGoal>
    <plugins>
    <plugin>
    <groupId>org.andromda.maven.plugins</groupId>
    <artifactId>andromdapp-maven-plugin</artifactId>
    <version>3.2</version>
    </plugin>
    </plugins>
    </build>

    <repositories>
    <repository>
    <id>andromda</id>
    <name>AndroMDA Repository</name>
    <url>http://team.andromda.org/maven2</url>
    </repository>
    </repositories>

    <pluginRepositories>
    <pluginRepository>
    <id>andromda</id>
    <name>AndroMDA Repository</name>
    <url>http://team.andromda.org/maven2</url>
    </pluginRepository>
    </pluginRepositories>

    </project>
  7. Open a Command Prompt in the directory where you created this pom.xml and run the command mvn without any arguments. Make sure the command completes successfully by displaying the BUILD SUCCESSFUL message.
  8. You can now delete the temporary directory you created in step 1.
  9. use maven 2.0.5 or 2.0.4. i was getting following error with 2.0.6, see andromda forum topic for details. You get "An invalid artifact was detected." error with maven 2.0.6
  10. Use following command to generate the project:
mvn org.andromda.maven.plugins:andromdapp-maven-plugin:generate

instead of

mvn andromdapp:generate

Wednesday, January 24, 2007

Getting started with Andromda

getting started with this link.
http://galaxy.andromda.org/docs/getting-started/java/create-application.html
you might face few problems

here are solutions to some problems that I faced:
1. maven-andromda jta jar need to be copied manually
2. http://galaxy.andromda.org/forum/viewtopic.php?t=3158&view=previous&sid=67ab10397f5b7c679840db5c0eff4120
3. repository problem
4. installing plugin for mavan 2

Tuesday, January 02, 2007

ibiblio maven repository 301 eorror

I was getting following error
Error retreiving artifact from [http://www.ibiblio.org/maven java.io.IOException: Unknown error downloading; status code was 301

public maven repository on ibiblio was moved to a mirror site. This is indicated by HTTP response status 301, which indicates file is permanently moved. Maven 1.0.2 do not seem to understand redirect.

Solution: Add http://mirrors.ibiblio.org/pub/mirrors/maven to the list of remote repositories in project.properties in addition to the original entry (http://www.ibiblio.org/maven).

For Maven 2, this should not cause any problem, as you can specify mirrors in settings.xml. For more details see the following link: http://maven.apache.org/guides/mini/guide-mirror-settings.html