Tuesday, 24 April 2012

Create Flex Project with Maven

Lately i have been compiling and managing my Flex projects with Flash Builder but it didn't sound like something cool. So decided to go for some build tool to help me build my projects from anywhere without any dependency on Flash Builder. Ant works fine on this but is too old to give a shot. So maven is a natural candidate as i have also been using maven to build my Java Projects.

Here is the maven command to create a Flex Project.
mvn archetype:generate 
-DarchetypeRepository=http://repository.sonatype.org/content/groups/public 
-DarchetypeGroupId=org.sonatype.flexmojos 
-DarchetypeArtifactId=flexmojos-archetypes-library 
-DarchetypeVersion=3.6.1  
-DgroupId=com.bharat.test.projectName 
-DartifactId=my-flex-application 
-DinteractiveMode=false

Change the archetypeVersion if a newer version is available. Check following maven repo and use the latest stable version
https://repository.sonatype.org/content/groups/public/org/sonatype/flexmojos/flexmojos-archetypes/

Build using following maven command. (skip tests if you are not ready with them.)
mvn clean install -Dmaven.test.skip=true

For running unit tests read more here.

https://docs.sonatype.org/display/FLEXMOJOS/Running+unit+tests


Reference
http://www.sonatype.com/books/mvnref-book/reference/flex-dev-sect-creating-with-archetype.html#ex-flex-dev-app-pom

Friday, 20 April 2012

Reading and writing propertis file

Properties file is a simple text file with extension .properties generally used to store projects configurations like database connection details and text labels in case project is a web application.

Here is a sample to read data from a properties file and then write it back to an xml file.

I have a properties file named test.properties with one entry as below.
// test.properties
msg1=hello bharat, this is comming from properties file.

package com.bharat.io;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStream;
import java.util.Properties;


public class ReadWritePropertiesFile
{

  public static void main(String... args) throws Exception
  {
    Properties props = new Properties();
    props.load(new FileReader("test.properties"));
    System.out.println(props.get("msg1"));
    
    props.put("msg2", "This was written to properties file from java");
    
    
    OutputStream os = new FileOutputStream(new File("test.xml"));
    props.storeToXML(os, "my test comment");
  }
} 
 
This is the xml file where properties get written to.
// test.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <comment>my test comment</comment>
    <entry key="msg2">This was written to properties file from java</entry>
    <entry key="msg1">hello bharat, this is comming from properties file.</entry>
</properties>

I prefer storing ddl sql statements in a xml properties file as queries are formatted and readable.

Thursday, 19 April 2012

Tomcat Startup Error: Error in dependencyCheck

SEVERE: Error in dependencyCheck
java.util.zip.ZipException: too many length or distance symbols
    at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:147)
    at java.util.zip.ZipInputStream.read(ZipInputStream.java:146)
    at java.util.jar.JarInputStream.read(JarInputStream.java:177)
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:256)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
    at java.util.jar.JarInputStream.getBytes(JarInputStream.java:88)
    at java.util.jar.JarInputStream.<init>(JarInputStream.java:65)
    at java.util.jar.JarInputStream.<init>(JarInputStream.java:43)
    at org.apache.catalina.util.ExtensionValidator.getManifest(ExtensionValidator.java:378)
    at org.apache.catalina.util.ExtensionValidator.validateApplication(ExtensionValidator.java:189)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4530)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
    at org.apache.catalina.core.StandardService.start(StandardService.java:525)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Apr 19, 2012 12:58:21 PM org.apache.catalina.core.StandardContext start
SEVERE: Error getConfigured
Apr 19, 2012 12:58:21 PM org.apache.catalina.core.StandardContext start
SEVERE: Context [/rde] startup failed due to previous errors
Apr 19, 2012 12:58:21 PM org.apache.catalina.core.StandardContext stop
INFO: Container org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/rde] has not been started
Apr 19, 2012 12:58:21 PM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor host-manager.xml
Apr 19, 2012 12:58:21 PM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor manager.xml
Apr 19, 2012 12:58:21 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive rdt-reference-gui-server.war
Apr 19, 2012 12:58:21 PM org.apache.catalina.core.StandardContext start


I was getting above error while starting tomcat, due to this error my tomcat started but the application was not up.

Resolution
The problem was with the MENIFEST.MF file. I checked my project's menisfest file but it was correct.
I later resolved this error by checking my dependencies, few of the jars were corrupt and i was not able to open their MENIFEST.MF file.

I replaced those jars with the newer ones and also removed a few jars that were really not required. 
Also note that in a MANIFEST.MF file the length of characters per row should not exceed 70.

Friday, 13 April 2012

Read Excel File Using Apache POI

Here is some nice code snippet to read Excel file using Apache POI Api.
Download Apache POI form here
Create a project in eclipse and add following jars in your classpath. (Not all are required for this example)
poi-3.8-20120326.jar
poi-examples-3.8-20120326.jar
poi-excelant-3.8-20120326.jar
poi-ooxml-3.8-20120326.jar
poi-ooxml-schemas-3.8-20120326.jar
poi-scratchpad-3.8-20120326.jar
commons-logging-1.1.jar
junit-3.8.1.jar
log4j-1.2.13.jar
package com.bharat.poi;

package com.bharat.poi;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Iterator;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadExcelFile
{
  public static void main(String... args)
  {
    // name of Excel file you want to read.
    String fileName = "bond-2-8.xlsx";
    
    // Read an Excel File and Store in an ArrayList
    ArrayList<ArrayList<XSSFCell>> dataHolder = readExcelFile(fileName);
    
    // Print the data read
    printCellDataToConsole(dataHolder);
  }

  public static ArrayList<ArrayList<XSSFCell>> readExcelFile(String fileName)
  {
    /**
     * --Define a List --Holds List Of Cells
     */
    ArrayList<ArrayList<XSSFCell>> cellListHolder =
        new ArrayList<ArrayList<XSSFCell>>();

    try
    {
      /* Creating Input Stream */
      FileInputStream myInput = new FileInputStream(fileName);

      /* Create a workbook using Stream */
      XSSFWorkbook myWorkBook = new XSSFWorkbook(myInput);

      /* Get the first sheet from workbook */
      XSSFSheet mySheet = myWorkBook.getSheetAt(0);

      /* iterate through the cells. */
      
      Iterator rowIter = mySheet.rowIterator();

      while (rowIter.hasNext())
      {
        XSSFRow myRow = (XSSFRow) rowIter.next();
        Iterator cellIter = myRow.cellIterator();
        ArrayList<XSSFCell> cellStoreList = new ArrayList<XSSFCell>();
        while (cellIter.hasNext())
        {
          XSSFCell cell = (XSSFCell) cellIter.next();
          cellStoreList.add(cell);
        }
        cellListHolder.add(cellStoreList);
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    return cellListHolder;
  }

  /**
   * prints data to console
   * 
   * @param dataHolder
   */
  private static void printCellDataToConsole(ArrayList<ArrayList<XSSFCell>> dataHolder)
  {

    for(ArrayList<XSSFCell> cellList : dataHolder)
    {
      for(XSSFCell cell: cellList)
      {
        String stringCellValue = cell.toString();
        System.out.print(stringCellValue +"\t");
      }
      System.out.println();
    }

  }
}