KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > dispatch > DispatchServer


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

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 JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.Iterator JavaDoc;
40
41 /**
42  * The dispatch server is responsible for building Invocations,
43  * specifically for creating the FilterChain for the invocation.
44  */

45 public class DispatchServer implements Dependency {
46   private String JavaDoc _serverId = "";
47
48   private DispatchBuilder _dispatchBuilder;
49
50   // Cache of uri -> invocation maps
51
private LruCache<Object JavaDoc,Invocation> _invocationCache;
52
53   private InvocationDecoder _invocationDecoder;
54
55   private HashMap JavaDoc<String JavaDoc,Object JavaDoc> _attributeMap = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>();
56
57   private ArrayList JavaDoc<ServerListener> _listeners =
58     new ArrayList JavaDoc<ServerListener>();
59
60   private int _invocationCacheSize = 64 * 1024;
61   private int _maxURLLength = 256;
62
63   private final Lifecycle _lifecycle = new Lifecycle();
64
65   /**
66    * Gets the server's id.
67    */

68   public String JavaDoc getServerId()
69   {
70     return _serverId;
71   }
72
73   /**
74    * Gets the server's id.
75    */

76   public void setServerId(String JavaDoc serverId)
77   {
78     _serverId = serverId;
79   }
80
81   /**
82    * Gets the class loader.
83    */

84   public ClassLoader JavaDoc getClassLoader()
85   {
86     return null;
87   }
88
89   /**
90    * Sets the dispatch builder.
91    */

92   public void setDispatchBuilder(DispatchBuilder builder)
93   {
94     _dispatchBuilder = builder;
95   }
96
97   /**
98    * Gets the dispatch builder.
99    */

100   public DispatchBuilder getDispatchBuilder()
101   {
102     return _dispatchBuilder;
103   }
104
105   /**
106    * Return true for ignoring client disconnect.
107    */

108   public boolean isIgnoreClientDisconnect()
109   {
110     return true;
111   }
112
113   /**
114    * Sets the invocation cache size.
115    */

116   public void setInvocationCacheSize(int size)
117   {
118     _invocationCacheSize = size;
119
120     if (_invocationCacheSize <= 16)
121       _invocationCacheSize = 16;
122   }
123
124   /**
125    * Sets the max url length.
126    */

127   public void setInvocationCacheMaxURLLength(int length)
128   {
129     _maxURLLength = length;
130   }
131
132   /**
133    * Initializes the server.
134    */

135   @PostConstruct
136   public void init()
137   {
138     _invocationCache = new LruCache<Object JavaDoc,Invocation>(_invocationCacheSize);
139   }
140
141   /**
142    * Returns the InvocationDecoder.
143    */

144   public InvocationDecoder getInvocationDecoder()
145   {
146     if (_invocationDecoder == null)
147       _invocationDecoder = new InvocationDecoder();
148
149     return _invocationDecoder;
150   }
151
152   /**
153    * Returns the invocation decoder for configuration.
154    */

155   public InvocationDecoder createInvocationDecoder()
156   {
157     return getInvocationDecoder();
158   }
159
160   /**
161    * Returns the cached invocation.
162    */

163   public final Invocation getInvocation(Object JavaDoc protocolKey)
164   {
165     Invocation invocation = null;
166
167     // XXX: see if can remove this
168
LruCache<Object JavaDoc,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   /**
184    * Creates an invocation.
185    */

186   public Invocation createInvocation()
187   {
188     return new Invocation();
189   }
190
191   /**
192    * Builds the invocation, saving its value keyed by the protocol key.
193    *
194    * @param protocolKey protocol-specific key to save the invocation in
195    * @param invocation the invocation to build.
196    */

197   public Invocation buildInvocation(Object JavaDoc protocolKey, Invocation invocation)
198     throws Throwable JavaDoc
199   {
200     buildInvocation(invocation);
201
202     // XXX: see if can remove this, and rely on the invocation cache existing
203
LruCache<Object JavaDoc,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   /**
223    * Builds the invocation.
224    */

225   public void buildInvocation(Invocation invocation)
226     throws Throwable JavaDoc
227   {
228     getDispatchBuilder().buildInvocation(invocation);
229   }
230
231   /**
232    * Clears the proxy cache.
233    */

234   public void clearCache()
235   {
236     // XXX: see if can remove this, and rely on the invocation cache existing
237
LruCache<Object JavaDoc,Invocation> invocationCache = _invocationCache;
238
239     if (invocationCache != null) {
240       synchronized (invocationCache) {
241     invocationCache.clear();
242       }
243     }
244   }
245
246   /**
247    * Clears matching entries.
248    */

249   protected void invalidateMatchingInvocations(InvocationMatcher matcher)
250   {
251     // XXX: see if can remove this, and rely on the invocation cache existing
252
LruCache<Object JavaDoc,Invocation> invocationCache = _invocationCache;
253
254     if (invocationCache != null) {
255       synchronized (invocationCache) {
256     Iterator JavaDoc<LruCache.Entry<Object JavaDoc,Invocation>> iter;
257     iter = invocationCache.iterator();
258
259     while (iter.hasNext()) {
260       LruCache.Entry<Object JavaDoc,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   /**
272    * Returns the invocation cache hit count.
273    */

274   public long getInvocationCacheHitCount()
275   {
276     LruCache<Object JavaDoc,Invocation> invocationCache = _invocationCache;
277
278     if (invocationCache != null)
279       return invocationCache.getHitCount();
280     else
281       return 0;
282   }
283
284   /**
285    * Returns the invocation cache hit count.
286    */

287   public long getInvocationCacheMissCount()
288   {
289     LruCache<Object JavaDoc,Invocation> invocationCache = _invocationCache;
290
291     if (invocationCache != null)
292       return invocationCache.getMissCount();
293     else
294       return 0;
295   }
296
297   /**
298    * Returns the named attribute.
299    *
300    * @param key the attribute key
301    *
302    * @return the attribute value
303    */

304   public synchronized Object JavaDoc getAttribute(String JavaDoc key)
305   {
306     return _attributeMap.get(key);
307   }
308
309   /**
310    * Returns an iteration of attribute names.
311    *
312    * @return the iteration of names
313    */

314   public synchronized Iterator JavaDoc getAttributeNames()
315   {
316     return _attributeMap.keySet().iterator();
317   }
318
319   /**
320    * Sets the named attribute.
321    *
322    * @param key the attribute key
323    * @param value the attribute value
324    *
325    * @return the old attribute value
326    */

327   public synchronized Object JavaDoc setAttribute(String JavaDoc key, Object JavaDoc value)
328   {
329     return _attributeMap.put(key, value);
330   }
331
332   /**
333    * Removes the named attribute.
334    *
335    * @param key the attribute key
336    *
337    * @return the old attribute value
338    */

339   public synchronized Object JavaDoc removeAttribute(String JavaDoc key)
340   {
341     return _attributeMap.remove(key);
342   }
343
344   /**
345    * Returns true if the server has been modified and needs restarting.
346    */

347   public boolean isModified()
348   {
349     return false;
350   }
351
352   /**
353    * Adds a listener.
354    */

355   public void addServerListener(ServerListener listener)
356   {
357     _listeners.add(listener);
358   }
359
360   /**
361    * Returns true if the server is destroyed.
362    */

363   public boolean isDestroyed()
364   {
365     return _lifecycle.isDestroyed();
366   }
367
368   /**
369    * Closes the server.
370    */

371   public void update()
372   {
373     destroy();
374   }
375
376   /**
377    * Closes the server.
378    */

379   public void restart()
380   {
381     destroy();
382   }
383
384   /**
385    * Closes the server.
386    */

387   public void destroy()
388   {
389     ArrayList JavaDoc<ServerListener> listeners;
390     listeners = new ArrayList JavaDoc<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