Tuesday, April 7, 2009

R&D Source Code


Sample text
package com.dfn.jbcg.session;


import com.dfn.jbcg.cache.CacheFactory;

import javax.ejb.Stateless;

/**
 * com.dfn.mtr.session.MTRCacheFacadeBean
 */
@Stateless(name = "CacheFacade")
public class CacheFacadeBean implements CacheFacade {

    public void addToCache(String fullyQualifiedName, Object key, Object value) {
        CacheFactory cacheFactory = new CacheFactory();
        cacheFactory.addToCache(fullyQualifiedName, key, value);
    }

    public Object loadFromCache(String fullyQualifiedName, String key) {
        CacheFactory cacheFactory = new CacheFactory();
        return cacheFactory.loadFromCache(fullyQualifiedName, key);
    }

    public void removeFromCache(String fullyQualifiedName){
        CacheFactory cacheFactory = new CacheFactory();
        cacheFactory.removeFromCache(fullyQualifiedName);
    }
}


-----------------------------------------------------------------

package com.dfn.jbcg.session;

import javax.ejb.Remote;

/**
 * com.dfn.mtr.session.MTRCacheFacade
 */
@Remote
public interface CacheFacade {
    public void addToCache(String fullyQualifiedName, Object key, Object value);

    public Object loadFromCache(String fullyQualifiedName, String key);

    public void removeFromCache(String fullyQualifiedName);
}


-----------------------------------------------------------------

package com.dfn.jbcg.cache;

import org.apache.log4j.Logger;
import org.jboss.cache.*;
import org.jboss.cache.config.ConfigurationException;

/**
 * com.dfn.mtr.cache.MTRCacheFactory
 */
public class CacheFactory {

    Logger logger = Logger.getLogger("CacheFactory.class");

    private static Cache cache;
    private String configFileClassPath = "META-INF";
    private String configFileName = "cache-configuration.xml";    

    private Cache getCache() {
        if (cache == null) {
            try {
                org.jboss.cache.CacheFactory factory = new DefaultCacheFactory();
                cache = factory.createCache(configFileClassPath + "/" + configFileName);

                MBeanServer mBeanServer = MBeanServerLocator.locate();
                ObjectName on = new ObjectName("jboss.cache:service=MTRCache");

                JmxRegistrationManager jmxManager = new JmxRegistrationManager(mBeanServer, cache, on);
                jmxManager.registerAllMBeans();

                cache.start();
            }
            catch (ConfigurationException e) {
                logger.error(e.getMessage());
            }
            catch (MalformedObjectNameException e) {
                logger.error(e.getMessage());
            }
        }
        return cache;
    }

    public void addToCache(String fullyQualifiedName, Object key, Object value) {
        Node rootNode = getCache().getRoot();
        Fqn parentNode = Fqn.fromString(fullyQualifiedName);

        Node childNode = rootNode.addChild(parentNode);
        childNode.put(key, value);
        logger.info(fullyQualifiedName + " added to the cache.");
    }

    public Object loadFromCache(String fullyQualifiedName, String key) {
        Node rooteNode = getCache().getRoot();

        Fqn fqn = Fqn.fromString(fullyQualifiedName);
        Node nde = rooteNode.getChild(fqn);
        if (nde==null || nde.equals("")){
            return null;
        }
        return nde.get(key);
    }

    public Object removeFromCache(String fullyQualifiedName) {
        Node rooteNode = getCache().getRoot();

        Fqn fqn = Fqn.fromString(fullyQualifiedName);
        return rooteNode.removeChild(fqn);
    }
}

-----------------------------------------------------------------

package com.dfn.jbcg.cache;

import java.io.Serializable;

/**
 * com.dfn.mtr.cache.CacheMockObject
 */
public class CacheMockObject implements Serializable{
    private int objectId;
    private String objectName;

    public CacheMockObject(int objectId, String objectName){
        this.objectId = objectId;
        this.objectName = objectName;
    }

    public int getObjectId() {
        return objectId;
    }

    public void setObjectId(int objectId) {
        this.objectId = objectId;
    }

    public String getObjectName() {
        return objectName;
    }

    public void setObjectName(String objectName) {
        this.objectName = objectName;
    }

    public String toString(){
        return "Object Id = " + this.objectId + "\n" +
        "Object Name = " + this.objectName;
    }
}

Labels: ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home