001: /* ====================================================================
002: The Jicarilla Software License
003:
004: Copyright (c) 2003 Leo Simons.
005: All rights reserved.
006:
007: Permission is hereby granted, free of charge, to any person obtaining
008: a copy of this software and associated documentation files (the
009: "Software"), to deal in the Software without restriction, including
010: without limitation the rights to use, copy, modify, merge, publish,
011: distribute, sublicense, and/or sell copies of the Software, and to
012: permit persons to whom the Software is furnished to do so, subject to
013: the following conditions:
014:
015: The above copyright notice and this permission notice shall be
016: included in all copies or substantial portions of the Software.
017:
018: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
019: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
020: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
021: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
022: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
023: TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
024: SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
025: ==================================================================== */
026: package org.jicarilla.webserver.plumbing;
027:
028: import org.jicarilla.lang.Assert;
029: import org.jicarilla.lang.Selector;
030: import org.jicarilla.plumbing.Screener;
031: import org.jicarilla.plumbing.SimpleScreener;
032: import org.jicarilla.plumbing.Sink;
033:
034: import java.net.InetAddress;
035: import java.util.ArrayList;
036: import java.util.Iterator;
037: import java.util.List;
038: import java.util.regex.Pattern;
039:
040: /**
041: *
042: *
043: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
044: * @version $Id: HTTPScreenerBuilderImpl.java,v 1.1 2004/03/31 12:11:00 lsimons Exp $
045: */
046: public class HTTPScreenerBuilderImpl implements HTTPScreenerBuilder {
047: protected List sinks = new ArrayList();
048:
049: public Screener create() throws Exception {
050: final Screener s = new SimpleScreener();
051: populate(s);
052: return s;
053: }
054:
055: public HTTPScreenerBuilder addStage(
056: final Object selectionCriterion, final Sink sink) {
057: sinks.add(new Entry(selectionCriterion, sink));
058: return this ;
059: }
060:
061: protected void populate(final Screener s) {
062: final Iterator it = sinks.iterator();
063: while (it.hasNext()) {
064: final Entry entry = (Entry) it.next();
065: if (entry.criterion instanceof HTTPSelector) {
066: s.addSink((Selector) entry.criterion, entry.sink);
067: continue;
068: }
069:
070: final HTTPSelector selector;
071:
072: if (entry.criterion instanceof Pattern) {
073: selector = new HTTPSelector();
074: selector.addURLPattern((Pattern) entry.criterion);
075: s.addSink(selector, entry.sink);
076: continue;
077: }
078:
079: if (entry.criterion instanceof InetAddress) {
080: selector = new HTTPSelector();
081: selector.addVirtualHost((InetAddress) entry.criterion);
082: s.addSink(selector, entry.sink);
083: continue;
084: }
085: }
086: }
087:
088: protected static class Entry {
089: public Object criterion;
090: public Sink sink;
091:
092: protected Entry(final Object aCriterion, final Sink aSink) {
093: Assert.assertNotNull("criterion argument may not be null",
094: criterion);
095: Assert.assertNotNull("sink argument may not be null", sink);
096:
097: criterion = aCriterion;
098: sink = aSink;
099: }
100: }
101: }
|