# Generating GUIDs in WebObjects

I've been working on some code to generate GUIDs as part of the suite of services for use in the next version of the software that runs CAREO.

When in doubt, hit Google to see who's already solved it. Turns out David Newmann did a GUID generator as part of his security stuff, and it's been rolled into the WOExampleHarness.framework provided at /Developer/Examples/JavaWebObjects/Frameworks/WOExamplesHarness (and specifically in the WXUtilties.java class).

In his sample code, he uses a substring of the generated GUID (the first 10 characters). This kept generating the same GUID for me (going against the whole idea of using a GUID), so I told it to use the entire string, rather than the first 10 characters.

It turns out that I actually need the first 23 characters in order to be unique to a single machine. It also appends the machine name (domain name) and IP address so the entire string spits back something guaranteed to be globally unique.

Anyway, the code to generate a GUID and return the portion without the machine name etc... is (assuming you've built the WOExampleHarness.framework project):

com.webobjects.examples.utilities.WXUtilities.generateGUID().substring(0, 23)

which will return something like this:

3b9a5f:f642c68f4b:-7fff

Or, to get a truly, really, absolutely GUID, drop the substring() portion, which will return something like this:

3b9a5f:f642c68f4b:-8000|pc063-110.commons.ucalgary.ca/136.159.110.63

Here's the javadoc comment from David's generateGUID() method:

    /**
* Returns an global ID string that is unique with respect to the
* host on which it is generated.  This GUID is unique under the
* following conditions: a) the machine takes more than one second
* to reboot, and b) the machine's clock is never set backward.
* The method uses the java.rmi.UID class to create a unique number
* in a name space limited to your machine and then combines this
*/