| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| org.apache.jcs.auxiliary.lateral.socket.tcp.LateralTCPCacheFactory |
|
|
| 1 | package org.apache.jcs.auxiliary.lateral.socket.tcp; |
|
| 2 | ||
| 3 | /* |
|
| 4 | * Licensed to the Apache Software Foundation (ASF) under one |
|
| 5 | * or more contributor license agreements. See the NOTICE file |
|
| 6 | * distributed with this work for additional information |
|
| 7 | * regarding copyright ownership. The ASF licenses this file |
|
| 8 | * to you under the Apache License, Version 2.0 (the |
|
| 9 | * "License"); you may not use this file except in compliance |
|
| 10 | * with the License. You may obtain a copy of the License at |
|
| 11 | * |
|
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
| 13 | * |
|
| 14 | * Unless required by applicable law or agreed to in writing, |
|
| 15 | * software distributed under the License is distributed on an |
|
| 16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
|
| 17 | * KIND, either express or implied. See the License for the |
|
| 18 | * specific language governing permissions and limitations |
|
| 19 | * under the License. |
|
| 20 | */ |
|
| 21 | ||
| 22 | import java.util.ArrayList; |
|
| 23 | import java.util.StringTokenizer; |
|
| 24 | ||
| 25 | import org.apache.commons.logging.Log; |
|
| 26 | import org.apache.commons.logging.LogFactory; |
|
| 27 | import org.apache.jcs.auxiliary.AuxiliaryCache; |
|
| 28 | import org.apache.jcs.auxiliary.AuxiliaryCacheAttributes; |
|
| 29 | import org.apache.jcs.auxiliary.lateral.LateralCacheAbstractFactory; |
|
| 30 | import org.apache.jcs.auxiliary.lateral.LateralCacheNoWait; |
|
| 31 | import org.apache.jcs.auxiliary.lateral.LateralCacheNoWaitFacade; |
|
| 32 | import org.apache.jcs.auxiliary.lateral.behavior.ILateralCacheAttributes; |
|
| 33 | import org.apache.jcs.auxiliary.lateral.socket.tcp.behavior.ITCPLateralCacheAttributes; |
|
| 34 | import org.apache.jcs.auxiliary.lateral.socket.tcp.discovery.UDPDiscoveryManager; |
|
| 35 | import org.apache.jcs.auxiliary.lateral.socket.tcp.discovery.UDPDiscoveryService; |
|
| 36 | import org.apache.jcs.engine.behavior.ICache; |
|
| 37 | import org.apache.jcs.engine.behavior.ICompositeCacheManager; |
|
| 38 | ||
| 39 | /** |
|
| 40 | * Constructs a LateralCacheNoWaitFacade for the given configuration. Each |
|
| 41 | * lateral service / local relationship is managed by one manager. This manager |
|
| 42 | * can have multiple caches. The remote relationships are consolidated and |
|
| 43 | * restored via these managers. |
|
| 44 | * <p> |
|
| 45 | * The facade provides a front to the composite cache so the implementation is |
|
| 46 | * transparent. |
|
| 47 | * |
|
| 48 | */ |
|
| 49 | 14 | public class LateralTCPCacheFactory |
| 50 | extends LateralCacheAbstractFactory |
|
| 51 | { |
|
| 52 | 28 | private final static Log log = LogFactory.getLog( LateralTCPCacheFactory.class ); |
| 53 | ||
| 54 | /* |
|
| 55 | * (non-Javadoc) |
|
| 56 | * |
|
| 57 | * @see org.apache.jcs.auxiliary.AuxiliaryCacheFactory#createCache(org.apache.jcs.auxiliary.AuxiliaryCacheAttributes, |
|
| 58 | * org.apache.jcs.engine.behavior.ICompositeCacheManager) |
|
| 59 | */ |
|
| 60 | public AuxiliaryCache createCache( AuxiliaryCacheAttributes iaca, ICompositeCacheManager cacheMgr ) |
|
| 61 | { |
|
| 62 | 27 | ITCPLateralCacheAttributes lac = (ITCPLateralCacheAttributes) iaca; |
| 63 | 27 | ArrayList noWaits = new ArrayList(); |
| 64 | ||
| 65 | // pars up the tcp servers and set the tcpServer value and |
|
| 66 | // get the manager and then get the cache |
|
| 67 | // no servers are required. |
|
| 68 | 27 | if ( lac.getTcpServers() != null ) |
| 69 | { |
|
| 70 | 27 | StringTokenizer it = new StringTokenizer( lac.getTcpServers(), "," ); |
| 71 | 27 | if ( log.isDebugEnabled() ) |
| 72 | { |
|
| 73 | 0 | log.debug( "Configured for [" + it.countTokens() + "] servers." ); |
| 74 | } |
|
| 75 | 53 | while ( it.hasMoreElements() ) |
| 76 | { |
|
| 77 | 27 | String server = (String) it.nextElement(); |
| 78 | 27 | if ( log.isDebugEnabled() ) |
| 79 | { |
|
| 80 | 0 | log.debug( "tcp server = " + server ); |
| 81 | } |
|
| 82 | 27 | ITCPLateralCacheAttributes lacC = (ITCPLateralCacheAttributes) lac.copy(); |
| 83 | 27 | lacC.setTcpServer( server ); |
| 84 | 27 | LateralTCPCacheManager lcm = LateralTCPCacheManager.getInstance( lacC, cacheMgr ); |
| 85 | 27 | ICache ic = lcm.getCache( lacC.getCacheName() ); |
| 86 | 26 | if ( ic != null ) |
| 87 | { |
|
| 88 | 26 | noWaits.add( ic ); |
| 89 | 26 | } |
| 90 | else |
|
| 91 | { |
|
| 92 | 0 | if ( log.isDebugEnabled() ) |
| 93 | { |
|
| 94 | 0 | log.debug( "noWait is null, no lateral connection made" ); |
| 95 | } |
|
| 96 | } |
|
| 97 | 26 | } |
| 98 | } |
|
| 99 | ||
| 100 | 26 | createListener( (ILateralCacheAttributes) iaca, cacheMgr ); |
| 101 | ||
| 102 | // create the no wait facade. |
|
| 103 | 26 | LateralCacheNoWaitFacade lcnwf = new LateralCacheNoWaitFacade( (LateralCacheNoWait[]) noWaits |
| 104 | .toArray( new LateralCacheNoWait[0] ), (ILateralCacheAttributes)iaca ); |
|
| 105 | ||
| 106 | // create udp discovery if available. |
|
| 107 | 26 | createDiscoveryService( lac, lcnwf, cacheMgr ); |
| 108 | ||
| 109 | 26 | return lcnwf; |
| 110 | } |
|
| 111 | ||
| 112 | /* |
|
| 113 | * (non-Javadoc) |
|
| 114 | * |
|
| 115 | * @see org.apache.jcs.auxiliary.lateral.LateralCacheAbstractFactory#createListener(org.apache.jcs.auxiliary.lateral.LateralCacheAttributes, |
|
| 116 | * org.apache.jcs.engine.behavior.ICompositeCacheManager) |
|
| 117 | */ |
|
| 118 | public void createListener( ILateralCacheAttributes lac, ICompositeCacheManager cacheMgr ) |
|
| 119 | { |
|
| 120 | 26 | ITCPLateralCacheAttributes attr = (ITCPLateralCacheAttributes) lac; |
| 121 | // don't create a listener if we are not receiving. |
|
| 122 | 26 | if ( attr.isReceive() ) |
| 123 | { |
|
| 124 | 26 | if ( log.isInfoEnabled() ) |
| 125 | { |
|
| 126 | 26 | log.info( "Creating listener for " + lac ); |
| 127 | } |
|
| 128 | ||
| 129 | try |
|
| 130 | { |
|
| 131 | // make a listener. if one doesn't exist |
|
| 132 | 26 | LateralTCPListener.getInstance( attr, cacheMgr ); |
| 133 | } |
|
| 134 | 0 | catch ( Exception e ) |
| 135 | { |
|
| 136 | 0 | log.error( "Problem creating lateral listener", e ); |
| 137 | 26 | } |
| 138 | 0 | } |
| 139 | else |
|
| 140 | { |
|
| 141 | 0 | if ( log.isDebugEnabled() ) |
| 142 | { |
|
| 143 | 0 | log.debug( "Not creating a listener since we are not receiving." ); |
| 144 | } |
|
| 145 | } |
|
| 146 | 26 | } |
| 147 | ||
| 148 | /** |
|
| 149 | * Creates the discovery service. Only creates this for tcp laterals right |
|
| 150 | * now. |
|
| 151 | * |
|
| 152 | * @param lac |
|
| 153 | * ITCPLateralCacheAttributes |
|
| 154 | * @param lcnwf |
|
| 155 | * @param cacheMgr |
|
| 156 | * @return null if none is created. |
|
| 157 | */ |
|
| 158 | private UDPDiscoveryService createDiscoveryService( ITCPLateralCacheAttributes lac, LateralCacheNoWaitFacade lcnwf, |
|
| 159 | ICompositeCacheManager cacheMgr ) |
|
| 160 | { |
|
| 161 | 26 | UDPDiscoveryService discovery = null; |
| 162 | ||
| 163 | // create the UDP discovery for the TCP lateral |
|
| 164 | 26 | if ( lac.isUdpDiscoveryEnabled() ) |
| 165 | { |
|
| 166 | // need a factory for this so it doesn't |
|
| 167 | // get dereferenced, also we don't want one for every region. |
|
| 168 | 26 | discovery = UDPDiscoveryManager.getInstance().getService( lac, cacheMgr ); |
| 169 | ||
| 170 | 26 | discovery.addNoWaitFacade( lcnwf, lac.getCacheName() ); |
| 171 | ||
| 172 | 26 | if ( log.isInfoEnabled() ) |
| 173 | { |
|
| 174 | 26 | log.info( "Created UDPDiscoveryService for TCP lateral cache." ); |
| 175 | } |
|
| 176 | } |
|
| 177 | 26 | return discovery; |
| 178 | } |
|
| 179 | } |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |