/** * Create a request, process, extract the payload. * <p> * @param cacheName * @param key * @return ICacheElement * @throws IOException */ @Override public ICacheElement<K, V> get( String cacheName, K key ) throws IOException { return get( cacheName, key, 0 ); }
/** * Gets multiple items from the cache matching the pattern. * <p> * @param cacheName * @param pattern * @return a map of K key to ICacheElement<K, V> element, or an empty map if there is no * data in cache matching the pattern. * @throws IOException */ @Override public Map<K, ICacheElement<K, V>> getMatching( String cacheName, String pattern ) throws IOException { return getMatching( cacheName, pattern, 0 ); }
/** * Gets multiple items from the cache based on the given set of keys. * <p> * @param cacheName * @param keys * @return a map of K key to ICacheElement<K, V> element, or an empty map if there is no * data in cache for any of these keys * @throws IOException */ @Override public Map<K, ICacheElement<K, V>> getMultiple( String cacheName, Set<K> keys ) throws IOException { return getMultiple( cacheName, keys, 0 ); }
/** * The provides an extension point. If you want to extend this and use a special dispatcher, * here is the place to do it. * <p> * @param attributes */ @Override public void initialize( RemoteHttpCacheAttributes attributes ) { setRemoteDispatcher( new RemoteHttpCacheDispatcher( attributes ) ); if ( log.isInfoEnabled() ) { log.info( "Created remote Dispatcher." + getRemoteDispatcher() ); } setInitialized( true ); }
/** * Puts a cache item to the cache. * <p> * @param cacheElement * @param requesterId * @throws IOException */ @Override public void update( ICacheElement<K, V> cacheElement, long requesterId ) throws IOException { if ( !isInitialized() ) { String message = "The Remote Http Client is not initialized. Cannot process request."; log.warn( message ); throw new IOException( message ); } RemoteCacheRequest<K, V> remoteHttpCacheRequest = RemoteCacheRequestFactory.createUpdateRequest( cacheElement, requesterId ); getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest ); }
/** * Verify get functionality * <p> * @throws IOException */ public void testGet_nullFromDispatcher() throws IOException { // SETUP RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes(); RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<String, String>( attributes ); MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher(); client.setRemoteDispatcher( mockDispatcher ); String cacheName = "test"; String key = "key"; mockDispatcher.setupRemoteCacheResponse = null; // DO WORK ICacheElement<String, String> result = client.get( cacheName, key ); // VERIFY assertNull( "Wrong result.", result ); assertEquals( "Wrong type.", RemoteRequestType.GET, mockDispatcher.lastRemoteCacheRequest .getRequestType() ); }
/** * Verify remove functionality * <p> * @throws IOException */ public void testRemove_normal() throws IOException { // SETUP RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes(); RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<String, String>( attributes ); MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher(); client.setRemoteDispatcher( mockDispatcher ); String cacheName = "test"; String key = "key"; // DO WORK client.remove( cacheName, key ); // VERIFY assertEquals( "Wrong type.", RemoteRequestType.REMOVE, mockDispatcher.lastRemoteCacheRequest .getRequestType() ); }
/** * Verify update functionality * <p> * @throws IOException */ public void testUpdate_normal() throws IOException { // SETUP RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes(); RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<String, String>( attributes ); MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher(); client.setRemoteDispatcher( mockDispatcher ); String cacheName = "test"; ICacheElement<String, String> element = new CacheElement<String, String>( cacheName, "key", "value" ); // DO WORK client.update( element ); // VERIFY assertEquals( "Wrong type.", RemoteRequestType.UPDATE, mockDispatcher.lastRemoteCacheRequest .getRequestType() ); }
/** * Verify removeall functionality * <p> * @throws IOException */ public void testRemoveAll_normal() throws IOException { // SETUP RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes(); RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<String, String>( attributes ); MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher(); client.setRemoteDispatcher( mockDispatcher ); String cacheName = "test"; // DO WORK client.removeAll( cacheName ); // VERIFY assertEquals( "Wrong type.", RemoteRequestType.REMOVE_ALL, mockDispatcher.lastRemoteCacheRequest .getRequestType() ); }
/** * Verify get functionality * <p> * @throws IOException */ public void testGetMatching_normal() throws IOException { // SETUP RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes(); RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<String, String>( attributes ); MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher(); client.setRemoteDispatcher( mockDispatcher ); String cacheName = "test"; String pattern = "key"; ICacheElement<String, String> expected = new CacheElement<String, String>( cacheName, "key", "value" ); Map<String, ICacheElement<String, String>> expectedMap = new HashMap<String, ICacheElement<String,String>>(); expectedMap.put( "key", expected ); RemoteCacheResponse<Map<String, ICacheElement<String, String>>> remoteHttpCacheResponse = new RemoteCacheResponse<Map<String,ICacheElement<String,String>>>(); remoteHttpCacheResponse.setPayload( expectedMap ); mockDispatcher.setupRemoteCacheResponse = remoteHttpCacheResponse; // DO WORK Map<String, ICacheElement<String, String>> result = client.getMatching( cacheName, pattern ); // VERIFY assertEquals( "Wrong result.", expected, result.get( "key" ) ); assertEquals( "Wrong type.", RemoteRequestType.GET_MATCHING, mockDispatcher.lastRemoteCacheRequest.getRequestType() ); }
/** * Verify get functionality * <p> * @throws IOException */ public void testGetMultiple_normal() throws IOException { // SETUP RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes(); RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<String, String>( attributes ); MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher(); client.setRemoteDispatcher( mockDispatcher ); String cacheName = "test"; Set<String> keys = Collections.emptySet(); ICacheElement<String, String> expected = new CacheElement<String, String>( cacheName, "key", "value" ); Map<String, ICacheElement<String, String>> expectedMap = new HashMap<String, ICacheElement<String,String>>(); expectedMap.put( "key", expected ); RemoteCacheResponse<Map<String, ICacheElement<String, String>>> remoteHttpCacheResponse = new RemoteCacheResponse<Map<String,ICacheElement<String,String>>>(); remoteHttpCacheResponse.setPayload( expectedMap ); mockDispatcher.setupRemoteCacheResponse = remoteHttpCacheResponse; // DO WORK Map<String, ICacheElement<String, String>> result = client.getMultiple( cacheName, keys ); // VERIFY assertEquals( "Wrong result.", expected, result.get( "key" ) ); assertEquals( "Wrong type.", RemoteRequestType.GET_MULTIPLE, mockDispatcher.lastRemoteCacheRequest.getRequestType() ); }
/** * Verify dispose functionality * <p> * @throws IOException */ public void testDispose_normal() throws IOException { // SETUP RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes(); RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<String, String>( attributes ); MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher(); client.setRemoteDispatcher( mockDispatcher ); String cacheName = "test"; // DO WORK client.dispose( cacheName ); // VERIFY assertEquals( "Wrong type.", RemoteRequestType.DISPOSE, mockDispatcher.lastRemoteCacheRequest .getRequestType() ); } }
/** * Remove all keys from the specified cache. * <p> * @param cacheName * @throws IOException */ @Override public void removeAll( String cacheName ) throws IOException { removeAll( cacheName, 0 ); }
/** * Puts a cache item to the cache. * <p> * @param item * @throws IOException */ @Override public void update( ICacheElement<K, V> item ) throws IOException { update( item, 0 ); }
/** * Removes the given key from the specified cache. * <p> * @param cacheName * @param key * @throws IOException */ @Override public void remove( String cacheName, K key ) throws IOException { remove( cacheName, key, 0 ); }
/** Verify that we get the default. */ public void testCreateRemoteHttpCacheClient_Bad() { // SETUP String remoteHttpClientClassName = "junk"; RemoteHttpCacheAttributes cattr = new RemoteHttpCacheAttributes(); cattr.setRemoteHttpClientClassName( remoteHttpClientClassName ); RemoteHttpCacheFactory factory = new RemoteHttpCacheFactory(); // DO WORK IRemoteHttpCacheClient<String, String> result = factory.createRemoteHttpCacheClientForAttributes( cattr ); // VEIFY assertNotNull( "Should have a cache.", result ); assertTrue( "Wrong default.", result instanceof RemoteHttpCacheClient ); assertTrue( "Should be initialized", ((RemoteHttpCacheClient<String, String>)result).isInitialized() ); }
/** * This is an extension point. The manager and other classes will only create * RemoteHttpCacheClient through this method. * <p> * @param cattr * @return IRemoteHttpCacheClient */ protected <K, V> IRemoteHttpCacheClient<K, V> createRemoteHttpCacheClientForAttributes( RemoteHttpCacheAttributes cattr ) { IRemoteHttpCacheClient<K, V> client = OptionConverter.instantiateByClassName( cattr .getRemoteHttpClientClassName(), null ); if ( client == null ) { if ( log.isInfoEnabled() ) { log.info( "Creating the default client." ); } client = new RemoteHttpCacheClient<K, V>( ); } client.initialize( cattr ); return client; }
/** * Removes the given key from the specified cache. * <p> * @param cacheName * @param key * @param requesterId * @throws IOException */ @Override public void remove( String cacheName, K key, long requesterId ) throws IOException { if ( !isInitialized() ) { String message = "The Remote Http Client is not initialized. Cannot process request."; log.warn( message ); throw new IOException( message ); } RemoteCacheRequest<K, V> remoteHttpCacheRequest = RemoteCacheRequestFactory.createRemoveRequest( cacheName, key, requesterId ); getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest ); }
/** * Verify get functionality * <p> * @throws IOException */ public void testGet_nullFromDispatcher() throws IOException { // SETUP RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes(); RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<String, String>( attributes ); MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher(); client.setRemoteDispatcher( mockDispatcher ); String cacheName = "test"; String key = "key"; mockDispatcher.setupRemoteCacheResponse = null; // DO WORK ICacheElement<String, String> result = client.get( cacheName, key ); // VERIFY assertNull( "Wrong result.", result ); assertEquals( "Wrong type.", RemoteRequestType.GET, mockDispatcher.lastRemoteCacheRequest .getRequestType() ); }
/** * Verify remove functionality * <p> * @throws IOException */ public void testRemove_normal() throws IOException { // SETUP RemoteHttpCacheAttributes attributes = new RemoteHttpCacheAttributes(); RemoteHttpCacheClient<String, String> client = new RemoteHttpCacheClient<String, String>( attributes ); MockRemoteCacheDispatcher mockDispatcher = new MockRemoteCacheDispatcher(); client.setRemoteDispatcher( mockDispatcher ); String cacheName = "test"; String key = "key"; // DO WORK client.remove( cacheName, key ); // VERIFY assertEquals( "Wrong type.", RemoteRequestType.REMOVE, mockDispatcher.lastRemoteCacheRequest .getRequestType() ); }