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