sreda, 7. september 2011

Creating java commmand line programs hell

Let's say you need to create quick command line program in java, to process 10000 folders with some xml files in it. You can do it in java of course. If you run it from your IDE, cool no problems. But if you want to run this on some other machine, let's say linux, and you have additional jars and you do not have debugger on that machine you quickly end up in few days of real hell.

There is a really very quick workaround for this, it'c called Groovy. Groovy let's you do that:

  • use your existing java code
  • use your existing classpath libraries
  • allows you to write in pure java
  • allows you to change script's on the fly
  • allows you to work with source only, no precompilation, recompilation
  • you can add debug statements on the fly
Sounds like heaven? Well, it is. Look at example below, where is some stupid routine to traverse one root folder and output sub folder names. I't uses existing java code from original java projects, and it runs from command line with all the pros from before.

package com.antaranga.groovy.utils

import java.util.Hashtable;
import java.io.File;
import java.lang.annotation.ElementType;
import java.math.SignedMutableBigInteger;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import java.util.Date;

class Cleanup {

static main(args) {
boolean dryRun = false;
String rootFolder = "c:\\root";
System.out.println(String.format("Start processing folder %s , readonlyMode =  %s.", rootFolder, dryRun));
Cleanup c = new Cleanup();
List<DirectoryWalkerStruct> workFolderList = c.walkin(new File(rootFolder));
for (DirectoryWalkerStruct myDir : workFolderList){
System.out.println("Folder found: " + myDir.directoryName);
}
System.out.println("End.");
}

//**************  Function below are copy paste from existing java code
private List<DirectoryWalkerStruct> walkin(File dir) {
List<DirectoryWalkerStruct> workFolderList = new ArrayList<DirectoryWalkerStruct>();
File[] listFile = dir.listFiles();
if(listFile != null) {
for (File f : listFile) {
if(f.isDirectory()) {
workFolderList.add(new DirectoryWalkerStruct(f.getName(), f.getAbsolutePath(), new Date(f.lastModified())));
}
}
}
return workFolderList;
}
class DirectoryWalkerStruct {
private String directoryName;
private String fullPath;
private Date lastModified;
public DirectoryWalkerStruct(String directoryName, String fullPath, Date lastModified) {
super();
this.directoryName = directoryName;
this.fullPath = fullPath;
this.lastModified = lastModified;
}
public String getDirectoryName() {
return directoryName;
}
public void setDirectoryName(String directoryName) {
this.directoryName = directoryName;
}
public String getFullPath() {
return fullPath;
}
public void setFullPath(String fullPath) {
this.fullPath = fullPath;
}
public Date getLastModified() {
return lastModified;
}
public void setLastModified(Date lastModified) {
this.lastModified = lastModified;
}
}
}

You need to read some xml?

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document xmlRootDoc = builder.parse( new File( xmlPath ) );

//get the <root> nodes
Node rootNode = xmlRootDoc.getChildNodes().item(0); 

//get the first level elements 
NodeList childNodes = rootNode.getChildNodes();

//get element
Element firstElement = (Element) childNodes.item(0);
String firstAtt = firstElement .getAttribute("someAtt");

One of the most platforms is Grails (web framework based on groovy), with all the java stuff + grails stuff, and possibility to use grails code as a java beans (JSF, JSP, EJB).

Let's hope you can quickly crawly out from this hell  (just to fall into another one next time).



Ni komentarjev:

Objavite komentar