Tuesday, July 29, 2008

What jar is that class in?

So, I'm a pretty big fan of python now.  Just about a convert, at least for some tasks...

One of the first things I did with python was to find a script that would recursively look for your jar files given a directory and then open them up and look for the class you're looking for. 

The script I found was very complicated using generator and all kinds of things that I'm just not familiar with.  I don't know if generators don't translate to Java very well or what but I just couldn't seem to wrap my head around that script.

So I wrote my own.  Here it is (I have the root directory and class file I'm looking for hard coded) but my version does just about everything the other version did with just about the same number of lines but it is way simpler.  Let me know what you think:


import os
import fnmatch
import re

jarExe = '/Progra~1/Java/jdk1.6.0_03/bin/jar.exe'
rootOfSearch = '/RAD7/SDP70/runtimes/base_v61/''
lookingForClass = 'com/ibm/ws/util/ImplFactory.class'

def showDirectory (directory):
  for aFile in directory[2]:
    if (fnmatch.fnmatch(aFile, '*.jar')):
      print 'searching file: %s' % aFile
      myclasses = os.popen('/Progra~1/Java/jdk1.6.0_03/bin/jar.exe -tf %s/%s' % (directory[0], aFile))
      for aClass in myclasses:
        if fnmatch.fnmatch(aClass, lookingForClass):
          print 'found: %s in %s/%s' % (aClass, rootOfSearch, aFile)
          return True

def findJars ():
  tree = os.walk(rootOfSearch)
  found = False
  subDirs = ''
  for directory in tree:
    subDirs = directory[1]
    found = showDirectory(directory)
      print 'found class in: %s' % directory[0]
      return True
  print 'Not found'
if __name__ == "__main__":



Wednesday, July 09, 2008

Snakes and Kool-Aid

So, I've been playing with Python a lot in the last couple of weeks.  And although it definitely has some oddities, I am nonetheless totally jazzed up about it.  I had forgotten how much fun and power you have when doing OS level scripting.  The last time I was seriously into scripting I was using perl and Korn shell (about 7 years ago!).

So, some of the fancy things I've done lately is to integrate python with my Maven build process and let me just say it is a thing of beauty... Not the code so much, but the power of what happens when you mix these two tools.  By simply calling the python script my application gets compiled, tested, packaged and deployed to all or just some of the environments I'm working in.  All environments are kept in sync (unless I don't want them to be of course).

I've been using python to write some testing scripts as well.  Since our services are exposed via JSON we can simply fire off a request and it just works... the code looks kind of like this:

import urllib
import demjson

class Response:
  def __init__(self, jsonDictionary):
    message = jsonDictionary['message']
    self.statusCode = jsonDictionary['statusCode']

responseObj = urllib.urlopen("http://localhost/webappName/servletName.do? /
responseString = responseObj.read()

response = Response(demjson.decode(responseString))

print 'successful: %s' % response.statusCode

If you are unfamiliar with JSON here is a high level read about it.


So by taking just a little bit of effort to create a json string and sending that to a server you can do some very powerful things...

And although Python plays a small part in these tasks it is a very effective and pleasant glue that holds everything together.


But why Python you ask? Why not Ruby, PHP or Perl (the list goes on and on).  There are several answers to that question but the biggest reason is *I think* I'm going to be pushing our back-end services out onto Google's infrastructure using AppEngine, and right now AppEngine uses Python. (that is a capital period by the way)

But Python is not so bad, it's fun and powerful - but do NOT mix tabs and spaces!  Using indentation to denote blocks of code is kind of an oddity to me - I kind of like curly brackets... but aside from that - I have found Python to be a lot of fun to work with...    And I'm looking forward to rewriting our back-end services that use MySql and Java to Google's App Engine and their big Object Database.

So yeah - Google Android, GWT and eventually App Engine... I have drank the Google Kool-Aid and it is oh so tasty...




What is JSON???

If you don't know what JSON is it stands for JavaScript Object Notation and basically looks like this:

{"key":"value", "key2":"value2"}

But you can do so much more: like nesting JSON objects in JSON objects:

{"key":"value", "key2":"value2", "key3":{"nestedKey":"nestedValue"}}

and adding arrays to the JSON Object:

{"key":"value", "key2":"value2", "key3":{"nestedKey":"nestedValue"}, "key4":[{"arrayKey1":"arrayValue1"},{"arrayKey2":"arrayValue2"},{"arrayKey3":"arrayValue3"}]}

With all of those quotes and brackets it gets ugly quick so there is a little python library called simplejson and by simply executing the following line:

print simplejson.dumps(nastyJsonString, sort_keys=True, indent=2)

you get this:

  "key": "value",
  "key2": "value2",
  "key3": {
    "nestedKey": "nestedValue"
  "key4": [
      "arrayKey1": "arrayValue1"
      "arrayKey2": "arrayValue2"
      "arrayKey3": "arrayValue3"


It is simple but incredibly powerful it is way better than SOAP and shames EJB's and their complexities to no end.


If you are doing remoting or communicating between processes of any kind; you should take a look at JSON there are libraries for just about any language you can think of.