1 package org.apache.jcs.auxiliary.lateral.socket.tcp;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 public class LateralTCPCacheFactory
50 extends LateralCacheAbstractFactory
51 {
52 private final static Log log = LogFactory.getLog( LateralTCPCacheFactory.class );
53
54
55
56
57
58
59
60 public AuxiliaryCache createCache( AuxiliaryCacheAttributes iaca, ICompositeCacheManager cacheMgr )
61 {
62 ITCPLateralCacheAttributes lac = (ITCPLateralCacheAttributes) iaca;
63 ArrayList noWaits = new ArrayList();
64
65
66
67
68 if ( lac.getTcpServers() != null )
69 {
70 StringTokenizer it = new StringTokenizer( lac.getTcpServers(), "," );
71 if ( log.isDebugEnabled() )
72 {
73 log.debug( "Configured for [" + it.countTokens() + "] servers." );
74 }
75 while ( it.hasMoreElements() )
76 {
77 String server = (String) it.nextElement();
78 if ( log.isDebugEnabled() )
79 {
80 log.debug( "tcp server = " + server );
81 }
82 ITCPLateralCacheAttributes lacC = (ITCPLateralCacheAttributes) lac.copy();
83 lacC.setTcpServer( server );
84 LateralTCPCacheManager lcm = LateralTCPCacheManager.getInstance( lacC, cacheMgr );
85 ICache ic = lcm.getCache( lacC.getCacheName() );
86 if ( ic != null )
87 {
88 noWaits.add( ic );
89 }
90 else
91 {
92 if ( log.isDebugEnabled() )
93 {
94 log.debug( "noWait is null, no lateral connection made" );
95 }
96 }
97 }
98 }
99
100 createListener( (ILateralCacheAttributes) iaca, cacheMgr );
101
102
103 LateralCacheNoWaitFacade lcnwf = new LateralCacheNoWaitFacade( (LateralCacheNoWait[]) noWaits
104 .toArray( new LateralCacheNoWait[0] ), (ILateralCacheAttributes)iaca );
105
106
107 createDiscoveryService( lac, lcnwf, cacheMgr );
108
109 return lcnwf;
110 }
111
112
113
114
115
116
117
118 public void createListener( ILateralCacheAttributes lac, ICompositeCacheManager cacheMgr )
119 {
120 ITCPLateralCacheAttributes attr = (ITCPLateralCacheAttributes) lac;
121
122 if ( attr.isReceive() )
123 {
124 if ( log.isInfoEnabled() )
125 {
126 log.info( "Creating listener for " + lac );
127 }
128
129 try
130 {
131
132 LateralTCPListener.getInstance( attr, cacheMgr );
133 }
134 catch ( Exception e )
135 {
136 log.error( "Problem creating lateral listener", e );
137 }
138 }
139 else
140 {
141 if ( log.isDebugEnabled() )
142 {
143 log.debug( "Not creating a listener since we are not receiving." );
144 }
145 }
146 }
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 UDPDiscoveryService discovery = null;
162
163
164 if ( lac.isUdpDiscoveryEnabled() )
165 {
166
167
168 discovery = UDPDiscoveryManager.getInstance().getService( lac, cacheMgr );
169
170 discovery.addNoWaitFacade( lcnwf, lac.getCacheName() );
171
172 if ( log.isInfoEnabled() )
173 {
174 log.info( "Created UDPDiscoveryService for TCP lateral cache." );
175 }
176 }
177 return discovery;
178 }
179 }