Tuesday, 21 May 2013

JExcel vs Apache POI : Reading and Writing Excel Files

JExcel is a relatively simple and robust Java based library for manipulating Excel (.xls) files. While it has relatively less functionality than its bigger and more popular brother, Apache POI, it offers a simple API for Excel manipulation.Some features that made me choose JExcel were its relatively smaller memory foot print, simple API and upto date documentation.
On the other hand, some drawbacks that made me wary of choosing the tool were its lack of support for .xlsx files and the fact that the last release was in October 2009, more than 3 years ago. This discussion has listed a significant number of differences between JExcel and POI and while it may appear that Apache POI would be the obvious winner, it all boils down to your requirements. After all you can travel from point A to B in a car or do the same trip in a truck. In situations where the requirements are not very clear, are extensive  or might require support for .xlsx files in the future, Apache POI might be a better choice but if your requirements are clear, you need a fast and efficient Excel generating tool with minimum fuss, choose JExcel.
  
  
   net.sourceforge.jexcelapi
   jxl
   2.6.12
  

  

Thursday, 9 May 2013

Java 7 : I/O using the NIO package

The NIO package in Java 7 is an important and much needed update to the Java I/O mechanism. With regards to I/O, the java.nio.file and the java.nio.file.attribute packages are key. An important change to the previous mechanism is the introduction of the Path object which is analogous to the File object. Other changes are documented here.

A simple example that reads in a list of entries from a text file and appends a special character at the end of every line is given below:

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FileReaderAndSpecialCharacterInserter {
             
     private static String SPECIAL_CHARACTER = ",";

     /**
       * @param args
      */
     public static void main(String[] args) {
     FileReaderAndSpecialCharacterInserter fileReader = new FileReaderAndSpecialCharacterInserter();
                                fileReader.readFile();

      }
                private void readFile()
                {
                                StringBuffer outputString = new StringBuffer();
                             
                                Path path = Paths.get("c:/test.txt");
                                Charset charset = Charset.forName("US-ASCII");
                                try (BufferedReader reader = Files.newBufferedReader(path, charset))
                                {
                                    String line = null;
                                    while ((line = reader.readLine()) != null)
                                    {
                                        System.out.println(line);
                                        outputString.append(line);
                                        outputString.append(SPECIAL_CHARACTER);
                                    }
                                }
                                catch (IOException x)
                                {
                                    System.err.format("IOException: %s%n", x);
                                }
                             
                                System.out.print("The final output string is : -> " + outputString.toString());
                }
}

---------------
As evident in the example above, the Path object sets up a channel to the file to be read and essays the role played by the File object in the I/O mechanism available upto Java 6. The advantages of this new File I/O mechanism are mainly scalability and error handling  and are documented in detail here.

Thursday, 25 April 2013

Setting up Solr 4.0 on Tomcat 7.0.28

Generally every user centric application requires a Search capability. With Google easily leading the Search domain, user expectations are generally high when they come to a web-application and perform a search. If giving user's the Google experience is not possible, consider Apache's Solr  which is a popular, open-source Java based search tool that runs Lucene under hood.
While Solr comes packaged with Jetty and has a basic tutorial, that provides pointers on its different features, setting up the tool to run within an existing Apache Tomcat instance can require a bit of effort as there are several files and folders and it is not immediately clear which files control the configuration of the tool. This post will document the various steps involved in setting up Solr to run with Apache Tomcat 7.0.28 on a Windows server.

Step 1 : Download a copy of Solr and unzip it  to apache-solr-4.0.0.

Step 2: Create new directory which will be the home of Solr c:\solr_home and copy the following files and folders into it.  ( You will find  them within  the unzipped apache-solr-4.0.0 folder).
bin
collection1
solr.xml
zoo.cfg

collection1 is an existing example which we will load up into our Tomcat.Within the directory collection1, you'll find two other folders conf & data. Just verify that these are present.

Now we'll connect Solr to Tomcat.

Step 3:  Copy the apache-solr-4.0.0.war (which you will find in apache-solr-4.0.0\dist)  into your Tomcat web-apps directory.

Step 4:  Finally, the last step. Tell Solr where the existing search configuration files for collection1 reside.
To do this add the location of the Solr Home directory to your Tomcat JAVA_OPTS. I am setting this value in the setenv.bat file and the declaration looks like this :

set JAVA_OPTS=%JAVA_OPTS% -Dfile.encoding=UTF-8 -server -Xms1536m -Xmx1536m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m -XX:MaxPermSize=256m -XX:+DisableExplicitGC -Dsolr.solr.home=C:/solr_home

Step 5:  Start Tomcat and browse to your loaded Solr instance at http://localhost:8080/apache-solr-4.0.0/#/ You should see the Solr Admin screen as below :
Solr Admin Console for Collection 1
To check if Solr has been configured and set up correctly, select collection1 in the left hand side bar and select the query option. Enter 'solr' in the textbox labelled 'q' as shown in the screenshot below.

Default Search Interface
On submitting the query, one result is returned by Solr. On clicking on the result link, the record is displayed as an XML.
Search Result for 'Solr'


In the next post, I'll document the steps of integrating Solr with an existing web-application which is running on the same Tomcat instance with a MySQL back-end.

Tuesday, 2 April 2013

Creating an executable jar with a Maven plugin

While there are several non Maven based ways of creating an executable Jar that includes all required dependencies, Maven as a build tool offers three plugins for directly achieving this result. The Maven Assembly plugin is the first of these options. Creating an executable jar is just one of the objectives of the Assembly plugin. It can be used to create distributions in various file formats such as the zip format.
To configure the Maven Assembly plugin to create an executable jar with all required dependencies included, add the following configuration definition to your pom.xml in the build section.



   ..........
   
   maven-assembly-plugin
   2.4
   
     false
         standalone-${artifactId}-${version}
     
       jar-with-dependencies
     
     
        
           com.data.john.MainClass
           true
        
     
   
     
        
           make-assembly 
           package
           
              single
           
        
    
 
.............
.............


The second option is the Maven Shade plugin. The primary objective of this plugin is to create an executable jar and so it is much more suited for this use case. The configuration setup is as follows:



.................   

    org.apache.maven.plugins
    maven-shade-plugin
    2.0
    
        
             package
            
                 shade
            
            
            standalone-${artifactId}-${version}
            
              
              *:*
              
               META-INF/*.SF
               META-INF/*.DSA
               META-INF/*.RSA
              
             
            
            
                 
                    com.data.masker.controller.MaskerApp
                
            
           
        
    

..............




While tossing up between the Assembly and the Shade plugin, It should be mentioned that with using Maven Assembly, you run the risk of overwriting files with similar namespaces. These scenarios can be better handled using the Shade plugin, that provides more granular control over the build process by merging files with the same name instead of overwriting them.
Finally, this discussion would not be complete without mentioning the Maven Jar plugin, yet another way of  creating an executable jar with some use case examples given here.
All three options can be invoked using the mvn package call.