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