Search |
||
Bin Bash Java (Chapter 1)Posted by evanx on May 24, 2006 at 1:37 AM PDT
In the preceding blogs "Java is all you'll ever need" and "A Fool's Errand" I alluded to using Java for "small tasks" eg. file/system tasks, rather than shell scripts. I promised to present some examples along these lines.
This is Chapter 1 of many, and presents a basic design. We'll thrash it out in subsequent chapters.
My motivation for trying to move away from shell scripting (or using some other scripting language like python or groovy), in favour of writing "tasklets" in Java, are as follows.
A typical task is backing up our computer, so let's consider this one of our first goals. OK, this could take a while. Hopefully we'll achieve this goal in subsequent chapters. Doing a good design here would be a great start.
Of course our "framework" should be reusable for other tasks, besides the backup one. And very convenient to use. So that some day soon, we can write all our file and system tasks in Java, and never look back, woohoo!
Let's create some helper classes, eg. TFileHelper for handling files, and TProcessHelper for handling processes. And we will create a TTaskContext which exposes our "library" to our tasklets, and/or can be used as a superclass by our tasklet classes.
Note that I've choosen the letter T to prefix our "tasking" project classes. This avoids any potential namespace conflicts, and makes it clear which classes are our new ones.
To give us some flexibility, let's create our own classes to represent files and processes, namely TFile and TProcess. We will extend those eg. TDir and TZipFile from TFile.
First let's design our file helper class. (Note that I'm using abstract declarations to highlight the interface at this stage, rather than the implementation.)
That's just for starters. We can expect this class to grow a lot!
Next let's design our process helper class.
The file reader and writer processes above would be used for redirection, eg. the file reader as the first argument in the pipe(), and/or the file writer as the last, as follows.
As you can see, we are loving Java5 varargs :)
For convenience, let's introduce a superclass for our tasklets.
Let's slide this baby into gear and start putting rubber to road.
So let's imagine what our first backup tasklet implementation might look like, eg. for zipping up our home directory on Linux.
Ok, so if we wanna write tasklets like the above, we still gotta bit of work to do. It's gonna rock tho, innit.
So you spotted those annotations. The idea is that our framework could prompt for those eg. if not specified on the command-line...
So we gonna use annotations to tell the framework what it needs to know to provide a "tasklet container" with a management console in future maybe. Now we're talking!
I'm getting way ahead of myself here, but let's dream on...
This "management console" could have a Swing interface, and/or a web interface.
On the other hand, a web interface would be quick and easy for administering remote boxen, eg. backing up our media center in the lounge using our laptop. Even on a local machine, having a web-based console for tasks would be handy.
You know what, I just decided we gonna embed a web server into our tasklet framework. So that our tasklets are always remoteable, and deployable via a single self-contained jar...
OK, I better stop, I'm getting too excited here!
Let's hope for accidents of the former "loud and clear" type, rather than misdirections of the latter, later type.
In the next chapter, we might refine this design before we kick off, so please post comments to that effect.
Then we can start rolling up our sleeves to implement some of the file IO functionality. Uh oh, sounds like work. So we'll google and cut and paste. We are developers after all, with a limited amount of time, so the less code we actually write the better, innit ;)
Update. Check out NailGun which aims to obviate the start time of the JVM, eg. for Java command-line tasks. They say, "Java has an extensive and robust core API and huge number of available open source libraries. It's a great big hammer, making almost any programming project look like a nail." :)
Postscript. Here's punting my blog "Swing thumps Ajax" again. Check it out, dog. »
Related Topics >>
Java Tools Comments
Comments are listed in date ascending order (oldest first)
|
||
|
|