1 29 30 package com.caucho.server.dispatch; 31 32 import com.caucho.lifecycle.Lifecycle; 33 import com.caucho.util.LruCache; 34 import com.caucho.vfs.Dependency; 35 36 import javax.annotation.PostConstruct; 37 import java.util.ArrayList ; 38 import java.util.HashMap ; 39 import java.util.Iterator ; 40 41 45 public class DispatchServer implements Dependency { 46 private String _serverId = ""; 47 48 private DispatchBuilder _dispatchBuilder; 49 50 private LruCache<Object ,Invocation> _invocationCache; 52 53 private InvocationDecoder _invocationDecoder; 54 55 private HashMap <String ,Object > _attributeMap = new HashMap <String ,Object >(); 56 57 private ArrayList <ServerListener> _listeners = 58 new ArrayList <ServerListener>(); 59 60 private int _invocationCacheSize = 64 * 1024; 61 private int _maxURLLength = 256; 62 63 private final Lifecycle _lifecycle = new Lifecycle(); 64 65 68 public String getServerId() 69 { 70 return _serverId; 71 } 72 73 76 public void setServerId(String serverId) 77 { 78 _serverId = serverId; 79 } 80 81 84 public ClassLoader getClassLoader() 85 { 86 return null; 87 } 88 89 92 public void setDispatchBuilder(DispatchBuilder builder) 93 { 94 _dispatchBuilder = builder; 95 } 96 97 100 public DispatchBuilder getDispatchBuilder() 101 { 102 return _dispatchBuilder; 103 } 104 105 108 public boolean isIgnoreClientDisconnect() 109 { 110 return true; 111 } 112 113 116 public void setInvocationCacheSize(int size) 117 { 118 _invocationCacheSize = size; 119 120 if (_invocationCacheSize <= 16) 121 _invocationCacheSize = 16; 122 } 123 124 127 public void setInvocationCacheMaxURLLength(int length) 128 { 129 _maxURLLength = length; 130 } 131 132 135 @PostConstruct 136 public void init() 137 { 138 _invocationCache = new LruCache<Object ,Invocation>(_invocationCacheSize); 139 } 140 141 144 public InvocationDecoder getInvocationDecoder() 145 { 146 if (_invocationDecoder == null) 147 _invocationDecoder = new InvocationDecoder(); 148 149 return _invocationDecoder; 150 } 151 152 155 public InvocationDecoder createInvocationDecoder() 156 { 157 return getInvocationDecoder(); 158 } 159 160 163 public final Invocation getInvocation(Object protocolKey) 164 { 165 Invocation invocation = null; 166 167 LruCache<Object ,Invocation> invocationCache = _invocationCache; 169 170 if (invocationCache != null) 171 invocation = invocationCache.get(protocolKey); 172 173 if (invocation == null) 174 return null; 175 else if (invocation.isModified()) { 176 return null; 177 } 178 else { 179 return invocation; 180 } 181 } 182 183 186 public Invocation createInvocation() 187 { 188 return new Invocation(); 189 } 190 191 197 public Invocation buildInvocation(Object protocolKey, Invocation invocation) 198 throws Throwable 199 { 200 buildInvocation(invocation); 201 202 LruCache<Object ,Invocation> invocationCache = _invocationCache; 204 205 if (invocationCache != null) { 206 synchronized (invocationCache) { 207 Invocation oldInvocation; 208 oldInvocation = invocationCache.get(protocolKey); 209 210 if (oldInvocation != null) 211 return oldInvocation; 212 213 if (invocation.getURLLength() < _maxURLLength) { 214 invocationCache.put(protocolKey, invocation); 215 } 216 } 217 } 218 219 return invocation; 220 } 221 222 225 public void buildInvocation(Invocation invocation) 226 throws Throwable 227 { 228 getDispatchBuilder().buildInvocation(invocation); 229 } 230 231 234 public void clearCache() 235 { 236 LruCache<Object ,Invocation> invocationCache = _invocationCache; 238 239 if (invocationCache != null) { 240 synchronized (invocationCache) { 241 invocationCache.clear(); 242 } 243 } 244 } 245 246 249 protected void invalidateMatchingInvocations(InvocationMatcher matcher) 250 { 251 LruCache<Object ,Invocation> invocationCache = _invocationCache; 253 254 if (invocationCache != null) { 255 synchronized (invocationCache) { 256 Iterator <LruCache.Entry<Object ,Invocation>> iter; 257 iter = invocationCache.iterator(); 258 259 while (iter.hasNext()) { 260 LruCache.Entry<Object ,Invocation> entry = iter.next(); 261 Invocation value = entry.getValue(); 262 263 if (value != null && matcher.isMatch(value)) { 264 iter.remove(); 265 } 266 } 267 } 268 } 269 } 270 271 274 public long getInvocationCacheHitCount() 275 { 276 LruCache<Object ,Invocation> invocationCache = _invocationCache; 277 278 if (invocationCache != null) 279 return invocationCache.getHitCount(); 280 else 281 return 0; 282 } 283 284 287 public long getInvocationCacheMissCount() 288 { 289 LruCache<Object ,Invocation> invocationCache = _invocationCache; 290 291 if (invocationCache != null) 292 return invocationCache.getMissCount(); 293 else 294 return 0; 295 } 296 297 304 public synchronized Object getAttribute(String key) 305 { 306 return _attributeMap.get(key); 307 } 308 309 314 public synchronized Iterator getAttributeNames() 315 { 316 return _attributeMap.keySet().iterator(); 317 } 318 319 327 public synchronized Object setAttribute(String key, Object value) 328 { 329 return _attributeMap.put(key, value); 330 } 331 332 339 public synchronized Object removeAttribute(String key) 340 { 341 return _attributeMap.remove(key); 342 } 343 344 347 public boolean isModified() 348 { 349 return false; 350 } 351 352 355 public void addServerListener(ServerListener listener) 356 { 357 _listeners.add(listener); 358 } 359 360 363 public boolean isDestroyed() 364 { 365 return _lifecycle.isDestroyed(); 366 } 367 368 371 public void update() 372 { 373 destroy(); 374 } 375 376 379 public void restart() 380 { 381 destroy(); 382 } 383 384 387 public void destroy() 388 { 389 ArrayList <ServerListener> listeners; 390 listeners = new ArrayList <ServerListener>(_listeners); 391 _listeners.clear(); 392 393 for (int i = 0; i < listeners.size(); i++) { 394 ServerListener listener = listeners.get(i); 395 396 listener.closeEvent(this); 397 } 398 399 _invocationCache = null; 400 } 401 } 402 | Popular Tags |