Saturday 6 October 2012

Ninite - Rebuilding your system

I have been using Ninite for the  last few months to install some essential utilities and have found it to be a useful service to set up browsers, media tools, anti-virus, messaging and document management tools. The selected tools are bundled into a single exe and are installed via the Ninite created installer. Thus, installing and rebuilding a system becomes simpler and faster.

Saturday 22 September 2012

Hibernate Annotation One-to-One Mapping Example

While the Hibernate documentation section 2.2.5 is a good place to start for a concrete example for an example of a one-to-one annotation based relationship mapping, the example below describes a couple of scenarios and the annotations that worked in setting up the relationships.

Give below is an example of  a One-to-One  bi-directional relationship between a User object and a Registration object,

In the User object, set up the annotations as follows :

@Entity
@Table(name = "user")
public class User implements Serializable {
........
........
private UserRegistration userRegistration;
.........
  @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
  @JoinColumn(name = "USR_REG_SYS_ID")
   public UserRegistration getUserRegistration() {
        return this.userRegistration;
    }
.........
} // End of User class

The JoinColumn annotation refers to the to the USER_REGN_ID column in the User table that has a Foreign Key reference to the User_Registration table.

In the UserRegistration object, set up the annotation as follows:

@Entity
@Table(name = " userRegistration")
public class UserRegistration implements Serializable {

........
........
private User user;

........
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "userRegistration")
 public  User  getUser() {
        return this.user;
 }

The mappedBy annotation implies that the userRegistration attribute in the User object manages the relationship reference between the objects. In other words, the User table holds the (Foreign) key reference to the relationship.


Wednesday 19 September 2012

Selecting duplicates in a database table

Selecting duplicates in a database table can be accomplished in various ways and all these approaches involve various trade-offs with respect to efficiency and speed but before we start examining some of these approaches we need a table with duplicates so here is one. The Primary Key in this table is the ID and the Emp_Id is meant to be Unique and will be used for filtering duplicates.


Listing the number of times each record occurs with duplicate rows listed once.

(1) select *, count(*) from employee group by Emp_Id;



Listing the number of times each record occurs with a listing of the first row in the group.

(2) select *, count(*) from employee group by Emp_Id having count(*) > 1;



To list all duplicate rows, the following query would work;


(3) select   a.* from
    employee as a
inner join
    (select Emp_Id
     from employee
     group by Emp_Id
     having count(*) = 2) as b
  on a.Emp_Id = b.Emp_Id;

Now our table has a Tele_No column and some of these numbers are incorrect

Selecting the duplicates using a self-join.

In this case, we need to select all records that have the same Emp_Id but a different Tele_No. We can use a simple self-join with the Tele_No and the column as the distinguishing key to separate our records.
Option 1

(4) select e1.* from employee e1, employee e2 where e1.Emp_Id = e2.Emp_Id and e1.Tele_No != e2.Tele_No order by e1.Id asc;




The exact same result could also be obtained with a sub-query as follows:
Option 2

(5) select e1.* from
employee e1 where e1.Emp_Id IN (
select e2.Emp_Id from employee e2
where e1.Emp_Id = e2.Emp_Id
and e1.Tele_No != e2.Tele_No
order by e1.Id asc;

In Option 1 & 2, we have the condition e1.Tele_No != e2.Tele_No that prevents us from getting a cartesian product of the records. If such a condition is not available then just use the Primary Key column as follows:


(6) select e1.* from
employee e1 where e1.Emp_Id IN (
select e2.Emp_Id from employee e2
where e1.Emp_Id = e2.Emp_Id
and e1.ID <> e2.ID
)
order by e1.Id asc;


If you are wondering whether a join or a sub-query would work best, it would ofcourse depend upon your query optimizer and the size of your tables but generally Joins are the way to go.

So, we have 6 records where the Emp_Id are the same but the Tele_Nos are different. To view these records in a tabular format we could modify (6) as follows.

(7) select e1.ID, e1.Tele_No,e2.ID, e2.Tele_no
from
employee e1, employee e2
         where e1.Emp_Id = e2.Emp_Id
               and e1.Tele_No != e2.Tele_No
               and e1.Id < e2.id
        order by e1.Id asc;



The change in the condition comparing the Primary Keys in the query above e1.Id < e2.id prevents a cartesian product.

Sunday 16 September 2012

Setting up an Image Gallery in Joomla

Most Joomla based sites end up requiring an image gallery of some sort and while there are a variety of  options available, you are liable to run into trouble with Javascript conflicts between the Javascript version or library of your Image Gallery plug-in with the other Javascript libraries loaded by the other plug-ins or the template. One option is ofcourse to hack the php template and debugging the Javascript conflicts using a browser based debugger which could be a tricky as well as tedious exercise.
The other and more staright forward approach is to use a non-Javascript based image gallery plug-in. One such plug-in is the css-gallery plugin. The plug-in is easy to install and has a simple configurable interface which is well documented. It is also  possible to add captions and link to the gallery images. It took 5 minutes to set up the image gallery for the St John English School website. An example of the css-image gallery in action can be viewed here

Sunday 26 August 2012

Setting up HIbernate Search on a Maven build

Hibernate Search is an interesting companion of the Hibernate core components. Since most applications with a public facing view invariably require, a search capability, Hibernate Search presents itself as an east to configure and implement, search tool that fits in well with the Hibernate ORM framework, Under the covers, Hibernate Search uses the Apache Lucene search engine and it is possible to write search queries directly targeting Lucene.Since Hibernate Search is annotation driven, it is preferable to have an annotation based implementation of Hibernate Core of your data model.

The following dependency definitions in your pom.xml will add the required Hibernate Search jars to your project.


org.hibernate
hibernate-search 
3.4.2.Final 

Add the following property settings to your Hibernate.cfg.xml file.



org.hibernate.search.store.FSDirectoryProvider


lucene

These setting will direct Hibernate Search to create the Index base and store it on the File System in a directory named Lucene.

This is all you need to add Hibernate Search to your current project setup. Now all you need is to annotate the fields in your domain model that need to be searchable,

The key annotations are as follows:

@Indexed
@Table(name = "User")
public class User implements Serializable
{

@Field(index=Index.TOKENIZED, store=Store.NO)
private String userName;
}

The @Indexed annotation tells Search to Index this particular table.
The @Field(index=Index.TOKENIZED, store=Store.NO) annotations on the attribute definition tells Search to tokenize the field using the default analyzer. The store annotation tells Hibernate not to store the data with the index and this is the default behaviour. Storing data within the index is required is using a Projection.

You can read more about setting up Hibernate Search at Getting Started

Sunday 19 August 2012

Managing PermGen and Heap allocations in Tomcat7

If you are developing a web-application that runs on Tomcat 7, it is highly likely that you will encounter a PermGen space error on Tomcat which happens when you deploy and undeploy the application several times and there is a memory leak. There could be several reasons for a memory leak and they could stem from loading third-party jars such as JDBC drivers and logger jars from within the web-application lib. To overcome some of these potential issues, it is best to load them directly into Tomcat lib and let Tomcat the Driver Manager take care of de-registering them when not in use.

While the best approach to dealing with this problems and other memory related problems is debugging the cause of the memory leak and fixing it, there are times when tuning the JVM parameters can alleviate the cause and improve performance of the application to a certain extent.

Before diving into the tuning the JVM parameters, it is important to understand how JVM organises its memory space and what is the difference between heap and permgen space and how they are used. This article gives a very good overview of the JVM memory layout.  To summarise the difference between the two, the permgen space is used to store classes loaded by the class loader, primitives,any static classes and other JVM related data. On the other hand, the heap space is mainly used to store objects created by the application. It is further divided as explained here into Eden, Survivor and Tenured Gen space.

It is also important to note that since Java 1.6 update 16, there have been some major changes to the process used to compute heap size in 32 bit and 64 bit JVMs and in client / server mode.

Having understood the basics of JVM memory organisation, we can look at tuning Tomcat's memory allocation for its heap and permgen. While there are several parameters that can be used for tuning the JVM, this post is just going to describe the minimum and maximum parameters for specifying the heap and the permgen space. The location of where to specify these parameters and their values depends on your machine specification (operating system, memory size, 32 bit or 64 bit) and how you invoke Tomcat.

Deciding on the actual values of the parameters should be taken after monitoring the JVM using some tools such as jVisualVM. The values given below are sample values that worked in my particular case.

Case 1: Tomcat is invoked from within Eclipse on a 32 bit machine.
Specs : Tomcat 7.0.28, Eclipse Juno, Sun Java 1.6.30, Windows 7.0

The parameters should be specified in Server Launch Configuration -> Open Launch Configuration -> Edit Launch Configuration Properties ->Arguments
Tomcat Launch Configuration in Eclipse
The detailed steps are available here.

Case 2: Tomcat is invoked via its start up script on a 32 bit virtual machine.
Specs : Tomcat 7.0.28, Sun Java 1.6.30, Ubuntu 12.04

Create a setenv.sh file and add the following one line in it.

export JAVA_OPTS="-Dfile.encoding=UTF-8 -server -Xms128m -Xmx256m -XX:MaxPermSize=128m -XX:+DisableExplicitGC"

Transfer this file to your Tomcat/bin directory and re-start Tomcat.

There are a number of ways of verifying that the JVM parameters were applied to Tomcat. The easiest way on Ubuntu is : ps -ef | grep tomcat

You should see something similar to the line below:

john     17841     1  5 15:23 pts/0    00:00:09 /usr/bin/java -


Djava.util.logging.config.file=/usr/local/apache-tomcat/conf/logging.properties -Xms128m -Xmx256m -XX:MaxPermSize=128m -XX:+DisableExplicitGC -
Djava.endorsed.dirs=/usr/local/apache-tomcat/endorsed -classpath /usr/local/apache-tomcat/bin/bootstrap.jar:/usr/local/apache-tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/apache-tomcat -Dcatalina.home=/usr/local/apache-tomcat -Djava.io.tmpdir=/usr/local/apache-tomcat/temp org.apache.catalina.startup.Bootstrap start



With respect to parameter values for the heap, it is important to note that :

(Managed Heap + native heap + (thread stack size * number of threads)) cannot exceed 2GB on 32bit x86 Windows or Linux systems (ref)


2GB is a theoretical limit. Since the heap requires contiguous memory space, it might be hard to push the heap beyond 1.4GB on a 32 bit Windows machine.

A final comment.
The JVM is an intelligent software and has several inbuilt processes built into it that work to optimize its performance. Attempting to tune the JVM may not always result in a performance improvement. Tuning the JVM is dependent on the machine specification, the design decisions taken behind the architecture of the web-application it is hosting, the number of applications hosted by the JVM and when all else fails, Trial and Error. 

Sunday 29 July 2012

Upgrade Hibernate from 3.1.3 to 3.6.10.Final using Maven

At the time of writing Hibernate is well into its 4.X releases with 5.X in sight so if you are looking to migrate from 3.x to 4.x read this or If you need a specific migration guide look here.

In my case I needed to migrate our application from 3.1.3 to 3.6.10.for which I have detailed the steps below.


  • Make sure that you have the necessary repositories to download the required Jars. You might need.


       mvn-public
       MVNRepository
       http://mvnrepository.com


       repository.jboss.org-public
       JBoss.org Maven repository
       https://repository.jboss.org/nexus/content/groups/public
  


  • Add the following dependencies into your pom.xml
   
  
   org.hibernate
   hibernate-core
   3.6.10.Final
  
  
  
   javassist
   javassist
   3.12.1.GA
  

Since Hibernate 3.4.X onwards, Hibernate switched to SL4J, you'll need:


   org.slf4j
   slf4j-log4j12
   1.5.6
  
  
   log4j
   log4j
   1.2.16
  

Now, configure your connection poolDownload the c3p0-0.9.1.2.jar from here and load it into your Tomcat/lib along with your MySQL driver jar.


Add the following configuration settings in your Hibernate (.properties or .cfg.) file.

5
20
300
50
3000

That's it !!
You WAR/lib should have the following jars present post-migration.
  • commons-collections-3.1.jar
  • hibernate-commons-annotations-3.2.0.Final.jar
  • hibernate-core-3.6.10.Final.jar
  • hibernate-jpa-2.0-api-1.0.1.Final.jar

  • javassist-3.12.1.GA.jar
  • jta-1.1.jar
  • slf4j-api-1.5.6.jar
  • slf4j-log4j12-1.5.6.jar


Tuesday 24 July 2012

Monitor your Tomcat Server with Psi-Probe

Psi-Probe is an excellent Tomcat and JBOSS server monitoring tool. Setting it up is as easy as uploading the application, which comes packaged as a WAR file. Before setting it up, create a user with the role probeuser, in your conf/tomcat-users.xml.
Psi-Probe allows you to manage and monitor the server's health (memory usage, cpu usage, OS and server information) as described in the screenshot below.
PSI-Probe Memory Monitoring Console

Psi-Probe also allows you to restart the server JVM and kill threads. Another interesting feature of Psi-Probe is that you can see which jsps are not being used by the application. These jsps get tagged with the status 'outdated' while the jsps which are being used by the application  have a status of 'compiled'. Further, you can also manage requests based on session IDs and monitor the average response time.

Saturday 30 June 2012

Eclipse Juno has arrived.

Continuing with the tradition of yearly June releases, Eclipse rolled out its latest offering - Juno. While as always there are a significant number of bug fixes and features, Juno mainly targets the Eclipse Development community by base lining the current version as the mainstream target platform for RCP development.
Another interesting offering is the Code Recommenders that promises to made coding 'smarter' by offering "by means of intelligent code completion, extended javadocs, smart bug detectors, stacktrace search engines and others...". While it would be unfair to expect an immediate performance boost from the Code Recommender feature, it can be regarded as a major stepping stone towards integrating coding standards and quality benchmarks within the IDE.

Saturday 23 June 2012

Bracketeer : Bracket your Java Code in Eclipse

As an application developer, I regularly have to  understand and re-factor code that has been authored by other developers. It could be code coming from an open-source library or code from within the existing application. If it is Java code, it is sure to have one thing - Brackets and Braces. For simplicity, I am going to use the term 'Brackets' interchangeably for the rest of the article.
Since well commented and well-formatted code makes understanding the logic of the method or block easier, working out the start and end brackets within a block can take up a fair amount of time, especially  if the code has a high degree of cyclomatic  complexity. This scenario is when Bracketeer, which is currently featured as one of the top-most popular new Eclipse plugins is useful. While the plug-in is just six months old (as on June 2012) and is still in a beta-stage, I installed it in Eclipse Indigo SR1 and gave it a run.
While Bracketeer's ability to match brackets and color them is not exactly a new feature for Eclipse users, it's ability to introduce comments at the closing end of brackets is something that I found useful.
For example, the screenshot below show's an Eclipse editor without Bracketeer activated and the screenshot following it shows the comments that come up when Bracketeer's commenting feature is activated.
Bracketeer - Inactivated. No comments are visible
The screenshot below displays the result after activating, Bracketeer's commenting feature. It generates a comment that appears at the end of each closing bracket. This comment is similar to a pop-up and does not get added to the source file. Thus, resolving the start and end of braces becomes considerably simpler. {}
Source code with the Commenting Feature Enabled.

Finally, Bracketeer is customizable and can be configured to cater to your coloring and bracket matching requirements.Bracketeer can be installed as an Eclipse plug-in from here.
Customizable Interface for Bracketeer

Monday 11 June 2012

ZK: Simplifying AJAX

I have been using ZK for the last few months and have found it to be a very scalable and impressive framework in terms of its features and the ease with which they can be implemented within an enterprise application. ZK  is described as the No 1 Ajax framework on SourceForge based on the number of downloads and it is easy to see why it might be so popular. It integrates well with Spring, Hibernate, JQuery , JSON and other frameworks that make up the web application stack. Dreamworks has put up a concise and very focussed presentation that describes the features and the functionality of ZK.
One major advantage in using ZK is that the developer need not worry about writing Javascript to realise Ajax, instead all coding is carried out in Java. ZK also provides a very rich array of  UI components that can be implicitly attached to the business logic using a feature called 'data binding'. Thus, the need to explicitly attach UI components to backing beans in order to retrieve and send data is not required.
Finally, ZK provides a robust security architecture which can be further extended via third party libraries such as Spring Security. The MVC pattern that forms the core of the ZK implementation pattern, makes designing components intuitive. Further, components can be customised using a rich set of custom-renderers that cater for  extensibility and reuse.
In conclusion, the ZK framework is easy to learn and implement. It provides a very efficient and scalable framework for developing Ajax based enterprise level applications featuring a rich UI set.

Wednesday 21 March 2012

JDom : The Java based solution for XML manipulation

JDom is a Java based open-source XML manipulation tool that is easy to install and use. Whereas technologies like JaXB are focussed on the marshalling and unmarshalling of XML using Java beans, JDom is more of a XML manipulation tool that is an alternative to DOM and SAX even though as the FAQ says, it integrates well with both these technologies.The best way to understand how well JDOM fits into your requirements is to read the very detailed FAQ. JDom version 1.1.13 is the latest version which is now available from maven-central.

  org.jdom
  jdom
  1.1.3
  
  
   
    maven-plugins
    maven-cobertura-plugin
   
   
    maven-plugins
    maven-findbugs-plugin
   
  
 
xPath support in JDom via Jaxen makes accessing and manipulating XML document trees a breeze. Thus, if you are looking for a low cost solution for manipulating XML documents from a pre-existing template or querying an XML document for extracting xml attribute values, give JDOM a run.

Thursday 19 January 2012

Setting up a database based authentication realm in Tomcat 7.0.0

Setting up an authentication module is one of the primary tasks that come across a web-developer's canvas when coding a web-application.In this post, I'll detail the basic steps needed to set up a database backed authentication realm and outline the configuration files that need to be updated when using a Tomcat 7.0.0 servlet container. This post is NOT about security or securing the web-app context so please if you do implement these steps, please do some more research on what is required to secure your web-application.It is also important to note that this post pertains to Tomcat version 7.0.0 and some steps may not be required in more recent versions.
Tomcat 7.0.0 implements the Servlet 3.0 specification (JSR 315) which provide login / logout methods to be invoked on the HttpServletRequest. 
Step 1: Create the user and user-roles table in your database using the sql scripts described here. Insert a couple of records in each table.
Step 2: Set up Tomcat to connect to the database realm
(a) In the context.xml(located in server/conf directory), set up the datasource  
    
(b) In server.xml (located in server/conf directory) add the following snippets in the correct location within the xml file. This will set up the connection between the datasource and the authentication mechanism :


.......

..........








Step 3: Download the jdbc driver based on your database & the tomcat-jdbc jar to set up the JDBC Connection Pool (You do not need to download the tomcat-jdbc jar if you are using Tomcat version 7.0.19 and above).


Step 4: Write the logic for passing the login & password from the user form to the backing bean.
 public String login() {  
          FacesContext context = FacesContext.getCurrentInstance();  
          HttpServletRequest request = (HttpServletRequest) context  
                                             .getExternalContext().getRequest();  
             
          try 
          {  
               request.login(username, password);  

          } catch (ServletException e) {  
            e.printStackTrace();
               context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Login   failed!", null));  
               return "failed-login";  
          }  
             
 //You can fetch user from database for authenticated principal and do some action  
          Principal principal = request.getUserPrincipal();  
          log.info("Authenticated user: " + principal.getName());  
             
             
          if(request.isUserInRole("administrator")) {  
            setLoginSectionVisible(false);
            setUploadSectionVisible(true);
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Login successful!", null));  
               return "admin-login";  
          } else {  
               return "user-login";  
          }  
     }