KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > vfs > TcpPath


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.vfs;
31
32 import com.caucho.util.CharBuffer;
33
34 import java.io.IOException JavaDoc;
35 import java.net.InetSocketAddress JavaDoc;
36 import java.net.SocketAddress JavaDoc;
37 import java.util.Map JavaDoc;
38
39 /**
40  * Implements a tcp stream, essentially just a socket pair.
41  */

42 public class TcpPath extends Path {
43   // Attribute name for connection timeouts
44
public static final String JavaDoc CONNECT_TIMEOUT = "connect-timeout";
45   
46   protected String JavaDoc _host;
47   protected int _port;
48   protected SocketAddress JavaDoc _address;
49   protected long _timeout = 5000L;
50
51   public TcpPath(TcpPath root, String JavaDoc userPath,
52          Map JavaDoc<String JavaDoc,Object JavaDoc> newAttributes,
53          String JavaDoc host, int port)
54   {
55     super(root);
56
57     setUserPath(userPath);
58
59     _host = host;
60     _port = port == 0 ? 80 : port;
61
62     if (newAttributes != null) {
63       Object JavaDoc timeout = newAttributes.get("connect-timeout");
64
65       if (timeout instanceof Number JavaDoc)
66     _timeout = ((Number JavaDoc) timeout).longValue();
67     }
68   }
69
70   /**
71    * Lookup the new path assuming we're the scheme root.
72    */

73   protected Path schemeWalk(String JavaDoc userPath,
74                             Map JavaDoc<String JavaDoc,Object JavaDoc> newAttributes,
75                 String JavaDoc uri,
76                             int offset)
77   {
78     int length = uri.length();
79
80     if (length < 2 + offset ||
81         uri.charAt(offset) != '/' ||
82         uri.charAt(1 + offset) != '/')
83       throw new RuntimeException JavaDoc("bad scheme");
84
85     CharBuffer buf = new CharBuffer();
86     int i = 2 + offset;
87     int ch = 0;
88     for (; i < length && (ch = uri.charAt(i)) != ':' && ch != '/' && ch != '?';
89      i++) {
90       buf.append((char) ch);
91     }
92
93     String JavaDoc host = buf.toString();
94     if (host.length() == 0)
95       throw new RuntimeException JavaDoc("bad host");
96
97     int port = 0;
98     if (ch == ':') {
99       for (i++; i < length && (ch = uri.charAt(i)) >= '0' && 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,
108                String JavaDoc userPath, Map JavaDoc<String JavaDoc,Object JavaDoc> newAttributes,
109                String JavaDoc host, int port)
110   {
111     return new TcpPath(root, userPath, newAttributes, host, port);
112   }
113
114   public String JavaDoc getScheme()
115   {
116     return "tcp";
117   }
118
119   public String JavaDoc getURL()
120   {
121     return (getScheme() + "://" + getHost() + ":" + getPort());
122   }
123
124   public String JavaDoc getPath()
125   {
126     return "";
127   }
128
129   public String JavaDoc getHost()
130   {
131     return _host;
132   }
133
134   public int getPort()
135   {
136     return _port;
137   }
138
139   public SocketAddress JavaDoc getSocketAddress()
140   {
141     if (_address == null)
142       _address = new InetSocketAddress JavaDoc(_host, _port);
143
144     return _address;
145   }
146
147   /*
148   public void setAttribute(String attr, Object value)
149     throws IOException
150   {
151     if (attr.equals("timeout")) {
152       Long timeout = (Long) value;
153       _timeout = timeout.longValue();
154     }
155     else
156       super.setAttribute(attr, value);
157   }
158   */

159
160   public StreamImpl openReadImpl() throws IOException JavaDoc
161   {
162     return TcpStream.openRead(this, _timeout);
163   }
164
165   public StreamImpl openReadWriteImpl() throws IOException JavaDoc
166   {
167     return TcpStream.openReadWrite(this, _timeout);
168   }
169
170   @Override JavaDoc
171   protected Path cacheCopy()
172   {
173     return new TcpPath(null, getUserPath(), null, _host, _port);
174   }
175
176   public String JavaDoc toString()
177   {
178     return getURL();
179   }
180 }
181
Popular Tags