1 package org.apache.jcs.auxiliary.remote;
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.HashMap;
24 import java.util.StringTokenizer;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.jcs.auxiliary.AuxiliaryCache;
29 import org.apache.jcs.auxiliary.AuxiliaryCacheAttributes;
30 import org.apache.jcs.auxiliary.AuxiliaryCacheFactory;
31 import org.apache.jcs.engine.behavior.ICache;
32 import org.apache.jcs.engine.behavior.ICompositeCacheManager;
33
34 /***
35 * The RemoteCacheFactory creates remote caches for the cache hub. It returns a no wait facade which
36 * is a wrapper around a no wait. The no wait object is either an active connection to a remote
37 * cache or a balking zombie if the remote cache is not accessible. It should be transparent to the
38 * clients.
39 */
40 public class RemoteCacheFactory
41 implements AuxiliaryCacheFactory
42 {
43 private final static Log log = LogFactory.getLog( RemoteCacheFactory.class );
44
45 private String name;
46
47 /*** store reference of facades to initiate failover */
48 private final static HashMap facades = new HashMap();
49
50 /***
51 * For LOCAL clients we get a handle to all the failovers, but we do not register a listener
52 * with them. We create the RemoteCacheManager, but we do not get a cache. The failover runner
53 * will get a cache from the manager. When the primary is restored it will tell the manager for
54 * the failover to deregister the listener.
55 * <p>
56 * (non-Javadoc)
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 RemoteCacheAttributes rca = (RemoteCacheAttributes) iaca;
63
64 ArrayList noWaits = new ArrayList();
65
66
67 if ( rca.getRemoteType() == RemoteCacheAttributes.LOCAL )
68 {
69
70 ArrayList failovers = new ArrayList();
71
72
73
74
75 boolean primayDefined = false;
76 if ( rca.getRemoteHost() != null )
77 {
78 primayDefined = true;
79
80 failovers.add( rca.getRemoteHost() + ":" + rca.getRemotePort() );
81
82 RemoteCacheManager rcm = RemoteCacheManager.getInstance( rca, cacheMgr );
83 ICache ic = rcm.getCache( rca );
84 if ( ic != null )
85 {
86 noWaits.add( ic );
87 }
88 else
89 {
90 log.info( "noWait is null" );
91 }
92 }
93
94
95 String failoverList = rca.getFailoverServers();
96 if ( failoverList != null )
97 {
98 StringTokenizer fit = new StringTokenizer( failoverList, "," );
99 int fCnt = 0;
100 while ( fit.hasMoreElements() )
101 {
102 fCnt++;
103
104 String server = (String) fit.nextElement();
105 failovers.add( server );
106
107 rca.setRemoteHost( server.substring( 0, server.indexOf( ":" ) ) );
108 rca.setRemotePort( Integer.parseInt( server.substring( server.indexOf( ":" ) + 1 ) ) );
109 RemoteCacheManager rcm = RemoteCacheManager.getInstance( rca, cacheMgr );
110
111
112 if ( ( !primayDefined && fCnt == 1 ) || noWaits.size() <= 0 )
113 {
114 ICache ic = rcm.getCache( rca );
115 if ( ic != null )
116 {
117 noWaits.add( ic );
118 }
119 else
120 {
121 log.info( "noWait is null" );
122 }
123 }
124 }
125
126 }
127
128
129 rca.setFailovers( (String[]) failovers.toArray( new String[0] ) );
130
131
132 }
133 else if ( rca.getRemoteType() == RemoteCacheAttributes.CLUSTER )
134 {
135
136 StringTokenizer it = new StringTokenizer( rca.getClusterServers(), "," );
137 while ( it.hasMoreElements() )
138 {
139
140 String server = (String) it.nextElement();
141
142 rca.setRemoteHost( server.substring( 0, server.indexOf( ":" ) ) );
143 rca.setRemotePort( Integer.parseInt( server.substring( server.indexOf( ":" ) + 1 ) ) );
144 RemoteCacheManager rcm = RemoteCacheManager.getInstance( rca, cacheMgr );
145 rca.setRemoteType( RemoteCacheAttributes.CLUSTER );
146 ICache ic = rcm.getCache( rca );
147 if ( ic != null )
148 {
149 noWaits.add( ic );
150 }
151 else
152 {
153 log.info( "noWait is null" );
154 }
155 }
156
157 }
158
159
160 RemoteCacheNoWaitFacade rcnwf = new RemoteCacheNoWaitFacade( (RemoteCacheNoWait[]) noWaits
161 .toArray( new RemoteCacheNoWait[0] ), rca, cacheMgr );
162
163 getFacades().put( rca.getCacheName(), rcnwf );
164
165 return rcnwf;
166 }
167
168
169
170 /***
171 * Gets the name attribute of the RemoteCacheFactory object
172 * @return The name value
173 */
174 public String getName()
175 {
176 return this.name;
177 }
178
179 /***
180 * Sets the name attribute of the RemoteCacheFactory object
181 * @param name The new name value
182 */
183 public void setName( String name )
184 {
185 this.name = name;
186 }
187
188 /***
189 * The facades are what the cache hub talks to.
190 * @return Returns the facades.
191 */
192 public static HashMap getFacades()
193 {
194 return facades;
195 }
196
197 }