%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.jcs.auxiliary.remote.RemoteCacheFactory |
|
|
1 | package org.apache.jcs.auxiliary.remote; |
|
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.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 | 0 | public class RemoteCacheFactory |
41 | implements AuxiliaryCacheFactory |
|
42 | { |
|
43 | 0 | private final static Log log = LogFactory.getLog( RemoteCacheFactory.class ); |
44 | ||
45 | private String name; |
|
46 | ||
47 | /** store reference of facades to initiate failover */ |
|
48 | 0 | 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 | 0 | RemoteCacheAttributes rca = (RemoteCacheAttributes) iaca; |
63 | ||
64 | 0 | ArrayList noWaits = new ArrayList(); |
65 | ||
66 | // if LOCAL |
|
67 | 0 | if ( rca.getRemoteType() == RemoteCacheAttributes.LOCAL ) |
68 | { |
|
69 | // a list toi be turned into an array of failover server information |
|
70 | 0 | ArrayList failovers = new ArrayList(); |
71 | ||
72 | // not necessary if a failover list is defined |
|
73 | // REGISTER PRIMARY LISTENER |
|
74 | // if it is a primary |
|
75 | 0 | boolean primayDefined = false; |
76 | 0 | if ( rca.getRemoteHost() != null ) |
77 | { |
|
78 | 0 | primayDefined = true; |
79 | ||
80 | 0 | failovers.add( rca.getRemoteHost() + ":" + rca.getRemotePort() ); |
81 | ||
82 | 0 | RemoteCacheManager rcm = RemoteCacheManager.getInstance( rca, cacheMgr ); |
83 | 0 | ICache ic = rcm.getCache( rca ); |
84 | 0 | if ( ic != null ) |
85 | { |
|
86 | 0 | noWaits.add( ic ); |
87 | 0 | } |
88 | else |
|
89 | { |
|
90 | 0 | log.info( "noWait is null" ); |
91 | } |
|
92 | } |
|
93 | ||
94 | // GET HANDLE BUT DONT REGISTER A LISTENER FOR FAILOVERS |
|
95 | 0 | String failoverList = rca.getFailoverServers(); |
96 | 0 | if ( failoverList != null ) |
97 | { |
|
98 | 0 | StringTokenizer fit = new StringTokenizer( failoverList, "," ); |
99 | 0 | int fCnt = 0; |
100 | 0 | while ( fit.hasMoreElements() ) |
101 | { |
|
102 | 0 | fCnt++; |
103 | ||
104 | 0 | String server = (String) fit.nextElement(); |
105 | 0 | failovers.add( server ); |
106 | ||
107 | 0 | rca.setRemoteHost( server.substring( 0, server.indexOf( ":" ) ) ); |
108 | 0 | rca.setRemotePort( Integer.parseInt( server.substring( server.indexOf( ":" ) + 1 ) ) ); |
109 | 0 | RemoteCacheManager rcm = RemoteCacheManager.getInstance( rca, cacheMgr ); |
110 | // add a listener if there are none, need to tell rca what |
|
111 | // number it is at |
|
112 | 0 | if ( ( !primayDefined && fCnt == 1 ) || noWaits.size() <= 0 ) |
113 | { |
|
114 | 0 | ICache ic = rcm.getCache( rca ); |
115 | 0 | if ( ic != null ) |
116 | { |
|
117 | 0 | noWaits.add( ic ); |
118 | 0 | } |
119 | else |
|
120 | { |
|
121 | 0 | log.info( "noWait is null" ); |
122 | } |
|
123 | } |
|
124 | 0 | } |
125 | // end while |
|
126 | } |
|
127 | // end if failoverList != null |
|
128 | ||
129 | 0 | rca.setFailovers( (String[]) failovers.toArray( new String[0] ) ); |
130 | ||
131 | // if CLUSTER |
|
132 | 0 | } |
133 | 0 | else if ( rca.getRemoteType() == RemoteCacheAttributes.CLUSTER ) |
134 | { |
|
135 | // REGISTER LISTENERS FOR EACH SYSTEM CLUSTERED CACHEs |
|
136 | 0 | StringTokenizer it = new StringTokenizer( rca.getClusterServers(), "," ); |
137 | 0 | while ( it.hasMoreElements() ) |
138 | { |
|
139 | // String server = (String)it.next(); |
|
140 | 0 | String server = (String) it.nextElement(); |
141 | // p( "tcp server = " + server ); |
|
142 | 0 | rca.setRemoteHost( server.substring( 0, server.indexOf( ":" ) ) ); |
143 | 0 | rca.setRemotePort( Integer.parseInt( server.substring( server.indexOf( ":" ) + 1 ) ) ); |
144 | 0 | RemoteCacheManager rcm = RemoteCacheManager.getInstance( rca, cacheMgr ); |
145 | 0 | rca.setRemoteType( RemoteCacheAttributes.CLUSTER ); |
146 | 0 | ICache ic = rcm.getCache( rca ); |
147 | 0 | if ( ic != null ) |
148 | { |
|
149 | 0 | noWaits.add( ic ); |
150 | 0 | } |
151 | else |
|
152 | { |
|
153 | 0 | log.info( "noWait is null" ); |
154 | } |
|
155 | 0 | } |
156 | ||
157 | } |
|
158 | // end if CLUSTER |
|
159 | ||
160 | 0 | RemoteCacheNoWaitFacade rcnwf = new RemoteCacheNoWaitFacade( (RemoteCacheNoWait[]) noWaits |
161 | .toArray( new RemoteCacheNoWait[0] ), rca, cacheMgr ); |
|
162 | ||
163 | 0 | getFacades().put( rca.getCacheName(), rcnwf ); |
164 | ||
165 | 0 | return rcnwf; |
166 | } |
|
167 | ||
168 | // end createCache |
|
169 | ||
170 | /** |
|
171 | * Gets the name attribute of the RemoteCacheFactory object |
|
172 | * @return The name value |
|
173 | */ |
|
174 | public String getName() |
|
175 | { |
|
176 | 0 | 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 | 0 | this.name = name; |
186 | 0 | } |
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 | 0 | return facades; |
195 | } |
|
196 | ||
197 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |