1 package com.icesoft.faces.webapp.http.common.standard; 2 3 import com.icesoft.faces.webapp.http.common.Request; 4 import com.icesoft.faces.webapp.http.common.Server; 5 6 import java.util.ArrayList ; 7 import java.util.Iterator ; 8 import java.util.List ; 9 import java.util.regex.Pattern ; 10 11 public class PathDispatcherServer implements Server { 12 private List matchers = new ArrayList (); 13 14 public void service(Request request) throws Exception { 15 String path = request.getURI().getPath(); 16 Iterator i = matchers.iterator(); 17 boolean matched = false; 18 while (!matched && i.hasNext()) { 19 matched = ((Matcher) i.next()).serviceOnMatch(path, request); 20 } 21 22 if (!matched) { 23 request.respondWith(NotFoundHandler.HANDLER); 24 } 25 } 26 27 public void dispatchOn(String pathExpression, Server toServer) { 28 matchers.add(new Matcher(pathExpression, toServer)); 29 } 30 31 public void shutdown() { 32 Iterator i = matchers.iterator(); 33 while (i.hasNext()) { 34 Matcher matcher = (Matcher) i.next(); 35 matcher.shutdown(); 36 } 37 } 38 39 private class Matcher { 40 private Pattern pattern; 41 private Server server; 42 43 public Matcher(String expression, Server server) { 44 this.pattern = Pattern.compile(expression); 45 this.server = server; 46 } 47 48 boolean serviceOnMatch(String path, Request request) throws Exception { 49 if (pattern.matcher(path).find()) { 50 server.service(request); 51 return true; 52 } else { 53 return false; 54 } 55 } 56 57 public void shutdown() { 58 server.shutdown(); 59 } 60 } 61 } 62 | Popular Tags |