Tuesday, August 22, 2006

The non OO part of J2EE

GUI development has always been a pain. Looking back, the most commercially successfull, and easy to use tool for GUI based application development was Visual Basic. It had an easy form creator where you can drag drop your widgets and create your own application UI in minutes. Once you are done with it, you can simply tie some application logic to each form button.. u have your application ready!

Then came the web based era of application development where UI was interpreted as HTML and rendered by a web browser. Application logic would run decoupled in a middleware server. Microsoft was quick to launch its new technology based on IIS(web server) called ASP.

java kept developing in the background. Java's concept was simple: Object Oriented, open source and platform independent. For UI based application development, java had awt and then swing. Everything was object oriented.. Eventually swing evolved, new tools were introduced that made it easy to develop forms using java-swing(vb style). In the mean time Java based web technology(asp or .net equivalent) developed independent to swing into the massive J2EE framework. J2EE does everything one would expect from a good middleware framework but for the UI part.

I must say Java couldnt keep its promise of "object orinted" when it comes to the web/html part of J2EE. JSP(an answer to asp) was deviced as java embeded in html which will generate html based UI's at run time to be displayed in the user browser. JSP is (html + java) highly non object oriented. JSP discussion is incomplete without the underlying java counter part: servlets. Servlets are object oriented but I must say it is difficult if not impossible to design UI based forms using servlets. The main problem here is "browser based html" and the decoupling between browser and the middle ware application.

This complexity was addressed using different design patterns, frameworks etc. For instance STRUTS the popular framework to seperate UI, navigation and business logic. And then JSF to remove the disadvantages of STRUTS and to provide a better standard for tool developers. Its all good but where are the objects? For instance, JSF which defines user element placement has become more XML oriented then object oriented.

With evey new standard and framework introduction in the J2EE(web tier) world, we are only moving away from complete object orientation. If you want to know what an complete object oriented application is, and what the advantages are, then just create an application using Java-Swing. In the java-swing world you can create the entire application using the plane old java classes(completely OO) and may be few property files to store customaziable information external to the application code!

So web part of J2EE is bad? I dont have an answer. This is just an evolution, it had to evolve this way. May be the sane thoughts tossed during the evolution didnt win the votes of sponsorers over what is practical.

I would like to propose here one such "sane" but not very practical thought for web based appliaction development:
Create your entire application as an pure java-swing based app. Then create a layer on top of it which will convert the java based swing forms into browser based html pages. Let this layer take care of navigation and other other conversion logistics. We have a simplified framework!


I ran across an commerical application that does just this!
@link to webcreme
This might sound more like an integration tool that must be used to expose the existing java application to web based clients.

But if this concept is explored properly and if the application is designed properly, I think we have a new era of Java web based development.

Feb 13:
@GWT
@Java2Script, RCP to RIA

javadocs in Windows help format?

Ever thought of converting or reading your javadocs in a PDF or windows help file(chm)? Not a bad idea hun? Well check this:
@Commercial tool that does it all
@javadocs in chm
[tbd]@free tool

Monday, August 14, 2006

Free enterprise collaboration platform: TWiki

What is TWiki?

TWiki, a flexible, powerful, and easy to use enterprise collaboration platform and knowledge management system. It is a Structured Wiki, typically used to run a project development space, a document management system, a knowledge base, or any other groupware tool, on an intranet or on the internet. Web content can be created collaboratively by using just a browser. Users without programming skills can create web applications. Developers can extend the functionality of TWiki with Plugins.

"We use TWiki internally to manage documentation and project planning for our products." said Eric Baldeschwieler, Director of Software Development of Yahoo! "Our development team includes hundreds of people in various locations all over the world, so web collaboration is VERY important to us. TWiki has changed the way we run meetings, plan releases, document our product and generally communicate with each other. We're great fans of your work!"

@link

Friday, August 11, 2006

Project build system

Krysalis Centipede is a project build system based on Apache Ant.
It's made to be easy, extensible and powerful.
You can just grab it and start building your project. Anything that is needed for a start is there, and the creation of personalized build targets is straightforward, because it's based on plain Ant. Centipede is un-intrusive, and gives you power without taking any. It also has an interactive target facility, so that the user can just run "build" and choose from a menu.
Link: @Centipede
Other links: @Maven | @Jira

Wednesday, August 09, 2006

Text based graphics: figlet

Ever wonder where the Graphics fonts made out of text come from while chatting in irc or channels alike?
Obviously thro s.w. Unless someone is sitting in N. pole providing his service @ 1c per character! :D

@link

How to run java application as Windows Service?

Procrun is the answer. And thankfully this product is from the open source company Apache.

Procrun is a set of libraries and applications for making Java applications to run on WIN32 much easier.

Prunsrv is a service application for running applications as services. It can convert any application to run as a service.

Prunmgr is a GUI application for monitoring and configuring procrun services.

FYI: Apache Tomcat service in win32 uses the procrun service.

@link

Thursday, August 03, 2006

Insert invisible frame at will

Ever thought of inseting a "hidden" frame in your page, where you can do magical things? Well we all do. Invisible frame is the magic wand for java developers. Well, now you can keep your source(html, jsp, jsf, etc) clean and insert this frame with a function call.

Code below will not only insert a invisible frame, but it will also return you its handle. If you wish you can pass a frame id of your choice, or be careless and the code will generate one.
<br /> /*<br /> *Add a invisible frame in your document and get a handle to it.<br /> *It is not necessary to specify the frame id.<br /> */<br /> var frameCount = 0;<br /> function getInvisibleFrameHandle(frameId)<br /> {<br /> var frameName,html;<br /><br /> if (!frameId)<br /> frameName="_invisibleFrame__" + (frameCount++);<br /> else<br /> frameName = frameId;<br /> if (document.getElementById(frameName))<br /> return document.frames[frameName];<br /> else<br /> appendDocument("<iframe id=" + frameName + " style="visibility: visible; height: 0pt; width: 100%;">");&amp;amp;lt;br&amp;amp;gt; return document.frames[frameName];&amp;amp;lt;br&amp;amp;gt; } &amp;amp;lt;/xmp&amp;amp;gt;</iframe><br /><br />

A generic js function to append text in HTML

Here is a simple js function to append whatever you feel like after your document(html) is loaded in the user browser.

/*
*Append HTML to document.
*WORKS IN ALL BROWSERS
*/

function appendDocument(html)
{
if (document.all)
document.body.insertAdjacentHTML('beforeEnd', html);
else if (document.createRange) {
var range = document.createRange();
range.setStartAfter(document.body.lastChild);
var docFrag = range.createContextualFragment(html);
document.body.appendChild(docFrag);
}
else if (document.layers) {
var l = new Layer(window.innerWidth);
l.document.open();
l.document.write(html);
l.document.close();
l.top = document.height;
document.height += l.document.height;
l.visibility = 'show';
}
}

Java code to create Reports using jasper and text data

Copy paste the code below in Editplus to understand it. It is pretty straight forward to read and understand.
"generateLabels" is a function that will take a string which is really a txt datasource for your designed jrxml file. I used ireports to design the jrxml.

jrxml will return a byte array that can be displayed(response.write) as embeded pdf(or any other format) in the user browser.


public byte[] generateLabels(String dataSource)

{ byte[] pdfBytes=null;

JasperDesign jrDesign;

JasperReport jrReport;

JasperPrint jrPrint;

try{

ByteArrayInputStream dataSourceByteInputStream = new

ByteArrayInputStream(dataSource.getBytes());

JRCsvDataSource jrCsvDs = new

JRCsvDataSource(dataSourceByteInputStream);

String[] header={"label", "details"};

jrCsvDs.setFieldDelimiter(',');

jrCsvDs.setRecordDelimiter(";");

jrCsvDs.setColumnNames(header);

InputStream jrStream =

FileUtil.readConfigFileFromClassPath(this.getClass(),"etc/label.jrxml").getByteStream();

Map parameters = new HashMap();

parameters.put("ReportTitle", "Labels");

jrDesign = JRXmlLoader.load(jrStream);

jrReport =

JasperCompileManager.compileReport(jrDesign);

jrPrint =

JasperFillManager.fillReport(jrReport,parameters,jrCsvDs);

JasperExportManager.exportReportToPdfFile(

jrPrint,"labels.pdf");

pdfBytes =

JasperExportManager.exportReportToPdf(jrPrint);

PdfReader pdfr = new PdfReader(pdfBytes);

ByteArrayOutputStream baos = new

ByteArrayOutputStream();

PdfStamper stamp = new PdfStamper(pdfr, baos);

stamp.addJavaScript(

"this.print({bUI: false, bSilent: false});\r");

stamp.close();

pdfBytes = baos.toByteArray();

}

catch(Exception ex)

{

ex.printStackTrace();

}

return pdfBytes;

}

Auto print on document load

Below is a simple process/hack to bypass the browser print dialog.
  • Generate a pdf for whatever(html or else) you want to print (This is typically done at the server. I use itext) using your fav api.
  • Once you have the pdf(from any source), you are 33% done.
  • Now we will ask the pdf to do something onLoad.
    • guess what "something" is: "Print"!
    • You can embed a piece of js in the pdf that will run at pdf load event.
    • In this js we will ask Acrobat reader to directly print the document to printer
  • Next you will have to load this pdf in an invisible iframe on your page, using javascript.
  • You are done! Trigger the invisible iframe load using either a button click or html-onLoad event. hurray your document is send straight to the printer!!!
  • This method works both in firefox and IE. It ha to, we are basically bypassing the browser security!
Here is the code that will embed javascript in the pdf document. Ofcourse, we will be using java and itext libs to do this.:

PdfReader pdfr = new PdfReader(pdfBytes);

ByteArrayOutputStream baos = new ByteArrayOutputStream();

PdfStamper stamp = new PdfStamper(pdfr, baos);

stamp.addJavaScript("this.print({bUI: false, bSilent: false});\r");

stamp.close();



Loading pdf in an invisible frame is a easy task ... I would let you play arround with this code. Or may be I will post it if time permits!

How to extract a list values from URL

Assuming you are using java servlet, here is a simple method:

Sample:
http://localhost:8080/extract.do?ist=val1&ist=val2

for(String ist:req.getParameterValues("ist")) {

System.out.println("ist: " + ist);

}