JPOX
JPOX
JPOX  |  Version 1.0  |  Version 1.1  |  Version 1.2  |  JDO 
1.1 | Preparation | Runtime | Tutorials and Examples | Developer
JPOX 1.1 Runtime
Runtime Tools
Queries
Plug-ins
RDBMS Datastores
Plugins : Value Generators

JPOX is developed as a plugin-driven framework and one of the components that is pluggable is the generation of identity or field values. JPOX provides a large selection of generators but is structured so that you can easily add your own variant and have it usable within your JPOX usage. This is available in JPOX from version 1.1.2

Let's use an example to demonstrate the steps necessary to do this. Firstly you need a class that generates the identity/values. This must implement org.jpox.store.poid.PoidGenerator (which itself extends javax.jdo.datastore.Sequence). So you need to implement the following interface

    String getName ();

    Object next ();

    void allocate (int additional);

    Object current ();

    long nextValue();

    long currentValue();

JPOX provides an abstract base class org.jpox.store.poid.AbstractPoidGenerator to extend if you don't require datastore access. If you do require (RDBMS) datastore access for your PoidGenerator then you can extend org.jpox.store.rdbms.poid.AbstractRDBMSPoidGenerator Let's give an example, here we want a generator that provides a form of UUID identity. We define our class as

package org.mydomain;

import org.jpox.store.poid.AbstractPoidGenerator;

/**
 * Example Value Generator for use with JPOX.
 */
public class MyUUIDPoidGenerator extends AbstractPoidGenerator
{
    public MyUUIDPoidGenerator(String name, Properties props)
    {
        super(name, props);
    }

    /**
     * Method to reserve "size" POIDs to the PoidBlock.
     * @param size The block size
     * @return The reserved block
     */
    public PoidBlock reserveBlock(long size)
    {
        Object[] ids = new Object[(int) size];
        for (int i = 0; i < size; i++)
        {
            ids[i] = getIdentifier();
        }
        return new PoidBlock(ids);
    }

    /**
     * Create a UUID identifier.
     * @return The identifier
     */
    private String getIdentifier()
    {
        ... Write this method to generate the identifier
    }
}

In the constructor you see that you are passed some properties. JPOX will pass in the following properties always so you can use them in your generator.

  • class-name Name of the class that the value is being added to
  • root-class-name Name of the root class in this inheritance tree
  • field-name Name of the field whose value is being set (not provided if this is datastore identity field)
  • catalog-name Catalog that objects of the class are stored in
  • schema-name Schema that objects of the class are stored in
  • table-name Name of the (root) table storing this field
  • column-name Name of the column storing this field
  • sequence-name Name of the sequence (if specified in the MetaData)

So we now have our custom "value generator" and we just need to make this into a JPOX plugin. To do this you simply add a file plugin.xml to your JAR either at the root, or in META-INF/plugins. The file plugin.xml should look like this

<?xml version="1.0"?>
<plugin id="org.jpox" name="JPOX plug-ins" provider-name="JPOX">
    <extension point="org.jpox.store.valuegenerator">
        <valuegenerator name="myuuid" class-name="org.mydomain.MyUUIDPoidGenerator" unique="true"/>
    </extension>
</plugin>

The name "myuuid" is what you will use as the "strategy" when specifying to use it in MetaData. The flag "unique" is only needed if your generator is to be unique across all requests. For example if your generator was only unique for a particular class then you should omit that part. Thats all. You now have a JPOX "value generator" plugin. To use it you would reference it in your JDO MetaData

<class name="MyClass">
    <datastore-identity strategy="myuuid"/>
    ...
</class>

Don't forget that if you write a value generator that could be of value to others you could easily donate it to JPOX for inclusion in the next release.