1 package org.sapia.resource; 2 3 import java.io.IOException ; 4 import java.util.ArrayList ; 5 import java.util.List ; 6 7 14 public class ResourceHandlerChain implements ResourceCapable{ 15 16 private List _handlers = new ArrayList (); 17 18 25 public synchronized void prepend(ResourceHandler handler) { 26 _handlers.add(0, handler); 27 } 28 29 35 public synchronized void append(ResourceHandler handler) { 36 _handlers.add(handler); 37 } 38 39 public synchronized Resource resolveResource(String uri) 40 throws ResourceNotFoundException, IOException { 41 ResourceHandler handler = doSelect(uri); 42 if(handler == null){ 43 throw new ResourceNotFoundException("Could not find resource handler for: " + uri); 44 } 45 return handler.getResourceObject(uri); 46 } 47 48 61 public synchronized ResourceHandler select(String uri) { 62 63 return doSelect(uri); 64 } 65 66 protected ResourceHandler doSelect(String uri){ 67 ResourceHandler handler = null; 68 for(int i = 0; i < _handlers.size(); i++) { 69 handler = (ResourceHandler) _handlers.get(i); 70 if(handler.accepts(uri)) { 71 break; 72 } 73 else{ 74 handler = null; 75 } 76 } 77 return handler; 78 } 79 80 } 81 | Popular Tags |