Sunday, December 5, 2010

Design Patterns -Singleton Pattern

his is one of the most commonly used patterns. There are some instances in the application where we have to use just one instance of a particular class. This Pattern is the Common Creational Pattern in terms of Creation. In Case of Multiple Threading, Synchronization is Necessary. So Object Creation should be in Synchronization Block.
For Eg: LogSingleton.java

import java.util.*;
public class LogSingleton
{
private static LogSingleton instance=null;

private LogSingleton()
{
instance=this;
}

public static LogSingleton getLogger()
{
if (instance == null)
{
//In Case of MultiThreading Logging
synchronized(LogSingleton.class)
{
instance = new LogSingleton();
}
}
return instance;
}
public void logMsg(String msg)
{
System.out.println("Msgasd:"+msg);
}
}

Difference between static class and static method approaches:
One question which a lot of people have been asking me. What’s the difference between a singleton class and a static class? The answer is static class is one approach to make a class “Singleton”.

We can create a class and declare it as “final” with all the methods “static”. In this case, you can’t create any instance of class and can call the static methods directly.

Example:

final class Logger {
//a static class implementation of Singleton pattern
static public void logMessage(String s) {
System.out.println(s);
}
}// End of class

//==============================
public class StaticLogger {
public static void main(String args[]) {
Logger.logMessage("This is SINGLETON");
}
}// End of class




Sunday, August 22, 2010

Generating MultiSheet Excel in BIRT Report Using Customized API

Generation of MultiSheet Excel Report Using Customized Java API.
For this API to be Useful, Each Excel Sheet Report Should be Generated before calling this and the file names are Stored in ArrayList .
StyleBuffer is Initialized by calling the getStyleIds() of MultiSheetHelper Class which is given Below:


Here
fileNames is a ArrayList containing FileNames with Absolute Pathnames for eg:/home/user1/xls/abc.xls,
/home/user1/xls/abc1.xls

ArrayList
fileNames=new ArrayList();
fileNames.add("
/home/user1/xls/abc.xls");
fileNames.add("/home/user1/xls/abc1.xls");
String fileNm=null;
String outputFileName="/home/user1/xls/out.xls"
String[] sheetNames={"Sheet1","Sheet2"};
MultiSheetExcelHelper multiSheetExcelHelper=new MultiSheetExcelHelper();

//To Replace the StyleIds of Each file and Storing the Style id details into a Buffer.
StringBuffer styleBuffer=multiSheetExcelHelper.getStyleIds(fileNames);



fileNm=multiSheetExcelHelper.generateMultiSheet(fileNames,outputFileName,styleBuffer,sheetNames);



// Below Java Code is used to Merge all the Files into a Single Workbook containing the Sheets( Each Sheet is Containing the Contents of Each File which is in ArrayList). This is For BIRT 2.2.2,Since There is no Multiple Excel Sheet Generation in that tool.

import java.io.*;
import java.util.*;

public class MultiSheetExcelHelper
{
public String generateMultiSheet(ArrayList fileNames,String outputFile,StringBuffer styleBuffer,String[] sheetNames) throws Exception
{
String strTemp="";
int sheetNo=2;
BufferedWriter bufferWriter= null;
BufferedReader bufferReader=null;
try
{
bufferWriter=new BufferedWriter(new FileWriter(outputFile));
String tempFile=(String)fileNames.get(0);
bufferReader = new BufferedReader(new InputStreamReader(new FileInputStream(tempFile)));
boolean styleFlag=false;
while((strTemp=bufferReader.readLine())!=null)
{
//System.out.println(st);
if((strTemp.indexOf("")>=0)&& !styleFlag)
{
bufferWriter.write(strTemp);
bufferWriter.write("\n");
styleFlag=true;

}
if(styleFlag)
{
if(strTemp.indexOf("
")>=0)
{
bufferWriter.write(styleBuffer.toString());
bufferWriter.write("\n");
bufferWriter.write(strTemp);
bufferWriter.write("\n");
styleFlag=false;
}

}

else if(strTemp.indexOf("")<=-1) { if(strTemp.indexOf("=0)
{
strTemp=strTemp.replaceAll("Sheet1",sheetNames[0]);
}
if(strTemp.indexOf("ss:StyleID=")>=0)
{
int index=strTemp.indexOf("ss:StyleID=");
String temp=strTemp.substring(index+12);
int lastIndex=temp.indexOf("\"");
String styleId=temp.substring(0,lastIndex);
String inTemp="ss:StyleID=\""+styleId;
String outTemp="ss:StyleID=\""+styleId+"_sheet1";
strTemp=strTemp.replaceAll(inTemp,outTemp);
bufferWriter.write(strTemp);
inTemp=null;
outTemp=null;
}
else
{
bufferWriter.write(strTemp);
}

bufferWriter.write("\n");
}
strTemp=null;
}
tempFile=null;
if(fileNames!=null && fileNames.size()>1)
{
int i=1;
while(i=0)
{
flag=true;
strTemp=strTemp.replaceAll("Sheet1",sheetNames[i]);
sheetNo++;
}
if(flag)
{
if(strTemp.indexOf("
")<=-1) { if(strTemp.indexOf("ss:StyleID=")>=0)
{
int index=strTemp.indexOf("ss:StyleID=");
String tempStr=strTemp.substring(index+12);
int lastIndex=tempStr.indexOf("\"");
String styleId=tempStr.substring(0,lastIndex);
String inTemp="ss:StyleID=\""+styleId;
String outTemp="ss:StyleID=\""+styleId+"_sheet"+(sheetNo-1);
strTemp=strTemp.replaceAll(inTemp,outTemp);
bufferWriter.write(strTemp);
inTemp=null;
outTemp=null;
}
else
{
bufferWriter.write(strTemp);
}
bufferWriter.write("\n");

}
}
strTemp=null;
}
i++;
}
}
bufferWriter.write("
");

}
catch(Exception e)
{
outputFile=null;
e.printStackTrace();
throw new Exception(e);
}
finally
{
try
{
if(bufferReader!=null)
{
bufferReader.close();
bufferReader=null;
}
if(bufferWriter!=null)
{
bufferWriter.close();
bufferWriter=null;
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
return outputFile;


}

public StringBuffer getStyleIds(ArrayList fileNames)
{
StringBuffer styleBuffer=new StringBuffer("");
BufferedReader bf=null;
String st="";

try
{
int sheetNo=1;
for(int i=0;i")>=0 ) && !styleFlag)
{
styleFlag=true;
st="";
}
if((st.indexOf("
")>=0 ) && styleFlag)
{
styleFlag=false;
st="";
break;
}

if(styleFlag)
{
if(st!=null && st.trim().length()>0)
{


if(st.indexOf("Style ss:ID=")>=0)
{
/* int firstIndex=st.indexOf("ss:ID=");
String tempStr=st.substring(firstIndex+7);
int secondIndex=tempStr.indexOf("\"");
String styleId=tempStr.substring(0,secondIndex);
st=st.replaceAll()*/
st=st.replaceAll("\">","_sheet"+sheetNo+"\">");
}
tempBuffer.append(st);
tempBuffer.append("\n");
}

}

}
if(!styleFlag)
{
styleBuffer.append(tempBuffer);
}
sheetNo++;

if(bf!=null)
{
bf.close();
bf=null;
}

}

}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if(bf!=null)
{
bf.close();
bf=null;
}
}
catch(Exception e)
{
System.out.println("Exception in Finally"+e);
}

}


return styleBuffer;
}


}


This is Written in the Concept Since the BIRT 2.2.2 is generating the XLS File as XML Format






Which is supported by Microsoft Excel 2003 and 2007

Friday, August 20, 2010

Solaris

Solaris Admin Guide

iText and Java

How to Make the PDF document compressable Using java and iText Library?.

Use this Flag while creating PDFDocument
Document.compress = true;
writer.setPdfVersion('1.6');
writer.setFullCompression();

Sample Code for Compression to make File Size Lesser (PDF File previously not compressed one)
public class SampleCompress
{
public static void main(String[] args)
{
try
{
PdfReader reader = new PdfReader("D:\\temp\\temp.pdf");
PdfStamper stamper = new PdfStamper(reader,
new FileOutputStream("D:\\temp\\temp_compress.pdf"),
PdfWriter.VERSION_1_6);
stamper.setFullCompression();
stamper.close();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{

}
}

}