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 EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
029: import org.jicarilla.lang.Assert;
030: import org.jicarilla.lang.RecyclingObjectFactory;
031: import org.jicarilla.net.Event;
032: import org.jicarilla.plumbing.NoopSink;
033: import org.jicarilla.plumbing.Stage;
034: import org.mortbay.http.HttpServer;
035:
036: import java.net.InetAddress;
037:
038: /**
039: * Delegates request/response handling to Jetty completely. If you
040: * use this stage, it should be the only one.
041: *
042: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
043: * @version $Id: JettyChannelFactory.java,v 1.1 2004/03/31 12:11:00 lsimons Exp $
044: */
045: public class JettyChannelFactory extends RecyclingObjectFactory {
046: protected HttpServer m_delegate;
047: protected InetAddress m_address;
048: protected Stage m_stage;
049: protected Stage m_processor;
050:
051: public JettyChannelFactory(final HttpServer delegate,
052: final InetAddress address) {
053: m_delegate = delegate;
054: m_address = address;
055: m_stage = new JettyStage(m_delegate, m_address,
056: new LinkedQueue(), new NoopSink());
057: m_processor = createProcessor();
058: }
059:
060: public synchronized Object makeObject() throws Exception {
061: return m_processor;
062: }
063:
064: protected Stage createProcessor() {
065: return new Stage() {
066: public void put(final Object o) throws InterruptedException {
067: final HTTPEvent e = getEvent(o);
068: m_stage.put(e);
069: }
070:
071: public boolean offer(final Object o, final long l)
072: throws InterruptedException {
073: final HTTPEvent e = getEvent(o);
074: return m_stage.offer(e, l);
075: }
076:
077: protected HTTPEvent getEvent(final Object o) {
078: Assert.assertTrue(o instanceof Event);
079:
080: if (o instanceof HTTPEvent)
081: return (HTTPEvent) o;
082:
083: final Event ev = (Event) o;
084: final HTTPEvent e = new HTTPEvent();
085: e.setChannel(ev.getChannel());
086: e.setContext(ev.getContext());
087: return e;
088: }
089:
090: public Object take() throws InterruptedException {
091: return m_stage.take();
092: }
093:
094: public Object poll(final long l)
095: throws InterruptedException {
096: return m_stage.poll(l);
097: }
098:
099: public Object peek() {
100: return m_stage.peek();
101: }
102: };
103: }
104:
105: }
|