JPOX
JPOX
 Project  |  Ver 1.1  |  Ver 1.2  |  JDO  |  JPA  |  Guides  |  Tools
1.1 | Preparation | O/R Mapping | Runtime | Extensions | Developer
Extensions
Plugins : Class Enhancer

JPOX is developed as a plugin-driven framework and one of the components that is pluggable is the enhancer of classes. This is available in JPOX from version 1.1.2

Interface

Any class enhancer plugin will need to implement org.jpox.enhancer.ClassEnhancer. So you need to implement the following interface

package org.jpox.enhancer;

public interface ClassEnhancer
{
    /**
     * Check whether the class is enhanced.
     * @return Return true if already enhanced class.
     */
    boolean checkEnhanced();

    /**
     * Method to verify the enhancement state.
     * @throws Exception Thrown if an error occurs while verifying
     **/
    void verify() throws Exception;

    /**
     * Method to enhance the class definition internally (but not write it).
     */
    void enhance();

    /**
     * Enhance the class, overwriting the existing class location.
     * @throws IOException If an I/O error occurs in the write.
     */
    void update() throws IOException;

    /**
     * Enhance the class writing to the specified location.
     * @param dir Directory into which we write the new class definition
     * @throws IOException If an I/O error occurs in the write
     */
    void store(String dir) throws IOException;

    /**
     * Access the class in byte array format
     * @return the class in byte array format
     */
    byte[] getBytes();

    /**
     * Accessor for the ClassLoaderResolver in use.
     * @return ClassLoader resolver
     */
    ClassLoaderResolver getClassLoaderResolver();
}

Be aware that you can extend org.jpox.enhancer.AbstractClassEnhancer.

Plugin Specification

When we have defined our "ClassEnhancer" we just need to make it 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.enhancer.enhancer">
        <identifierfactory name="myEnhancer" class-name="mydomain.MyClassEnhancer"/>
    </extension>
</plugin>
Plugin Usage

The only thing remaining is to use your new ClassEnhancer plugin. You do this by having your plugin in the CLASSPATH at runtime, and passing the argument enhancerName with value myEnhancer (the name you specified in the plugin.xml file) to the JPOX Enhancer.