Tuesday, June 29, 2010

Generic solution to prepare the application URL from server end.

Problem description :
I was quite annoyed few days back when I got stuck for sometime while doing a very small task .
The requirement was to display an URL on a JSP page and the situation was such that, the URL value should be a DTO property value (may be populated by an Struts action class or whatever ) .
Say your application URL is "http://xyz.com:9080/PayrollApp".Now you want to display an URL which is like "http://xyz.com:9080/PayrollApp/edit.do".How to create such URL String ( without any sort of hard coding ) from an action class ? Well apparently it is quite simple , in fact it is , specially after you read this post , but trust me otherwise it is not.

The first thing that comes to our mind is "request.getContextPath()" will serve the purpose . Yes off course , but wait a minute ....it will do your job only partially . You will only get the application name (in this case the "PayrollApp" portion ) using request.getContextPath().


What about the previous parts ? The schema,host name ,port number ? How you will populate their value ? Confused ? Honestly speaking , I was .

Lets discuss about URLs in general . Generally what are the different parts of an URL ?
1.The first part is the protocol (here it is http).
2.The protocol is followed by ://
3.Next comes the host (in our example it is "xyz.com" )
4.You may have a port number also after the host name (9080 ) .
5. After the domain you may have your application name (as we have PayrollApp) .But always that is not the case.


Analysis and solution:

Well , the following code is self explanatory .Just use it ...

Sample Code:

String applicationPath = request.getScheme()+"://"+request.getServerName()+":"+
request.getServerPort()+request.getContextPath();


String finalURI=applicationPath+"/edit.do"

Notes :
Please note I have kept the "request.getServerPort()" , but that is not always the case .

Create a DB2 Data Source in JBOSS access it programmetically outside JBOSS context.

Problem Description:

It was a long lasting pain which only sufferers understand!!


Solution:
Just follow the steps mentioned one by one ...

1.We begin by adding its driver to the CLASSPATH: copy db2java.zip to the /server/default/lib directory. To configure the JBoss server with the DB2 data source, copy /docs/examples/jca/db2-ds.xml to the /server/default/deploy directory.

2.Next we modify the db2-ds.xml configuration file, by setting to

COM.ibm.db2.jdbc.app.DB2Driver and to jdbc:db2:, where is the DB2 database name.

Here it is start of db2-ds.xml

################################

"



test

false

jdbc:db2:tset

COM.ibm.db2.jdbc.app.DB2Driver

db2admin

db2admin

2

SELECT * FROM TEST.USER_TABLE

DB2


"

####################################

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

PLEASE NOTE :In the above file I have one entry

false

It is kept false for connecting to JBoss Datasource from a standalone client.

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

3.Remove hsqldb-ds.xml from /server/default/deploy

4.Go to jboss-4.2.0.GA\server\default\conf\standardjbosscmp-jdbc.xml

Then we modify standardjaws.xml (or jaws.xml) to set and .

java:/lkrgt

DB2

5.Now We need to test the data source from a Java class .

Copy all jar files from jboss-4.2.0.GA\client and place to lib or add to class path



Sample code:

package com.ayan;

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.Statement;

import java.util.Hashtable;


import javax.naming.InitialContext;

import javax.sql.DataSource;


public class TestConnect

{

public static void main(String[] args)

{

try

{

final String sql = "select * from LKRGT.TRAINING_REQUEST ";

Hashtable ht=new Hashtable();

ht.put(InitialContext.INITIAL_CONTEXT_FACTORY,

"org.jnp.interfaces.NamingContextFactory");

ht.put(InitialContext.PROVIDER_URL,"jnp://localhost:1099");

ht.put(InitialContext.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");

InitialContext ic=new InitialContext(ht);

if(ic!=null)

{

System.out.println("Initial Context success");

}

DataSource ds=(DataSource)ic.lookup("lkrgt");

if(ds!=null)

{

System.out.println("Data Source success");

}

Connection con = null;

Statement stmt = null;

ResultSet rs = null;

try

{

con = ds.getConnection();

stmt = con.createStatement();

rs = stmt.executeQuery(sql);

while(rs.next()) {

System.out.println("Query '" + sql + "' returned " + rs.getString(1));

}

}

finally

{

if(rs != null)

rs.close();

if(stmt != null)

stmt.close();

if(con != null)

con.close();

}

}

catch (Exception e)

{

e.printStackTrace();

}

}


}



Generate DDL to a text file using Hibernate rather than firing it to DB.

Problem Description:
I never liked the idea of using hbm2ddl.auto attribute directly inside hibernate.cfg.xml file directly . Suppose you have this mapping equal to "update" or "create" in local/development environment and somehow you change the connection related parameters to connect to production environment .Now if forget to change/disable the hbm2ddl.auto property,what next ?? This is really really dangerous .But then how to generate the DDL ?

I do not want the risk of using hbm2ddl.auto , but I want the DDL .

Solution:
Using the following code which uses SchemaExport class of Hibernate , generate the text file which contains the DDL.Copy that DDL and fire to DB as per your requirement .

Sample code:
public class SchemaExportUtil
{
public static void main(String[] args)
{

Configuration configuration = new Configuration();
configuration.configure();
SchemaExport schemaExport = new SchemaExport(configuration);
boolean generateScript = true;
boolean fireScriptToDB = false;
String schemaExportFileName = "c://script/DDL.txt";
schemaExport.setOutputFile(schemaExportFileName);
schemaExport.create(generateScript, fireScriptToDB);

}

}

How to use this program :

If the mappings are correct scripts will be generated according to the dialect mentioned in hibernate.cfg.xml.

If you the script to be fired at the database , just set fireScriptToDB=true.Otherwise it will write the DDL to the mentioned text file.The choice is yours now !

Mapping issue with nullable primitive types in hibernate.

Problem description :
You have many columns in a table which are mapped as primitives in Hibernate and you have the getters and setters ready .But the code is giving "PropertyAccessException" .It is quite a common exception for hibernate users .There is a post regarding this in the Hibernate Official Web site in common problem section , which is like the following :

Hibernate throws a PropertyAccessException or NullPointerException
when I load or query an object!
A PropertyAccessException often occurs when the object being passed to the setter method is of the wrong type. Check your type mappings for the offending property. (To see exactly which property was the problem, you might need to disable the CGLIB reflection optimizer.) However, the most common cause of this problem is that Hibernate attempted to assign null to a property of primitive type.
If your object has a primitive-type property mapped to a nullable database column then you will need to use a Hibernate custom type to assign a sensible default (primitive) value for the case of a
null column value. A better solution is usually to use a wrapper type for the Java property.

Analysis and solution:
Fair enough...,providing an Integer mapping instead of int (i.e. using wrapper class of Java) is off course a better solution (as well as common practice ) when the design is in your hand .
But
what if the POJOs are already written and you can not modify them? (Oh my God !!!)

Well, in that case my solution is using Hibernates formula attribute .It will be like the following:

property name="primitiveNullableDemoField" formula=”COALESCE(primitiveNullableDemoField, 0)


So the core idea is to replace the null field with default (not null ) initialization value.

Disclaimer:
Now there are two issues
1. COALESCE () function is DB2 specific solution; it will not work in Oracle.
2. Adding a function in the query adds to database overhead .I have heard about situations where DBAs do not allow to the developers to fire any DB functions considering the performance issue in critical scenarios (Too much, isn’t it?).
Regarding point 1, I do not know a common or generic function to replace null value that works for all Databases. Instead we can use case statements to bypass this.

Monday, June 28, 2010

You need to read an application server log file , but you do not know where it is !!!

Background:

One Friday at around 10.30 at night , a very close friend of mine called me on my mobile and told me that he is facing a problem.He is working on a maintenance project which runs on a server , of which the log file location is unknown.He does not have the documents or time to do RnD on searching the log files also.But he needs to have a look at the log immediately !! There is a severe bug he needs to fix within few hours , otherwise there will be some sort of escalations.

Initially I suggested him to read the server related docs (which I myself have read on rare occasions !! ) and do some Googling on this .In reply he told me that he has already done that and still he is stuck .He needs a quick simple working solution for the time being !!

Problem Description:

To get the log file of a server at a known directory , irrespective of its default location.

Solution (or patch ?? ):

What I suggested for a quick fix is to use the System.setErr() and System.setOut() method of java, to point the console output to a custom location.Both methods should have a PrintStream object as argument, which will point to a specific file provided by you .In that case all the prints or exceptions will be stored in that specific location.

Sample code:


package com.ayan.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;

/**
* This class is an utility class containing a method to change the console
* output location as per the choice of the user .
*
* To use this util file ,the User just needs to add one liner code like :
* ConsoleUtil.setSystemOutPutLocation(outFileLocation,isError);
*
*
* @author ayandutta
*
*/
public class ConsoleUtil
{
public static String NORMAL_OUTPUT_FILE_NAME = "SysoutFile.txt";
public static String ERROR_FILE_NAME = "SysError.txt";
public static String OUTPUT_DIRECTORY = "c:" ;

public static String NORMAL_OUTPUT_FILE_LOCATION = OUTPUT_DIRECTORY + File.separator + File.separator+NORMAL_OUTPUT_FILE_NAME;
public static String ERROR_FILE_LOCATION =OUTPUT_DIRECTORY + File.separator + File.separator+ERROR_FILE_NAME;

/**
* This method sets the console out put location file.
* It works for sysout or syserror
* @param outFileLocation
* @param isError
*/
public static void setSystemOutPutLocation(String outFileLocation,boolean isError)
{
try
{
FileOutputStream outFileStr = new FileOutputStream(outFileLocation, true);
PrintStream outFilePrintStream = new PrintStream(outFileStr);
if(isError)
{
System.setErr(outFilePrintStream);
//System.out.println(" Error file is changed to : " +outFileLocation);
}
else
{
System.setOut(outFilePrintStream);
//System.out.println(" Output file is changed to : " +outFileLocation);
}
}
catch (Exception e)
{
System.out.println("Exception inside method setSystemOutPutLocation(),printing stack trace...");
e.printStackTrace();
}
}

public static void main(String[] args)
{


//Normal Sysouts and Syserrors Before console location is changed
System.out.println("Text for normal out put before file configuration");
System.err.println("Text error out put before file configuration");

//Here is the code where Sysouts and Syserrors console location is changed
setSystemOutPutLocation(NORMAL_OUTPUT_FILE_LOCATION, false);
setSystemOutPutLocation(ERROR_FILE_LOCATION, true);

//Normal Sysouts and Syserrors after console location is changed
System.out.println("Text for normal out put after file configuration");
System.err.println("Text error out put after file configuration");


//By force generating an Exception
generateException();
}

/**
* Just a method to create an exception by force
*/
public static void generateException()
{
try
{
String nullStr = null;
System.out.println(nullStr.length());
}
catch (Exception e)
{
System.out.println("Exception inside method generateException(),printing stack trace...");
e.printStackTrace();
}
}

/**
* Just a method to create an exception by force
*/
public static void printExecutionLocation()
{
System.out.println(System.getProperty("user.dir"));
}

public static void printAllEnvironmentInfo()
{
System.out.println(System.getProperties());
}
}

How to use this program :

Now invoke setSystemOutPutLocation(NORMAL_OUTPUT_FILE_LOCATION, false) and setSystemOutPutLocation(ERROR_FILE_LOCATION, true) from any of the classes of your running application .The log file will be generated on the location provided.

Disclaimer:

The solution (I would rather mention it as a patch ) that I provided at that point of time is just for solving the log file related issue quickly .It is not at all a complete solution .