Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id BF0EB10BE6 for ; Sun, 23 Feb 2014 10:38:50 +0000 (UTC) Received: (qmail 10858 invoked by uid 500); 23 Feb 2014 10:38:48 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 10803 invoked by uid 500); 23 Feb 2014 10:38:48 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 10796 invoked by uid 99); 23 Feb 2014 10:38:47 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 23 Feb 2014 10:38:47 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 23 Feb 2014 10:38:43 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 3470F2388B34; Sun, 23 Feb 2014 10:38:00 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1570993 [7/24] - in /commons/proper/jcs/trunk/src: assembly/ changes/ conf/ java/org/apache/commons/jcs/access/ java/org/apache/commons/jcs/access/exception/ java/org/apache/commons/jcs/admin/ java/org/apache/commons/jcs/auxiliary/ java/or... Date: Sun, 23 Feb 2014 10:37:52 -0000 To: commits@commons.apache.org From: tv@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20140223103800.3470F2388B34@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Modified: commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/remote/http/client/RemoteHttpCacheClient.java URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/remote/http/client/RemoteHttpCacheClient.java?rev=1570993&r1=1570992&r2=1570993&view=diff ============================================================================== --- commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/remote/http/client/RemoteHttpCacheClient.java (original) +++ commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/remote/http/client/RemoteHttpCacheClient.java Sun Feb 23 10:37:48 2014 @@ -1,479 +1,479 @@ -package org.apache.commons.jcs.auxiliary.remote.http.client; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import java.io.IOException; -import java.io.Serializable; -import java.util.Collections; -import java.util.Map; -import java.util.Set; - -import org.apache.commons.jcs.auxiliary.remote.behavior.IRemoteCacheDispatcher; -import org.apache.commons.jcs.auxiliary.remote.http.client.behavior.IRemoteHttpCacheClient; -import org.apache.commons.jcs.auxiliary.remote.util.RemoteCacheRequestFactory; -import org.apache.commons.jcs.auxiliary.remote.value.RemoteCacheRequest; -import org.apache.commons.jcs.auxiliary.remote.value.RemoteCacheResponse; -import org.apache.commons.jcs.engine.behavior.ICacheElement; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -/** This is the service used by the remote http auxiliary cache. */ -public class RemoteHttpCacheClient - implements IRemoteHttpCacheClient -{ - /** The Logger. */ - private final static Log log = LogFactory.getLog( RemoteHttpCacheClient.class ); - - /** The internal client. */ - private IRemoteCacheDispatcher remoteDispatcher; - - /** The remote attributes */ - private RemoteHttpCacheAttributes remoteHttpCacheAttributes; - - /** Set to true when initialize is called */ - private boolean initialized = false; - - /** For factory construction. */ - public RemoteHttpCacheClient() - { - // does nothing - } - - /** - * Constructs a client. - *

- * @param attributes - */ - public RemoteHttpCacheClient( RemoteHttpCacheAttributes attributes ) - { - setRemoteHttpCacheAttributes( attributes ); - initialize( attributes ); - } - - /** - * The provides an extension point. If you want to extend this and use a special dispatcher, - * here is the place to do it. - *

- * @param attributes - */ - public void initialize( RemoteHttpCacheAttributes attributes ) - { - setRemoteDispatcher( new RemoteHttpCacheDispatcher( attributes ) ); - - if ( log.isInfoEnabled() ) - { - log.info( "Created remote Dispatcher." + getRemoteDispatcher() ); - } - setInitialized( true ); - } - - /** - * Create a request, process, extract the payload. - *

- * @param cacheName - * @param key - * @return ICacheElement - * @throws IOException - */ - public ICacheElement get( String cacheName, K key ) - throws IOException - { - return get( cacheName, key, 0 ); - } - - /** - * Create a request, process, extract the payload. - *

- * @param cacheName - * @param key - * @param requesterId - * @return ICacheElement - * @throws IOException - */ - public ICacheElement get( 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 remoteHttpCacheRequest = - RemoteCacheRequestFactory.createGetRequest( cacheName, key, requesterId ); - - RemoteCacheResponse> remoteHttpCacheResponse = - getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest ); - - if ( log.isDebugEnabled() ) - { - log.debug( "Get [" + key + "] = " + remoteHttpCacheResponse ); - } - - if ( remoteHttpCacheResponse != null) - { - return remoteHttpCacheResponse.getPayload(); - } - - return null; - } - - /** - * Gets multiple items from the cache matching the pattern. - *

- * @param cacheName - * @param pattern - * @return a map of K key to ICacheElement element, or an empty map if there is no - * data in cache matching the pattern. - * @throws IOException - */ - public Map> getMatching( String cacheName, String pattern ) - throws IOException - { - return getMatching( cacheName, pattern, 0 ); - } - - /** - * Gets multiple items from the cache matching the pattern. - *

- * @param cacheName - * @param pattern - * @param requesterId - * @return a map of K key to ICacheElement element, or an empty map if there is no - * data in cache matching the pattern. - * @throws IOException - */ - public Map> getMatching( String cacheName, String pattern, 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 remoteHttpCacheRequest = - RemoteCacheRequestFactory.createGetMatchingRequest( cacheName, pattern, requesterId ); - - RemoteCacheResponse>> remoteHttpCacheResponse = - getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest ); - - if ( log.isDebugEnabled() ) - { - log.debug( "GetMatching [" + pattern + "] = " + remoteHttpCacheResponse ); - } - - return remoteHttpCacheResponse.getPayload(); - } - - /** - * Gets multiple items from the cache based on the given set of keys. - *

- * @param cacheName - * @param keys - * @return a map of K key to ICacheElement element, or an empty map if there is no - * data in cache for any of these keys - * @throws IOException - */ - public Map> getMultiple( String cacheName, Set keys ) - throws IOException - { - return getMultiple( cacheName, keys, 0 ); - } - - /** - * Gets multiple items from the cache based on the given set of keys. - *

- * @param cacheName - * @param keys - * @param requesterId - * @return a map of K key to ICacheElement element, or an empty map if there is no - * data in cache for any of these keys - * @throws IOException - */ - public Map> getMultiple( String cacheName, Set keys, 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 remoteHttpCacheRequest = - RemoteCacheRequestFactory.createGetMultipleRequest( cacheName, keys, requesterId ); - - RemoteCacheResponse>> remoteHttpCacheResponse = - getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest ); - - if ( log.isDebugEnabled() ) - { - log.debug( "GetMultiple [" + keys + "] = " + remoteHttpCacheResponse ); - } - - return remoteHttpCacheResponse.getPayload(); - } - - /** - * Removes the given key from the specified cache. - *

- * @param cacheName - * @param key - * @throws IOException - */ - public void remove( String cacheName, K key ) - throws IOException - { - remove( cacheName, key, 0 ); - } - - /** - * Removes the given key from the specified cache. - *

- * @param cacheName - * @param key - * @param requesterId - * @throws IOException - */ - 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 remoteHttpCacheRequest = - RemoteCacheRequestFactory.createRemoveRequest( cacheName, key, requesterId ); - - getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest ); - } - - /** - * Remove all keys from the specified cache. - *

- * @param cacheName - * @throws IOException - */ - public void removeAll( String cacheName ) - throws IOException - { - removeAll( cacheName, 0 ); - } - - /** - * Remove all keys from the sepcified cache. - *

- * @param cacheName - * @param requesterId - * @throws IOException - */ - public void removeAll( String cacheName, 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 remoteHttpCacheRequest = - RemoteCacheRequestFactory.createRemoveAllRequest( cacheName, requesterId ); - - getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest ); - } - - /** - * Puts a cache item to the cache. - *

- * @param item - * @throws IOException - */ - public void update( ICacheElement item ) - throws IOException - { - update( item, 0 ); - } - - /** - * Puts a cache item to the cache. - *

- * @param cacheElement - * @param requesterId - * @throws IOException - */ - public void update( ICacheElement 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 remoteHttpCacheRequest = - RemoteCacheRequestFactory.createUpdateRequest( cacheElement, requesterId ); - - getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest ); - } - - /** - * Frees the specified cache. - *

- * @param cacheName - * @throws IOException - */ - public void dispose( String cacheName ) - throws IOException - { - if ( !isInitialized() ) - { - String message = "The Remote Http Client is not initialized. Cannot process request."; - log.warn( message ); - throw new IOException( message ); - } - - RemoteCacheRequest remoteHttpCacheRequest = - RemoteCacheRequestFactory.createDisposeRequest( cacheName, 0 ); - - getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest ); - } - - /** - * Frees the specified cache. - *

- * @throws IOException - */ - public void release() - throws IOException - { - // noop - } - - /** - * Return the keys in this cache. - *

- * @param cacheName the name of the cache - * @see org.apache.commons.jcs.auxiliary.AuxiliaryCache#getKeySet() - */ - public Set getKeySet( String cacheName ) throws IOException - { - if ( !isInitialized() ) - { - String message = "The Remote Http Client is not initialized. Cannot process request."; - log.warn( message ); - throw new IOException( message ); - } - - RemoteCacheRequest remoteHttpCacheRequest = - RemoteCacheRequestFactory.createGetKeySetRequest(cacheName, 0 ); - - RemoteCacheResponse> remoteHttpCacheResponse = getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest ); - - if ( remoteHttpCacheResponse != null && remoteHttpCacheResponse.getPayload() != null ) - { - return remoteHttpCacheResponse.getPayload(); - } - - return Collections.emptySet(); - } - - /** - * Make and alive request. - *

- * @return true if we make a successful alive request. - * @throws IOException - */ - public boolean isAlive() - throws IOException - { - if ( !isInitialized() ) - { - String message = "The Remote Http Client is not initialized. Cannot process request."; - log.warn( message ); - throw new IOException( message ); - } - - RemoteCacheRequest remoteHttpCacheRequest = RemoteCacheRequestFactory.createAliveCheckRequest( 0 ); - RemoteCacheResponse remoteHttpCacheResponse = - getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest ); - - if ( remoteHttpCacheResponse != null ) - { - return remoteHttpCacheResponse.isSuccess(); - } - - return false; - } - - /** - * @param remoteDispatcher the remoteDispatcher to set - */ - public void setRemoteDispatcher( IRemoteCacheDispatcher remoteDispatcher ) - { - this.remoteDispatcher = remoteDispatcher; - } - - /** - * @return the remoteDispatcher - */ - public IRemoteCacheDispatcher getRemoteDispatcher() - { - return remoteDispatcher; - } - - /** - * @param remoteHttpCacheAttributes the remoteHttpCacheAttributes to set - */ - public void setRemoteHttpCacheAttributes( RemoteHttpCacheAttributes remoteHttpCacheAttributes ) - { - this.remoteHttpCacheAttributes = remoteHttpCacheAttributes; - } - - /** - * @return the remoteHttpCacheAttributes - */ - public RemoteHttpCacheAttributes getRemoteHttpCacheAttributes() - { - return remoteHttpCacheAttributes; - } - - /** - * @param initialized the initialized to set - */ - protected void setInitialized( boolean initialized ) - { - this.initialized = initialized; - } - - /** - * @return the initialized - */ - protected boolean isInitialized() - { - return initialized; - } -} +package org.apache.commons.jcs.auxiliary.remote.http.client; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.IOException; +import java.io.Serializable; +import java.util.Collections; +import java.util.Map; +import java.util.Set; + +import org.apache.commons.jcs.auxiliary.remote.behavior.IRemoteCacheDispatcher; +import org.apache.commons.jcs.auxiliary.remote.http.client.behavior.IRemoteHttpCacheClient; +import org.apache.commons.jcs.auxiliary.remote.util.RemoteCacheRequestFactory; +import org.apache.commons.jcs.auxiliary.remote.value.RemoteCacheRequest; +import org.apache.commons.jcs.auxiliary.remote.value.RemoteCacheResponse; +import org.apache.commons.jcs.engine.behavior.ICacheElement; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** This is the service used by the remote http auxiliary cache. */ +public class RemoteHttpCacheClient + implements IRemoteHttpCacheClient +{ + /** The Logger. */ + private final static Log log = LogFactory.getLog( RemoteHttpCacheClient.class ); + + /** The internal client. */ + private IRemoteCacheDispatcher remoteDispatcher; + + /** The remote attributes */ + private RemoteHttpCacheAttributes remoteHttpCacheAttributes; + + /** Set to true when initialize is called */ + private boolean initialized = false; + + /** For factory construction. */ + public RemoteHttpCacheClient() + { + // does nothing + } + + /** + * Constructs a client. + *

+ * @param attributes + */ + public RemoteHttpCacheClient( RemoteHttpCacheAttributes attributes ) + { + setRemoteHttpCacheAttributes( attributes ); + initialize( attributes ); + } + + /** + * The provides an extension point. If you want to extend this and use a special dispatcher, + * here is the place to do it. + *

+ * @param attributes + */ + public void initialize( RemoteHttpCacheAttributes attributes ) + { + setRemoteDispatcher( new RemoteHttpCacheDispatcher( attributes ) ); + + if ( log.isInfoEnabled() ) + { + log.info( "Created remote Dispatcher." + getRemoteDispatcher() ); + } + setInitialized( true ); + } + + /** + * Create a request, process, extract the payload. + *

+ * @param cacheName + * @param key + * @return ICacheElement + * @throws IOException + */ + public ICacheElement get( String cacheName, K key ) + throws IOException + { + return get( cacheName, key, 0 ); + } + + /** + * Create a request, process, extract the payload. + *

+ * @param cacheName + * @param key + * @param requesterId + * @return ICacheElement + * @throws IOException + */ + public ICacheElement get( 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 remoteHttpCacheRequest = + RemoteCacheRequestFactory.createGetRequest( cacheName, key, requesterId ); + + RemoteCacheResponse> remoteHttpCacheResponse = + getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest ); + + if ( log.isDebugEnabled() ) + { + log.debug( "Get [" + key + "] = " + remoteHttpCacheResponse ); + } + + if ( remoteHttpCacheResponse != null) + { + return remoteHttpCacheResponse.getPayload(); + } + + return null; + } + + /** + * Gets multiple items from the cache matching the pattern. + *

+ * @param cacheName + * @param pattern + * @return a map of K key to ICacheElement element, or an empty map if there is no + * data in cache matching the pattern. + * @throws IOException + */ + public Map> getMatching( String cacheName, String pattern ) + throws IOException + { + return getMatching( cacheName, pattern, 0 ); + } + + /** + * Gets multiple items from the cache matching the pattern. + *

+ * @param cacheName + * @param pattern + * @param requesterId + * @return a map of K key to ICacheElement element, or an empty map if there is no + * data in cache matching the pattern. + * @throws IOException + */ + public Map> getMatching( String cacheName, String pattern, 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 remoteHttpCacheRequest = + RemoteCacheRequestFactory.createGetMatchingRequest( cacheName, pattern, requesterId ); + + RemoteCacheResponse>> remoteHttpCacheResponse = + getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest ); + + if ( log.isDebugEnabled() ) + { + log.debug( "GetMatching [" + pattern + "] = " + remoteHttpCacheResponse ); + } + + return remoteHttpCacheResponse.getPayload(); + } + + /** + * Gets multiple items from the cache based on the given set of keys. + *

+ * @param cacheName + * @param keys + * @return a map of K key to ICacheElement element, or an empty map if there is no + * data in cache for any of these keys + * @throws IOException + */ + public Map> getMultiple( String cacheName, Set keys ) + throws IOException + { + return getMultiple( cacheName, keys, 0 ); + } + + /** + * Gets multiple items from the cache based on the given set of keys. + *

+ * @param cacheName + * @param keys + * @param requesterId + * @return a map of K key to ICacheElement element, or an empty map if there is no + * data in cache for any of these keys + * @throws IOException + */ + public Map> getMultiple( String cacheName, Set keys, 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 remoteHttpCacheRequest = + RemoteCacheRequestFactory.createGetMultipleRequest( cacheName, keys, requesterId ); + + RemoteCacheResponse>> remoteHttpCacheResponse = + getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest ); + + if ( log.isDebugEnabled() ) + { + log.debug( "GetMultiple [" + keys + "] = " + remoteHttpCacheResponse ); + } + + return remoteHttpCacheResponse.getPayload(); + } + + /** + * Removes the given key from the specified cache. + *

+ * @param cacheName + * @param key + * @throws IOException + */ + public void remove( String cacheName, K key ) + throws IOException + { + remove( cacheName, key, 0 ); + } + + /** + * Removes the given key from the specified cache. + *

+ * @param cacheName + * @param key + * @param requesterId + * @throws IOException + */ + 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 remoteHttpCacheRequest = + RemoteCacheRequestFactory.createRemoveRequest( cacheName, key, requesterId ); + + getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest ); + } + + /** + * Remove all keys from the specified cache. + *

+ * @param cacheName + * @throws IOException + */ + public void removeAll( String cacheName ) + throws IOException + { + removeAll( cacheName, 0 ); + } + + /** + * Remove all keys from the sepcified cache. + *

+ * @param cacheName + * @param requesterId + * @throws IOException + */ + public void removeAll( String cacheName, 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 remoteHttpCacheRequest = + RemoteCacheRequestFactory.createRemoveAllRequest( cacheName, requesterId ); + + getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest ); + } + + /** + * Puts a cache item to the cache. + *

+ * @param item + * @throws IOException + */ + public void update( ICacheElement item ) + throws IOException + { + update( item, 0 ); + } + + /** + * Puts a cache item to the cache. + *

+ * @param cacheElement + * @param requesterId + * @throws IOException + */ + public void update( ICacheElement 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 remoteHttpCacheRequest = + RemoteCacheRequestFactory.createUpdateRequest( cacheElement, requesterId ); + + getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest ); + } + + /** + * Frees the specified cache. + *

+ * @param cacheName + * @throws IOException + */ + public void dispose( String cacheName ) + throws IOException + { + if ( !isInitialized() ) + { + String message = "The Remote Http Client is not initialized. Cannot process request."; + log.warn( message ); + throw new IOException( message ); + } + + RemoteCacheRequest remoteHttpCacheRequest = + RemoteCacheRequestFactory.createDisposeRequest( cacheName, 0 ); + + getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest ); + } + + /** + * Frees the specified cache. + *

+ * @throws IOException + */ + public void release() + throws IOException + { + // noop + } + + /** + * Return the keys in this cache. + *

+ * @param cacheName the name of the cache + * @see org.apache.commons.jcs.auxiliary.AuxiliaryCache#getKeySet() + */ + public Set getKeySet( String cacheName ) throws IOException + { + if ( !isInitialized() ) + { + String message = "The Remote Http Client is not initialized. Cannot process request."; + log.warn( message ); + throw new IOException( message ); + } + + RemoteCacheRequest remoteHttpCacheRequest = + RemoteCacheRequestFactory.createGetKeySetRequest(cacheName, 0 ); + + RemoteCacheResponse> remoteHttpCacheResponse = getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest ); + + if ( remoteHttpCacheResponse != null && remoteHttpCacheResponse.getPayload() != null ) + { + return remoteHttpCacheResponse.getPayload(); + } + + return Collections.emptySet(); + } + + /** + * Make and alive request. + *

+ * @return true if we make a successful alive request. + * @throws IOException + */ + public boolean isAlive() + throws IOException + { + if ( !isInitialized() ) + { + String message = "The Remote Http Client is not initialized. Cannot process request."; + log.warn( message ); + throw new IOException( message ); + } + + RemoteCacheRequest remoteHttpCacheRequest = RemoteCacheRequestFactory.createAliveCheckRequest( 0 ); + RemoteCacheResponse remoteHttpCacheResponse = + getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest ); + + if ( remoteHttpCacheResponse != null ) + { + return remoteHttpCacheResponse.isSuccess(); + } + + return false; + } + + /** + * @param remoteDispatcher the remoteDispatcher to set + */ + public void setRemoteDispatcher( IRemoteCacheDispatcher remoteDispatcher ) + { + this.remoteDispatcher = remoteDispatcher; + } + + /** + * @return the remoteDispatcher + */ + public IRemoteCacheDispatcher getRemoteDispatcher() + { + return remoteDispatcher; + } + + /** + * @param remoteHttpCacheAttributes the remoteHttpCacheAttributes to set + */ + public void setRemoteHttpCacheAttributes( RemoteHttpCacheAttributes remoteHttpCacheAttributes ) + { + this.remoteHttpCacheAttributes = remoteHttpCacheAttributes; + } + + /** + * @return the remoteHttpCacheAttributes + */ + public RemoteHttpCacheAttributes getRemoteHttpCacheAttributes() + { + return remoteHttpCacheAttributes; + } + + /** + * @param initialized the initialized to set + */ + protected void setInitialized( boolean initialized ) + { + this.initialized = initialized; + } + + /** + * @return the initialized + */ + protected boolean isInitialized() + { + return initialized; + } +} Modified: commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/remote/http/client/RemoteHttpCacheDispatcher.java URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/remote/http/client/RemoteHttpCacheDispatcher.java?rev=1570993&r1=1570992&r2=1570993&view=diff ============================================================================== --- commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/remote/http/client/RemoteHttpCacheDispatcher.java (original) +++ commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/remote/http/client/RemoteHttpCacheDispatcher.java Sun Feb 23 10:37:48 2014 @@ -1,231 +1,231 @@ -package org.apache.commons.jcs.auxiliary.remote.http.client; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import java.io.IOException; -import java.io.Serializable; -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; - -import org.apache.commons.httpclient.HttpException; -import org.apache.commons.httpclient.HttpMethod; -import org.apache.commons.httpclient.HttpState; -import org.apache.commons.httpclient.methods.ByteArrayRequestEntity; -import org.apache.commons.httpclient.methods.PostMethod; -import org.apache.commons.httpclient.methods.RequestEntity; -import org.apache.commons.jcs.auxiliary.remote.behavior.IRemoteCacheDispatcher; -import org.apache.commons.jcs.auxiliary.remote.value.RemoteCacheRequest; -import org.apache.commons.jcs.auxiliary.remote.value.RemoteCacheResponse; -import org.apache.commons.jcs.utils.serialization.StandardSerializer; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -/** Calls the service. */ -public class RemoteHttpCacheDispatcher - extends AbstractHttpClient - implements IRemoteCacheDispatcher -{ - /** Named of the parameter */ - private static final String PARAMETER_REQUEST_TYPE = "RequestType"; - - /** Named of the parameter */ - private static final String PARAMETER_KEY = "Key"; - - /** Named of the parameter */ - private static final String PARAMETER_CACHE_NAME = "CacheName"; - - /** The Logger. */ - private final static Log log = LogFactory.getLog( RemoteHttpCacheDispatcher.class ); - - /** This needs to be standard, since the other side is standard */ - private StandardSerializer serializer = new StandardSerializer(); - - /** - * @param remoteHttpCacheAttributes - */ - public RemoteHttpCacheDispatcher( RemoteHttpCacheAttributes remoteHttpCacheAttributes ) - { - super( remoteHttpCacheAttributes ); - } - - /** - * All requests will go through this method. - *

- * TODO consider taking in a URL instead of using the one in the configuration. - *

- * @param remoteCacheRequest - * @return RemoteCacheResponse - * @throws IOException - */ - public - RemoteCacheResponse dispatchRequest( RemoteCacheRequest remoteCacheRequest ) - throws IOException - { - try - { - byte[] requestAsByteArray = serializer.serialize( remoteCacheRequest ); - - String url = addParameters( remoteCacheRequest, getRemoteHttpCacheAttributes().getUrl() ); - - byte[] responseAsByteArray = processRequest( requestAsByteArray, url ); - - RemoteCacheResponse remoteCacheResponse = null; - try - { - remoteCacheResponse = serializer.deSerialize( responseAsByteArray ); - } - catch ( ClassNotFoundException e ) - { - log.error( "Couldn't deserialize the response.", e ); - } - return remoteCacheResponse; - } - catch ( Exception e ) - { - log.error( "Problem dispatching request.", e ); - throw new IOException( e.getMessage() ); - } - } - - /** - * @param requestAsByteArray - * @param url - * @return byte[] - the response - * @throws IOException - * @throws HttpException - */ - protected byte[] processRequest( byte[] requestAsByteArray, String url ) - throws IOException, HttpException - { - PostMethod post = new PostMethod( url ); - RequestEntity requestEntity = new ByteArrayRequestEntity( requestAsByteArray ); - post.setRequestEntity( requestEntity ); - doWebserviceCall( post ); - byte[] response = post.getResponseBody(); - return response; - } - - /** - * @param remoteCacheRequest - * @param baseUrl - * @return String - */ - protected String addParameters( RemoteCacheRequest remoteCacheRequest, String baseUrl ) - { - StringBuffer url = new StringBuffer( baseUrl ); - - try - { - if ( baseUrl != null && ( baseUrl.indexOf( "?" ) == -1 ) ) - { - url.append( "?" ); - } - else - { - url.append( "&" ); - } - - if ( getRemoteHttpCacheAttributes().isIncludeCacheNameAsParameter() ) - { - if ( remoteCacheRequest.getCacheName() != null ) - { - url.append( PARAMETER_CACHE_NAME + "=" - + URLEncoder.encode( remoteCacheRequest.getCacheName(), "UTF-8" ) ); - } - } - if ( getRemoteHttpCacheAttributes().isIncludeKeysAndPatternsAsParameter() ) - { - String keyValue = ""; - switch ( remoteCacheRequest.getRequestType() ) - { - case GET: - keyValue = remoteCacheRequest.getKey() + ""; - break; - case REMOVE: - keyValue = remoteCacheRequest.getKey() + ""; - break; - case GET_MATCHING: - keyValue = remoteCacheRequest.getPattern(); - break; - case GET_MULTIPLE: - keyValue = remoteCacheRequest.getKeySet() + ""; - break; - case GET_KEYSET: - keyValue = remoteCacheRequest.getKey() + ""; - break; - case UPDATE: - keyValue = remoteCacheRequest.getCacheElement().getKey() + ""; - break; - default: - break; - } - String encodedKeyValue = URLEncoder.encode( keyValue, "UTF-8" ); - url.append( "&" + PARAMETER_KEY + "=" + encodedKeyValue ); - } - if ( getRemoteHttpCacheAttributes().isIncludeRequestTypeasAsParameter() ) - { - url.append( "&" - + PARAMETER_REQUEST_TYPE - + "=" - + URLEncoder.encode( remoteCacheRequest.getRequestType().toString(), "UTF-8" ) ); - } - } - catch ( UnsupportedEncodingException e ) - { - log.error( "Couldn't encode URL.", e ); - } - - if ( log.isDebugEnabled() ) - { - log.debug( "Url: " + url.toString() ); - } - - return url.toString(); - } - - /** - * Called before the executeMethod on the client. - *

- * @param post http method - * @return HttpState - * @throws IOException - */ - @Override - public HttpState preProcessWebserviceCall( HttpMethod post ) - throws IOException - { - // do nothing. Child can override. - return null; - } - - /** - * Called after the executeMethod on the client. - *

- * @param post http method - * @param httpState state - * @throws IOException - */ - @Override - public void postProcessWebserviceCall( HttpMethod post, HttpState httpState ) - throws IOException - { - // do nothing. Child can override. - } -} +package org.apache.commons.jcs.auxiliary.remote.http.client; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.IOException; +import java.io.Serializable; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + +import org.apache.commons.httpclient.HttpException; +import org.apache.commons.httpclient.HttpMethod; +import org.apache.commons.httpclient.HttpState; +import org.apache.commons.httpclient.methods.ByteArrayRequestEntity; +import org.apache.commons.httpclient.methods.PostMethod; +import org.apache.commons.httpclient.methods.RequestEntity; +import org.apache.commons.jcs.auxiliary.remote.behavior.IRemoteCacheDispatcher; +import org.apache.commons.jcs.auxiliary.remote.value.RemoteCacheRequest; +import org.apache.commons.jcs.auxiliary.remote.value.RemoteCacheResponse; +import org.apache.commons.jcs.utils.serialization.StandardSerializer; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** Calls the service. */ +public class RemoteHttpCacheDispatcher + extends AbstractHttpClient + implements IRemoteCacheDispatcher +{ + /** Named of the parameter */ + private static final String PARAMETER_REQUEST_TYPE = "RequestType"; + + /** Named of the parameter */ + private static final String PARAMETER_KEY = "Key"; + + /** Named of the parameter */ + private static final String PARAMETER_CACHE_NAME = "CacheName"; + + /** The Logger. */ + private final static Log log = LogFactory.getLog( RemoteHttpCacheDispatcher.class ); + + /** This needs to be standard, since the other side is standard */ + private StandardSerializer serializer = new StandardSerializer(); + + /** + * @param remoteHttpCacheAttributes + */ + public RemoteHttpCacheDispatcher( RemoteHttpCacheAttributes remoteHttpCacheAttributes ) + { + super( remoteHttpCacheAttributes ); + } + + /** + * All requests will go through this method. + *

+ * TODO consider taking in a URL instead of using the one in the configuration. + *

+ * @param remoteCacheRequest + * @return RemoteCacheResponse + * @throws IOException + */ + public + RemoteCacheResponse dispatchRequest( RemoteCacheRequest remoteCacheRequest ) + throws IOException + { + try + { + byte[] requestAsByteArray = serializer.serialize( remoteCacheRequest ); + + String url = addParameters( remoteCacheRequest, getRemoteHttpCacheAttributes().getUrl() ); + + byte[] responseAsByteArray = processRequest( requestAsByteArray, url ); + + RemoteCacheResponse remoteCacheResponse = null; + try + { + remoteCacheResponse = serializer.deSerialize( responseAsByteArray ); + } + catch ( ClassNotFoundException e ) + { + log.error( "Couldn't deserialize the response.", e ); + } + return remoteCacheResponse; + } + catch ( Exception e ) + { + log.error( "Problem dispatching request.", e ); + throw new IOException( e.getMessage() ); + } + } + + /** + * @param requestAsByteArray + * @param url + * @return byte[] - the response + * @throws IOException + * @throws HttpException + */ + protected byte[] processRequest( byte[] requestAsByteArray, String url ) + throws IOException, HttpException + { + PostMethod post = new PostMethod( url ); + RequestEntity requestEntity = new ByteArrayRequestEntity( requestAsByteArray ); + post.setRequestEntity( requestEntity ); + doWebserviceCall( post ); + byte[] response = post.getResponseBody(); + return response; + } + + /** + * @param remoteCacheRequest + * @param baseUrl + * @return String + */ + protected String addParameters( RemoteCacheRequest remoteCacheRequest, String baseUrl ) + { + StringBuffer url = new StringBuffer( baseUrl ); + + try + { + if ( baseUrl != null && ( baseUrl.indexOf( "?" ) == -1 ) ) + { + url.append( "?" ); + } + else + { + url.append( "&" ); + } + + if ( getRemoteHttpCacheAttributes().isIncludeCacheNameAsParameter() ) + { + if ( remoteCacheRequest.getCacheName() != null ) + { + url.append( PARAMETER_CACHE_NAME + "=" + + URLEncoder.encode( remoteCacheRequest.getCacheName(), "UTF-8" ) ); + } + } + if ( getRemoteHttpCacheAttributes().isIncludeKeysAndPatternsAsParameter() ) + { + String keyValue = ""; + switch ( remoteCacheRequest.getRequestType() ) + { + case GET: + keyValue = remoteCacheRequest.getKey() + ""; + break; + case REMOVE: + keyValue = remoteCacheRequest.getKey() + ""; + break; + case GET_MATCHING: + keyValue = remoteCacheRequest.getPattern(); + break; + case GET_MULTIPLE: + keyValue = remoteCacheRequest.getKeySet() + ""; + break; + case GET_KEYSET: + keyValue = remoteCacheRequest.getKey() + ""; + break; + case UPDATE: + keyValue = remoteCacheRequest.getCacheElement().getKey() + ""; + break; + default: + break; + } + String encodedKeyValue = URLEncoder.encode( keyValue, "UTF-8" ); + url.append( "&" + PARAMETER_KEY + "=" + encodedKeyValue ); + } + if ( getRemoteHttpCacheAttributes().isIncludeRequestTypeasAsParameter() ) + { + url.append( "&" + + PARAMETER_REQUEST_TYPE + + "=" + + URLEncoder.encode( remoteCacheRequest.getRequestType().toString(), "UTF-8" ) ); + } + } + catch ( UnsupportedEncodingException e ) + { + log.error( "Couldn't encode URL.", e ); + } + + if ( log.isDebugEnabled() ) + { + log.debug( "Url: " + url.toString() ); + } + + return url.toString(); + } + + /** + * Called before the executeMethod on the client. + *

+ * @param post http method + * @return HttpState + * @throws IOException + */ + @Override + public HttpState preProcessWebserviceCall( HttpMethod post ) + throws IOException + { + // do nothing. Child can override. + return null; + } + + /** + * Called after the executeMethod on the client. + *

+ * @param post http method + * @param httpState state + * @throws IOException + */ + @Override + public void postProcessWebserviceCall( HttpMethod post, HttpState httpState ) + throws IOException + { + // do nothing. Child can override. + } +} Modified: commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/remote/http/client/RemoteHttpCacheFactory.java URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/remote/http/client/RemoteHttpCacheFactory.java?rev=1570993&r1=1570992&r2=1570993&view=diff ============================================================================== --- commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/remote/http/client/RemoteHttpCacheFactory.java (original) +++ commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/remote/http/client/RemoteHttpCacheFactory.java Sun Feb 23 10:37:48 2014 @@ -1,129 +1,129 @@ -package org.apache.commons.jcs.auxiliary.remote.http.client; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.HashMap; - -import org.apache.commons.jcs.auxiliary.AuxiliaryCache; -import org.apache.commons.jcs.auxiliary.AuxiliaryCacheAttributes; -import org.apache.commons.jcs.auxiliary.AuxiliaryCacheFactory; -import org.apache.commons.jcs.auxiliary.remote.RemoteCacheNoWait; -import org.apache.commons.jcs.auxiliary.remote.RemoteCacheNoWaitFacade; -import org.apache.commons.jcs.auxiliary.remote.server.behavior.RemoteType; -import org.apache.commons.jcs.engine.behavior.ICache; -import org.apache.commons.jcs.engine.behavior.ICompositeCacheManager; -import org.apache.commons.jcs.engine.behavior.IElementSerializer; -import org.apache.commons.jcs.engine.logging.behavior.ICacheEventLogger; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -/** - * The RemoteCacheFactory creates remote caches for the cache hub. It returns a no wait facade which - * is a wrapper around a no wait. The no wait object is either an active connection to a remote - * cache or a balking zombie if the remote cache is not accessible. It should be transparent to the - * clients. - */ -public class RemoteHttpCacheFactory - implements AuxiliaryCacheFactory -{ - /** The logger. */ - private final static Log log = LogFactory.getLog( RemoteHttpCacheFactory.class ); - - /** The name of this auxiliary */ - private String name; - - /** store reference of facades to initiate failover */ - private final static HashMap> facades = - new HashMap>(); - - /** - * For LOCAL clients we get a handle to all the failovers, but we do not register a listener - * with them. We create the RemoteCacheManager, but we do not get a cache. - *

- * The failover runner will get a cache from the manager. When the primary is restored it will - * tell the manager for the failover to deregister the listener. - *

- * @param iaca - * @param cacheMgr - * @param cacheEventLogger - * @param elementSerializer - * @return AuxiliaryCache - */ - public AuxiliaryCache createCache( AuxiliaryCacheAttributes iaca, ICompositeCacheManager cacheMgr, - ICacheEventLogger cacheEventLogger, IElementSerializer elementSerializer ) - { - RemoteHttpCacheAttributes rca = (RemoteHttpCacheAttributes) iaca; - - ArrayList> noWaits = new ArrayList>(); - - RemoteHttpCacheManager rcm = RemoteHttpCacheManager.getInstance( cacheMgr, cacheEventLogger, elementSerializer ); - // TODO, use the configured value. - rca.setRemoteType( RemoteType.LOCAL ); - ICache ic = rcm.getCache( rca ); - if ( ic != null ) - { - noWaits.add( ic ); - } - else - { - log.info( "noWait is null" ); - } - - @SuppressWarnings("unchecked") // No generic arrays in java - RemoteCacheNoWait[] rcnwArray = noWaits.toArray( new RemoteCacheNoWait[0] ); - RemoteCacheNoWaitFacade rcnwf = - new RemoteCacheNoWaitFacade(rcnwArray, rca, cacheMgr, cacheEventLogger, elementSerializer ); - - getFacades().put( rca.getCacheName(), rcnwf ); - - return rcnwf; - } - - /** - * Gets the name attribute of the RemoteCacheFactory object - *

- * @return The name value - */ - public String getName() - { - return this.name; - } - - /** - * Sets the name attribute of the RemoteCacheFactory object - *

- * @param name The new name value - */ - public void setName( String name ) - { - this.name = name; - } - - /** - * The facades are what the cache hub talks to. - * @return Returns the facades. - */ - public static HashMap> getFacades() - { - return facades; - } -} +package org.apache.commons.jcs.auxiliary.remote.http.client; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.HashMap; + +import org.apache.commons.jcs.auxiliary.AuxiliaryCache; +import org.apache.commons.jcs.auxiliary.AuxiliaryCacheAttributes; +import org.apache.commons.jcs.auxiliary.AuxiliaryCacheFactory; +import org.apache.commons.jcs.auxiliary.remote.RemoteCacheNoWait; +import org.apache.commons.jcs.auxiliary.remote.RemoteCacheNoWaitFacade; +import org.apache.commons.jcs.auxiliary.remote.server.behavior.RemoteType; +import org.apache.commons.jcs.engine.behavior.ICache; +import org.apache.commons.jcs.engine.behavior.ICompositeCacheManager; +import org.apache.commons.jcs.engine.behavior.IElementSerializer; +import org.apache.commons.jcs.engine.logging.behavior.ICacheEventLogger; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * The RemoteCacheFactory creates remote caches for the cache hub. It returns a no wait facade which + * is a wrapper around a no wait. The no wait object is either an active connection to a remote + * cache or a balking zombie if the remote cache is not accessible. It should be transparent to the + * clients. + */ +public class RemoteHttpCacheFactory + implements AuxiliaryCacheFactory +{ + /** The logger. */ + private final static Log log = LogFactory.getLog( RemoteHttpCacheFactory.class ); + + /** The name of this auxiliary */ + private String name; + + /** store reference of facades to initiate failover */ + private final static HashMap> facades = + new HashMap>(); + + /** + * For LOCAL clients we get a handle to all the failovers, but we do not register a listener + * with them. We create the RemoteCacheManager, but we do not get a cache. + *

+ * The failover runner will get a cache from the manager. When the primary is restored it will + * tell the manager for the failover to deregister the listener. + *

+ * @param iaca + * @param cacheMgr + * @param cacheEventLogger + * @param elementSerializer + * @return AuxiliaryCache + */ + public AuxiliaryCache createCache( AuxiliaryCacheAttributes iaca, ICompositeCacheManager cacheMgr, + ICacheEventLogger cacheEventLogger, IElementSerializer elementSerializer ) + { + RemoteHttpCacheAttributes rca = (RemoteHttpCacheAttributes) iaca; + + ArrayList> noWaits = new ArrayList>(); + + RemoteHttpCacheManager rcm = RemoteHttpCacheManager.getInstance( cacheMgr, cacheEventLogger, elementSerializer ); + // TODO, use the configured value. + rca.setRemoteType( RemoteType.LOCAL ); + ICache ic = rcm.getCache( rca ); + if ( ic != null ) + { + noWaits.add( ic ); + } + else + { + log.info( "noWait is null" ); + } + + @SuppressWarnings("unchecked") // No generic arrays in java + RemoteCacheNoWait[] rcnwArray = noWaits.toArray( new RemoteCacheNoWait[0] ); + RemoteCacheNoWaitFacade rcnwf = + new RemoteCacheNoWaitFacade(rcnwArray, rca, cacheMgr, cacheEventLogger, elementSerializer ); + + getFacades().put( rca.getCacheName(), rcnwf ); + + return rcnwf; + } + + /** + * Gets the name attribute of the RemoteCacheFactory object + *

+ * @return The name value + */ + public String getName() + { + return this.name; + } + + /** + * Sets the name attribute of the RemoteCacheFactory object + *

+ * @param name The new name value + */ + public void setName( String name ) + { + this.name = name; + } + + /** + * The facades are what the cache hub talks to. + * @return Returns the facades. + */ + public static HashMap> getFacades() + { + return facades; + } +} Modified: commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/remote/http/client/RemoteHttpCacheManager.java URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/remote/http/client/RemoteHttpCacheManager.java?rev=1570993&r1=1570992&r2=1570993&view=diff ============================================================================== --- commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/remote/http/client/RemoteHttpCacheManager.java (original) +++ commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/remote/http/client/RemoteHttpCacheManager.java Sun Feb 23 10:37:48 2014 @@ -1,274 +1,274 @@ -package org.apache.commons.jcs.auxiliary.remote.http.client; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import java.io.Serializable; -import java.util.HashMap; -import java.util.Map; - -import org.apache.commons.jcs.auxiliary.AuxiliaryCacheManager; -import org.apache.commons.jcs.auxiliary.remote.RemoteCacheNoWait; -import org.apache.commons.jcs.auxiliary.remote.behavior.IRemoteCacheAttributes; -import org.apache.commons.jcs.auxiliary.remote.behavior.IRemoteCacheClient; -import org.apache.commons.jcs.auxiliary.remote.http.client.behavior.IRemoteHttpCacheClient; -import org.apache.commons.jcs.engine.behavior.ICompositeCacheManager; -import org.apache.commons.jcs.engine.behavior.IElementSerializer; -import org.apache.commons.jcs.engine.behavior.IShutdownObserver; -import org.apache.commons.jcs.engine.control.CompositeCacheManager; -import org.apache.commons.jcs.engine.logging.behavior.ICacheEventLogger; -import org.apache.commons.jcs.utils.config.OptionConverter; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -/** - * This is a very crude copy of the RMI remote manager. It needs a lot of work! - */ -public class RemoteHttpCacheManager - implements AuxiliaryCacheManager, IShutdownObserver -{ - /** Don't change */ - private static final long serialVersionUID = 798077557166389498L; - - /** The logger */ - private final static Log log = LogFactory.getLog( RemoteHttpCacheManager.class ); - - /** Contains mappings of Location instance to RemoteCacheManager instance. */ - private static RemoteHttpCacheManager instance; - - /** Contains instances of RemoteCacheNoWait managed by a RemoteCacheManager instance. */ - static final Map> caches = - new HashMap>(); - - /** The configuration attributes. */ - private IRemoteCacheAttributes remoteCacheAttributes; - - /** The event logger. */ - private final ICacheEventLogger cacheEventLogger; - - /** The serializer. */ - private final IElementSerializer elementSerializer; - - /** The cache manager listeners will need to use to get a cache. */ - private final ICompositeCacheManager cacheMgr; - - /** Remote cache monitor. */ - private static RemoteHttpCacheMonitor monitor; - - /** - * Constructs an instance to with the given remote connection parameters. If the connection - * cannot be made, "zombie" services will be temporarily used until a successful re-connection - * is made by the monitoring daemon. - *

- * @param cacheMgr - * @param cacheEventLogger - * @param elementSerializer - */ - private RemoteHttpCacheManager( ICompositeCacheManager cacheMgr, ICacheEventLogger cacheEventLogger, - IElementSerializer elementSerializer ) - { - this.cacheMgr = cacheMgr; - this.cacheEventLogger = cacheEventLogger; - this.elementSerializer = elementSerializer; - - // register shutdown observer - // TODO add the shutdown observable methods to the interface - if ( this.cacheMgr instanceof CompositeCacheManager ) - { - ( (CompositeCacheManager) this.cacheMgr ).registerShutdownObserver( this ); - } - } - - /** - * Gets the defaultCattr attribute of the RemoteCacheManager object. - *

- * @return The defaultCattr value - */ - public IRemoteCacheAttributes getDefaultCattr() - { - return this.remoteCacheAttributes; - } - - /** @return Returns an instance if it exists. else null. */ - public synchronized static RemoteHttpCacheManager getInstance() - { - return instance; - } - - /** - * Get the singleton instance. - *

- * @param cacheMgr - * @param cacheEventLogger - * @param elementSerializer - * @return The instance value - */ - public synchronized static RemoteHttpCacheManager getInstance( ICompositeCacheManager cacheMgr, - ICacheEventLogger cacheEventLogger, - IElementSerializer elementSerializer ) - { - if ( instance == null ) - { - instance = new RemoteHttpCacheManager( cacheMgr, cacheEventLogger, elementSerializer ); - } - - // Fires up the monitoring daemon. - if ( monitor == null ) - { - monitor = RemoteHttpCacheMonitor.getInstance(); - // If the returned monitor is null, it means it's already started - // elsewhere. - if ( monitor != null ) - { - Thread t = new Thread( monitor ); - t.setDaemon( true ); - t.start(); - } - } - - return instance; - } - - /** - * Returns a remote cache for the given cache name. - *

- * @param cacheName - * @return The cache value - */ - public RemoteCacheNoWait getCache( String cacheName ) - { - // TODO get some defaults! - // Perhaps we will need a manager per URL???? - RemoteHttpCacheAttributes ca = new RemoteHttpCacheAttributes(); - ca.setCacheName( cacheName ); - return getCache( ca ); - } - - /** - * Gets a RemoteCacheNoWait from the RemoteCacheManager. The RemoteCacheNoWait objects are - * identified by the cache name value of the RemoteCacheAttributes object. - *

- * If the client is configured to register a listener, this call results on a listener being - * created if one isn't already registered with the remote cache for this region. - *

- * @param cattr - * @return The cache value - */ - public RemoteCacheNoWait getCache( RemoteHttpCacheAttributes cattr ) - { - RemoteCacheNoWait remoteCacheNoWait = null; - - synchronized ( caches ) - { - @SuppressWarnings("unchecked") // Need to cast because of common map for all caches - RemoteCacheNoWait remoteCacheNoWait2 = (RemoteCacheNoWait) caches.get( cattr.getCacheName() + cattr.getUrl() ); - remoteCacheNoWait = remoteCacheNoWait2; - if ( remoteCacheNoWait == null ) - { - RemoteHttpClientListener listener = new RemoteHttpClientListener( cattr, cacheMgr ); - - IRemoteHttpCacheClient remoteService = createRemoteHttpCacheClientForAttributes( cattr ); - - IRemoteCacheClient remoteCacheClient = new RemoteHttpCache( cattr, remoteService, listener ); - remoteCacheClient.setCacheEventLogger( cacheEventLogger ); - remoteCacheClient.setElementSerializer( elementSerializer ); - - remoteCacheNoWait = new RemoteCacheNoWait( remoteCacheClient ); - remoteCacheNoWait.setCacheEventLogger( cacheEventLogger ); - remoteCacheNoWait.setElementSerializer( elementSerializer ); - - caches.put( cattr.getCacheName() + cattr.getUrl(), remoteCacheNoWait ); - } - // might want to do some listener sanity checking here. - } - - return remoteCacheNoWait; - } - - /** - * This is an extension point. The manager and other classes will only create - * RemoteHttpCacheClient through this method. - *

- * @param cattr - * @return IRemoteHttpCacheClient - */ - protected IRemoteHttpCacheClient createRemoteHttpCacheClientForAttributes( RemoteHttpCacheAttributes cattr ) - { - IRemoteHttpCacheClient client = OptionConverter.instantiateByClassName( cattr - .getRemoteHttpClientClassName(), null ); - - if ( client == null ) - { - if ( log.isInfoEnabled() ) - { - log.info( "Creating the default client." ); - } - client = new RemoteHttpCacheClient( ); - } - client.initialize( cattr ); - return client; - } - - /** - * Gets the stats attribute of the RemoteCacheManager object - *

- * @return The stats value - */ - public String getStats() - { - StringBuffer stats = new StringBuffer(); - for (RemoteCacheNoWait c : caches.values()) - { - if ( c != null ) - { - stats.append( c.getCacheName() ); - } - } - return stats.toString(); - } - - /** - * Shutdown callback from composite cache manager. - *

- * @see org.apache.commons.jcs.engine.behavior.IShutdownObserver#shutdown() - */ - public void shutdown() - { - if ( log.isInfoEnabled() ) - { - log.info( "Observed shutdown request." ); - } - //release(); - } - - /** - * Logs an event if an event logger is configured. - *

- * @param source - * @param eventName - * @param optionalDetails - */ - protected void logApplicationEvent( String source, String eventName, String optionalDetails ) - { - if ( cacheEventLogger != null ) - { - cacheEventLogger.logApplicationEvent( source, eventName, optionalDetails ); - } - } -} +package org.apache.commons.jcs.auxiliary.remote.http.client; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.jcs.auxiliary.AuxiliaryCacheManager; +import org.apache.commons.jcs.auxiliary.remote.RemoteCacheNoWait; +import org.apache.commons.jcs.auxiliary.remote.behavior.IRemoteCacheAttributes; +import org.apache.commons.jcs.auxiliary.remote.behavior.IRemoteCacheClient; +import org.apache.commons.jcs.auxiliary.remote.http.client.behavior.IRemoteHttpCacheClient; +import org.apache.commons.jcs.engine.behavior.ICompositeCacheManager; +import org.apache.commons.jcs.engine.behavior.IElementSerializer; +import org.apache.commons.jcs.engine.behavior.IShutdownObserver; +import org.apache.commons.jcs.engine.control.CompositeCacheManager; +import org.apache.commons.jcs.engine.logging.behavior.ICacheEventLogger; +import org.apache.commons.jcs.utils.config.OptionConverter; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * This is a very crude copy of the RMI remote manager. It needs a lot of work! + */ +public class RemoteHttpCacheManager + implements AuxiliaryCacheManager, IShutdownObserver +{ + /** Don't change */ + private static final long serialVersionUID = 798077557166389498L; + + /** The logger */ + private final static Log log = LogFactory.getLog( RemoteHttpCacheManager.class ); + + /** Contains mappings of Location instance to RemoteCacheManager instance. */ + private static RemoteHttpCacheManager instance; + + /** Contains instances of RemoteCacheNoWait managed by a RemoteCacheManager instance. */ + static final Map> caches = + new HashMap>(); + + /** The configuration attributes. */ + private IRemoteCacheAttributes remoteCacheAttributes; + + /** The event logger. */ + private final ICacheEventLogger cacheEventLogger; + + /** The serializer. */ + private final IElementSerializer elementSerializer; + + /** The cache manager listeners will need to use to get a cache. */ + private final ICompositeCacheManager cacheMgr; + + /** Remote cache monitor. */ + private static RemoteHttpCacheMonitor monitor; + + /** + * Constructs an instance to with the given remote connection parameters. If the connection + * cannot be made, "zombie" services will be temporarily used until a successful re-connection + * is made by the monitoring daemon. + *

+ * @param cacheMgr + * @param cacheEventLogger + * @param elementSerializer + */ + private RemoteHttpCacheManager( ICompositeCacheManager cacheMgr, ICacheEventLogger cacheEventLogger, + IElementSerializer elementSerializer ) + { + this.cacheMgr = cacheMgr; + this.cacheEventLogger = cacheEventLogger; + this.elementSerializer = elementSerializer; + + // register shutdown observer + // TODO add the shutdown observable methods to the interface + if ( this.cacheMgr instanceof CompositeCacheManager ) + { + ( (CompositeCacheManager) this.cacheMgr ).registerShutdownObserver( this ); + } + } + + /** + * Gets the defaultCattr attribute of the RemoteCacheManager object. + *

+ * @return The defaultCattr value + */ + public IRemoteCacheAttributes getDefaultCattr() + { + return this.remoteCacheAttributes; + } + + /** @return Returns an instance if it exists. else null. */ + public synchronized static RemoteHttpCacheManager getInstance() + { + return instance; + } + + /** + * Get the singleton instance. + *

+ * @param cacheMgr + * @param cacheEventLogger + * @param elementSerializer + * @return The instance value + */ + public synchronized static RemoteHttpCacheManager getInstance( ICompositeCacheManager cacheMgr, + ICacheEventLogger cacheEventLogger, + IElementSerializer elementSerializer ) + { + if ( instance == null ) + { + instance = new RemoteHttpCacheManager( cacheMgr, cacheEventLogger, elementSerializer ); + } + + // Fires up the monitoring daemon. + if ( monitor == null ) + { + monitor = RemoteHttpCacheMonitor.getInstance(); + // If the returned monitor is null, it means it's already started + // elsewhere. + if ( monitor != null ) + { + Thread t = new Thread( monitor ); + t.setDaemon( true ); + t.start(); + } + } + + return instance; + } + + /** + * Returns a remote cache for the given cache name. + *

+ * @param cacheName + * @return The cache value + */ + public RemoteCacheNoWait getCache( String cacheName ) + { + // TODO get some defaults! + // Perhaps we will need a manager per URL???? + RemoteHttpCacheAttributes ca = new RemoteHttpCacheAttributes(); + ca.setCacheName( cacheName ); + return getCache( ca ); + } + + /** + * Gets a RemoteCacheNoWait from the RemoteCacheManager. The RemoteCacheNoWait objects are + * identified by the cache name value of the RemoteCacheAttributes object. + *

+ * If the client is configured to register a listener, this call results on a listener being + * created if one isn't already registered with the remote cache for this region. + *

+ * @param cattr + * @return The cache value + */ + public RemoteCacheNoWait getCache( RemoteHttpCacheAttributes cattr ) + { + RemoteCacheNoWait remoteCacheNoWait = null; + + synchronized ( caches ) + { + @SuppressWarnings("unchecked") // Need to cast because of common map for all caches + RemoteCacheNoWait remoteCacheNoWait2 = (RemoteCacheNoWait) caches.get( cattr.getCacheName() + cattr.getUrl() ); + remoteCacheNoWait = remoteCacheNoWait2; + if ( remoteCacheNoWait == null ) + { + RemoteHttpClientListener listener = new RemoteHttpClientListener( cattr, cacheMgr ); + + IRemoteHttpCacheClient remoteService = createRemoteHttpCacheClientForAttributes( cattr ); + + IRemoteCacheClient remoteCacheClient = new RemoteHttpCache( cattr, remoteService, listener ); + remoteCacheClient.setCacheEventLogger( cacheEventLogger ); + remoteCacheClient.setElementSerializer( elementSerializer ); + + remoteCacheNoWait = new RemoteCacheNoWait( remoteCacheClient ); + remoteCacheNoWait.setCacheEventLogger( cacheEventLogger ); + remoteCacheNoWait.setElementSerializer( elementSerializer ); + + caches.put( cattr.getCacheName() + cattr.getUrl(), remoteCacheNoWait ); + } + // might want to do some listener sanity checking here. + } + + return remoteCacheNoWait; + } + + /** + * This is an extension point. The manager and other classes will only create + * RemoteHttpCacheClient through this method. + *

+ * @param cattr + * @return IRemoteHttpCacheClient + */ + protected IRemoteHttpCacheClient createRemoteHttpCacheClientForAttributes( RemoteHttpCacheAttributes cattr ) + { + IRemoteHttpCacheClient client = OptionConverter.instantiateByClassName( cattr + .getRemoteHttpClientClassName(), null ); + + if ( client == null ) + { + if ( log.isInfoEnabled() ) + { + log.info( "Creating the default client." ); + } + client = new RemoteHttpCacheClient( ); + } + client.initialize( cattr ); + return client; + } + + /** + * Gets the stats attribute of the RemoteCacheManager object + *

+ * @return The stats value + */ + public String getStats() + { + StringBuffer stats = new StringBuffer(); + for (RemoteCacheNoWait c : caches.values()) + { + if ( c != null ) + { + stats.append( c.getCacheName() ); + } + } + return stats.toString(); + } + + /** + * Shutdown callback from composite cache manager. + *

+ * @see org.apache.commons.jcs.engine.behavior.IShutdownObserver#shutdown() + */ + public void shutdown() + { + if ( log.isInfoEnabled() ) + { + log.info( "Observed shutdown request." ); + } + //release(); + } + + /** + * Logs an event if an event logger is configured. + *

+ * @param source + * @param eventName + * @param optionalDetails + */ + protected void logApplicationEvent( String source, String eventName, String optionalDetails ) + { + if ( cacheEventLogger != null ) + { + cacheEventLogger.logApplicationEvent( source, eventName, optionalDetails ); + } + } +}