Extensions : Identity Key Translators

Plugin

DataNucleus is developed as a plugin-driven framework and one of the components that is pluggable is translation of identities. When you call pm.getObjectById(cls, key) you pass in the key. This object can be the toString() form of an identity, or the key of a single-string form. Some store managers (e.g GAE/J) allow non-standard key input and this allows for the translation into a standardised key form. Alternatively you could do this in your own code, but the facility is provided. This means that in your application you only use your own form of identities.

You can extend DataNucleuss capabilities using the plugin extension org.datanucleus.identity_key_translator.

Interface

Any identifier factory plugin will need to implement org.datanucleus.store.IdentifierKeyFactory. Javadoc. So you need to implement the following interface

package org.datanucleus.identity;

public interface IdentityKeyTranslator
{
    /**
     * Method to translate the string into the identity.
     * @param ec ExecutionContext
     * @param cls The persistable class
     * @param key The input key
     * @return The returned key
     */
    Object getKey(ObjectManager om, Class cls, Object key);
}

Plugin Specification

When we have defined our IdentityKeyTranslator we just need to make it into a DataNucleus plugin. To do this you simply add a file plugin.xml to your JAR at the root. The file plugin.xml should look like this

<?xml version="1.0"?>
<plugin id="mydomain" name="DataNucleus plug-ins" provider-name="My Company">
    <extension point="org.datanucleus.identity_key_translator">
        <identitykeytranslator name="mytranslator" class-name="mydomain.MyIdKeyTranslator"/>
    </extension>
</plugin>

Note that you also require a MANIFEST.MF file as per the Extensions Guide.

Plugin Usage

The only thing remaining is to use your new IdentityStringTranslator plugin. You do this by having your plugin in the CLASSPATH at runtime, and setting the persistence property datanucleus.identityKeyTranslatorType to mytranslator (the name you specified in the plugin.xml file).