01: package com.icesoft.faces.webapp.http.common.standard;
02:
03: import com.icesoft.faces.webapp.http.common.Request;
04: import com.icesoft.faces.webapp.http.common.Server;
05:
06: import java.util.ArrayList;
07: import java.util.Iterator;
08: import java.util.List;
09: 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())
20: .serviceOnMatch(path, request);
21: }
22:
23: if (!matched) {
24: request.respondWith(NotFoundHandler.HANDLER);
25: }
26: }
27:
28: public void dispatchOn(String pathExpression, Server toServer) {
29: matchers.add(new Matcher(pathExpression, toServer));
30: }
31:
32: public void shutdown() {
33: Iterator i = matchers.iterator();
34: while (i.hasNext()) {
35: Matcher matcher = (Matcher) i.next();
36: matcher.shutdown();
37: }
38: }
39:
40: private class Matcher {
41: private Pattern pattern;
42: private Server server;
43:
44: public Matcher(String expression, Server server) {
45: this .pattern = Pattern.compile(expression);
46: this .server = server;
47: }
48:
49: boolean serviceOnMatch(String path, Request request)
50: throws Exception {
51: if (pattern.matcher(path).find()) {
52: server.service(request);
53: return true;
54: } else {
55: return false;
56: }
57: }
58:
59: public void shutdown() {
60: server.shutdown();
61: }
62: }
63: }
|