?? heritrixprotocolsocketfactory.html
字號:
<a name="97" href="#97">97</a> <em> * determined</em><a name="98" href="#98">98</a> <em> * @throws ConnectTimeoutException if socket cannot be connected within the</em><a name="99" href="#99">99</a> <em> * given time limit</em><a name="100" href="#100">100</a> <em> *</em><a name="101" href="#101">101</a> <em> * @since 3.0</em><a name="102" href="#102">102</a> <em> */</em><a name="103" href="#103">103</a> <strong>public</strong> Socket createSocket(<a name="104" href="#104">104</a> <strong>final</strong> String host,<a name="105" href="#105">105</a> <strong>final</strong> <strong>int</strong> port,<a name="106" href="#106">106</a> <strong>final</strong> InetAddress localAddress,<a name="107" href="#107">107</a> <strong>final</strong> <strong>int</strong> localPort,<a name="108" href="#108">108</a> <strong>final</strong> HttpConnectionParams params)<a name="109" href="#109">109</a> throws IOException, UnknownHostException, ConnectTimeoutException {<a name="110" href="#110">110</a> <em class="comment">// Below code is from the DefaultSSLProtocolSocketFactory#createSocket</em><a name="111" href="#111">111</a> <em class="comment">// method only it has workarounds to deal with pre-1.4 JVMs. I've</em><a name="112" href="#112">112</a> <em class="comment">// cut these out.</em><a name="113" href="#113">113</a> <strong>if</strong> (params == <strong>null</strong>) {<a name="114" href="#114">114</a> <strong>throw</strong> <strong>new</strong> IllegalArgumentException(<span class="string">"Parameters may not be null"</span>);<a name="115" href="#115">115</a> }<a name="116" href="#116">116</a> Socket socket = <strong>null</strong>;<a name="117" href="#117">117</a> <strong>int</strong> timeout = params.getConnectionTimeout();<a name="118" href="#118">118</a> <strong>if</strong> (timeout == 0) {<a name="119" href="#119">119</a> socket = createSocket(host, port, localAddress, localPort);<a name="120" href="#120">120</a> } <strong>else</strong> {<a name="121" href="#121">121</a> socket = <strong>new</strong> Socket();<a name="122" href="#122">122</a> <a href="../../../../org/archive/crawler/datamodel/ServerCache.html">ServerCache</a> cache = (ServerCache)params.<a name="123" href="#123">123</a> getParameter(FetchHTTP.SERVER_CACHE_KEY);<a name="124" href="#124">124</a> InetAddress hostAddress =<a name="125" href="#125">125</a> (cache != <strong>null</strong>)? getHostAddress(cache, host): <strong>null</strong>;<a name="126" href="#126">126</a> InetSocketAddress address = (hostAddress != <strong>null</strong>)?<a name="127" href="#127">127</a> <strong>new</strong> InetSocketAddress(hostAddress, port):<a name="128" href="#128">128</a> <strong>new</strong> InetSocketAddress(host, port);<a name="129" href="#129">129</a> socket.bind(<strong>new</strong> InetSocketAddress(localAddress, localPort));<a name="130" href="#130">130</a> <strong>try</strong> {<a name="131" href="#131">131</a> socket.connect(address, timeout);<a name="132" href="#132">132</a> } <strong>catch</strong> (SocketTimeoutException e) {<a name="133" href="#133">133</a> <em class="comment">// Add timeout info. to the exception.</em><a name="134" href="#134">134</a> <strong>throw</strong> <strong>new</strong> SocketTimeoutException(e.getMessage() +<a name="135" href="#135">135</a> <span class="string">": timeout set at "</span> + Integer.toString(timeout) + <span class="string">"ms."</span>);<a name="136" href="#136">136</a> }<a name="137" href="#137">137</a> assert socket.isConnected(): <span class="string">"Socket not connected "</span> + host;<a name="138" href="#138">138</a> }<a name="139" href="#139">139</a> <strong>return</strong> socket;<a name="140" href="#140">140</a> }<a name="141" href="#141">141</a> <a name="142" href="#142">142</a> <em>/**<em>*</em></em><a name="143" href="#143">143</a> <em> * Get host address using first the heritrix cache of addresses, then,</em><a name="144" href="#144">144</a> <em> * failing that, go to the dnsjava cache.</em><a name="145" href="#145">145</a> <em> * </em><a name="146" href="#146">146</a> <em> * Default access and static so can be used by other classes in this</em><a name="147" href="#147">147</a> <em> * package.</em><a name="148" href="#148">148</a> <em> *</em><a name="149" href="#149">149</a> <em> * @param host Host whose address we're to fetch.</em><a name="150" href="#150">150</a> <em> * @return an IP address for this host or null if one can't be found</em><a name="151" href="#151">151</a> <em> * in caches.</em><a name="152" href="#152">152</a> <em> * @exception IOException If we fail to get host IP from ServerCache.</em><a name="153" href="#153">153</a> <em> */</em><a name="154" href="#154">154</a> <strong>static</strong> InetAddress getHostAddress(<strong>final</strong> <a href="../../../../org/archive/crawler/datamodel/ServerCache.html">ServerCache</a> cache,<a name="155" href="#155">155</a> <strong>final</strong> String host) throws IOException {<a name="156" href="#156">156</a> InetAddress result = <strong>null</strong>;<a name="157" href="#157">157</a> <strong>if</strong> (cache != <strong>null</strong>) {<a name="158" href="#158">158</a> CrawlHost ch = cache.getHostFor(host);<a name="159" href="#159">159</a> <strong>if</strong> (ch != <strong>null</strong>) {<a name="160" href="#160">160</a> result = ch.getIP();<a name="161" href="#161">161</a> }<a name="162" href="#162">162</a> }<a name="163" href="#163">163</a> <strong>if</strong> (result == <strong>null</strong>) {<a name="164" href="#164">164</a> <strong>throw</strong> <strong>new</strong> IOException(<span class="string">"Failed to get host "</span> + host +<a name="165" href="#165">165</a> <span class="string">" address from ServerCache"</span>);<a name="166" href="#166">166</a> }<a name="167" href="#167">167</a> <strong>return</strong> result;<a name="168" href="#168">168</a> }<a name="169" href="#169">169</a> <a name="170" href="#170">170</a> <em>/**<em>*</em></em><a name="171" href="#171">171</a> <em> * @see ProtocolSocketFactory#createSocket(java.lang.String,int)</em><a name="172" href="#172">172</a> <em> */</em><a name="173" href="#173">173</a> <strong>public</strong> Socket createSocket(String host, <strong>int</strong> port)<a name="174" href="#174">174</a> throws IOException, UnknownHostException {<a name="175" href="#175">175</a> <strong>return</strong> <strong>new</strong> Socket(host, port);<a name="176" href="#176">176</a> }<a name="177" href="#177">177</a> <a name="178" href="#178">178</a> <em>/**<em>*</em></em><a name="179" href="#179">179</a> <em> * All instances of DefaultProtocolSocketFactory are the same.</em><a name="180" href="#180">180</a> <em> * @param obj Object to compare.</em><a name="181" href="#181">181</a> <em> * @return True if equal</em><a name="182" href="#182">182</a> <em> */</em><a name="183" href="#183">183</a> <strong>public</strong> <strong>boolean</strong> equals(Object obj) {<a name="184" href="#184">184</a> <strong>return</strong> ((obj != <strong>null</strong>) &&<a name="185" href="#185">185</a> obj.getClass().equals(HeritrixProtocolSocketFactory.<strong>class</strong>));<a name="186" href="#186">186</a> }<a name="187" href="#187">187</a> <a name="188" href="#188">188</a> <em>/**<em>*</em></em><a name="189" href="#189">189</a> <em> * All instances of DefaultProtocolSocketFactory have the same hash code.</em><a name="190" href="#190">190</a> <em> * @return Hash code for this object.</em><a name="191" href="#191">191</a> <em> */</em><a name="192" href="#192">192</a> <strong>public</strong> <strong>int</strong> hashCode() {<a name="193" href="#193">193</a> <strong>return</strong> HeritrixProtocolSocketFactory.<strong>class</strong>.hashCode();<a name="194" href="#194">194</a> }<a name="195" href="#195">195</a> }</pre><hr/><div id="footer">This page was automatically generated by <a href="http://maven.apache.org/">Maven</a></div></body></html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -