A
download AbstractRMSBasedCache.java
Language: Java
License: SPL
LOC: 72
Project Info
kommons - JavaME Reusable Objects(kommons)
Server: java.net
Type: cvs
...\java2me\kommons\cache\rms\
   AbstractRMSBasedCache.java
   EntityRecordMapping.java
   package.html
   RMSCacheManager.java
   RMSHelper.java

/**
 * kommons, J2ME Reusable Objects
 *
 * Distributable under SUN PUBLIC LICENSE Version 1.0.
 * See terms of license at http://www.opensource.org/licenses/sunpublic.php.
 */
package org.java2me.kommons.cache.rms;

import java.util.Hashtable;

import javax.microedition.rms.RecordStore;

import org.java2me.kommons.cache.Cache;
import org.java2me.kommons.cache.PersistentObject;
import org.java2me.kommons.exceptions.LogicalException;
import org.java2me.kommons.exceptions.ResourceException;
import org.java2me.kommons.logger.Logger;
import org.java2me.kommons.util.StringUtils;


/**
 * AbstractRMSBasedCache.
 *
 * The Cache implemented using the RMS storage. It's a singleton.
 *
 * @author aterreno
 * @version $Id: AbstractRMSBasedCache.java,v 1.1 2005/07/22 07:58:27 aterreno
 *          Exp $
 */
public abstract class AbstractRMSBasedCache implements Cache {
    private final static Logger logger = Logger.getLogger("AbstractRMSBasedCache");
    private Hashtable cache = new Hashtable();

    /**
     * Every time you save an entity you save it into the RMS
     *
     * @throws LogicalException
     * @throws ResourceException
     * @see RMSSupport.save()
     */
    public final void save(PersistentObject e) throws ResourceException, LogicalException {
        String id = e.getId();

        if (RMSCacheManager.isRmsstoring()) {
            RMSHelper.save(e, fromObjToXML(e),RMSCacheManager.RS);
        }

        cache.put(id, e);
    }

    /**
     * If an Entity is not contianed in the cache It will be searched into the
     * RMS If found into the RMS is putted in cache for further reading.
     *
     * @throws LogicalException
     * @throws ResourceException
     * @see RMSSupport.read()
     */
    public final PersistentObject find(String id) throws LogicalException, ResourceException {
        PersistentObject ret = null;
        if (logger.isDebugEnabled()) {
            logger.debug("find(id=" + id + ")");
        }

        if (cache.containsKey(id)) {
            ret = (PersistentObject) cache.get(id);
        } else if (RMSCacheManager.isRmsstoring()) {
            String xml = RMSHelper.read(id,RMSCacheManager.RS);

            if (!StringUtils.isEmtpy(xml)) {
                ret = fromXmlToObj(xml);
            }

            if (ret != null) {
                cache.put(id, ret);
            }
        }

        return ret;
    }

    /**
     * Deletes a persistent object from the cache.
     * @throws ResourceException
     */
    public final void delete(PersistentObject e) throws ResourceException {
        String id = e.getId();

        if (cache.containsKey(id)) {
            cache.remove(id);

            if (RMSCacheManager.isRmsstoring()) {
                RMSHelper.delete(e,RMSCacheManager.RS);
            }
        }
    }

    /**
     * Resets everything.<br>
     * First it resets the RMS containing the data and the mapping,
     * Then it resets the live cache.
     *
     * @throws ResourceException
     */
    public final void reset() throws ResourceException {
        if (logger.isDebugEnabled()) {
            logger.debug("Resetting cache containing " + cache.size() + " Entities");
        }

        if (RMSCacheManager.isRmsstoring()) {
            RMSHelper.closeRecordStore(RMSCacheManager.RS);

            RecordStore rsMapping = RMSHelper.openRecordStore(EntityRecordMapping.ER_MAPPING_RS,
                    true);
            RMSHelper.closeRecordStore(rsMapping);
            RMSHelper.deleteRecordStore(RMSCacheManager.RS_NAME);
            RMSHelper.deleteRecordStore(EntityRecordMapping.ER_MAPPING_RS);
        }

        cache.clear();

        if (logger.isDebugEnabled()) {
            logger.debug("reset() - end");
        }
    }

    public final boolean isEmpty() {
        return cache.isEmpty();
    }

    public final String toString() {
        return cache.toString();
    }

    /**
     * Entities must be written into RMS as XML Strings.
     *
     * @throws LogicalException
     */
    protected abstract PersistentObject fromXmlToObj(String xml) throws LogicalException;

    /**
     * PersistentObjects must be written into RMS as XML Strings.
     *
     * @throws LogicalException
     */
    protected abstract String fromObjToXML(PersistentObject e) throws LogicalException;
}

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us