| JBoss JMX Collection: how to create a PDF from text |
|
|
|
| Written by F.Marchioni | ||||||
| Thursday, 10 December 2009 14:59 | ||||||
|
Starting from now we will collect all interesting MBeans and POJO Services and make available to readers. In this tutorial we will show a POJO Service that transforms a text file into a PDF table using the iText library
The prerequisite of this sample is the iText library which needs to be downloaded from:
To create a service POJO all you need is adding a few annotations to your POJO Class, which will act as a singleton service. Here's the service POJO Class:
package com.sample;
import org.jboss.ejb3.annotation.Management;
import org.jboss.ejb3.annotation.Service;
@Service(objectName = "textToPdf:service=PDFConverter")
@Management(PDFConverter.class)
public class PDFConverterService implements PDFConverter{
// Lifecycle methods
public void create() throws Exception {}
@Override
public void createReportPDF(String inputfileName,String outputfileName) {
try {
PDFCreator.createReportPDF(inputfileName,outputfileName);
} catch (Exception e) {
e.printStackTrace();
}
}
}
What is really interesting about the POJO service are the two annotations:
@Service(objectName = "textToPdf:service=PDFConverter") @Management(PDFConverter.class) With the first one (@Service) you are binding the POJO in the textToPdf domain using the PDFConverter service name The Second annotation (@Management) contains the interface name which will be exposed to the client. The interface is nothing more than a plain Java interface:
package com.sample;
public interface PDFConverter {
public void createReportPDF(String inputfileName,String outputfileName);
}
The real class which does the job of converting a text file into a PDF formatted table is PDFCreator which basically reads the input file name stores the content into an Array using the "|" as separator between fields.
package com.sample;
import java.io.*;
import java.util.ArrayList;
import java.util.StringTokenizer;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
public class PDFCreator {
public static void createReportPDF(String inputfileName, String outputfileName) throws DocumentException, IOException {
ArrayList mainList = new ArrayList();
// READ TEXT FILE
BufferedReader in = new BufferedReader(new FileReader(inputfileName));
String line;
int rows=0;
int columns=0;
while((line = in.readLine()) != null) {
ArrayList subList = new ArrayList();
StringTokenizer st = new StringTokenizer(line,"|");
while (st.hasMoreElements()) {
String cell = st.nextToken();
subList.add(cell);
columns=subList.size();
}
mainList.add(subList);
}
in.close();
rows=mainList.size();
// CREATE PDF DOCUMENT
Document document = new Document();
PdfWriter.getInstance(document,
new FileOutputStream(outputfileName));
document.open();
PdfPTable table = new PdfPTable(columns);
for (int i=0;i<mainList.size();i++) {
ArrayList subList = (ArrayList)mainList.get(i);
for (int y=0;y<subList.size();y++) {
String cell = (String)subList.get(y);
table.addCell(cell);
}
}
document.add(table);
document.close();
}
}
Deploying the Service POJO is just a matter of packaging the files into a JAR archive and copying the archive into the deploy folder of JBoss AS. The following output will be produced on the JBoss AS Console:
For testing the POJO Service you can navigate to the JMX Console and add the input and output file name:
This is a sample of input file: column1|column2|column3 aaaaaaa|bbbbbbb|ccccccc ddddddd|eeeeeee|fffffff ggggggg|hhhhhhh|iiiiiii This is the PDF output: ![]() Resources: Service POJO Tutorial
JBoss.org Search
Custom Search
Only registered users can write comments!
Powered by !JoomlaComment 3.26
3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved." |
||||||
| Last Updated ( Thursday, 10 December 2009 16:27 ) |






