net.ivoi.core.mvc.model.cache.EhCacheProvider.java Source code

Java tutorial

Introduction

Here is the source code for net.ivoi.core.mvc.model.cache.EhCacheProvider.java

Source

//$Id: EhCacheProvider.java 9964 2006-05-30 15:40:54Z epbernard $
/**
 *  Copyright 2003-2006 Greg Luck, Jboss Inc
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package net.ivoi.core.mvc.model.cache;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Hashtable;

/**
 *
 */
public class EhCacheProvider implements CacheProvider {

    private static final Log log = LogFactory.getLog(EhCacheProvider.class);

    private net.sf.ehcache.CacheManager manager;
    private Hashtable<String, EhCache> cacheManager;

    public Cache build(String name) throws CacheException {
        EhCache ehcache = cacheManager.get(name);
        if (null == ehcache) {
            try {
                synchronized (manager) {
                    net.sf.ehcache.Cache cache = manager.getCache(name);
                    if (cache == null) {
                        log.warn("Could not find configuration [" + name + "]; using defaults.");
                        manager.addCache(name);
                        cache = manager.getCache(name);
                        log.debug("started EHCache region: " + name);
                    }
                    synchronized (cacheManager) {
                        ehcache = new EhCache(cache);
                        cacheManager.put(name, ehcache);
                        return ehcache;
                    }
                }
            } catch (net.sf.ehcache.CacheException e) {
                throw new CacheException(e);
            }
        }
        return ehcache;
    }

    /**
     */
    public void start() throws CacheException {
        if (manager != null) {
            log.warn("Attempt to restart an already started EhCacheProvider. Use sessionFactory.close() "
                    + " between repeated calls to buildSessionFactory. Using previously created EhCacheProvider."
                    + " If this behaviour is required, consider using net.sf.ehcache.hibernate.SingletonEhCacheProvider.");
            return;
        }
        manager = new net.sf.ehcache.CacheManager();
        cacheManager = new Hashtable<String, EhCache>();
    }

    /**
     */
    public void stop() {
        if (manager != null) {
            manager.shutdown();
            manager = null;
        }
    }

}