Saturday, August 14, 2010

Property developers threaten globally important seed bank

This could be a very bad sign for our future as a species when even the highest authorities in a major country (in this case, Russia), with all the information we currently have on climate change and decreasing global bio-diversity, to put the interests of a property development company (the-scientist.com) above those of a globally significant seed bank outside St Petersburg (BBC).

(Summary: the Pavlovsk experimental station is a seed bank of thousands of unique plant varieties that grows on a 70 hectare plot outside the city of St Petersburg, and Russia's Supreme Arbitration Court has given rights to build on the land to the Russian Housing Development Foundation, which will destroy nearly 100 years of work to preserve this globally significant resource to build houses)

Pass this on, and pass it up ... to your MP, your senator, your governor, to whatever representative you have available at a political level that can apply pressure to the St Petersburg authorities to save the seed bank.

Wednesday, August 11, 2010

Scottish Crannog Centre

A crannog is a round-house on an artificial island constructed on wooden pilings or stone in a loch or lake. They are almost exclusive to Scotland and Ireland, where there are significant networks of interconnected waterways. There is only one known in England. European lake dwellings tend to be multiple buildings on square platforms.

Last week, I visited the Scottish Crannog Centre at Kenmore (Loch Tay), near Pitlochry, with my family (wife and two teenage daughters), and I would thoroughly recommend the place to anyone with an interest in pre-Roman civilisation in the Celtic parts of Europe. The reconstructed crannog at the Centre is based on the Oakbank Crannog which is just along the loch-side from the Centre. Oakbank was occupied for around 200 years around 2,500 years ago (over 500 years before the Romans darkened British doors). Crannogs were in use from around that time right up until the 16th or 17th Century A.D.!

There's a good exhibition of materials about the distribution of crannogs around Scotland, and about the relatively new field of underwater archaeology. This is followed by a guided visit to the reconstructed 10m diameter crannog on the lake (reached via a wooden walkway). I would have liked to have been able to spend more time on the crannog itself, but the next group of visitors was waiting with their guide. Once back on the lake-side, our guide gave us demonstrations of some of the technologies and crafts available to the crannog-dwellers. (Including hypothesized lathes, techniques for making holes in stones, drop-spinning, the use of a saddle quern for grinding wheat, and fire-starting) - we could have a go at all of these too.

Being as this is a tourist site, there is a shop - but it's a good one, with no kilt towels nor the excessive bagpipe music you usually find in Scottish tourist-trap shops.

The staff were excellent - knowledgeable, engaging, and genuinely interested in talking to the punters. Our excellent guide was Rachel, and we also chatted with Dirk for a while after our tour.

The roads around this part of Scotland are a bit narrow and twisty - even compared to home on the Isle of Man, so be aware it takes time to get places. However, it is well worth the effort of going to the Scottish Crannog Centre. It's open 10am - 5.30pm between April and October (off-season visits by appointment).

We intend to return in a couple of years to see what else they've discovered and developed on the site. Yes, even my teenage daughters agree.

Update: here are the photos.

Friday, July 16, 2010

Bionic Legs now an achievable reality

My great uncle Ken was an inventor. Not one you've heard of, like Edison or Dyson, but one of the little guys who invents practical aids for people with disabilities. Unfortunately he died suddenly on the operating table in 2005. That was a shock and a shame at the time, for all the obvious reasons.

Today I discovered something I'm sure uncle Ken would have been very impressed to see: http://www.gizmag.com/rex-robotic-exoskeleton/15736/. And there's a video of the device in use too:

Robert Irving and Richard Little deserve every success with this device. It may not be fast, but it is clearly a device that has enormous potential, and I'm sure REX 2, when they build it, will be stronger, faster and lighter.

Well done guys.

Wednesday, July 07, 2010

How good a photographer are you really?

For my 40th birthday last October, I received sufficient cash to finally indulge one of my minor ambitions of the last 10 years: to go from film SLR to digital SLR photography. So I bought myself a cheap Sony dSLR, an α200K. Then at Christmas, I added a Sigma 70-300 lens. Nothing showy or pricey, just enough to learn the ropes. I already had my tripod so I don't count that as new expenditure, but even so, I've spent over £400 on kit!

I joined flickr.com, as you can see from the left side-bar. I even paid for a 'pro' account (primarily so that I can upload my photos fullsize), and I don't regret it.

Then I found the Sony α group and started reading their forum. Guess what, about 60% of the posts appear to be 'what new kit should I buy to shoot X' (where X is some broad category of work, like sports, or nature, or sunsets), and most of the answers are: spend £hundreds (at least) to get some specialist lens, filter system, tripod, or even a new camera body, like the α900, at around £2,000 for the body alone - no lens!

Now comes the complete antidote:
Firstly, of course, they're using amazing pro lights (most of the time - except for two $50 floods from Lowes), a studio space, a great model and so on, but at the center of it all is an iPhone 3GS, and an excellent photographer.

You really, really, really don't need all that fancy kit the 'pros' and the shops tell you (sounds like the audio-extremophile stuff I was writing about a few years ago). Yes, you need great conditions, and in a studio, that means loads of light, but it seems that the old adage: "cameras don't take photos, people do" is as accurate as ever, if not more so.

Sunday, May 23, 2010

Sense About Science

Bear with me... I'll get to the title of this posting in a moment.

I was reading my RSS feeds on Bloglines.com as I do every day and I came upon feed item that pointed me to a Slashdot.org posting about the death of Martin Gardner (a great writer about science and maths - I had one of his excellent books when I was 16, but seem to have lost it now). This lead me, as many interesting things do, to James Randi's post on the same subject - they were close friends apparently. And finally we get to the subject of this post: there was a small link at the bottom of James Randi's blog for a site called 'Sense About Science'.


I must admit, I'd never heard of these folks before, but just had to link to them from here because I completely agree that scientific debate must be free of 'he-said, she-said' argument and the chilling effect of the threat of libel action being taken when two scientists disagree. Just think what would have happened if British Chiropractic Association had won their case for libel against Simon Singh (what a coincidence: the Sense about Science guys were also supporting Simon Singh). Would you want to try to de-bunk bad science if the supporters of the bunkum could silence you with a libel suit? Can you afford to get caught out like that? Of course, this works both ways. If we want to be able to de-bunk poor science, we have to allow what we think of as poor scientists to question the science we judge good. However, the key difference between good and bad science is that good science is supported by independently, objectively reproducible proof, where poor science tries to hide behind waffle and important sounding names.

So there you have it, a trail of internet wanderings: from RSS feeds to a rant about the importance of free science. And there ends the lesson for today. ;-)

Friday, March 26, 2010

Moving (backwards) with The Times

The Times and the Sunday Times are about to make a seriously retrograde step. They are about to throw up a paywall around their news services. Now the BBC report only says 'charging to access their websites', so there's no direct indication whether News International intend all their content to be behind the wall, or just the articles (as opposed to the headlines), but either way, this is an idiotic decision.

If, and boy is that a big if these days, the Times and the Sunday Times were the only sources of (reasonably) trustworthy news (and thereby hangs a much more complex tale!) on the internet, then moving their content behind a paywall might entice people to spend money getting access to that news, but the reality is significantly different from that. I can state, with a pretty high level of confidence, that I've not needed to go to the Time nor the Sunday Times in many years. I've not wanted to go there either, for just as long. In fact, I don't think I've even bothered going there in at least 12 months, and even if I did, it was only because I found the news somewhere else, and happened to click through to their sites. (So much for the 'aggregators are stealing our content' argument - I'd never have gone to their sites in the first place if it weren't for said aggregators!)

Now, I'm not suggesting that my pattern of usage (or lack thereof) of the News International sites is normal, but I bet there are many, many, many thousands of regular users of the internet in the British Isles that somehow manage to avoid using the Times and the Sunday Times with very little effort, and very little loss to their understanding of the world. In fact, given the nature of some of the other titles in the NI stable (the Sun and the News of the World - two rags that push a particularly atrocious example of the UK equivalent of a red-neck world view), I'm sure many people are better off getting their news elsewhere.

There are so many free alternatives to NI sites, that they are simply wasting their time.

Goodbye, the Times and the Sunday Times, you won't be missed.

Friday, January 29, 2010

Freedom to mis-represent the truth?

If ever we needed proof of the scant regard for the truth as exhibited by British newspapers, here is an excellent example from that bastion of press honour, The Times.

Chew on that, Mr Murdoch!

Monday, January 25, 2010

Are you going to the wedding?

I don't normally do this, but I though this video was so excellent I had to include it:

Saturday, January 23, 2010

Hopefully this is a better looking stitching together of the previous panorama. However, it's quite dark at the left end, and a a bit over-exposed at the right end as a result of trying to even out the colour throughout.

Douglas Bay, Isle of Man

Saturday, January 02, 2010

Panorama of Douglas Bay

This is a panorama of Douglas Bay, Isle of Man. I took it from the end of the Victoria Pier, and the Tower of Refuge just nicely fell into the center of the image.

The original image is some 24,800 by 2,300 pixels, but neither blogger nor flickr seem to be happy with such an enormous image, even though it's only 8MB. Oh well.

Update: Thanks to a good friend, you can now look at the real photo, by clicking on the preview above.

Update 2: My friend's site appears to have been terminated, here's a link to the same shot at Flickr.

Wednesday, December 30, 2009

Security Theatre

Before I get started, this post is likely to come across as a bit of a rant. If you don't want to read my half-formed, lefty crap, at least read the article by Bruce Schneier on CNN that inspired it. It's much better written, and far more important that it gets wide coverage than my version. ;-)

Having said that, if you're still with me, here goes: wasting money bugs me, especially when it's a government that's doing it for the purposes of appeasing the 'voice of the people' (see the end of this for more on that meme!). When governments spend enormous sums of money on visible security activity at airports this gets me particularly riled because it's pointless. I quote from Bruce Schneier's opinion article on CNN:
Often, this "something" is directly related to the details of a recent event. We confiscate liquids, screen shoes, and ban box cutters on airplanes. We tell people they can't use an airplane restroom in the last 90 minutes of an international flight. But it's not the target and tactics of the last attack that are important, but the next attack. These measures are only effective if we happen to guess what the next terrorists are planning.
If we spend billions defending our rail systems, and the terrorists bomb a shopping mall instead, we've wasted our money. If we concentrate airport security on screening shoes and confiscating liquids, and the terrorists hide explosives in their brassieres and use solids, we've wasted our money. Terrorists don't care what they blow up and it shouldn't be our goal merely to force the terrorists to make a minor change in their tactics or targets.
Our current response to terrorism is a form of "magical thinking." It relies on the idea that we can somehow make ourselves safer by protecting against what the terrorists happened to do last time.
Of course, the result of this 'magical thinking' is also the terrible changes that are taking place to the due process of criminal proceedings at law. The introduction of arbitrary periods of detention without charge, the holding of terrorism-related trials in camera, the rhetoric of hatred of any other political or cultural outlook than our own... this is dangerous stuff that just serves to increase the threat from those opposed to our way of life. Back to Bruce:
Despite fearful rhetoric to the contrary, terrorism is not a transcendent threat. A terrorist attack cannot possibly destroy a country's way of life; it's only our reaction to that attack that can do that kind of damage. The more we undermine our own laws, the more we convert our buildings into fortresses, the more we reduce the freedoms and liberties at the foundation of our societies, the more we're doing the terrorists' job for them.
So, when you're thinking about who to vote for in future (in the UK general election, for example), why not challenge them to stop the damage that's being done to our relatively free way of life, pull back from culturally aggressive stance in international politics, and stop stoking the flames of the conflicts that spawn terrorist acts.

p.s. on the 'voice of the people', it appears the ‘newspapers’ are being deemed to be the ‘voice of the people’ in that mad country, the (dis-)United Kingdom, so a populist, theatrical, approach to security is the inevitable outcome. Of course, it is the broadcast & newsprint media that treat said ‘newspapers’ as ‘the voice’, in addition to their weak lackeys in Westminster. It’s a vicious circle of bullying and subservience maintained by those with the loudest voices, the deepest pockets and the least integrity.

Monday, December 28, 2009

Camera Update

Christmas gifts have come, and I now have a Sigma 70-300mm zoom lens for my camera. I've tried to photograph the moon with it, but it's amazing how difficult a subject it is. It took 16 shots before I even got something remotely better that a white blob in a black sky. The best I could do in 24 shots is in my photostream, but believe me, I'll be trying that again once I get my cable release!

I also tried to shoot the constellation of Orion - a nice easy constellation to recognise in our northern night sky. Last time I tried, I accidentally left my camera in noise reduction mode, which gave photos that consist of a dozen seriously blurred stars. This time I tried without NR... and instead got a dozen faint blobs with trails that indicated the camera had moved somehow during the 6-8 second exposures. Not quite sure how this happened, but all I can put it down to is that a) my house is old and I was on the top floor, where the floor-boards are not the smoothest and most solid in the world, b) I was manually pressing the button, and c) maybe I breathed on the camera on it's tripod just a little too loudly? So, next time, I'll go to our back yard, so the tripod will be on brick, and I'll use my new cable release... expected from Amazon any day now.

Learning, learning, learning...

Sunday, December 27, 2009

Copenhagen Summit on Climate Change

It's taken me a while to figure out my reaction to the train-wreck that was the Copenhagen Summit on Climate Change, but what finally got me to the point where I can write something was this story (via Chris Samuel's blog in Oz) about China being the force behind the failure of Copenhagen.

Now I'm caught between "I can't believe China could act so terribly" and "Huh, now they've found their scapegoat." This does make we wonder why the 'western powers' didn't make it clearer at the time that China was the source of the problems and either shame them into proper action (a strategy unlikely to succeed due to the lack of the need for the Chinese leadership to respond to any kind of democratic process), or simply to exclude China and set the world on a path to economic exclusion of the country too - I'm sure there are other countries that would be very willing to take up the manufacturer industries currently supplied by China.

There is an interesting kicker in Chris's blog post, and that's from his wife - Donna, who commented on the post thus:
If China railroaded Copenhagen then isn’t it time those of us in Western nations stopped filling our Christmas stockings with cheap Chinese goodies whilst ignoring its those very actions that drive China to want to exhaust its ‘fair share’ of the ‘Industrial Revolution’. We may not have power over our leaders or Copenhagen, but Western buyers do have pocket power without which they can let China know what they think about ethics. Think about that next time you reach for your credit card for that latest gadget. Which regime are you supporting.
I guess this is the small-scale, personal approach to implementing the strategy I suggested above.

Monday, November 23, 2009

Photos

Recently I replaced my aging Canon completely manual SLR with a whizzy new digital SLR. Okay, so it's only a Sony alpha 200K, but it's new to me and fun not having to consider how many shots I can take before I run out of film.

I went for a wander this afternoon - first real opportunity since getting the camera with decent weather, and the best of the results are here.

Tuesday, September 01, 2009

Obligatory 9/11 post

There's been a lot of talk of gross government sponsored conspiracy theories surrounding the events of 9/11, and I'm still to be convinced that any government is truly that competent (why assume a massive, all-encompasing, conspiracy when there are much simpler explanations - the principle of Occam's Razor).

However, this article was sent to me by my Dad, who is rather more convinced of the sheer evil of the US government under George Bush (jnr).

I'm waiting with interest to see if the report is taken up by any more news sites than just a local California newsletter, but if taken at face value, this does seem a reasonably un-rabid, professional, and worrying report. (Not so, many of the readers' comments at the bottom of the article!)

Of course, if other media sites ignore this, it could be that they simply don't give the organisation credence, but conspiracy theorists will say that the report will be suppressed by the uber-powerful US government, and they'll tell the global media to ignore it too, and the global media will obey them... for some obscure reason. Pretty much a no-win situation there then. ;-)

Monday, August 31, 2009

Alan Turing Petition

If you're reading my blog, you're probably the kind of person who knows exactly who Alan Turing was, not to mention the Turing Test (which I also mentioned in my previous post). However, you may not know that he was convicted for gross indecency (in admitting to an actively gay relationship) in 1952, which led to him being chemically castrated, his loss of security privileges, and his loss of work with GCHQ (wiki), and thence (it is thought) to his suicide in January 1954.

Why do I bring this up today? Well, enough is enough, this man deserves a better ending than this. He deserves a full apology from the UK government for their treatment (legally, morally and chemically), and I'd like to see him acknowledged as one of the greatest minds ever to come out of UK science and mathematics. Every computing professional in the country owes this man a debt of gratitude for his work in establishing the science of computing, as does, indeed, the rest of the country for his code-breaking at Bletchley. (though, obviously, he was not alone in either field by any stretch of the imagination!)

There's an electronic petition being lodged with the UK government, and here's the BBC article that brought it to my attention.

Saturday, August 08, 2009

The Monomyth vs. the Automyth

I subscribe to the GameSetWatch RSS feed, and generally flick through their headlines on Bloglines (an RSS aggregator that I thoroughly endorse, especially the beta version) in a spare moment or two each day. Most of the content leaves me quite cold, but this article actually caught my attention!

I've read around half of Joseph Campbell's book, The Hero with a Thousand Faces, but got bored and bogged down in his continual obsession with his Speilbergian father-son conflict as the driving force of all narrative. (Or is that perhaps, Speilberg's obsession in his movies with Campbellian father-son narratives?) His 1940's writing style doesn't help, but then Christopher Booker's 2005 re-tread and elaboration of the material, The Seven Basic Plots (or here for the US), suffers from the same obsessions, is nearly twice as long, and could have done with some serious editing down to reduce redundant repetition. (Mind you, I did get 80% of the way through this one)

So, I've not read every last word on the subject of the monomyth, or the seven basic plots, but I believe I have an amateur understanding of them. (Yes, go on, my opinions are incomplete and therefore entirely invalid... feel free to go read someone else's blatherings instead).

(note to the reader that chooses to continue, where I use a male pronoun, please feel free to substitute a female one if it helps you)

Quick summary: hero in the 'normal' world is challenged to go beyond the boundaries, overcome 'evil' (the father) in some way, returning changed to the 'normal'. There are, of course, many more details to monomyth, and the seven basic plots (quest, rags-to-riches, overcoming the monster, voyage & return, comedy (ending in love or life), tragedy (ending in some form of death), and rebirth) have more subtlety than that, and the article at GameSetWatch gives a more complete summary of the basics.

However, they mostly miss the point that elements of good story telling also involve subverting the monomyth, or the expectations of the basic form of the plot chosen (consciously or otherwise). For example, the hero might return from the experiences unchanged in himself, but the 'normal' world might instead have been changed as a result of his actions.

Anyhow, the point of that article is to ask why, if the monomyth is so well explored by Campbell and his acolytes, has no-one actually built an 'automyth' - a monomyth generator. This is the intriguing bit. It's all very well stating that there's an algorithm for a story, and even that one can subvert (invert?) or skip one or more, or most, of the 17 steps in the algorithm, but without a detailed language to express a world model, or two (possibly more) in which to run that algorithm, automation is just a dream.

How does one tackle filling in the steps in the automyth algorithm? Perhaps by giving the machine a library of options:

  • people: farm boy, new advertising executive, grizzled mountain man, washed out detective
  • places: Alabama USA, the Shire (or an analogue of), a moon-base, a small shop in London
  • challenges: alien invasion, disease, death of a parent, financial ruin (personal or global)
  • monsters: the boss, an out-of-control robot, the Devil, the judge(s)
  • assistants: the girlfriend, the grand-parent, the old mage, the bestest buddy, the advocate
  • ... and so on
But does this actually achieve anything? Can we build a grammar with which to join these elements? Do we need to?

I think what we're looking at here is a new expression of the Turing Test (a test where one person cannot distinguish which one of two subjects is human, and which one a computer, by means of asking questions). And if we define a world, or set of worlds, within which the automyth machine will operate, we have a limited Turing Test (the questions are limited to a specific subject).

At which point, can we talk about applying this to ludonarrative generation? Certainly we could build a quest generator (a la Diablo II), but that's only a subset of the challenge. Can we really express enough full plots. I think not, at least, not until we have a way to describe the entire world to a computer, give it a way of imagining alternatives, then (and this is the really difficult bit) giving that computer a way to judge which of those alternatives could actually hang together as an alternative. This is such a big job, I think I'm off to do the laundry instead.

Thursday, August 06, 2009

VirtualBox on Windows Hosts

A friend of mine has just written up his comments on installing Ubuntu as a virtual machine on a Windows host using VirtualBox 3.0.0 (the latest version at the time of writing, 3.0.4 has been released now). It's clear from his instructions that this is a fairly simple process, with the possible exception of a gotcha around networking. However, I did notice one other issue with his instructions that will probably come back to haunt him in some six months time or so... and when it happens it will be hard to find the cause.

As a side note, I'm also using VirtualBox, but on an Ubuntu host, to enable me to have a Windows XP running occassionally whilst I use Ubuntu for most work, even though I am writing this whilst running Vista (dual boot on the same machine).

Andy, you might want to be careful about using a dynamically expanding disc file for your root file system. NTFS is notorious for fragmenting files as they grow - especially if your file is living on a partition with other activity. And as we all know, the more fragmented a file gets, the poorer the performance becomes as the disc heads have to seek across more and more diverse areas of the disc to get to the content of the file. Where you have a virtual file system in a file, this is only going to get worse, because the file system you have inside the file will have no knowledge of how the data is distributed over the real disc, so it can't make any decisions over data distribution within the file system that have any meaning on the disc platters.

To avoid this, you need to be able to set a specific size for the virtual disc file. However, this will require a little pre-set-up work. First you need to decide on what is a sensible, reasonably future-proof size for your root partition. I notice that the VirtualBox installer chose 8.6GB ... that sounds quite reasonable to me, though I'd be tempted to go for 10-12GB myself. Next, you need to defragment your chosen disk partition until there is a region of free space at least as large as your desired file, and as few other gaps as possible (in order to give NTFS as little choice over where it puts the file as possible).

Now you can click the VirtualBox "New" button, to create your virtual machine.

This same issue affects the Windows page files, by the way.

Thursday, July 16, 2009

Logging out with HTTP Basic Authentication

This post arose from the fact that I have an application running on an application server that doesn't seem to support HTTP BASIC authentication very well (Glassfish v2, if you're interested). So I have a web-server sat in front of the application server, doing the authentication, and passing through all authenticated requests to the application server behind. (This configuration also works around a bug in Glassfish v2, where the HTTP response size is wrongly calculated on static resources, which confuses Seamonkey, although Firefox copes ok)

There is a downside to this arrangement, and that is that HTTP BASIC authentication doesn't really support the concept of logging out!

As normal with a Java web application, the logout operation is a matter of invalidating the current HttpSession, throwing away any cached user configuration, and redirecting the user to a 'you are logged out' page. This can be achieved by having a filter or a servlet pick up the 'logout' request, mark the HttpSession associated with the current HttpRequest object, and deleting the JSESSION cookie that identified the session. The response then just needs to contain a logout message, or redirect, or whatever you desire for your site.

That's all well and good if it's just the web application involved. However, in our scenario, we have web server to consider too. As it stands, there's nothing in the HyperText Transfer Protocol ('HTTP' to you and me) that allows the web application to tell the web server that the HttpSession is over - the application doesn't talk to the server, so (according to almost all the web sites I could find with Google) there's no way to tell the server to invalidate it's knowledge of the end user and their credentials.

Or is there....

The key factor is that it is possible to make the server re-challenge the user for their credentials in the same way as they were when they started using the application, and this can be made to have the same effect as logging them out. Note that the critical fact that previous web-pages on this subject seem to have missed is that HTTP BASIC authentication has a realm parameter.

By knowing what the realm is that the web-server used to the authenticate the user, we can cause the browser to re-authenticate against that same realm, by challenging the browser with an HTTP 401 (just like the web-server does).

The process (in my application) works like this:

  1. The user clicks the logout link to go to /logout.html, which tells the user they are being logged out. This is a nice-to-have page to make this process a little more friendly, you could skip it and go straight to the next step
  2. The browser pauses for 1 second on this page, then redirects to /logout
  3. This url is mapped to a logout filter, which does the normal session termination activities I mentioned above. But in order for the next step to work, the filter also registers the current HttpSession key with the LoggedOutServlet (storing it in a singleton HashSet of keys), and creates a has-logged-out cookie with the HttpSession key as the value of the cookie.
  4. The logoutFilter then redirects to /loggedout which has been mapped to the LoggedOutServlet in my web.xml:
  5. <servlet>
      <description>Logout Servlet</description>
      <display-name>LoggedOutServlet</display-name>
      <servlet-name>loggedout</servlet-name>
      <servlet-class>
        com.xyzzy.web.LoggedOutServlet
      </servlet-class>
    </servlet>
    
    <servlet-mapping>
      <servlet-name>loggedout</servlet-name>
      <url-pattern>/loggedout</url-pattern>
    </servlet-mapping>
    

  6. The LoggedOutServlet looks for the has-logged-out cookie. If cookie exists, and the value is in the servlet's register of logging-out keys, then the key is removed from the register, the cookie marked as deleted, and an Unauthorised (HTTP 401) response is returned to the user:
    public void setupResponseForLogout(HttpServletResponse response) {
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); // HTTP 401
        response.setHeader("WWW-Authenticate", "Basic realm=\"xyzzy\"");
    }
    

  7. The user is then prompted by the browser to re-authenticate. The critical thing here is that the realm (set to 'xyzzy' above) is set correctly in the response. If this is done, then the web-server (which must be authenticating with the same realm) will correctly re-authenticate the user if they try to login again. So the consequence is that the user is effectively logged out, and cannot get back into the application without being challenged for their credentials again. This works because the browser has been asked to authenticate against a specific realm, so that will be used in the authentication process, and will force the web server to full re-check the user's credentials - refusing access if they get it wrong.
NB. all the urls used in this process must be accessible without logging in (i.e. anonymously).

Wednesday, July 01, 2009

TreeSet and implementing the Comparator interface

Time for a bit more technical content. You might begin to discern a pattern here: gotchas in equals(), hashcode() and Java Collections.

My experience of Collections thus far has taught me that most of them are built upon detecting object equality through the equals() and hashcode() implementation in your class. See my earlier post and the posts of Andy Beacock referenced therein. However, today I ran into problems with objects vanishing in a TreeSet.

Back to the requirements first. I have list of Bank Account objects I wish to show on a web-form, but that list should also be modifiable by the user with an Add Form, and a Remove button on each listed account. So far so good. One more thing, the list has got to be in account name order on display. For this example, a Bank Account consists of a pseudo-primary key (id), a Bank Name, an Account Name, an Account Number, and a Sort Code. The id is not shown, and the records are unique by Account Number and Sort Code (and therefore implicitly by Bank Name).

Obviously, Bank Names should should not really be store in a Bank Account record - they should be stored separately and referenced by foreign key, as indeed, should Sort Codes - in fact, in an ideal world, the Bank Account record would be an id, a name, an account number and a foreign key to a branch as identified by the sort code. However, you know as well as I do that this is not an ideal world, and most of the time, we have to deal with the world as we come to it.

So, back to my slightly contrived example. I'm going to ignore completely all the contextual information about display technology, form interaction, transactions, etc., and concentrate on the core issue: SortedSets are nasty wee buggers!

There are two ways to manage the display of sorted data: sort on insert, and sort on display. If you're displaying often, and inserting & deleting infrequently (as in this example), sort on insert is preferable, otherwise you spend a lot of time sorting your data again and again with no change to the data.

Having looked at the implementations of the SortedSet interface, we really only have one available in the Java SDK (I'm staying away from the Apache Collections for now), and that is TreeSet, for which you specify a Comparator at construction time. Naively, I assumed that my comparator would just be interested in the Account Name - that's the field by which I wish to sort, after all.

    @Override
    public int compare(BankAccount o1, BankAccount o2) {
        return o1.getAccountName().compareTo(o2.getAccountName());
    }


(I'm leaving out the normal defensive programming and exception handling for clarity)

However, my BankAccount class implements equals() and hashcode() as normal, with the sortcode and the account number - these being the 'business key' if you like. So, on this basis, I expected TreeSet to sort on name, and use equals() and/or hashcode() to determine whether the Set contains a given BankAccount on insertion - in the same way that HashSet would.

Ooops, no.

Turns out that TreeSet doesn't use equals() or hashcode() at all. It uses the Comparator for sorting and contains() checking. So now, my Comparator implementation looks like this:

@Override
public int compare(BankAccount o1, BankAccount o2) {
   int result = o1.getAccountName().compareTo(o2.getAccountName());
   if (result == 0) {
       result = o1.getSortCode().compareTo(o2.getSortCode());
   }
   if (result == 0) {
       result = o1.getAccountNumber().compareTo(o2.getAccountNumber());
   }
   return result;
}

Now I have data sorted on insert and display, and the data doesn't get lost if someone enters two accounts with the same Account Name.

In conclusion, I think that the only reason this gotcha got me was that I was motivated to use the TreeSet simply by the requirement to sort by name, so I wrote the Comparator with only that in mind. A correct implementation of really should include the business keys of the entity under consideration... of course, I'd just done that, I might have had even more trouble with the sort-by-name requirement (i.e. a non-key property).