Modified: commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/disk/jdbc/JDBCDiskCachePoolAccessManager.java URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/disk/jdbc/JDBCDiskCachePoolAccessManager.java?rev=1570993&r1=1570992&r2=1570993&view=diff ============================================================================== --- commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/disk/jdbc/JDBCDiskCachePoolAccessManager.java (original) +++ commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/disk/jdbc/JDBCDiskCachePoolAccessManager.java Sun Feb 23 10:37:48 2014 @@ -1,142 +1,161 @@ -package org.apache.commons.jcs.auxiliary.disk.jdbc; - -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; - -import org.apache.commons.jcs.utils.config.PropertySetter; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -/** - * Manages JDBCDiskCachePoolAccess instances. If a connectionPoolName value is supplied, the JDBC - * disk cache will try to use this manager to create a pool. Assuming the name is "MyPool": - * - * <pre> - * jcs.jdbcconnectionpool.MyPool.attributes.userName=MyUserName - * jcs.jdbcconnectionpool.MyPool.attributes.password=MyPassword - * jcs.jdbcconnectionpool.MyPool.attributes.url=MyUrl - * jcs.jdbcconnectionpool.MyPool.attributes.maxActive=MyMaxActive - * jcs.jdbcconnectionpool.MyPool.attributes.driverClassName=MyDriverClassName - * </pre> - */ -public class JDBCDiskCachePoolAccessManager -{ - /** Singleton instance */ - private static JDBCDiskCachePoolAccessManager instance; - - /** Pool name to JDBCDiskCachePoolAccess */ - private final Map<String, JDBCDiskCachePoolAccess> pools = new HashMap<String, JDBCDiskCachePoolAccess>(); - - /** props prefix */ - public static final String POOL_CONFIGURATION_PREFIX = "jcs.jdbcconnectionpool."; - - /** .attributes */ - public final static String ATTRIBUTE_PREFIX = ".attributes"; - - /** The logger. */ - private static final Log log = LogFactory.getLog( JDBCDiskCachePoolAccessManager.class ); - - /** - * You can specify the properties to be used to configure the thread pool. Setting this post - * initialization will have no effect. - */ - private Properties props = null; - - /** - * Singleton, private - * <p> - * @param props - */ - private JDBCDiskCachePoolAccessManager( Properties props ) - { - this.setProps( props ); - } - - /** - * returns a singleton instance - * <p> - * @param props - * @return JDBCDiskCachePoolAccessManager - */ - public static synchronized JDBCDiskCachePoolAccessManager getInstance( Properties props ) - { - if ( instance == null ) - { - instance = new JDBCDiskCachePoolAccessManager( props ); - } - return instance; - } - - /** - * Returns a pool for the name if one has been created. Otherwise it creates a pool. - * <p> - * @param poolName - * @return JDBCDiskCachePoolAccess - */ - public synchronized JDBCDiskCachePoolAccess getJDBCDiskCachePoolAccess( String poolName ) - { - JDBCDiskCachePoolAccess poolAccess = pools.get( poolName ); - - if ( poolAccess == null ) - { - JDBCDiskCachePoolAccessAttributes poolAttributes = configurePoolAccessAttributes( poolName ); - try - { - poolAccess = JDBCDiskCachePoolAccessFactory.createPoolAccess( poolAttributes ); - - if ( log.isInfoEnabled() ) - { - log.info( "Created shared pooled access for pool name [" + poolName + "]." ); - } - pools.put( poolName, poolAccess ); - } - catch ( Exception e ) - { - log.error( "Problem creating connection poolfor pool name [" + poolName + "].", e ); - } - } - - return poolAccess; - } - - - - /** - * Configures the attributes using the properties. - * <p> - * @param poolName - * @return JDBCDiskCachePoolAccessAttributes - */ - protected JDBCDiskCachePoolAccessAttributes configurePoolAccessAttributes( String poolName ) - { - JDBCDiskCachePoolAccessAttributes poolAttributes = new JDBCDiskCachePoolAccessAttributes(); - - String poolAccessAttributePrefix = POOL_CONFIGURATION_PREFIX + poolName + ATTRIBUTE_PREFIX; - PropertySetter.setProperties( poolAttributes, getProps(), poolAccessAttributePrefix + "." ); - - poolAttributes.setPoolName( poolName ); - - if ( log.isInfoEnabled() ) - { - log.info( "Configured attributes " + poolAttributes ); - } - return poolAttributes; - } - - /** - * @param props the props to set - */ - protected void setProps( Properties props ) - { - this.props = props; - } - - /** - * @return the props - */ - protected Properties getProps() - { - return props; - } -} +package org.apache.commons.jcs.auxiliary.disk.jdbc; + +/* + * 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.util.HashMap; +import java.util.Map; +import java.util.Properties; + +import org.apache.commons.jcs.utils.config.PropertySetter; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * Manages JDBCDiskCachePoolAccess instances. If a connectionPoolName value is supplied, the JDBC + * disk cache will try to use this manager to create a pool. Assuming the name is "MyPool": + * + * <pre> + * jcs.jdbcconnectionpool.MyPool.attributes.userName=MyUserName + * jcs.jdbcconnectionpool.MyPool.attributes.password=MyPassword + * jcs.jdbcconnectionpool.MyPool.attributes.url=MyUrl + * jcs.jdbcconnectionpool.MyPool.attributes.maxActive=MyMaxActive + * jcs.jdbcconnectionpool.MyPool.attributes.driverClassName=MyDriverClassName + * </pre> + */ +public class JDBCDiskCachePoolAccessManager +{ + /** Singleton instance */ + private static JDBCDiskCachePoolAccessManager instance; + + /** Pool name to JDBCDiskCachePoolAccess */ + private final Map<String, JDBCDiskCachePoolAccess> pools = new HashMap<String, JDBCDiskCachePoolAccess>(); + + /** props prefix */ + public static final String POOL_CONFIGURATION_PREFIX = "jcs.jdbcconnectionpool."; + + /** .attributes */ + public final static String ATTRIBUTE_PREFIX = ".attributes"; + + /** The logger. */ + private static final Log log = LogFactory.getLog( JDBCDiskCachePoolAccessManager.class ); + + /** + * You can specify the properties to be used to configure the thread pool. Setting this post + * initialization will have no effect. + */ + private Properties props = null; + + /** + * Singleton, private + * <p> + * @param props + */ + private JDBCDiskCachePoolAccessManager( Properties props ) + { + this.setProps( props ); + } + + /** + * returns a singleton instance + * <p> + * @param props + * @return JDBCDiskCachePoolAccessManager + */ + public static synchronized JDBCDiskCachePoolAccessManager getInstance( Properties props ) + { + if ( instance == null ) + { + instance = new JDBCDiskCachePoolAccessManager( props ); + } + return instance; + } + + /** + * Returns a pool for the name if one has been created. Otherwise it creates a pool. + * <p> + * @param poolName + * @return JDBCDiskCachePoolAccess + */ + public synchronized JDBCDiskCachePoolAccess getJDBCDiskCachePoolAccess( String poolName ) + { + JDBCDiskCachePoolAccess poolAccess = pools.get( poolName ); + + if ( poolAccess == null ) + { + JDBCDiskCachePoolAccessAttributes poolAttributes = configurePoolAccessAttributes( poolName ); + try + { + poolAccess = JDBCDiskCachePoolAccessFactory.createPoolAccess( poolAttributes ); + + if ( log.isInfoEnabled() ) + { + log.info( "Created shared pooled access for pool name [" + poolName + "]." ); + } + pools.put( poolName, poolAccess ); + } + catch ( Exception e ) + { + log.error( "Problem creating connection poolfor pool name [" + poolName + "].", e ); + } + } + + return poolAccess; + } + + + + /** + * Configures the attributes using the properties. + * <p> + * @param poolName + * @return JDBCDiskCachePoolAccessAttributes + */ + protected JDBCDiskCachePoolAccessAttributes configurePoolAccessAttributes( String poolName ) + { + JDBCDiskCachePoolAccessAttributes poolAttributes = new JDBCDiskCachePoolAccessAttributes(); + + String poolAccessAttributePrefix = POOL_CONFIGURATION_PREFIX + poolName + ATTRIBUTE_PREFIX; + PropertySetter.setProperties( poolAttributes, getProps(), poolAccessAttributePrefix + "." ); + + poolAttributes.setPoolName( poolName ); + + if ( log.isInfoEnabled() ) + { + log.info( "Configured attributes " + poolAttributes ); + } + return poolAttributes; + } + + /** + * @param props the props to set + */ + protected void setProps( Properties props ) + { + this.props = props; + } + + /** + * @return the props + */ + protected Properties getProps() + { + return props; + } +}
Modified: commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/lateral/LateralCacheAttributes.java URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/lateral/LateralCacheAttributes.java?rev=1570993&r1=1570992&r2=1570993&view=diff ============================================================================== --- commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/lateral/LateralCacheAttributes.java (original) +++ commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/lateral/LateralCacheAttributes.java Sun Feb 23 10:37:48 2014 @@ -40,10 +40,10 @@ public class LateralCacheAttributes /** THe type of lateral */ private String transmissionTypeName = "UDP"; - + /** indicates the lateral type, this needs to change */ private int transmissionType = UDP; - + /** The heep servers */ private String httpServers; @@ -70,7 +70,7 @@ public class LateralCacheAttributes /** If the primary fails, we will queue items before reconnect. This limits the number of items that can be queued. */ private int zombieQueueMaxSize = DEFAULT_ZOMBIE_QUEUE_MAX_SIZE; - + /** * Sets the httpServer attribute of the LateralCacheAttributes object * <P> @@ -303,7 +303,7 @@ public class LateralCacheAttributes { return zombieQueueMaxSize; } - + /** * @return debug string. */ Modified: commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/lateral/behavior/ILateralCacheAttributes.java URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/lateral/behavior/ILateralCacheAttributes.java?rev=1570993&r1=1570992&r2=1570993&view=diff ============================================================================== --- commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/lateral/behavior/ILateralCacheAttributes.java (original) +++ commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/lateral/behavior/ILateralCacheAttributes.java Sun Feb 23 10:37:48 2014 @@ -49,7 +49,7 @@ public interface ILateralCacheAttributes * loose our connection with the server. */ public static final int DEFAULT_ZOMBIE_QUEUE_MAX_SIZE = 1000; - + /** * Sets the httpServer attribute of the ILateralCacheAttributes object * <p> @@ -180,7 +180,7 @@ public interface ILateralCacheAttributes * @return true if we should have a listener connection */ public boolean isReceive(); - + /** * The number of elements the zombie queue will hold. This queue is used to store events if we * loose our connection with the server. @@ -195,5 +195,5 @@ public interface ILateralCacheAttributes * <p> * @return Returns the zombieQueueMaxSize. */ - public int getZombieQueueMaxSize(); + public int getZombieQueueMaxSize(); } Modified: commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPDiscoveryListener.java URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPDiscoveryListener.java?rev=1570993&r1=1570992&r2=1570993&view=diff ============================================================================== --- commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPDiscoveryListener.java (original) +++ commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPDiscoveryListener.java Sun Feb 23 10:37:48 2014 @@ -1,336 +1,355 @@ -package org.apache.commons.jcs.auxiliary.lateral.socket.tcp; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -import org.apache.commons.jcs.auxiliary.lateral.LateralCacheAttributes; -import org.apache.commons.jcs.auxiliary.lateral.LateralCacheNoWait; -import org.apache.commons.jcs.auxiliary.lateral.LateralCacheNoWaitFacade; -import org.apache.commons.jcs.auxiliary.lateral.socket.tcp.behavior.ITCPLateralCacheAttributes; -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.jcs.utils.discovery.DiscoveredService; -import org.apache.commons.jcs.utils.discovery.behavior.IDiscoveryListener; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -/** - * This knows how to add and remove discovered services. It observes UDP discovery events. - * <p> - * We can have one listener per region, or one shared by all regions. - */ -public class LateralTCPDiscoveryListener - implements IDiscoveryListener -{ - /** The log factory */ - private final static Log log = LogFactory.getLog( LateralTCPDiscoveryListener.class ); - - /** - * Map of no wait facades. these are used to determine which regions are locally configured to - * use laterals. - */ - private final Map<String, LateralCacheNoWaitFacade<? extends Serializable, ? extends Serializable>> facades = - Collections.synchronizedMap( new HashMap<String, LateralCacheNoWaitFacade<? extends Serializable, ? extends Serializable>>() ); - - /** - * List of regions that are configured differently here than on another server. We keep track of - * this to limit the amount of info logging. - */ - private final Set<String> knownDifferentlyConfiguredRegions = - Collections.synchronizedSet( new HashSet<String>() ); - - /** The cache manager. */ - private final ICompositeCacheManager cacheMgr; - - /** The event logger. */ - protected ICacheEventLogger cacheEventLogger; - - /** The serializer. */ - protected IElementSerializer elementSerializer; - - /** - * This plugs into the udp discovery system. It will receive add and remove events. - * <p> - * @param cacheMgr - * @param cacheEventLogger - * @param elementSerializer - */ - protected LateralTCPDiscoveryListener( ICompositeCacheManager cacheMgr, ICacheEventLogger cacheEventLogger, - IElementSerializer elementSerializer ) - { - this.cacheMgr = cacheMgr; - this.cacheEventLogger = cacheEventLogger; - this.elementSerializer = elementSerializer; - } - - /** - * Adds a nowait facade under this cachename. If one already existed, it will be overridden. - * <p> - * This adds nowaits to a facade for the region name. If the region has no facade, then it is - * not configured to use the lateral cache, and no facade will be created. - * <p> - * @param cacheName - the region name - * @param facade - facade (for region) => multiple lateral clients. - * @return true if the facade was not already registered. - */ - public synchronized boolean addNoWaitFacade( String cacheName, LateralCacheNoWaitFacade<? extends Serializable, ? extends Serializable> facade ) - { - boolean isNew = !containsNoWaitFacade( cacheName ); - - // override or put anew, it doesn't matter - facades.put( cacheName, facade ); - knownDifferentlyConfiguredRegions.remove( cacheName ); - - return isNew; - } - - /** - * Allows us to see if the facade is present. - * <p> - * @param cacheName - facades are for a region - * @return do we contain the no wait. true if so - */ - public boolean containsNoWaitFacade( String cacheName ) - { - return facades.containsKey( cacheName ); - } - - /** - * Allows us to see if the facade is present and if it has the no wait. - * <p> - * @param cacheName - facades are for a region - * @param noWait - is this no wait in the facade - * @return do we contain the no wait. true if so - */ - public <K extends Serializable, V extends Serializable> boolean containsNoWait( String cacheName, LateralCacheNoWait<K, V> noWait ) - { - @SuppressWarnings("unchecked") // Need to cast because of common map for all facades - LateralCacheNoWaitFacade<K, V> facade = (LateralCacheNoWaitFacade<K, V>)facades.get( noWait.getCacheName() ); - if ( facade == null ) - { - return false; - } - - return facade.containsNoWait( noWait ); - } - - /** - * When a broadcast is received from the UDP Discovery receiver, for each cacheName in the - * message, the add no wait will be called here. To add a no wait, the facade is looked up for - * this cache name. - * <p> - * Each region has a facade. The facade contains a list of end points--the other tcp lateral - * services. - * <p> - * @param noWait - * @return true if we found the no wait and added it. False if the no wait was not present or it - * we already had it. - */ - protected <K extends Serializable, V extends Serializable> boolean addNoWait( LateralCacheNoWait<K, V> noWait ) - { - @SuppressWarnings("unchecked") // Need to cast because of common map for all facades - LateralCacheNoWaitFacade<K, V> facade = (LateralCacheNoWaitFacade<K, V>)facades.get( noWait.getCacheName() ); - if ( log.isDebugEnabled() ) - { - log.debug( "addNoWait > Got facade for " + noWait.getCacheName() + " = " + facade ); - } - - if ( facade != null ) - { - boolean isNew = facade.addNoWait( noWait ); - if ( log.isDebugEnabled() ) - { - log.debug( "Called addNoWait, isNew = " + isNew ); - } - return isNew; - } - else - { - if ( !knownDifferentlyConfiguredRegions.contains( noWait.getCacheName() ) ) - { - if ( log.isInfoEnabled() ) - { - log.info( "addNoWait > Different nodes are configured differently or region [" - + noWait.getCacheName() + "] is not yet used on this side. " ); - } - knownDifferentlyConfiguredRegions.add( noWait.getCacheName() ); - } - return false; - } - } - - /** - * Look up the facade for the name. If it doesn't exist, then the region is not configured for - * use with the lateral cache. If it is present, remove the item from the no wait list. - * <p> - * @param noWait - * @return true if we found the no wait and removed it. False if the no wait was not present. - */ - protected <K extends Serializable, V extends Serializable> boolean removeNoWait( LateralCacheNoWait<K, V> noWait ) - { - @SuppressWarnings("unchecked") // Need to cast because of common map for all facades - LateralCacheNoWaitFacade<K, V> facade = (LateralCacheNoWaitFacade<K, V>)facades.get( noWait.getCacheName() ); - if ( log.isDebugEnabled() ) - { - log.debug( "removeNoWait > Got facade for " + noWait.getCacheName() + " = " + facade ); - } - - if ( facade != null ) - { - boolean removed = facade.removeNoWait( noWait ); - if ( log.isDebugEnabled() ) - { - log.debug( "Called removeNoWait, removed " + removed ); - } - return removed; - } - else - { - if ( !knownDifferentlyConfiguredRegions.contains( noWait.getCacheName() ) ) - { - if ( log.isInfoEnabled() ) - { - log.info( "removeNoWait > Different nodes are configured differently or region [" - + noWait.getCacheName() + "] is not yet used on this side. " ); - } - knownDifferentlyConfiguredRegions.add( noWait.getCacheName() ); - } - return false; - } - } - - /** - * Creates the lateral cache if needed. - * <p> - * We could go to the composite cache manager and get the the cache for the region. This would - * force a full configuration of the region. One advantage of this would be that the creation of - * the later would go through the factory, which would add the item to the no wait list. But we - * don't want to do this. This would force this client to have all the regions as the other. - * This might not be desired. We don't want to send or receive for a region here that is either - * not used or not configured to use the lateral. - * <p> - * Right now, I'm afraid that the region will get puts if another instance has the region - * configured to use the lateral and our address is configured. This might be a bug, but it - * shouldn't happen with discovery. - * <p> - * @param service - */ - public void addDiscoveredService( DiscoveredService service ) - { - // get a cache and add it to the no waits - // the add method should not add the same. - // we need the listener port from the original config. - LateralTCPCacheManager lcm = findManagerForServiceEndPoint( service ); - - ArrayList<String> regions = service.getCacheNames(); - if ( regions != null ) - { - // for each region get the cache - for (String cacheName : regions) - { - try - { - ICache<? extends Serializable, ? extends Serializable> ic = lcm.getCache( cacheName ); - - if ( log.isDebugEnabled() ) - { - log.debug( "Got cache, ic = " + ic ); - } - - // add this to the nowaits for this cachename - if ( ic != null ) - { - addNoWait( (LateralCacheNoWait<? extends Serializable, ? extends Serializable>) ic ); - if ( log.isDebugEnabled() ) - { - log.debug( "Called addNoWait for cacheName [" + cacheName + "]" ); - } - } - } - catch ( Exception e ) - { - log.error( "Problem creating no wait", e ); - } - } - } - else - { - log.warn( "No cache names found in message " + service ); - } - } - - /** - * Removes the lateral cache. - * <p> - * We need to tell the manager that this instance is bad, so it will reconnect the sender if it - * comes back. - * <p> - * @param service - */ - public void removeDiscoveredService( DiscoveredService service ) - { - // get a cache and add it to the no waits - // the add method should not add the same. - // we need the listener port from the original config. - LateralTCPCacheManager lcm = findManagerForServiceEndPoint( service ); - - ArrayList<String> regions = service.getCacheNames(); - if ( regions != null ) - { - // for each region get the cache - for (String cacheName : regions) - { - try - { - ICache<? extends Serializable, ? extends Serializable> ic = lcm.getCache( cacheName ); - - if ( log.isDebugEnabled() ) - { - log.debug( "Got cache, ic = " + ic ); - } - - // remove this to the nowaits for this cachename - if ( ic != null ) - { - removeNoWait( (LateralCacheNoWait<? extends Serializable, ? extends Serializable>) ic ); - if ( log.isDebugEnabled() ) - { - log.debug( "Called removeNoWait for cacheName [" + cacheName + "]" ); - } - } - } - catch ( Exception e ) - { - log.error( "Problem removing no wait", e ); - } - } - } - else - { - log.warn( "No cache names found in message " + service ); - } - } - - /** - * Gets the appropriate manager. - * <p> - * @param service - * @return LateralTCPCacheManager configured for that end point. - */ - private LateralTCPCacheManager findManagerForServiceEndPoint( DiscoveredService service ) - { - ITCPLateralCacheAttributes lca = new TCPLateralCacheAttributes(); - lca.setTransmissionType( LateralCacheAttributes.TCP ); - lca.setTcpServer( service.getServiceAddress() + ":" + service.getServicePort() ); - LateralTCPCacheManager lcm = LateralTCPCacheManager.getInstance( lca, cacheMgr, cacheEventLogger, - elementSerializer ); - return lcm; - } -} +package org.apache.commons.jcs.auxiliary.lateral.socket.tcp; + +/* + * 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.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import org.apache.commons.jcs.auxiliary.lateral.LateralCacheAttributes; +import org.apache.commons.jcs.auxiliary.lateral.LateralCacheNoWait; +import org.apache.commons.jcs.auxiliary.lateral.LateralCacheNoWaitFacade; +import org.apache.commons.jcs.auxiliary.lateral.socket.tcp.behavior.ITCPLateralCacheAttributes; +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.jcs.utils.discovery.DiscoveredService; +import org.apache.commons.jcs.utils.discovery.behavior.IDiscoveryListener; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * This knows how to add and remove discovered services. It observes UDP discovery events. + * <p> + * We can have one listener per region, or one shared by all regions. + */ +public class LateralTCPDiscoveryListener + implements IDiscoveryListener +{ + /** The log factory */ + private final static Log log = LogFactory.getLog( LateralTCPDiscoveryListener.class ); + + /** + * Map of no wait facades. these are used to determine which regions are locally configured to + * use laterals. + */ + private final Map<String, LateralCacheNoWaitFacade<? extends Serializable, ? extends Serializable>> facades = + Collections.synchronizedMap( new HashMap<String, LateralCacheNoWaitFacade<? extends Serializable, ? extends Serializable>>() ); + + /** + * List of regions that are configured differently here than on another server. We keep track of + * this to limit the amount of info logging. + */ + private final Set<String> knownDifferentlyConfiguredRegions = + Collections.synchronizedSet( new HashSet<String>() ); + + /** The cache manager. */ + private final ICompositeCacheManager cacheMgr; + + /** The event logger. */ + protected ICacheEventLogger cacheEventLogger; + + /** The serializer. */ + protected IElementSerializer elementSerializer; + + /** + * This plugs into the udp discovery system. It will receive add and remove events. + * <p> + * @param cacheMgr + * @param cacheEventLogger + * @param elementSerializer + */ + protected LateralTCPDiscoveryListener( ICompositeCacheManager cacheMgr, ICacheEventLogger cacheEventLogger, + IElementSerializer elementSerializer ) + { + this.cacheMgr = cacheMgr; + this.cacheEventLogger = cacheEventLogger; + this.elementSerializer = elementSerializer; + } + + /** + * Adds a nowait facade under this cachename. If one already existed, it will be overridden. + * <p> + * This adds nowaits to a facade for the region name. If the region has no facade, then it is + * not configured to use the lateral cache, and no facade will be created. + * <p> + * @param cacheName - the region name + * @param facade - facade (for region) => multiple lateral clients. + * @return true if the facade was not already registered. + */ + public synchronized boolean addNoWaitFacade( String cacheName, LateralCacheNoWaitFacade<? extends Serializable, ? extends Serializable> facade ) + { + boolean isNew = !containsNoWaitFacade( cacheName ); + + // override or put anew, it doesn't matter + facades.put( cacheName, facade ); + knownDifferentlyConfiguredRegions.remove( cacheName ); + + return isNew; + } + + /** + * Allows us to see if the facade is present. + * <p> + * @param cacheName - facades are for a region + * @return do we contain the no wait. true if so + */ + public boolean containsNoWaitFacade( String cacheName ) + { + return facades.containsKey( cacheName ); + } + + /** + * Allows us to see if the facade is present and if it has the no wait. + * <p> + * @param cacheName - facades are for a region + * @param noWait - is this no wait in the facade + * @return do we contain the no wait. true if so + */ + public <K extends Serializable, V extends Serializable> boolean containsNoWait( String cacheName, LateralCacheNoWait<K, V> noWait ) + { + @SuppressWarnings("unchecked") // Need to cast because of common map for all facades + LateralCacheNoWaitFacade<K, V> facade = (LateralCacheNoWaitFacade<K, V>)facades.get( noWait.getCacheName() ); + if ( facade == null ) + { + return false; + } + + return facade.containsNoWait( noWait ); + } + + /** + * When a broadcast is received from the UDP Discovery receiver, for each cacheName in the + * message, the add no wait will be called here. To add a no wait, the facade is looked up for + * this cache name. + * <p> + * Each region has a facade. The facade contains a list of end points--the other tcp lateral + * services. + * <p> + * @param noWait + * @return true if we found the no wait and added it. False if the no wait was not present or it + * we already had it. + */ + protected <K extends Serializable, V extends Serializable> boolean addNoWait( LateralCacheNoWait<K, V> noWait ) + { + @SuppressWarnings("unchecked") // Need to cast because of common map for all facades + LateralCacheNoWaitFacade<K, V> facade = (LateralCacheNoWaitFacade<K, V>)facades.get( noWait.getCacheName() ); + if ( log.isDebugEnabled() ) + { + log.debug( "addNoWait > Got facade for " + noWait.getCacheName() + " = " + facade ); + } + + if ( facade != null ) + { + boolean isNew = facade.addNoWait( noWait ); + if ( log.isDebugEnabled() ) + { + log.debug( "Called addNoWait, isNew = " + isNew ); + } + return isNew; + } + else + { + if ( !knownDifferentlyConfiguredRegions.contains( noWait.getCacheName() ) ) + { + if ( log.isInfoEnabled() ) + { + log.info( "addNoWait > Different nodes are configured differently or region [" + + noWait.getCacheName() + "] is not yet used on this side. " ); + } + knownDifferentlyConfiguredRegions.add( noWait.getCacheName() ); + } + return false; + } + } + + /** + * Look up the facade for the name. If it doesn't exist, then the region is not configured for + * use with the lateral cache. If it is present, remove the item from the no wait list. + * <p> + * @param noWait + * @return true if we found the no wait and removed it. False if the no wait was not present. + */ + protected <K extends Serializable, V extends Serializable> boolean removeNoWait( LateralCacheNoWait<K, V> noWait ) + { + @SuppressWarnings("unchecked") // Need to cast because of common map for all facades + LateralCacheNoWaitFacade<K, V> facade = (LateralCacheNoWaitFacade<K, V>)facades.get( noWait.getCacheName() ); + if ( log.isDebugEnabled() ) + { + log.debug( "removeNoWait > Got facade for " + noWait.getCacheName() + " = " + facade ); + } + + if ( facade != null ) + { + boolean removed = facade.removeNoWait( noWait ); + if ( log.isDebugEnabled() ) + { + log.debug( "Called removeNoWait, removed " + removed ); + } + return removed; + } + else + { + if ( !knownDifferentlyConfiguredRegions.contains( noWait.getCacheName() ) ) + { + if ( log.isInfoEnabled() ) + { + log.info( "removeNoWait > Different nodes are configured differently or region [" + + noWait.getCacheName() + "] is not yet used on this side. " ); + } + knownDifferentlyConfiguredRegions.add( noWait.getCacheName() ); + } + return false; + } + } + + /** + * Creates the lateral cache if needed. + * <p> + * We could go to the composite cache manager and get the the cache for the region. This would + * force a full configuration of the region. One advantage of this would be that the creation of + * the later would go through the factory, which would add the item to the no wait list. But we + * don't want to do this. This would force this client to have all the regions as the other. + * This might not be desired. We don't want to send or receive for a region here that is either + * not used or not configured to use the lateral. + * <p> + * Right now, I'm afraid that the region will get puts if another instance has the region + * configured to use the lateral and our address is configured. This might be a bug, but it + * shouldn't happen with discovery. + * <p> + * @param service + */ + public void addDiscoveredService( DiscoveredService service ) + { + // get a cache and add it to the no waits + // the add method should not add the same. + // we need the listener port from the original config. + LateralTCPCacheManager lcm = findManagerForServiceEndPoint( service ); + + ArrayList<String> regions = service.getCacheNames(); + if ( regions != null ) + { + // for each region get the cache + for (String cacheName : regions) + { + try + { + ICache<? extends Serializable, ? extends Serializable> ic = lcm.getCache( cacheName ); + + if ( log.isDebugEnabled() ) + { + log.debug( "Got cache, ic = " + ic ); + } + + // add this to the nowaits for this cachename + if ( ic != null ) + { + addNoWait( (LateralCacheNoWait<? extends Serializable, ? extends Serializable>) ic ); + if ( log.isDebugEnabled() ) + { + log.debug( "Called addNoWait for cacheName [" + cacheName + "]" ); + } + } + } + catch ( Exception e ) + { + log.error( "Problem creating no wait", e ); + } + } + } + else + { + log.warn( "No cache names found in message " + service ); + } + } + + /** + * Removes the lateral cache. + * <p> + * We need to tell the manager that this instance is bad, so it will reconnect the sender if it + * comes back. + * <p> + * @param service + */ + public void removeDiscoveredService( DiscoveredService service ) + { + // get a cache and add it to the no waits + // the add method should not add the same. + // we need the listener port from the original config. + LateralTCPCacheManager lcm = findManagerForServiceEndPoint( service ); + + ArrayList<String> regions = service.getCacheNames(); + if ( regions != null ) + { + // for each region get the cache + for (String cacheName : regions) + { + try + { + ICache<? extends Serializable, ? extends Serializable> ic = lcm.getCache( cacheName ); + + if ( log.isDebugEnabled() ) + { + log.debug( "Got cache, ic = " + ic ); + } + + // remove this to the nowaits for this cachename + if ( ic != null ) + { + removeNoWait( (LateralCacheNoWait<? extends Serializable, ? extends Serializable>) ic ); + if ( log.isDebugEnabled() ) + { + log.debug( "Called removeNoWait for cacheName [" + cacheName + "]" ); + } + } + } + catch ( Exception e ) + { + log.error( "Problem removing no wait", e ); + } + } + } + else + { + log.warn( "No cache names found in message " + service ); + } + } + + /** + * Gets the appropriate manager. + * <p> + * @param service + * @return LateralTCPCacheManager configured for that end point. + */ + private LateralTCPCacheManager findManagerForServiceEndPoint( DiscoveredService service ) + { + ITCPLateralCacheAttributes lca = new TCPLateralCacheAttributes(); + lca.setTransmissionType( LateralCacheAttributes.TCP ); + lca.setTcpServer( service.getServiceAddress() + ":" + service.getServicePort() ); + LateralTCPCacheManager lcm = LateralTCPCacheManager.getInstance( lca, cacheMgr, cacheEventLogger, + elementSerializer ); + return lcm; + } +} Modified: commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPDiscoveryListenerManager.java URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPDiscoveryListenerManager.java?rev=1570993&r1=1570992&r2=1570993&view=diff ============================================================================== --- commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPDiscoveryListenerManager.java (original) +++ commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPDiscoveryListenerManager.java Sun Feb 23 10:37:48 2014 @@ -1,71 +1,90 @@ -package org.apache.commons.jcs.auxiliary.lateral.socket.tcp; - -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -import org.apache.commons.jcs.auxiliary.lateral.socket.tcp.behavior.ITCPLateralCacheAttributes; -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 factory holds an instance of this manager. This manager has a map of listeners, keyed to the - * discovery configuration. I'm not using a static map, because I'm trying to make JCS - * multi-instance. - * <p> - * During configuration, the factory is only created once per auxiliary definition. Two different - * laterals cannot use the same discovery service. We will likely want to change this. - */ -public class LateralTCPDiscoveryListenerManager -{ - /** Map of available instances, keyed by port. Note, this is not static. */ - protected Map<String, LateralTCPDiscoveryListener> instances = - Collections.synchronizedMap( new HashMap<String, LateralTCPDiscoveryListener>() ); - - /** The logger */ - private final static Log log = LogFactory.getLog( LateralTCPDiscoveryListenerManager.class ); - - /** Does nothing. */ - public LateralTCPDiscoveryListenerManager() - { - if ( log.isInfoEnabled() ) - { - log.info( "Creating new LateralTCPDiscoveryListenerManager" ); - } - } - - /** - * Gets the instance attribute of the LateralCacheTCPListener class. - * <p> - * @param ilca ITCPLateralCacheAttributes - * @param cacheMgr - * @param cacheEventLogger - * @param elementSerializer - * @return The instance value - */ - public synchronized LateralTCPDiscoveryListener getDiscoveryListener( ITCPLateralCacheAttributes ilca, - ICompositeCacheManager cacheMgr, - ICacheEventLogger cacheEventLogger, - IElementSerializer elementSerializer ) - { - String key = ilca.getUdpDiscoveryAddr() + ":" + ilca.getUdpDiscoveryPort(); - LateralTCPDiscoveryListener ins = instances.get( key ); - - if ( ins == null ) - { - ins = new LateralTCPDiscoveryListener( cacheMgr, cacheEventLogger, elementSerializer ); - - instances.put( key, ins ); - - if ( log.isInfoEnabled() ) - { - log.info( "Created new discovery listener for " + key + " cacheName for request " + ilca.getCacheName() ); - } - } - - return ins; - } -} +package org.apache.commons.jcs.auxiliary.lateral.socket.tcp; + +/* + * 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.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.jcs.auxiliary.lateral.socket.tcp.behavior.ITCPLateralCacheAttributes; +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 factory holds an instance of this manager. This manager has a map of listeners, keyed to the + * discovery configuration. I'm not using a static map, because I'm trying to make JCS + * multi-instance. + * <p> + * During configuration, the factory is only created once per auxiliary definition. Two different + * laterals cannot use the same discovery service. We will likely want to change this. + */ +public class LateralTCPDiscoveryListenerManager +{ + /** Map of available instances, keyed by port. Note, this is not static. */ + protected Map<String, LateralTCPDiscoveryListener> instances = + Collections.synchronizedMap( new HashMap<String, LateralTCPDiscoveryListener>() ); + + /** The logger */ + private final static Log log = LogFactory.getLog( LateralTCPDiscoveryListenerManager.class ); + + /** Does nothing. */ + public LateralTCPDiscoveryListenerManager() + { + if ( log.isInfoEnabled() ) + { + log.info( "Creating new LateralTCPDiscoveryListenerManager" ); + } + } + + /** + * Gets the instance attribute of the LateralCacheTCPListener class. + * <p> + * @param ilca ITCPLateralCacheAttributes + * @param cacheMgr + * @param cacheEventLogger + * @param elementSerializer + * @return The instance value + */ + public synchronized LateralTCPDiscoveryListener getDiscoveryListener( ITCPLateralCacheAttributes ilca, + ICompositeCacheManager cacheMgr, + ICacheEventLogger cacheEventLogger, + IElementSerializer elementSerializer ) + { + String key = ilca.getUdpDiscoveryAddr() + ":" + ilca.getUdpDiscoveryPort(); + LateralTCPDiscoveryListener ins = instances.get( key ); + + if ( ins == null ) + { + ins = new LateralTCPDiscoveryListener( cacheMgr, cacheEventLogger, elementSerializer ); + + instances.put( key, ins ); + + if ( log.isInfoEnabled() ) + { + log.info( "Created new discovery listener for " + key + " cacheName for request " + ilca.getCacheName() ); + } + } + + return ins; + } +} Modified: commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/TCPLateralCacheAttributes.java URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/TCPLateralCacheAttributes.java?rev=1570993&r1=1570992&r2=1570993&view=diff ============================================================================== --- commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/TCPLateralCacheAttributes.java (original) +++ commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/TCPLateralCacheAttributes.java Sun Feb 23 10:37:48 2014 @@ -90,13 +90,13 @@ public class TCPLateralCacheAttributes /** don't remove it the hashcode is the same */ private boolean filterRemoveByHashCode = DEFAULT_FILTER_REMOVE_BY_HASH_CODE; - + /** Only block for socketTimeOut seconds before timing out on a read. */ private int socketTimeOut = DEFAULT_SOCKET_TIME_OUT; /** Only block for openTimeOut seconds before timing out on startup. */ private int openTimeOut = DEFAULT_OPEN_TIMEOUT; - + /** * Sets the tcpServer attribute of the ILateralCacheAttributes object * <p> Modified: commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/behavior/ITCPLateralCacheAttributes.java URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/behavior/ITCPLateralCacheAttributes.java?rev=1570993&r1=1570992&r2=1570993&view=diff ============================================================================== --- commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/behavior/ITCPLateralCacheAttributes.java (original) +++ commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/behavior/ITCPLateralCacheAttributes.java Sun Feb 23 10:37:48 2014 @@ -195,7 +195,7 @@ public interface ITCPLateralCacheAttribu * @param filter */ public void setFilterRemoveByHashCode( boolean filter ); - + /** * @param socketTimeOut the socketTimeOut to set */ @@ -214,5 +214,5 @@ public interface ITCPLateralCacheAttribu /** * @return the openTimeOut */ - public int getOpenTimeOut(); + public int getOpenTimeOut(); }
