1 package org.apache.commons.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.io.IOException;
23 import java.util.ArrayList;
24
25 import org.apache.commons.jcs.auxiliary.remote.behavior.IRemoteCacheAttributes;
26 import org.apache.commons.jcs.auxiliary.remote.behavior.IRemoteCacheListener;
27 import org.apache.commons.jcs.auxiliary.remote.server.behavior.RemoteType;
28 import org.apache.commons.jcs.engine.ZombieCacheServiceNonLocal;
29 import org.apache.commons.jcs.engine.behavior.ICacheServiceNonLocal;
30 import org.apache.commons.jcs.engine.stats.StatElement;
31 import org.apache.commons.jcs.engine.stats.Stats;
32 import org.apache.commons.jcs.engine.stats.behavior.IStatElement;
33 import org.apache.commons.jcs.engine.stats.behavior.IStats;
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36
37
38
39
40
41
42
43 public class RemoteCache<K, V>
44 extends AbstractRemoteAuxiliaryCache<K, V>
45 {
46
47 private static final Log log = LogFactory.getLog( RemoteCache.class );
48
49
50 private RemoteCacheMonitor monitor;
51
52
53 private AbstractRemoteCacheNoWaitFacade<K, V> facade;
54
55
56
57
58
59
60
61
62
63
64
65
66 public RemoteCache( IRemoteCacheAttributes cattr,
67 ICacheServiceNonLocal<K, V> remote,
68 IRemoteCacheListener<K, V> listener,
69 RemoteCacheMonitor monitor )
70 {
71 super( cattr, remote, listener );
72 this.monitor = monitor;
73
74 RemoteUtils.configureGlobalCustomSocketFactory( getRemoteCacheAttributes().getRmiSocketFactoryTimeoutMillis() );
75 }
76
77
78
79
80 @Override
81 public IStats getStatistics()
82 {
83 IStats stats = new Stats();
84 stats.setTypeName( "Remote Cache" );
85
86 ArrayList<IStatElement<?>> elems = new ArrayList<IStatElement<?>>();
87
88 elems.add(new StatElement<String>( "Remote Host:Port", getIPAddressForService() ) );
89 elems.add(new StatElement<String>( "Remote Type", this.getRemoteCacheAttributes().getRemoteTypeName() ) );
90
91
92
93
94
95
96
97 IStats sStats = super.getStatistics();
98 elems.addAll(sStats.getStatElements());
99
100 stats.setStatElements( elems );
101
102 return stats;
103 }
104
105
106
107
108
109
110 protected void setFacade(AbstractRemoteCacheNoWaitFacade<K, V> facade)
111 {
112 this.facade = facade;
113 }
114
115
116
117
118
119
120 protected AbstractRemoteCacheNoWaitFacade<K, V> getFacade()
121 {
122 return facade;
123 }
124
125
126
127
128
129
130
131
132
133
134 @Override
135 protected void handleException( Exception ex, String msg, String eventName )
136 throws IOException
137 {
138 String message = "Disabling remote cache due to error: " + msg;
139
140 logError( cacheName, "", message );
141 log.error( message, ex );
142
143
144 if ( getRemoteCacheService() == null || !( getRemoteCacheService() instanceof ZombieCacheServiceNonLocal ) )
145 {
146
147 setRemoteCacheService( new ZombieCacheServiceNonLocal<K, V>( getRemoteCacheAttributes().getZombieQueueMaxSize() ) );
148 }
149
150
151
152 monitor.notifyError();
153
154 if ( log.isDebugEnabled() )
155 {
156 log.debug( "Initiating failover, rcnwf = " + facade );
157 }
158
159 if ( facade != null && facade.getAuxiliaryCacheAttributes().getRemoteType() == RemoteType.LOCAL )
160 {
161 if ( log.isDebugEnabled() )
162 {
163 log.debug( "Found facade, calling failover" );
164 }
165
166
167 facade.failover( facade.getPrimaryServer() );
168 }
169
170 if ( ex instanceof IOException )
171 {
172 throw (IOException) ex;
173 }
174 throw new IOException( ex );
175 }
176
177
178
179
180
181
182 @Override
183 public String toString()
184 {
185 return "RemoteCache: " + cacheName + " attributes = " + getRemoteCacheAttributes();
186 }
187
188
189
190
191
192
193 @Override
194 public String getEventLoggingExtraInfo()
195 {
196 return getIPAddressForService();
197 }
198
199
200
201
202
203
204
205
206 protected String getIPAddressForService()
207 {
208 String ipAddress = "(null)";
209 if (this.getRemoteCacheAttributes().getRemoteLocation() != null)
210 {
211 ipAddress = this.getRemoteCacheAttributes().getRemoteLocation().toString();
212 }
213 return ipAddress;
214 }
215 }