Tuesday, June 23, 2009

Java shell

I've been having fun writting a utility java shell, I use cygwin on windows but I needed something more integrated with java such has building class paths, invoking java/ant/groovy /os scripts, scanning in context class loader certain classes | @Annotations, etc, etc. And also when I switch to Linux there it is for me as well. Some basic commands are os file system handling such as ls / rm / mk / grep / etc.
Internals use java.io.Console and a custom annotation:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Command {
String name()        default "";
String description() default "";
String abbrev()      default "";
String def()         default "";
}
So anytime I need a new command I can annotate it like below and instantly it becomes available via method reflection invocation:
@Command(name="cmd", description="sample cmd", abbrev=".", def="?")
public void cmd( String str ) throws Exception {
...
}
Here's an example below for few common file system cmds: ls , lsr (just like ls but with recursive capabilities building a tree)
Note that commands can be chained 'piped' taking res output as input like in *nix such as:
lsr src | grep '06-11'
And also io redirect '>' result:
ls . | lsr src | grep '06-11' > result.log
Or io redirect appended '>>'
ls . | lsr src | grep '06-11' >> result.log etc I'll have to admit that grep command which takes regular expressions to search is my favorite command of them all (specially in windows cuz u know y), it's so powerfull when combined with others.
Here's a list of the help for all cmds implemented so far (keeps growing by the day) Today I included a disassembler via 'javap' which scans in context class loader via a package prefix and shows public class signatures, it comes handy when you need to introspect bytecode loaded and being currently invoked. It also can open a script file and sequentially execute cmds but for that there's a groovy cmd which I can invoke and run groovy scripts with dyn-language richness, one example is I run [def ant = new AntBuilder()] via groovy ;-).
Now one thing I hate to do is walking by hand on file system typing long paths, so I added a simple 'fs' cmd which launches a swing UI which facilitates file system operations, I hooked most common cmds in a menu and results are directed to console:
I know it looks like a toy but it surely comes handy on day by day con$ole operations, heck I don't know what I'll add next but it sure saves the day, I love being productive with tools that do things my way ;-) and I can easily throw a new feature at it in a matter of minutes. One final trick (for windows) is that you can invoke it directly from windows file explorer anytime you right-click on a folder (shows on menu): file: reg.reg
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\jell]
@="jell"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\jell\command]
@="cmd.exe /k \"cd %L\"&jell"
I'll probably pack it once it becomes stable so I can share as GA, that is if anybody finds this useful, Well, till next time, cheers...

3 comments:

Anonymous said...

Martin - this approach looks very interesting, did you ever decide to distribute your code?

Regards,
Bill

Martin Zoldano said...

Hi Bill, I got distracted with other projects, but it did materialize into something, I can share it with you, just send me your @

cheers

TheAlchemist said...

Martin: That looks great! Would you mind shooting me an email at kap4020 at gmail?

I'd love to pick up where you left off, if you're still distracted ;)