View Javadoc

1   /*   Open Source Java Caching Service
2   *    Copyright (C) 2002 Frank Karlstrøm
3   *    This library is free software; you can redistribute it and/or
4   *    modify it under the terms of the GNU Lesser General Public
5   *    License as published by the Free Software Foundation; either
6   *    version 2.1 of the License, or (at your option) any later version.
7   *
8   *    This library is distributed in the hope that it will be useful,
9   *    but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  *    Lesser General Public License for more details.
12  *
13  *    You should have received a copy of the GNU Lesser General Public
14  *    License along with this library; if not, write to the Free Software
15  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  *
17  *    The author can be contacted by email: fjankk@users.sourceforge.net
18  */
19  package org.fjank.jcache;
20  
21  import javax.util.jcache.Attributes;
22  import javax.util.jcache.Cache;
23  import javax.util.jcache.CacheAccessFactory;
24  import javax.util.jcache.CacheException;
25  import javax.util.jcache.CacheMap;
26  import javax.util.jcache.CacheNotAvailableException;
27  import javax.util.jcache.NullObjectNameException;
28  import javax.util.jcache.ObjectExistsException;
29  import org.fjank.jcache.collection.MapAdapter;
30  
31  /**
32   * A factory for CacheAccess objects.
33   *
34   * @author Frank Karlstrøm
35   */
36  public final class CacheAccessFactoryImpl extends CacheAccessFactory {
37      /**
38       * Creates a new CacheAccessFactoryImpl object.
39       */
40      public CacheAccessFactoryImpl() {
41      }
42  
43      /**
44       * Will create a named region within the cache. This defines a namespace
45       * for storing objects. Gets it default attributes from the properties
46       * file.
47       *
48       * @param name the name for the region. Must be globally unique.
49       *
50       * @throws ObjectExistsException if the name already exists in the cache.
51       * @throws NullObjectNameException if the region is attempted initialized
52       *         with <CODE>null</CODE> as name.
53       * @throws CacheNotAvailableException if the cache is not available, either
54       *         it is not initialized, or it is unavailable.
55       */
56      public void defineRegion(final String name)
57          throws ObjectExistsException, NullObjectNameException, 
58              CacheNotAvailableException {
59          Attributes att = CacheAccessFactory.getInstance().getDefaultAttributes();
60          defineRegion(name, att);
61      }
62  
63      /**
64       * Will create a named region within the cache. This defines a namespace
65       * for storing objects.
66       *
67       * @param name the name for the region. Must be globally unique.
68       * @param attributes sets the default attributes for objects in the new
69       *        region.
70       *
71       * @throws ObjectExistsException if the name already exists in the cache.
72       * @throws NullObjectNameException if the region is attempted initialized
73       *         with <CODE>null</CODE> as name.
74       * @throws CacheNotAvailableException if the cache is not available, either
75       *         it is not initialized, or it is unavailable.
76       */
77      public void defineRegion(final String name, final Attributes attributes)
78          throws ObjectExistsException, NullObjectNameException, 
79              CacheNotAvailableException {
80  		CacheImpl cache = CacheImpl.getCache(true);
81  		Attributes lAttribs=attributes;
82  		if(lAttribs==null) lAttribs=CacheAccessFactory.getInstance().getDefaultAttributes();
83  		cache.addRegion(name, lAttribs);
84      }
85  
86      /**
87       * A method to retrieve a Map based implementation
88       * of a CacheAccess to the default region.
89       * @return a Map based Cache implementation to the default region.
90       */
91  	public CacheMap getMapAccess() {
92  		return new MapAdapter(getNewAccessImpl(null));
93  	}
94  	/**
95  	 * A method to retrieve a Map based implementation
96  	 * of a CacheAccess to the specified region.
97  	 * @return a Map based Cache implementation to the specified region.
98  	 */
99  	public CacheMap getMapAccess(final String region) {
100 		return new MapAdapter(getNewAccessImpl(region));
101 	}
102 	CacheAccessImpl2 getNewAccessImpl(final String region) {
103 	    CacheImpl cache = CacheImpl.getCache(true);
104         CacheRegion reg = region!=null?cache.getRegion(region):cache.getRegion();
105         if (reg == null) {
106             throw new IllegalStateException("The region " + region
107                 + " does not exist in this cache.");
108         }
109         return new CacheAccessImpl2(reg);
110 	}
111     /**
112      * gets an initialized instance of the cache.
113      *
114      * @see javax.util.jcache.CacheAccessFactory#getCache()
115      */
116     public Cache getCache() throws CacheException {
117         return CacheImpl.getCache(true);
118     }
119 
120     /**
121      * gets an instance of the cache. if the parameter init is true the cache
122      * is initialized. Otherwise the cache is not initialized.
123      *
124      * @see javax.util.jcache.CacheAccessFactory#getCache(boolean)
125      */
126     public Cache getCache(final boolean init) throws CacheException {
127         return CacheImpl.getCache(init);
128     }
129 }