KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > soto > util > ResourceHandlerChain


1 package org.sapia.soto.util;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5
6 /**
7  * This class implements a chain of responsibility: an instance of this
8  * class holds a list of <code>ResourceHandler</code> instances. The
9  * instances are traversed when client application request a
10  * specific handler, given a URI.
11  *
12  * @author Yanick Duchesne
13  *
14  * <dl>
15  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2004 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
16  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
17  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
18  * </dl>
19  */

20 public class ResourceHandlerChain {
21     
22     private List JavaDoc _handlers = new ArrayList JavaDoc();
23     
24     /**
25      * Adds a resource handler to the beginning of the list that
26      * this instance holds.
27      * @param handler a <code>ResourceHandler</code>.
28      */

29     public synchronized void prepend(ResourceHandler handler){
30         _handlers.add(0, handler);
31     }
32
33     /**
34      * Adds a resource handler to the end of the list that
35      * this instance holds.
36      * @param handler a <code>ResourceHandler</code>.
37      */

38     public synchronized void append(ResourceHandler handler){
39         _handlers.add(handler);
40     }
41     
42     /**
43      * Selects a <code>ResourceHandler</code> from this instance and
44      * returns it. The handler is chose based on the given URI.
45      * <p/>
46      * This instance traverses its handlers, calling the <code>accepts()</code>
47      * method on them (the method takes the given URI as a parameter). The first
48      * handler that "accepts" the URI (by returning <code>true</code>) is returned
49      * to the caller. If no handler as accepted the URI, <code>null</code> is returned.
50      *
51      * @param uri a URI.
52      * @return a <b>ResourceHandler</b>, or <code>null</code> if no handler
53      * could be found for the given URI.
54      */

55     public synchronized ResourceHandler select(String JavaDoc uri){
56         ResourceHandler handler = null;
57         for(int i = 0; i < _handlers.size(); i++){
58             handler = (ResourceHandler)_handlers.get(i);
59             if(handler.accepts(uri)){
60                 return handler;
61             }
62         }
63         return handler;
64     }
65
66 }
67
Popular Tags