Posts Tagged swing

S3DropBox is now on GitHub

I’ve released a new version of my S3DropBox on GitHub. I’ve moved the project to GitHub so that I can have all my current active projects in one place. Check it out in its new home at https://github.com/tomcz/s3dropbox.

This release uses the AWS java libraries. They are finally good enough for me to stop creating my own wheels and vulcanising my own rubber. As a bonus the S3DropBox creates URLs in virtual hosted format (eg. https://mybucket.s3.amazonaws.com/myobject) if the bucket name permits that, uses HTTPS by default and is able to perform multipart uploads so that large files get uploaded faster.

Tags: , , , , ,

From Java to Groovy

Recently I’ve been playing with some old Java Swing applications and I found myself wishing that I could replace all the anonymous inner-classes with some clean Groovy closures. The job of converting a java source file to a groovy source file is pretty simple and eminently scriptable. Here’s my take on a python script to do just that:

from optparse import OptionParser
from subprocess import Popen, PIPE
import os

def rename(src, dest, use_svn):
    if use_svn:
        output = Popen(['svn', 'rename', src, dest], stdout=PIPE).communicate()
        print output[0]
    else:
        print 'mv', src, dest
        os.rename(src, dest)

def process_file(src, use_svn):
    if src.endswith('.java'):
        print 'Processing', src
        fd = open(src, 'r')
        lines = fd.readlines()
        fd.close()
        fd = open(src, 'w')
        for line in lines:
            text = line.rstrip()
            if text.endswith(';'):
                text = text[:-1]
            fd.write(text)
            fd.write('\n')
        fd.close()
        root = os.path.dirname(src)
        filename = os.path.basename(src)
        dest = os.path.join(root, filename[:-5] + '.groovy')
        rename(src, dest, use_svn)

def process_dir(dir_path, use_svn):
    for root, dirs, files in os.walk(dir_path):
        for filename in files:
            src = os.path.join(root, filename)
            process_file(src, use_svn)

if __name__ == '__main__':
    parser = OptionParser()
    parser.add_option('-s', '--svn', action='store_true', dest='use_svn', help='use svn to rename files')
    (options, args) = parser.parse_args()
    if len(args) > 0:
        target = args[0]
        if os.path.isfile(target):
            process_file(target, options.use_svn)
        else:
            process_dir(target, options.use_svn)
    else:
        process_dir(os.getcwd(), options.use_svn)

This means that I can now replace this:

public void actionPerformed(ActionEvent e) {
    executor.execute(new Runnable() {
        public void run() {
            controller.showBuckets();
        }
    });
}

with this:

public void actionPerformed(ActionEvent e) {
    executor.execute { controller.showBuckets() }
}

Now that’s refactoring ;-)

Tags: , , , ,

S3DropBox 1.2 is now released

I love feedback … including bugs (thanks Cam). The S3DropBox will now only permit the creation of a bucket when Amazon S3 returns a HTTP 404 status code in response to a HTTP HEAD request for the bucket prior to creation. The new version can now be downloaded from http://code.google.com/p/s3dropbox/.

Tags: , , , , , , ,