Friday, November 2, 2007

Searching Jars Redux

A previous post translated a Groovy file into Java using the closure proposal. The main goal was to see how the new Function Type fit into the Java syntax.

Unfortunately, as Neal pointed out, the resulting Java file wasn't very closure-esque. Here is the same idea, cleaned up.

The new file does this:

  • Accepts a search directory and a target string
  • Recursively traverses the search directory and, for each file found, applies a concept
  • The concept is, in essence, a closure. In this case, the concept is to (a) check to see if the file is a jar and, if so, (b) check the entry list of the jar for a target string
  • The key is that we now have an algorithm that calls a block of code. The block of code is bundled with any local state that it needs to process the input parameters.
Here is the code listing. It is very instructive to contrast (1) the original Groovy file (2) the first attempt at Java and (3) this program. Note that #2 in this list does something slightly different (i.e. it counts occurences, and does not traverse recursively).


import java.io.*;
import java.util.jar.*;
import java.util.Enumeration;
import java.util.*;

class FileExtension {
private String dirStr = null;

public FileExtension(String dirStr) {
this.dirStr = dirStr;
}

// @param block a closure
// @throws IOException (because the closure declares it)
// This method will recursively traverse a directory
// structure and call block for every file found.

public void eachFileRecurse(
{ File => void throws IOException } block
) throws IOException {
File file = new File(dirStr);

for( File thisFile : file.listFiles() ) {
if( thisFile.isFile() ) {
// file
block.invoke( thisFile );
} else {
// dir
FileExtension subFileExtension =
new FileExtension( thisFile.toString() );
subFileExtension.eachFileRecurse( block );
}
}
}
}

public class JarSearcher2 {
static final private String JAR = ".jar";
static private String target;

// closure block
// Iterates through a directory tree and applies the
// closure to each file
// @param file an input file
// @free JAR used as an extension
// @free a target string in a jar's list of entries
// @throws IOException

static private { File => void throws IOException } myClosure =
{ File file =>
String fileName = file.getName();

if( fileName.contains(JAR) ) {

JarFile jarFile = new JarFile(file);

// old-style Enumeration
// don't blame me or closures!
for( Enumeration e = jarFile.entries() ;
e.hasMoreElements() ; ) {
JarEntry entry = (JarEntry) e.nextElement();

if( entry.getName().contains(target) ) {
System.out.println("found " + target
+ " in: " + file.toString() );
}
}
}
};

// iterates through a directory tree and applies the
// closure to each file
// @param args[]
// @throws IOException
public static void main(String[] args) throws IOException {
String searchDirStr = args[0];
target = args[1];

(new FileExtension(searchDirStr)).eachFileRecurse( myClosure );
}
}

3 comments:

Stephan said...

When dealing with Enumerations a static utility method is quite useful.

public static Iterable iterate(Enumeration enum) { ... }

Then you can write:

for (File file: iterate(entries)) {
...
}

Peace
-stephan

--
Stephan Schmidt :: stephan@reposita.org
Reposita Open Source - Monitor your software development
http://www.reposita.org
Blog at http://stephan.reposita.org - No signal. No noise.

Anonymous said...

The World of Kung fu Gold seems also important. WoKf gold in the game is just like the money in the life. It is different of the buy World of Kung fu Gold online in the game world. You can have cheap World of Kung fu Gold to update your weapons. And the World of Kung fu money should be more and more.
The devaluation of the fiesta Gold grows faster than we think of it. I hope it is reflects to the management department about the question of the fiesta money. It is caused by the continuous influx of fiesta online gold market currencies. On the other hand, buy fiesta Gold is one of these questions. The fiesta online money is one of the causes.

Anonymous said...

Now do you worried about that in the game do not had enough aion kina to play the game, now you can not worried, my friend told me a website, in here you can buy a lot aion online kina and only spend a little money, do not hesitate, it was really, in here we had much aion gold, we can sure that you will get the cheap aion kina, quick to come here to buy aion kina.

Now do you worried about that in the game do not had enough Anarchy credits to play the game, now you can not worried, my friend told me a website, in here you can buy a lot Anarchy Online credits and only spend a little money, do not hesitate, it was really, in here we had much Anarchy gold, we can sure that you will get the Anarchy online gold, quick to come here to buy AO credits.