Monday, April 28, 2008

HTTP Debugger

HTTP Debug proxying tools generally allows us to log HTTP traffic between the computer and the Internet. These logs can be helpful to identify performance issues while loading screens. There are lots of commercial tools and open source tools available in the market.

I have used the following products:
Commercial Product:
Charles Web debugging proxy is really good and it automatically takes up the settings from the browser and proxies the request.
URL: http://www.xk72.com/charles/

Open source Tool:
Fiddler
URL: http://www.fiddlertool.com/fiddler/

Friday, April 18, 2008

Apache + SSL

1.0 Download Apache SSL and Open SSL

Download Apache With SSL Support and OpenSSL

Extra both zip files into two different location, and replace all *.exe, *.dll, *.so files from Apache with OpenSSL support to your Apache local installation.

Download ssl.conf into APACHE_HOME/conf

Download openssl.cnf into your OpenSSL directory Also copy the files ssleay32.dll and libeay32.dll from the OpenSSL distribution directory to WINDOWS\System32 directory !!!

2.0 Create Certificate

Enter the following command
openssl req -config openssl.cnf -new -out my-server.csr
This creates a certificate signing request and a private key.

openssl rsa -in privkey.pem -out my-server.key
This removes the passphrase from the private key.

openssl x509 -in my-server.csr -out my-server.cert -req -signkey my-server.key
This creates a self-signed certificate.

Create an APACHE_HOME/conf/ssl directory and move my-server.key and my-server.cert into it.

3.0 Configure mod_ssl and Apache

Edit APACHE_HOME/conf/httpd.conf add the following line

LoadModule ssl_module modules/mod_ssl.so

# SSL
SSLMutex default
SSLRandomSeed startup builtin
SSLSessionCache none


SSLEngine On
SSLCertificateFile conf/ssl/my-server.cert
SSLCertificateKeyFile conf/ssl/my-server.key


You also need to modify APACHE_HOME/conf/ssl.conf to update DocumentRoot and ServerName

Restart your Apache server and you could test it on https://localhost/

For reference please go tohttp://www.thompsonbd.com/tutorials/apachessl.php and http://tud.at/programm/apache-ssl-win32-howto.php3

Tuesday, April 15, 2008

How Spring can Leverage any ORM

I was previously working with Spring with Hibernate (2.3) Combination. It was really painful because hibernate was throwing all the exceptions as checked exceptions and we were forced to catch all the exceptions in the Service Layer. But now a days it is very simple as we can catch the exceptions as Throwable in Service Layer and can use an Interceptor to log those message and rethrow as application specific exceptions. I came across a good article about the Spring leveraging any ORM and thought of adding its content into my blog with the reference to the source site.

In a typical Spring architecture, DAOs are effectively Strategy Pattern objects, isolating service layer objects from the technology specifics of obtaining and saving persistent objects.

Spring supports a range of ORM tools and persistence frameworks, including (but not limited to) TopLink, Hibernate, JDO (Java Data Objects), iBATIS SQL Maps, and Apache OJB (ObjectRelational Bridge).

Spring provides a consistent approach to working with all these tools, but doesn't attempt to completely abstract the ORM tool, because doing so would be counterproductive. Thus, for example, implementations of DAO interfaces will use the native query API of the ORM tool: Spring does not attempt to create a query abstraction, which would be less powerful than that of leading ORM products.

How Spring can Leverage any ORM ?

For DAO interfaces to be independent of the underlying persistence technology, we need to solve some crucial issues. Spring provides important help here, especially in the areas of exception handling; resource acquisition and release; and transaction management. Spring exception handling: Two key features of Spring's exception handling enable it to remain independent of the underlying persistence technology. First, it uses a class-based hierarchy of generic data access exceptions (Figure shows the most important classes in the hierarchy) that can leverage any underlying ORM's exception mechanisms. The exception hierarchy can easily be extended. Thus, for example, you can write service layer code to handle DataIntegrityViolationException (violation of a constraint such as a primary key constraint), without concern as to the data access tool in use, which may be JDBC, TopLink, JDO, Hibernate or another supported framework. This hierarchy is even extensible without modifying Spring code, if you wish to add mappings to error conditions unique to your database or persistence tool.




Secondly, Spring's exception handling approach uses unchecked exceptions. Using unchecked exceptions works particularly well in conjunction with an exception hierarchy, because it is only necessary to catch a particular subclass that may be recoverable. Catching subclasses of checked exceptions is less useful, since the base class still needs to be caught. (Note that JDBC was always something of an odd API out, in terms of using checked exceptions: TopLink and JDO, for example, both use unchecked exceptions. In version 3, Hibernate will switch from checked to unchecked exceptions.)

Resource acquisition and release: One of the major challenges of working with any data access technology is that resources such as JDBC connections or ORM sessions must be acquired and released correctly even in the event of errors.

Spring solves the problem of resource acquisition and release by using a callback approach. The framework is responsible for acquiring and releasing resources, while the application developer implements code in a callback method to perform the necessary persistence operation with the desired resource. Thus application developers focus on the unique needs of their application, rather than writing plumbing code.

Helper classes that provide this functionality are termed templates, and Spring provides a consistent approach across many APIs where such resource acquisition and release is a problem, such as JDBC, JNDI and JMS.

Transaction management and thread binding: Spring's resource acquisition and release strategy is integrated with its transaction management support. It's important to get the same resource for every operation in the same transaction. While JTA and JCA may provide support for this in a full-blown J2EE environment (depending on the persistence tool), Spring provides support for this in any environment, including J2SE environments. The necessary Spring configuration is also simpler than JCA configuration.

Spring does this by binding resources such as JDBC connections or TopLink or Hibernate Sessions to the current thread. Many application developers have implemented such support in their own applications. However, the Spring solution not only has the merit of being far more sophisticated than typical in-house solutions (with support for transaction suspension and many other complex-to-implement value adds), but eliminates the need to write and maintain such custom code. Again, generic concerns are best given a generic solution.

Source: http://www.oracle.com/technology/pub/articles/masterj2ee/j2ee_wk9.html


Asynchronous Messaging with Spring JMS


http://www.onjava.com/pub/a/onjava/2006/02/22/asynchronous-messaging-with-spring-jms.html?page=1

Wednesday, April 9, 2008

Improve Your Java Code Quality

What is code quality exactly? You may not know how to define it, but you know it when you see it. One thing is sure, though, high code quality usually correlates to fewer defects. Ensuring the quality of your Java code is a two-step process: write tests at all levels, early and often; and continually monitor quality metrics. Andrew Glover brings his considerable expertise as a consultant focused on improving code quality to this moderated discussion forum.

His associated article series In pursuit of code quality will offer best practices for ensuring your code is the best it can be.

Code Quality Discussion Forum

http://www.ibm.com/developerworks/forums/dw_forum.jsp?forum=812&cat=10



Encoding JSP Pages globally with specific encoding (Servlet 2.4 onwards)

if you are using servlet 2.4 and jsp 2.0
you can use the following special config in web.xml:

<jsp-config>
<jsp-property-group>
<display-name>Setup default encoding</display-name>
<url-pattern>*.jsp</url-pattern>
<page-encoding>ISO8859-1</page-encoding>
</jsp-property-group>
</jsp-config>

Reference article:
http://www.oracle.com/technology/sample_code/tech/java/codesnippet/jsps/jspconfig/jspconfig.html#encode

Tuesday, April 8, 2008

Exception handling and Logging Strategies for J2EE projects

Asyncronous Logging Framework
http://www.ibm.com/developerworks/websphere/library/techarticles/0207_barcia/barcia.html

Webmethods Exception handling
http://www.wmusers.com/ezine/2002dec_iandrosov_1.shtml


Implement Logging as an Aspect Using Spring's AOP Framework

http://www.devx.com/Java/Article/30799/1954

Exception management and error tracking in J2EE
http://www.javaworld.com/javaworld/jw-07-2005/jw-0711-exception.html?page=1

Best Practises in EJB exception handling
http://www.ibm.com/developerworks/java/library/j-ejbexcept.html

Exception Handling Framework for J2EE
http://www.onjava.com/pub/a/onjava/2006/01/11/exception-handling-framework-for-j2ee.html?page=1

Exception Handling & Logging Presentation
http://www.softwaresummit.com/2004/speakers/SteltingExceptionHandling.pdf

Logging in JSPs Tag Library
http://jakarta.apache.org/taglibs/doc/log-doc/intro.html

Java tutorial: exceptions
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/exception.html
http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html

Exception handling
http://www.objectsource.com/j2eechapters/Ch18-Exception_Handling.htm

Designing with Exceptions
http://www.artima.com/designtechniques/desexceptP.html

Best Practices for Exception Handling
http://www.onjava.com/pub/a/onjava/2003/11/19/exceptions.html

Three Rules for Effective Exception Handling
http://today.java.net/pub/a/today/2003/12/04/exceptions.html

Beware the dangers of generic Exceptions
http://www.javaworld.com/javaworld/jw-10-2003/jw-1003-generics.html

Exception Handling in Web Applications
http://weblogs.java.net/pub/wlg/987

Java Theory and Practice: The exceptions debate
http://www-106.ibm.com/developerworks/java/library/j-jtp05254.html

Best practices in EJB exception handling
http://www-106.ibm.com/developerworks/java/library/j-ejbexcept.html

Exception handling with JAX-RPC
http://www-106.ibm.com/developerworks/xml/library/ws-tip-jaxrpc.html

JavaOne2002 - Handling Exceptions in Distributed Systems
http://servlet.java.sun.com/javaone/sf2002/conf/sessions/display-1563.en.jsp

book: Java Platform Exception Handling
http://www.amazon.com/exec/obidos/ASIN/0131008528

Changing Log4j LogLevels Dynamically


http://blogs.averconsulting.com/2006/12/13/changing-log4j-logging-levels-dynamically.aspx