jaseninetaddressresolver.java
来自「spam source codejasen-0.9jASEN - java An」· Java 代码 · 共 165 行
JAVA
165 行
/*
* @(#)JasenInetAddressResolver.java 4/11/2004
*
* Copyright (c) 2004, 2005 jASEN.org
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the distribution.
*
* 3. The names of the authors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* 4. Any modification or additions to the software must be contributed back
* to the project.
*
* 5. Any investigation or reverse engineering of source code or binary to
* enable emails to bypass the filters, and hence inflict spam and or viruses
* onto users who use or do not use jASEN could subject the perpetrator to
* criminal and or civil liability.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JASEN.ORG,
* OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
package org.jasen.net;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.Security;
import java.util.HashMap;
import org.jasen.error.ErrorHandlerBroker;
import org.jasen.interfaces.InetAddressResolver;
/**
* <P>
* Simple implementation of an InetAddress resolver. This class uses a VERY rudamentary caching system
* </P>
* <p>
* This is the default InetAddress resolver, however if full caching etc is required, it is recommended that
* an alternate resolver be used
* </p>
* @author Jason Polites
*/
public class JasenInetAddressResolver implements InetAddressResolver
{
private HashMap inetAddressCache;
private long ttl = 86400; // 1 day
private int maxCache = 10000;
/**
*
*/
public JasenInetAddressResolver() {
super ();
// attempt to cache inetaddresses for 24 hours...
try
{
// TODO This doesn't seem to make any difference
// I think we need to add an additional security policy file
// maybe find a better way?
Security.setProperty("networkaddress.cache.ttl", "86400");
Security.setProperty("networkaddress.cache.negative.ttl", "86400");
}
catch (SecurityException e)
{
// Just leave the caching as it is...
ErrorHandlerBroker.getInstance().getErrorHandler().handleException(e);
}
}
/* (non-Javadoc)
* @see org.jasen.interfaces.InetAddressResolver#getByName(java.lang.String)
*/
public InetAddress getByName(String host) throws UnknownHostException {
CachedInetAddress cached = null;
InetAddress addr = null;
if(inetAddressCache == null) {
inetAddressCache = new HashMap();
}
// Try the cache first...
cached = (CachedInetAddress)inetAddressCache.get(host);
long time = System.currentTimeMillis() / 60L;
if(cached != null) {
// Have we expired?
if(time - cached.getCachedAt() > cached.getTtl() && cached.getTtl() > 0) {
inetAddressCache.remove(host);
// recurse
return getByName(host);
}
else
{
return cached.getInetAddress();
}
}
else
{
// Perform the lookup
if(inetAddressCache.size() <= maxCache) {
cached = new CachedInetAddress();
cached.setCachedAt(time);
cached.setTtl(ttl);
cached.setHostname(host);
}
try
{
addr = InetAddress.getByName(host);
// Cache the hostnames
addr.getHostName();
addr.getAddress();
addr.getHostAddress();
if(cached != null) {
cached.setInetAddress(addr);
}
}
catch (UnknownHostException e) {
if(cached != null) {
cached.unknown = true;
}
throw e;
}
finally {
inetAddressCache.put(host, cached);
}
return addr;
}
}
/* (non-Javadoc)
* @see org.jasen.interfaces.InetAddressResolver#getAllByName(java.lang.String)
*/
public InetAddress[] getAllByName(String host) throws UnknownHostException {
return InetAddress.getAllByName(host);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?