View Javadoc
1   package org.apache.commons.jcs.auxiliary.remote;
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.io.IOException;
23  import java.util.HashMap;
24  import java.util.LinkedList;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Set;
28  
29  import org.apache.commons.jcs.auxiliary.AbstractAuxiliaryCache;
30  import org.apache.commons.jcs.auxiliary.AuxiliaryCacheAttributes;
31  import org.apache.commons.jcs.auxiliary.remote.behavior.IRemoteCacheClient;
32  import org.apache.commons.jcs.auxiliary.remote.behavior.IRemoteCacheListener;
33  import org.apache.commons.jcs.engine.CacheStatus;
34  import org.apache.commons.jcs.engine.behavior.ICacheElement;
35  import org.apache.commons.jcs.engine.behavior.ICacheServiceNonLocal;
36  import org.apache.commons.jcs.engine.stats.behavior.IStats;
37  import org.apache.commons.logging.Log;
38  import org.apache.commons.logging.LogFactory;
39  
40  /**
41   * Used for testing the no wait.
42   * <p>
43   * @author Aaron Smuts
44   */
45  public class MockRemoteCacheClient<K, V>
46      extends AbstractAuxiliaryCache<K, V>
47      implements IRemoteCacheClient<K, V>
48  {
49      /** log instance */
50      private static final Log log = LogFactory.getLog( MockRemoteCacheClient.class );
51  
52      /** List of ICacheElement&lt;K, V&gt; objects passed into update. */
53      public List<ICacheElement<K, V>> updateList = new LinkedList<ICacheElement<K,V>>();
54  
55      /** List of key objects passed into remove. */
56      public List<K> removeList = new LinkedList<K>();
57  
58      /** status to return. */
59      public CacheStatus status = CacheStatus.ALIVE;
60  
61      /** Can setup values to return from get. values must be ICacheElement&lt;K, V&gt; */
62      public Map<K, ICacheElement<K, V>> getSetupMap = new HashMap<K, ICacheElement<K,V>>();
63  
64      /** Can setup values to return from get. values must be Map&lt;K, ICacheElement&lt;K, V&gt;&gt; */
65      public Map<Set<K>, Map<K, ICacheElement<K, V>>> getMultipleSetupMap =
66          new HashMap<Set<K>, Map<K,ICacheElement<K,V>>>();
67  
68      /** The last service passed to fixCache */
69      public ICacheServiceNonLocal<K, V> fixed;
70  
71      /** Attributes. */
72      public RemoteCacheAttributes attributes = new RemoteCacheAttributes();
73  
74      /**
75       * Stores the last argument as fixed.
76       */
77      @Override
78      @SuppressWarnings("unchecked") // Don't know how to do this properly
79      public void fixCache( ICacheServiceNonLocal<?, ?> remote )
80      {
81          fixed = (ICacheServiceNonLocal<K, V>)remote;
82      }
83  
84      /**
85       * @return long
86       */
87      @Override
88      public long getListenerId()
89      {
90          return 0;
91      }
92  
93      /**
94       * @return null
95       */
96      @Override
97      public IRemoteCacheListener<K, V> getListener()
98      {
99          return null;
100     }
101 
102     /**
103      * Adds the argument to the updatedList.
104      */
105     @Override
106     public void update( ICacheElement<K, V> ce )
107     {
108         updateList.add( ce );
109     }
110 
111     /**
112      * Looks in the getSetupMap for a value.
113      */
114     @Override
115     public ICacheElement<K, V> get( K key )
116     {
117         log.info( "get [" + key + "]" );
118         return getSetupMap.get( key );
119     }
120 
121     /**
122      * Gets multiple items from the cache based on the given set of keys.
123      * <p>
124      * @param keys
125      * @return a map of K key to ICacheElement&lt;K, V&gt; element, or an empty map if there is no
126      *         data in cache for any of these keys
127      */
128     @Override
129     public Map<K, ICacheElement<K, V>> getMultiple(Set<K> keys)
130     {
131         log.info( "get [" + keys + "]" );
132         return getMultipleSetupMap.get( keys );
133     }
134 
135     /**
136      * Adds the key to the remove list.
137      */
138     @Override
139     public boolean remove( K key )
140     {
141         removeList.add( key );
142         return false;
143     }
144 
145     /**
146      * Removes all cached items from the cache.
147      */
148     @Override
149     public void removeAll()
150     {
151         // do nothing
152     }
153 
154     /**
155      * Prepares for shutdown.
156      */
157     @Override
158     public void dispose()
159     {
160         // do nothing
161     }
162 
163     /**
164      * Returns the current cache size in number of elements.
165      * <p>
166      * @return number of elements
167      */
168     @Override
169     public int getSize()
170     {
171         return 0;
172     }
173 
174     /**
175      * Returns the status setup variable.
176      */
177     @Override
178     public CacheStatus getStatus()
179     {
180         return status;
181     }
182 
183     /**
184      * Returns the cache name.
185      * <p>
186      * @return usually the region name.
187      */
188     @Override
189     public String getCacheName()
190     {
191         return null;
192     }
193 
194     /**
195      * @return null
196      */
197     @Override
198     public Set<K> getKeySet( )
199     {
200         return null;
201     }
202 
203     /**
204      * @return null
205      */
206     @Override
207     public IStats getStatistics()
208     {
209         return null;
210     }
211 
212     /**
213      * Returns the setup attributes. By default they are not null.
214      */
215     @Override
216     public AuxiliaryCacheAttributes getAuxiliaryCacheAttributes()
217     {
218         return attributes;
219     }
220 
221     /**
222      * Returns the cache stats.
223      * <p>
224      * @return String of important historical information.
225      */
226     @Override
227     public String getStats()
228     {
229         return null;
230     }
231 
232     /** @return 0 */
233     @Override
234     public CacheType getCacheType()
235     {
236         return CacheType.REMOTE_CACHE;
237     }
238 
239     /**
240      * @param pattern
241      * @return Map
242      * @throws IOException
243      */
244     @Override
245     public Map<K, ICacheElement<K, V>> getMatching(String pattern)
246         throws IOException
247     {
248         return new HashMap<K, ICacheElement<K,V>>();
249     }
250 
251     /**
252      * Nothing important
253      * <p>
254      * @return null
255      */
256     @Override
257     public String getEventLoggingExtraInfo()
258     {
259         return null;
260     }
261 }