001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.vfs;
031:
032: import com.caucho.util.CharBuffer;
033:
034: import java.io.IOException;
035: import java.net.InetSocketAddress;
036: import java.net.SocketAddress;
037: import java.util.Map;
038:
039: /**
040: * Implements a tcp stream, essentially just a socket pair.
041: */
042: public class TcpPath extends Path {
043: // Attribute name for connection timeouts
044: public static final String CONNECT_TIMEOUT = "connect-timeout";
045:
046: protected String _host;
047: protected int _port;
048: protected SocketAddress _address;
049: protected long _timeout = 5000L;
050:
051: public TcpPath(TcpPath root, String userPath,
052: Map<String, Object> newAttributes, String host, int port) {
053: super (root);
054:
055: setUserPath(userPath);
056:
057: _host = host;
058: _port = port == 0 ? 80 : port;
059:
060: if (newAttributes != null) {
061: Object timeout = newAttributes.get("connect-timeout");
062:
063: if (timeout instanceof Number)
064: _timeout = ((Number) timeout).longValue();
065:
066: timeout = newAttributes.get("timeout");
067:
068: if (timeout instanceof Number)
069: _timeout = ((Number) timeout).longValue();
070: }
071: }
072:
073: /**
074: * Lookup the new path assuming we're the scheme root.
075: */
076: protected Path schemeWalk(String userPath,
077: Map<String, Object> newAttributes, String uri, int offset) {
078: int length = uri.length();
079:
080: if (length < 2 + offset || uri.charAt(offset) != '/'
081: || uri.charAt(1 + offset) != '/')
082: throw new RuntimeException("bad scheme");
083:
084: CharBuffer buf = new CharBuffer();
085: int i = 2 + offset;
086: int ch = 0;
087: for (; i < length && (ch = uri.charAt(i)) != ':' && ch != '/'
088: && ch != '?'; i++) {
089: buf.append((char) ch);
090: }
091:
092: String host = buf.toString();
093: if (host.length() == 0)
094: throw new RuntimeException("bad host");
095:
096: int port = 0;
097: if (ch == ':') {
098: for (i++; i < length && (ch = uri.charAt(i)) >= '0'
099: && ch <= '9'; i++) {
100: port = 10 * port + uri.charAt(i) - '0';
101: }
102: }
103:
104: return create(this , userPath, newAttributes, host, port);
105: }
106:
107: protected TcpPath create(TcpPath root, String userPath,
108: Map<String, Object> newAttributes, String host, int port) {
109: return new TcpPath(root, userPath, newAttributes, host, port);
110: }
111:
112: public String getScheme() {
113: return "tcp";
114: }
115:
116: public String getURL() {
117: return (getScheme() + "://" + getHost() + ":" + getPort());
118: }
119:
120: public String getPath() {
121: return "";
122: }
123:
124: public String getHost() {
125: return _host;
126: }
127:
128: public int getPort() {
129: return _port;
130: }
131:
132: public SocketAddress getSocketAddress() {
133: if (_address == null)
134: _address = new InetSocketAddress(_host, _port);
135:
136: return _address;
137: }
138:
139: public StreamImpl openReadImpl() throws IOException {
140: return TcpStream.openRead(this , _timeout);
141: }
142:
143: public StreamImpl openReadWriteImpl() throws IOException {
144: return TcpStream.openReadWrite(this , _timeout);
145: }
146:
147: @Override
148: protected Path cacheCopy() {
149: return new TcpPath(null, getUserPath(), null, _host, _port);
150: }
151:
152: public String toString() {
153: return getURL();
154: }
155: }
|