source: proiecte/HadoopJUnit/hadoop-0.20.1/src/core/org/apache/hadoop/net/SocksSocketFactory.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 4.3 KB
Line 
1/**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18package org.apache.hadoop.net;
19
20import java.io.IOException;
21import java.net.InetAddress;
22import java.net.InetSocketAddress;
23import java.net.Proxy;
24import java.net.Socket;
25import java.net.UnknownHostException;
26
27import javax.net.SocketFactory;
28
29import org.apache.hadoop.conf.Configurable;
30import org.apache.hadoop.conf.Configuration;
31
32/**
33 * Specialized SocketFactory to create sockets with a SOCKS proxy
34 */
35public class SocksSocketFactory extends SocketFactory implements
36    Configurable {
37
38  private Configuration conf;
39
40  private Proxy proxy;
41
42  /**
43   * Default empty constructor (for use with the reflection API).
44   */
45  public SocksSocketFactory() {
46    this.proxy = Proxy.NO_PROXY;
47  }
48
49  /**
50   * Constructor with a supplied Proxy
51   *
52   * @param proxy the proxy to use to create sockets
53   */
54  public SocksSocketFactory(Proxy proxy) {
55    this.proxy = proxy;
56  }
57
58  /* @inheritDoc */
59  @Override
60  public Socket createSocket() throws IOException {
61
62    return new Socket(proxy);
63  }
64
65  /* @inheritDoc */
66  @Override
67  public Socket createSocket(InetAddress addr, int port) throws IOException {
68
69    Socket socket = createSocket();
70    socket.connect(new InetSocketAddress(addr, port));
71    return socket;
72  }
73
74  /* @inheritDoc */
75  @Override
76  public Socket createSocket(InetAddress addr, int port,
77      InetAddress localHostAddr, int localPort) throws IOException {
78
79    Socket socket = createSocket();
80    socket.bind(new InetSocketAddress(localHostAddr, localPort));
81    socket.connect(new InetSocketAddress(addr, port));
82    return socket;
83  }
84
85  /* @inheritDoc */
86  @Override
87  public Socket createSocket(String host, int port) throws IOException,
88      UnknownHostException {
89
90    Socket socket = createSocket();
91    socket.connect(new InetSocketAddress(host, port));
92    return socket;
93  }
94
95  /* @inheritDoc */
96  @Override
97  public Socket createSocket(String host, int port,
98      InetAddress localHostAddr, int localPort) throws IOException,
99      UnknownHostException {
100
101    Socket socket = createSocket();
102    socket.bind(new InetSocketAddress(localHostAddr, localPort));
103    socket.connect(new InetSocketAddress(host, port));
104    return socket;
105  }
106
107  /* @inheritDoc */
108  @Override
109  public int hashCode() {
110    return proxy.hashCode();
111  }
112
113  /* @inheritDoc */
114  @Override
115  public boolean equals(Object obj) {
116    if (this == obj)
117      return true;
118    if (obj == null)
119      return false;
120    if (!(obj instanceof SocksSocketFactory))
121      return false;
122    final SocksSocketFactory other = (SocksSocketFactory) obj;
123    if (proxy == null) {
124      if (other.proxy != null)
125        return false;
126    } else if (!proxy.equals(other.proxy))
127      return false;
128    return true;
129  }
130
131  /* @inheritDoc */
132  public Configuration getConf() {
133    return this.conf;
134  }
135
136  /* @inheritDoc */
137  public void setConf(Configuration conf) {
138    this.conf = conf;
139    String proxyStr = conf.get("hadoop.socks.server");
140    if ((proxyStr != null) && (proxyStr.length() > 0)) {
141      setProxy(proxyStr);
142    }
143  }
144
145  /**
146   * Set the proxy of this socket factory as described in the string
147   * parameter
148   *
149   * @param proxyStr the proxy address using the format "host:port"
150   */
151  private void setProxy(String proxyStr) {
152    String[] strs = proxyStr.split(":", 2);
153    if (strs.length != 2)
154      throw new RuntimeException("Bad SOCKS proxy parameter: " + proxyStr);
155    String host = strs[0];
156    int port = Integer.parseInt(strs[1]);
157    this.proxy =
158        new Proxy(Proxy.Type.SOCKS, InetSocketAddress.createUnresolved(host,
159            port));
160  }
161}
Note: See TracBrowser for help on using the repository browser.