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

JPOX is developed as a plugin-driven framework and one of the components that is pluggable is the Level 2 caching of objects (between PersistenceManagers for the same PersistenceManagerFactory). JPOX provides a large selection of Level 2 caches (builtin, Tangosol, EHCache, OSCache, SwarmCache) 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

The following sections describe how to create your own Level 2 cache plugin for JPOX.

Interface

If you have your own Level2 cache you can easily use it with JPOX. JPOX defines a Level2Cache interface and you need to implement this.

package javax.jdo.datastore;
public interface DataStoreCache
{
    void evict (Object oid);

    void evictAll ();

    void evictAll (Object[] oids);

    void evictAll (Collection oids);

    void evictAll (Class pcClass, boolean subclasses);

    void pin (Object oid);

    void pinAll (Collection oids);

    void pinAll (Object[] oids);

    void pinAll (Class pcClass, boolean subclasses);

    void unpin(Object oid);

    void unpinAll(Collection oids);

    void unpinAll(Object[] oids);

    void unpinAll(Class pcClass, boolean subclasses);
}

package org.jpox.cache;
public interface Level2Cache extends javax.jdo.datastore.DataStoreCache
{
    int getNumberOfPinnedObjects();

    int getNumberOfUnpinnedObjects();

    int getSize();

    CachedPC get(Object oid);

    CachedPC put(Object oid, CachedPC pc);

    boolean isEmpty();

    void clear();
}


Implementation

Let's suppose your want to implement your own Level 2 cache MyLevel2Cache

package mydomain;

import org.jpox.cache.Level2Cache;

public class MyLevel2Cache implements Level2Cache
{
    /**
     * Constructor.
     * @param props Any properties to control the cache
     */
    public DefaultLevel2Cache(Properties props)
    {
        ...
    }

    ... (implement the interface)
}
Plugin Specification

Once you have this implementation you then need to make the class available as a JPOX plugin. You do this by putting a file plugin.xml in your JAR either at the root of the CLASSPATH or at META-INF/plugins/. The file plugin.xml will look like this

<?xml version="1.0"?>
<plugin id="mydomain" name="JPOX plug-ins" provider-name="My Company">
    <extension point="org.jpox.cache_level2">
	    <cache name="MyCache" class-name="mydomain.MyLevel2Cache"/>
    </extension>
</plugin>
Plugin Usage

The only thing remaining is to use your L2 Cache plugin. To do this you specify the PersistenceManagerFactory property org.jpox.cache.level2.type as MyCache (the "name" in plugin.xml). In JPOX 1.1.2 the "extension point" was "org.jpox.cache", but in 1.1.3 and later it is changed to "org.jpox.cache_level2"