View Javadoc

1   package org.apache.jcs.access;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Set;
23  
24  import org.apache.jcs.access.behavior.IGroupCacheAccess;
25  import org.apache.jcs.access.exception.CacheException;
26  import org.apache.jcs.engine.behavior.ICacheElement;
27  import org.apache.jcs.engine.behavior.ICompositeCacheAttributes;
28  import org.apache.jcs.engine.behavior.IElementAttributes;
29  import org.apache.jcs.engine.control.CompositeCache;
30  import org.apache.jcs.engine.control.CompositeCacheManager;
31  import org.apache.jcs.engine.control.group.GroupAttrName;
32  import org.apache.jcs.engine.control.group.GroupId;
33  
34  /***
35   * Access for groups.
36   */
37  public class GroupCacheAccess
38      extends CacheAccess
39      implements IGroupCacheAccess
40  {
41      /*** The underlying cache manager. */
42      private static CompositeCacheManager cacheMgr;
43  
44      /***
45       * Constructor for the GroupCacheAccess object
46       * <p>
47       * @param cacheControl
48       */
49      public GroupCacheAccess( CompositeCache cacheControl )
50      {
51          super( cacheControl );
52      }
53  
54      /***
55       * Gets the groupAccess attribute of the GroupCacheAccess class.
56       * <p>
57       * @param region
58       * @return The groupAccess value
59       * @throws CacheException
60       */
61      public static GroupCacheAccess getGroupAccess( String region )
62          throws CacheException
63      {
64          synchronized ( GroupCacheAccess.class )
65          {
66              if ( cacheMgr == null )
67              {
68                  cacheMgr = CompositeCacheManager.getInstance();
69              }
70          }
71          return new GroupCacheAccess( cacheMgr.getCache( region ) );
72      }
73  
74      /***
75       * Gets the groupAccess attribute of the GroupCacheAccess class.
76       * <p>
77       * @param region
78       * @param icca
79       * @return The groupAccess value
80       * @throws CacheException
81       */
82      public static GroupCacheAccess getGroupAccess( String region, ICompositeCacheAttributes icca )
83          throws CacheException
84      {
85          synchronized ( GroupCacheAccess.class )
86          {
87              if ( cacheMgr == null )
88              {
89                  cacheMgr = CompositeCacheManager.getInstance();
90              }
91          }
92  
93          return new GroupCacheAccess( cacheMgr.getCache( region, icca ) );
94      }
95  
96      /***
97       * Gets an item out of the cache that is in a specified group.
98       * <p>
99       * @param name
100      *            The key name.
101      * @param group
102      *            The group name.
103      * @return The cached value, null if not found.
104      */
105     public Object getFromGroup( Object name, String group )
106     {
107         ICacheElement element = this.cacheControl.get( getGroupAttrName( group, name ) );
108         return ( element != null ) ? element.getVal() : null;
109     }
110 
111     /***
112      * Internal method used for group functionality.
113      * <p>
114      * @param group
115      * @param name
116      * @return GroupAttrName
117      */
118     private GroupAttrName getGroupAttrName( String group, Object name )
119     {
120         GroupId gid = new GroupId( this.cacheControl.getCacheName(), group );
121         return new GroupAttrName( gid, name );
122     }
123 
124     /***
125      * Allows the user to put an object into a group within a particular cache
126      * region. This method sets the object's attributes to the default for the
127      * region.
128      * <p>
129      * @param name
130      *            The key name.
131      * @param groupName
132      *            The group name.
133      * @param value
134      *            The object to cache
135      * @throws CacheException
136      */
137     public void putInGroup( Object name, String groupName, Object value )
138         throws CacheException
139     {
140         putInGroup( name, groupName, value, null );
141     }
142 
143     /***
144      * Allows the user to put an object into a group within a particular cache
145      * region. This method allows the object's attributes to be individually
146      * specified.
147      * <p>
148      * @param name
149      *            The key name.
150      * @param groupName
151      *            The group name.
152      * @param value
153      *            The object to cache
154      * @param attr
155      *            The objects attributes.
156      * @throws CacheException
157      */
158     public void putInGroup( Object name, String groupName, Object value, IElementAttributes attr )
159         throws CacheException
160     {
161         // unbind object first if any.
162         remove( name, groupName );
163 
164         if ( attr == null )
165         {
166             put( getGroupAttrName( groupName, name ), value );
167         }
168         else
169         {
170             put( getGroupAttrName( groupName, name ), value, attr );
171         }
172     }
173 
174     /***
175      * @param name
176      * @param group
177      */
178     public void remove( Object name, String group )
179     {
180         GroupAttrName key = getGroupAttrName( group, name );
181         this.cacheControl.remove( key );
182     }
183 
184     /***
185      * Gets the set of keys of objects currently in the group.
186      * <p>
187      * @param group
188      * @return A Set of keys.
189      */
190     public Set getGroupKeys( String group )
191     {
192         return this.cacheControl.getGroupKeys( group );
193     }
194 
195     /***
196      * Invalidates a group: remove all the group members
197      * <p>
198      * @param group
199      *            The name of the group to invalidate
200      */
201     public void invalidateGroup( String group )
202     {
203         this.cacheControl.remove( new GroupId( this.cacheControl.getCacheName(), group ) );
204     }
205 }