/* Open Source Java Caching Service
* Copyright (C) 2002 Frank Karlstr�m
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* The author can be contacted by email: fjankk@users.sourceforge.net
*/
package org.fjank.jcache;
import javax.util.jcache.Attributes;
import javax.util.jcache.Cache;
import javax.util.jcache.CacheAccessFactory;
import javax.util.jcache.CacheException;
import javax.util.jcache.CacheMap;
import javax.util.jcache.CacheNotAvailableException;
import javax.util.jcache.NullObjectNameException;
import javax.util.jcache.ObjectExistsException;
import org.fjank.jcache.collection.MapAdapter;
/**
* A factory for CacheAccess objects.
*
* @author Frank Karlstr�m
*/
public final class CacheAccessFactoryImpl extends CacheAccessFactory {
/**
* Creates a new CacheAccessFactoryImpl object.
*/
public CacheAccessFactoryImpl() {
}
/**
* Will create a named region within the cache. This defines a namespace
* for storing objects. Gets it default attributes from the properties
* file.
*
* @param name the name for the region. Must be globally unique.
*
* @throws ObjectExistsException if the name already exists in the cache.
* @throws NullObjectNameException if the region is attempted initialized
* with <CODE>null</CODE> as name.
* @throws CacheNotAvailableException if the cache is not available, either
* it is not initialized, or it is unavailable.
*/
public void defineRegion(final String name)
throws ObjectExistsException, NullObjectNameException,
CacheNotAvailableException {
Attributes att = CacheAccessFactory.getInstance().getDefaultAttributes();
defineRegion(name, att);
}
/**
* Will create a named region within the cache. This defines a namespace
* for storing objects.
*
* @param name the name for the region. Must be globally unique.
* @param attributes sets the default attributes for objects in the new
* region.
*
* @throws ObjectExistsException if the name already exists in the cache.
* @throws NullObjectNameException if the region is attempted initialized
* with <CODE>null</CODE> as name.
* @throws CacheNotAvailableException if the cache is not available, either
* it is not initialized, or it is unavailable.
*/
public void defineRegion(final String name, final Attributes attributes)
throws ObjectExistsException, NullObjectNameException,
CacheNotAvailableException {
CacheImpl cache = CacheImpl.getCache(true);
Attributes lAttribs=attributes;
if(lAttribs==null) lAttribs=CacheAccessFactory.getInstance().getDefaultAttributes();
cache.addRegion(name, lAttribs);
}
/**
* A method to retrieve a Map based implementation
* of a CacheAccess to the default region.
* @return a Map based Cache implementation to the default region.
*/
public CacheMap getMapAccess() {
return new MapAdapter(getNewAccessImpl(null));
}
/**
* A method to retrieve a Map based implementation
* of a CacheAccess to the specified region.
* @return a Map based Cache implementation to the specified region.
*/
public CacheMap getMapAccess(final String region) {
return new MapAdapter(getNewAccessImpl(region));
}
CacheAccessImpl2 getNewAccessImpl(final String region) {
CacheImpl cache = CacheImpl.getCache(true);
CacheRegion reg = region!=null?cache.getRegion(region):cache.getRegion();
if (reg == null) {
throw new IllegalStateException("The region " + region
+ " does not exist in this cache.");
}
return new CacheAccessImpl2(reg);
}
/**
* gets an initialized instance of the cache.
*
* @see javax.util.jcache.CacheAccessFactory#getCache()
*/
public Cache getCache() throws CacheException {
return CacheImpl.getCache(true);
}
/**
* gets an instance of the cache. if the parameter init is true the cache
* is initialized. Otherwise the cache is not initialized.
*
* @see javax.util.jcache.CacheAccessFactory#getCache(boolean)
*/
public Cache getCache(final boolean init) throws CacheException {
return CacheImpl.getCache(init);
}
}