01: /*
02: * <copyright>
03: *
04: * Copyright 1997-2007 BBNT Solutions, LLC
05: * under sponsorship of the Defense Advanced Research Projects
06: * Agency (DARPA).
07: *
08: * You can redistribute this software and/or modify it under the
09: * terms of the Cougaar Open Source License as published on the
10: * Cougaar Open Source Website (www.cougaar.org).
11: *
12: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
16: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23: *
24: * </copyright>
25: */
26:
27: package org.cougaar.lib.web.micro.base;
28:
29: import java.io.IOException;
30: import java.io.Serializable;
31: import java.net.Socket;
32: import java.net.URI;
33: import java.net.URL;
34: import java.util.Map;
35: import javax.servlet.ServletInputStream;
36: import javax.servlet.ServletOutputStream;
37: import javax.servlet.http.HttpServletRequest;
38:
39: /**
40: * A standard {@link Socket}-based client connection factory implementation.
41: */
42: public class SocketClientFactory implements ClientFactory {
43:
44: /**
45: * @param o must be a URI
46: */
47: public Connection connect(Object o, Map metaData)
48: throws IOException {
49: if (!(o instanceof URI)) {
50: throw new IllegalArgumentException("Expecting a URI, not "
51: + (o == null ? "null" : o.getClass().getName()));
52: }
53: URI uri = (URI) o;
54:
55: final Socket socket = new Socket(uri.getHost(), uri.getPort());
56:
57: // FIXME ignore metaData? If we're sure we're connecting to our own
58: // SocketServerFactory then we could send this data as a data header, but
59: // if we're connecting to a standard HTTP listener then we have nowhere
60: // to put this info...
61:
62: return new Connection() {
63: public Map getMetaData() {
64: return null; // not applicable
65: }
66:
67: public AnnotatedInputStream getInputStream()
68: throws IOException {
69: return AnnotatedInputStream
70: .toAnnotatedInputStream(socket.getInputStream());
71: }
72:
73: public AnnotatedOutputStream getOutputStream()
74: throws IOException {
75: return AnnotatedOutputStream
76: .toAnnotatedOutputStream(socket
77: .getOutputStream());
78: }
79:
80: public void close() throws IOException {
81: socket.close();
82: }
83: };
84: }
85:
86: }
|