Saturday, January 29, 2011

BerkeleyDB

This week's assignment focused on bringing everything together into one monster webservice.  The build requirements called for a Wicket Client which accessed a RESTful Restlet server, which stored data in a BerkeleyDB database.  Basically, a nice web-based GUI that pulled data from a Server that stored stuff in a Database; a Turducken if you will. 

Recursively-stuffed fowl aside, the focus of this week was on Persistency (read: saving stuff).  Prior to this, my only method of storing data was in a Property Map, or Static Class, which unfortunately didn't stick around after execution  <insert guillotine joke here>.  The answer of course is to put things in a database. 

BerkeleyDB (BDB) is a neat little database service that runs in Java and a myriad of other languages (but most importantly, Java).  Having worked with DBs and webservices before, let me tell you, this thing rocks.  Set up is a breeze, and BDB is pretty darn flexible.  The best part?

NO CONNECTION STRINGS.

In this exercise, I had Contact objects which had unique IDs as primary keys.  All I had to do to was throw a @PrimaryKey attribute above the uniqueID attribute in the Contact class, and BDB associated it as the PrimaryKey.  Then, in the ContactDAO Class, a few lines of code let me define a "mini-DB" object, which let me call out mini-DB.get(key), mini-DB.delete(key), etc. BDB supports multiple mini-DB objects for various keys; in Kata 01, I used two (one for the PrimaryKey, and one for the SecondaryKey).  BDB even has built-in iterators if you need to return multiple entities.  Long story short, if you need a java database, go with BDB.

While I praise BDB for its ease of use and great utility, I should point out that Wicket (*glare*) is not so nice and took up all of a very nice Saturday afternoon.

On to the Katas!
Kata 01:
Easy Peasy introduction into BDB, simply add a timestamp as a secondary key, and add functionalities for a get-timestamp and get-range function.  <3 hrs (actually less than 2, but I accidentally overwrote it and had to do it again :c )

Kata 02:
The mastery Kata, combining Wicket, Restlet, and BDB to make an end-to-end Java-based web-service.   I theoretically had all the pieces from previous Katas, but putting them together took some thought.  I had to draw a diagram...twice.  Coding the back end was easy enough, and took about an hour.  Although many painful hours were spent on Wicket, which sent me maliciously deceptive error messages, before mysteriously clearing itself up. 


BerkeleyDB: http://en.wikipedia.org/wiki/Berkeley_DB

Distribution:
NOTE* Katas are in two separate files in the zip.
http://ics414-spring2011.googlecode.com/files/BerkeleyDB-GBurgess.zip

Saturday, January 22, 2011

Restlet pt II

Distribution: http://ics414-spring2011.googlecode.com/files/restlet-contactservice-gburgess-1.0.122.zip
This past week my ICS 414 class dove deeper into Restlet.  Interestingly, we started using XML, which had me overjoyed, because I've had quite a bit of self-taught experience with XML, webservices and java.  Sufficed to say the assignment this week was pretty simple.  After completing the assignment, I ended up with a cute non-persistant local server hosting a little webservice that sent both XML and Java objects back and forth between client and server applications. 

The back-end(java) database used for this assignment was a single, static (black magic!) class that held a map between keys and values.  The important part was the static declaration, which meant that StaticClass.class.get() from anywhere returned the database.  Pretty nifty.

As disussed in a previous post, the REST interface supports GET, PUT, POST and DELETE protocols for resources living on the web.  You may be asking yourself how XML fits into the equation.  Well, most webservices available online or over intranets usually pass data formatted as XML.  XML is extremely well suited to being a "go between" language as data and metadata can be presented in a universally-readable format.  In fact, many proprietary software systems (I'm looking at you Microsoft) use XML as a back-end data storage mechanism.  If you think about it, this is pretty handy, it means that you, the average (or maybe not so average) Joe can parse data out of these files (granted theres a lot of meta-garbage floating around in there). 

DOM (Document Object Model) is used extensively in XML based webservices.  Basically its a Document object (read: box) that has a root element (read: tree).  The nice thing is that because XML is well suited to handling objects, so Java objects translate over nicely, and since XML is implemented as a linked-list/tree heiarchy, you can easily loop/iterate through the whole thing, pulling whatever data you need.  Handling these DOM objects in Restlet is a bit troublesome (LOTS of auxilliary method calls), however it should be noted that most XML extention libraries have fairly simmiliar method names.  One of the cool things about Java is that it supports a system by which XML and Java Objects can be encoded and decoded between eachother.  This is extremely useful as constructors on the Java side can be passed XML representations of Objects, saving you a bunch of method calls. 


If your planning on investigating on your own, here are a few tips:
1. Make sure you're using a browser that can parse XML (almost anything except chrome and safari), or else the tags will disappear, and you'll never know whats going on.
2. If in doubt about traversing/handling the DOM, getChildNodes() will return a NodeList of child nodes, and getItem(x) will return the node at position x in a NodeList.
3. You may have to call getChildNodes() twice to get to the NodeList that contains all the data.


As for the Katas:
Kata 05: Agregate Contacts
This kata focused on creating a DOM object that held the URIs of objects in the database.  My experience with the DOM system made this one pretty easy, less than two hours.

Kata 06: Implement a get-all and delete-all command for the database
This was more like Kata 05 pt II, basically parsing the URI's from the first part and getting the data out of each link in order to display.  Deletion was even easier, just grab the unique ID and call delete().  <2hrs

Kata 07: Extend Contact to implement a phone number
Easy peasy, until I had to deal with a FindBugs/PMD argument over somestupid Exception.  Basically updating the Contact object, and all calls & adding the phone number variable to various locations.  <2hrs

Distribution:
http://ics414-spring2011.googlecode.com/files/restlet-contactservice-gburgess-1.0.122.zip

Tuesday, January 18, 2011

Restlet pt I

As part of my ICS 414 (Software Engineering II) class (the continuation of ICS 413 from last semester), I was recently thrown into the deep end of Restlet.  REST, or REpresentational State Transfer is basically a set of concepts dreamt up by Roy Fielding a number of years ago.  REST provides a way for resources (and services) to be accessed remotely by machines.  Sounds a lot like the web doesn't it?  Well, your right, but wrong at the same time.  The WWW protocol provides a perfect platform to implement REST, but not vice-a-versa. 

Honestly its all a bit confusing, so screw your head back on and enjoy these educational links:
http://tomayko.com/writings/rest-to-my-wife (Highly reccomended)
http://en.wikipedia.org/wiki/Representational_State_Transfer
https://sites.google.com/site/ics414spring2011/modules/rest

The last link is my class webpage, which holds the assignment du jour: Assignment A04 : Restlet Code Katas.
I should point out that these were not the nice little Katas from last semester.  These were big, scary, nun-chuck-wielding, feat-of-programming-prowess Katas.  Let me start by pointing out that the documentation for Rest is not great, so if your thinking about picking it up in your spare time, expect to spend a few hours trolling tech-forums for the most obscure solutions you've never thought of.  The crowning jewel in all this is that you can programatically pull data from online resources, and use them for your own (potentially nefarious) purposes. 

To the Katas:
Distribution: http://ics414-spring2011.googlecode.com/files/gburgess-restlet-1.0.118.zip

Kata 1: Time resources
This was fairly simple, just copying, pasting and tweaking.  This ultimately provides resources to pull the hour, minute, and second.  This was simple enough to finish in class (<60 mins).

Kata2: Logging
This sounded easy, especially since my professor indicated "logging in restlet can be done in just a few lines".  Well, I spent hours working on this one.  The restlet API was of help.  I came pretty close to figuring it out before one of my teammates solved it.  I passed over the answer in my search for truth, but overlooked it because it didn't sound like it fit the bill.  (>5 hours)

Kata 3: Authentication
It sounded easy...but unfortunately these things rarely are.  I got it working, although my cohorts report some difficulty getting it to work on Mac (*scoff*) platforms (to which I point out the ninety-something-percent compatibility rating).  The devils in the details.  I swear I spent more than 8 hours trying to figure out how to make three lines of code jive.  Although I must say, this is actually pretty useful.  (>8hrs)

Kata 4: Wicket
Writing this little program was like trying to catch a hornet with a thimble.  It sounded difficult, and it was.  After a marathon 8 hour programming session, I gave up.  Essentially the order was for a wicket form that pulled data using rest from the dateserver.  I could pull data from the date server without a problem, and the other little details (jar.build.xml & ivy downloads) were taken care of by another team member.  My real struggle was with the wicket form.  I for the life of me could not figure out how to handle the session properties.  The plan was to implement a bunch of checkboxes, so users could choose what data to pull, and then have them click a submit button to pull up the desired data.  Well as it turns out, keeping track of what checkboxes are checked, is really hard.  Aside from the wicket form, everything else worked all right. ( >8hrs)



http://ics414-spring2011.googlecode.com/files/gburgess-restlet-1.0.118.zip