Shizzle

My little notebook

Buzz – Google’s backdoor into Facebook?

February 11, 2010

Having activated and quickly turned off again GMail’s new feature I was left wondering why a great email reader was being polluted by some shitty Twitter crap. I like my communication formal and simply don’t have the need to tell the world in a scattergun approach what I am currently doing.

Buzz in itself is also not very Googley so I was scratching my head what it was all about. Then I read column in the New York Times:

Facebook and Twitter will face renewed pressure to publish and consume standardized data feeds as well now. If Buzz is big enough, it could break the dam holding back a flood of standardized data. Where there is standized (sic) data, there is scalable network effects, consumer choice, competition and thus innovation.

Maybe Google isn’t really interested in providing another me-too product. Maybe they want to pry open Facebook and their social data feeds so they can organise and rank them, making them useful for their users. Think of a Google Reader for Twitter/Facebook/Friendfeed. Then do what Google does best and slap a few ads in it and bingo! As always Google isn’t really a content provider but rather a way to channel all the available content out there and make it into a neat, bite-size parcel. That’s what made them what they are today.

No Comments

Dependency injection for beginners

February 6, 2010

Pro-forma preamble

When I started learning how dependency injection works it was extremely hard for me to understand. Once I got what it does I still didn’t quite get what the benefits of this technique were. I just thought it was some overly complex design pattern that is just making life difficult for the sake of it. After all, what is so bad about using new anyway?

Well, in this blog post I want to share what I have learned about dependency injection since leaving university and a becoming full-time programmer last September. I hope I can help a newbie to understand a little more about this design approach. Don’t be frustrated, however, if you don’t get it straight away: It took me the best part of my first month to really dive into dependency injection even though I had read lots of articles and blog posts about the topic.

Dependency injection vs. Inversion of control

Some authors strangely claim that DI is the same thing as IoC. I however think that DI is a type of IoC, namely to tell the to be injected object what its dependencies are. Inversion of control, to me, means something more general: That there is a predefined workflow (the control part) that the developer hooks her own components into. This principle applies to virtually all libraries and frameworks. For example, your favourite web development framework allows you to write a request handler for a URL, but most likely you can’t change the nature of the request itself. You will always receive a HttpRequest as the input of your request handling code. Dependency injection however is a specialisation of this principle.

Modules and dependencies

When you start to build big systems you naturally tend to modularise. Lets take online shopping as an example. You have one module of your code handling the user input for an order and validating form fields; lets call this module the OrderHandler. Then you have another module, which opens a connection to your payment provider and checks that the credit card data the user just gave are actually kosher and the payment can go ahead. We call this module the CreditCardPaymentService.

So, when the OrderHandler has validated all the form fields it passes the data over to the CreditCardPaymentService. But before it can do that it needs to have or create an instance of the CreditCardPaymentService. In (pseudo-) code this would probably look something like this:

class OrderHandler:
   this.payment_service = new CreditCardPaymentService()
   handle_request(request):
     # do something to validate the user input...
     payment_data = request.get_parameters()
     response = this.payment_service.handle_payment(payment_data)
     if response.successful():
         return new HttpResponse("Payment accepted")
     else:
         return new HttpResponse("Payment declined")

So far, so good. But what happens when you have quite a few parts of your code taking orders and querying the PaymentService? They all call new CreditCardPaymentService.
Now, your boss has decided your going to switch from your old credit card provider to Paypal. You write a new PaymentProvider that sends a request to their server and authorises the payment. When you actually want to switch over, you will have to replace all instances of CreditCardPaymentProvider with PaypalPaymentProvider. Once you do these kinds of thing a lot, you’ll end up thinking that there’s got to be a better way to do this.

DI to the rescue

What if all the different modules of your shopping website, instead of creating new instances of PaymentProviders, would instead be given (or injected) those modules?

Maybe we could rewrite the above code like this:

class OrderHandler:
 
 this.payment_service=None
 
 set_payment_service(payment_service):
    this.payment_service=payment_service
 
 handle_request(request):
    # do something to validate the user input...
    payment_data = request.get_parameters()
    response = this.payment_service.handle_payment(payment_data)
    if response.successful():
        return new HttpResponse("Payment accepted")
    else:
        return new HttpResponse("Payment declined")

Obviously now it is easy to exchange on payment provider with another one. The downside of this that you have to pre-configure the OrderHandler with some type of PaymentProvider. Most DI frameworks do this using Factories and assign each configured, ready-to-use object a string. A factory is supplied with some configuration file that defines those objects and their dependencies.
Those config files could look like this:

/* objects.conf */
#cc_order_handler{
  class: OrderHandler
  payment_service: CreditCardPaymentService
}
 
#paypal_order_handler{
  class: OrderHandler
  payment_service: PaypalPaymentService
}

We now have a central piece of code that handles each module’s dependencies. It basically instantiates the OrderHandler, sets the right payment service and then gives this object to whoever wants to use it. If we wanted to fetch on of the order handlers we would do it like this.

factory = new ObjectFactory("objects.conf")
paypal_payment_handler = factory.getObject("paypal_order_handler")

What you have done now is to externalise the configuration process of  the Handler from inside it to the factory with calls the set_payment_provider method before it returns it to whoever is requesting the object.

As an added benefit we can now easily unit-test the OrderHandler by creating and injecting a fake PaymentService that always returns a positive response.

Implementations

This principle is currently used in a lot of enterprise Java applications. The most popular framework that uses this pattern is Spring. Spring uses XML files for configuration and much of what I described above stems directly from Spring, which actually many more things and DI is just one, albeit central, aspect of the framework.

Another piece of code I also want to have a look at is Google’s Guice which superficially looks less all-singing, all-dancing, but still very interesting.

1 Comment

What’s my Google OpenID URL?

December 14, 2009

Short answer

It’s the same for all Google accounts:

https://www.google.com/accounts/o8/id

Your username is not part of the OpenID.

Long answer

Well, it seems little strange that Google, your friendly neighbourhood search giant, is so coy about its OpenID support. I had to search around for quite a while to find my OpenID URL, which is the thing you paste into the OpenID box at the service you want to sign up to.

Logging in with your OpenID URL

Logging in with your OpenID URL

Why is Google doing this, you may ask? They are usually very good about these things and usually support an open standard (like they have done with XMPP, which they use for Google Chat.) My suspicion is that they want to plug their own OAuth instead, which is a similar protocol, but they effectively solve different problems.

Anyway, the URL that lets you sign into OpenID enabled services is
https://www.google.com/accounts/o8/id
This URL is the same for all Google accounts; it redirects you to Google’s servers for you to confirm the logging in process. That’s it.

A good example

Stackoverflow.com is doing OpenID signing in right. I doesn’t ask you to fiddle with an OpenID URL but rather gives you nice and easy logos to click on – it fills in the URL for you. Well done!

Stackoverflow.com login page

Stackoverflow.com login page

7 Comments

Guten Tag, Edd

November 13, 2009

If you are a regular user of the Edd, the spoke length calculator you may have noticed that there is a new little drop-down box at the top right hand corner which lets you select between different languages. That’s right, Edd is going to be available in multiple languages.

At the moment this is limited to only English and German, but as soon as the last few kinks of theinternationalisation have been ironed out, I will try to add more languages.

In the meanwhile, if you find something that has been translated incorrectly or something that hasn’t been translated at all, make sure to let me know either in the comments to this post or by writing an email to lenniboy@gmail.com.

Terve!

Pekka L has provided a Finnish translation: http://lenni.info/edd/fi.

Thanks Pekka.

4 Comments

Mavens’s “The artifact has no valid ranges”

October 10, 2009

I have just learned to use the really great Java build and project management tool Maven. It makes managing large projects with dozens of components a lot easier and its build configuration is simpler and less imperative compared to Ant, since it relies a lot on convention over configuration. Basically, Maven knows that pretty much every project needs to be

  • compiled
  • JavaDoc’d
  • unit tested

These things don’t need to be declared and are automatically done by Maven. Maven’s great advantage over Ant is it’s ability to handle the dependencies of your project, ie. the old “Hunt the JAR” game is a thing of the past as Maven will simply download whatever libraries or frameworks you will need. Most of the time your build will run smoothly and Maven will spit out a nice JAR or WAR without problems.

The curious error message

However, if there are build errors they tend to be on the cryptic side. “The artifact has no valid ranges” is one of those. Basically it means that you do have a valid range, however you have defined two (or more) different version ranges of an artifact in a fixed way. Most likely you haven’t defined those dependencies in the same project but through a transitive dependency.

Let’s have an example. Your project my-app, depends on an artifact super-framework at version 1.0, and only 1.0, nothing else. my-app also depends on another of your apps called my-app2. my-app2 has a dependency on super-framework but only for versions 0.5 to 0.9. This means that you have incompatible version ranges and you need to resolve this. You will have to edit my-app or my-app2 and change their version ranges for super-framework.

This took me a little while to figure out myself since the error kinda suggests to look in the wrong place. The range is perfectly valid, it’s just that there is too many of them.

Debugging this message

That is a little tricky. You can try running Maven with the -X flag, which will show a running log of how the version is chosen. Also, sometimes the Maven plugin for Eclipse can give you some clues, but sometimes it is way off.

One word of advice though, it seems to matter in which order you include your dependencies in the the POM file. So, if you include a dependency on

super-framework [0.5,1.5)

it will fetch the latest available version, say 1.1.

If you then have a transitive dependency further down that includes

super-framework [0.5, 1.0)

Maven will generate this misleading error, since it will not select anything other than the 1.1 it already has, even though it could just select 0.9 without producing a version conflict. If you swap the order, weirdly, it works.

No Comments

First impressions of OS X 10.6 Snow Leopard

September 8, 2009

I couldn’t wait to finally get my hands on a copy of the latest iteration of Apple’s OS and the postal strike here in the UK made matters worse.

Last Saturday the disc finally arrived! It didn’t take long to upgrade the system from Leopard and the process was smooth and polished – almost zero user input required as we are used form Apple.

Now to the actual OS: Well, startup and shutdown is noticeably faster but to be honest I couldn’t see a massive speed up in the main system. To be fair, it was pretty speedy on Leopard already and I never had the feeling that it was sluggish; therefore it probably went  from pretty fast to very fast and maybe that difference isn’t so big.

Lets get to the improvements. These aren’t earth-shaking but many of them fall under the category “Oh, that’s nice”.

  • Working CD/DVD burning support: This was a major gripe of mine on Leopard. Disc writing would often fail and Disk Utility was awful for burning – Leopard’s “burn folders” never worked either. If that wasn’t such a basic thing I would say “Well Done” to Apple but this should have been way better a long time ago.
Snow Leopard disc burning

Snow Leopard disc burning

  • Expose is more useful. Instead of the old seemingly random positioning of the windows they now arrange themselves on a grid and the dock and the menu bar turn black which I like better. Also, if you click on an active dock icon and then hold you can see all the windows that the program has open at this point in time.
  • Dock stacks are scrollable. Again this really isn’t a killer feature.
  • Address Book can sync with GMail. I’m liking that.
  • Quick Time looks nicer. I’m not quite sure what has happened under the hood there but I like the new interface. Just get yourself Perian and it is almost as good as VLC.
  • Mail supports Exchange server. Even though I didn’t test this, as I use neither, this could come in handy. It also means that Exchange server is better supported out-of-the box on OS X than on Windows – go figure.

Apparently, there is all sorts of stuff happening with Grand Central and OpenCL but I rarely have an application that is heavy on either CPU or GPU so this doesn’t really matter to me but is nice to know. Almost all of my programs still worked fine under Snow Leopard; the only one where I had to use a beta version is Cyberduck.

There is one little gripe: I used to have a developer preview version of Safari 4 which allowed websites to be saved as a web application which then had it own dock icon (and presumably its own process). I assumed this feature was held back until Snow Leopard came out but I can’t seem to find it. Hmm.

Baseline

I like it particularly for the fact that it isn’t jam-packed with features that no-one needs but is a good solid exercise in improving the core of the system. I got this update for £10 and I think even £30 would be no-brainer for all Leopard users. I don’t think I would pay the full retail price (£129.00 for the box set with iLife) for it though.

No Comments

Converting transparent PNGs for use with LaTeX

August 31, 2009

I have recently discovered the power and ease of use of LaTeX and in my last report in included quite a few screenshots of the application I was writing. However OS X’s screenshot tool also saves the nice drop shadow that the UI produces around it’s windows.

LaTeX on the other hand, doesn’t seem to like those drop shadows as they contain transparency (also know as alpha channel) which after the conversion to PDF appears as ugly black borders. Fortunately there is an easy fix for this problem, which I found in a mailing list message.

You will need to have ImageMagick installed for this to work. ImageMagick’s convert command line tool allows you to convert the transparent part into proper colours rather than a grey layer with added transparency information (which is what throws the tex-to- PDF converter). Before you add those images to the tex file, convert them like this:

convert -background white -layers merge input.png output.png

1 Comment

Changes to submitting measurements to Edd

August 29, 2009

I have made some changes to how submitting measurements works in my spoke length calculator, Edd. Up until now submission was anonymous, which had the obvious drawbacks of verification and me being unable to ask for clarification if a submission was unclear.

From now on you will have to supply an email address if you want to submit measurements for a hub or rim, but please be assured that this data will be for my eyes only. I won’t spam you or won’t give your email address to anyone.

4 Comments

In praise of Netbeans (and how I learned to hate Eclipse)

August 10, 2009

The dark ages

I use a MacBook for my development and until recently I have done my Java development in Eclipse. It is a very popular IDE and apparently very flexible and can be made to do absolutely everything. Also, all the Google Java products plugins only worked for Eclipse and that was the main reason for me to not switch.

However, it shortcomings became more and more apparent during the time I wrote my dissertation and became more serious about Java development. I noticed the following things about the OS X variant of Eclipse.

  1. It. Is. Slow. It is painful to work with an IDE that locks up every 5 minutes and whenever you open a menu that has so far not been opened. I heard something about the Java-Carbon bindings not being very good and I think performance is better on Windows but this was a real issue for me.
  2. Usability is abysmal. There are millions of message areas that pop up all the time, buttons are tiny and appear and disappear seemingly randomly. I know software development is supposed to be difficult but this is just unbearably hostile to the user.
  3. It is very much a Windows app. It feels a lot like they wrote the app to fit with the windows UI guidelines, realised that there is Mac developers too and just copied the UI button by button. I know us Mac people are demanding when it comes to OS integration but Eclipse is simply not cutting it.

The conversion

One day I was frustrated enough by how rubbish Eclipse was, that I was ready to switch IDE mid-project. I kept saying to myself that I would give Netbeans a spin at the next project but my patience with Eclipse was wearing thin. I have not looked back.

The main issue I was worried about was the project conversion process and I have to tell you it was the most seamless thing I have ever experienced. Absolutely no errors or incompatibilities turned up. I was impressed. It also turned out that Netbeans is much better at managing your classpath automatically, so conversion even resolved a few issues. The only thing I had to redo was the tomcat configuration but that took about 30 mins.

I didn’t know Java development could be bearable on a Mac

After trying Netbeans out for a few days I have the following things about it.

  1. It has a nicer UI. I know this will not get me any cred with the emacs crowd but if an application doesn’t shout “I’M UGLY!” all day long I like using it better than one that does. Call me superficial.
    I also managed to install a dark editor skin which makes Netbeans resemble Textmate a bit more which has also earned some plus points with me.
  2. Usability is way better. Buttons are clear and there are fewer of them. Netbeans also gives you more sane default behaviour so you don’t have to poke around in the settings too often.
  3. Classpath and lib folder management works. When you put something in the lib folder, it will be put on the classpath and copied into WEB-INF/lib. I couldn’t get Eclipse to do that no matter how hard I tried. I had to enable this for every JAR I downloaded.
  4. It’s faster. Not Textmate-fast but fast enough to not annoy me constantly. I like.
  5. Mercurial works out of the box. I didn’t install a plugin, I never activated anything. Netbeans just knew I was using it and started marking files as edited. (I guess this works for other SCMs too.)
Netbeans screenshot with Textmate style dark theme

Netbeans screenshot with Textmate-style dark theme

In retrospect, switching to Netbeans has been a huge boost in terms of productivity for me. I’m wondering why I didn’t try it earlier.

7 Comments

How to set OpenDNS as the DNS provider for your Speedtouch router

July 18, 2009

Here is a really neat trick on how to speed up your home internet quickly and for free.

A little background

Basically every time a browser requests a page from an human-readable address like google.com, it first needs to find out the IP address for the server. This lookup takes some time and is provided by DNS servers. However, most ISP’s DNS servers are slow because no one asks “How fast is the DNS lookup?” when they shop around for broadband.

OpenDNS

OpenDNS is a free service that does faster DNS lookup than your broadband provider. They finance themselves by showing you a suggestions page when you mistype an address and this suggestions page has some ads on it. For me that isn’t obstructive at all.

How to do it

The Speedtouch routers don’t have a GUI for changing the DNS servers so you need to login via the command line program telnet into the router, which by default has the address 192.168.1.254 . If you have changed the IP address of your router you will need to use that instead.

telnet 192.168.1.254

The default user for Speedtouch routers is Administrator and the default password is just blank. Obviously if you changed these setting then you’ll have to enter those details.

Now you need to issue three commands which change the DNS settings. First delete the old DNS settings:

dns server route flush

Then add the two OpenDNS ones:

dns server route add dns=208.67.222.222 metric=1 intf=RoutedEthoA
dns server route add dns=208.67.220.220 metric=1 intf=RoutedEthoA

Now save everything with

saveall

That’s it. Try it out now by mistyping an address, if you can see the OpenDNS page then you’ve done it!

No Comments