本文整理汇总了Java中sun.net.spi.nameservice.NameService类的典型用法代码示例。如果您正苦于以下问题:Java NameService类的具体用法?Java NameService怎么用?Java NameService使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
NameService类属于sun.net.spi.nameservice包,在下文中一共展示了NameService类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: install
import sun.net.spi.nameservice.NameService; //导入依赖的package包/类
/**
* Install the fake DNS resolver into the Java runtime.
*
* <p>
* In Java, name services are chained. So if one name service doesn't
* respond (or responds with {@code UnknownHostException}) then the next
* name service in the chain will be queried. If you want to do negative
* tests, i.e. "this should give me UnknownHostException", then you'll have
* to remove other name services too, so that only the FakeDns name service
* is installed. You do this by setting {@code removeOthers} to {@code true}.
*
* @param removeOthers if other name services should be removed
*/
public synchronized void install(boolean removeOthers) {
if (installed) {
return;
}
List<NameService> nameServices = getKnownNameServices();
if (nameServices != null) {
if (removeOthers) {
orgNameServices.clear();
orgNameServices.addAll(nameServices);
nameServices.clear();
}
// Install ourselves in position 0, ahead of everyone else
nameServices.add(0, new NameServiceInMemory());
installed = true;
}
}
示例2: unInstall
import sun.net.spi.nameservice.NameService; //导入依赖的package包/类
/**
* Puts Java's internal name service chain back to its original state.
* Clears any forward or reverse mappings that has been put into the
* FakeDns.
*/
public synchronized void unInstall() {
if (!installed) {
return;
}
List<NameService> nameServices = getKnownNameServices();
if (nameServices != null) {
NameService ns = nameServices.get(0);
if (ns != null) {
if (ns instanceof NameServiceInMemory) {
nameServices.remove(0);
installed = false;
}
}
if (!orgNameServices.isEmpty()) {
nameServices.addAll(orgNameServices);
orgNameServices.clear();
}
}
forwardResolutions.clear();
reverseResolutions.clear();
}
示例3: spyOnNameService
import sun.net.spi.nameservice.NameService; //导入依赖的package包/类
/**
* Spy on the Java DNS infrastructure.
* This likely only works on Sun-derived JDKs, but uses JUnit's
* Assume functionality so that any tests using it are skipped on
* incompatible JDKs.
*/
private NameService spyOnNameService() {
try {
Field f = InetAddress.class.getDeclaredField("nameServices");
f.setAccessible(true);
Assume.assumeNotNull(f);
@SuppressWarnings("unchecked")
List<NameService> nsList = (List<NameService>) f.get(null);
NameService ns = nsList.get(0);
Log log = LogFactory.getLog("NameServiceSpy");
ns = Mockito.mock(NameService.class,
new GenericTestUtils.DelegateAnswer(log, ns));
nsList.set(0, ns);
return ns;
} catch (Throwable t) {
LOG.info("Unable to spy on DNS. Skipping test.", t);
// In case the JDK we're testing on doesn't work like Sun's, just
// skip the test.
Assume.assumeNoException(t);
throw new RuntimeException(t);
}
}
示例4: testFileContextDoesntDnsResolveLogicalURI
import sun.net.spi.nameservice.NameService; //导入依赖的package包/类
/**
* Same test as above, but for FileContext.
*/
@Test
public void testFileContextDoesntDnsResolveLogicalURI() throws Exception {
FileSystem fs = HATestUtil.configureFailoverFs(cluster, conf);
NameService spyNS = spyOnNameService();
String logicalHost = fs.getUri().getHost();
Configuration haClientConf = fs.getConf();
FileContext fc = FileContext.getFileContext(haClientConf);
Path root = new Path("/");
fc.listStatus(root);
fc.listStatus(fc.makeQualified(root));
fc.getDefaultFileSystem().getCanonicalServiceName();
// Ensure that the logical hostname was never resolved.
Mockito.verify(spyNS, Mockito.never()).lookupAllHostAddr(Mockito.eq(logicalHost));
}
示例5: createNameService
import sun.net.spi.nameservice.NameService; //导入依赖的package包/类
@Override
public NameService createNameService() throws Exception {
NameService ns = new NameService() {
@Override
public InetAddress[] lookupAllHostAddr(String host)
throws UnknownHostException {
// Everything is localhost except NOT_EXISTING_HOST
if (NOT_EXISTING_HOST.equals(host)) {
throw new UnknownHostException("Unknown host name: "
+ NOT_EXISTING_HOST);
}
return new InetAddress[]{
InetAddress.getByAddress(host, new byte[]{127,0,0,1})
};
}
@Override
public String getHostByAddr(byte[] addr)
throws UnknownHostException {
// No reverse lookup, PrincipalName use original string
throw new UnknownHostException();
}
};
return ns;
}
示例6: testDoesntDnsResolveLogicalURI
import sun.net.spi.nameservice.NameService; //导入依赖的package包/类
/**
* Test that the client doesn't ever try to DNS-resolve the logical URI.
* Regression test for HADOOP-9150.
*/
@Test
public void testDoesntDnsResolveLogicalURI() throws Exception {
NameService spyNS = spyOnNameService();
FileSystem fs = HATestUtil.configureFailoverFs(cluster, conf);
String logicalHost = fs.getUri().getHost();
Path qualifiedRoot = fs.makeQualified(new Path("/"));
// Make a few calls against the filesystem.
fs.getCanonicalServiceName();
fs.listStatus(qualifiedRoot);
// Ensure that the logical hostname was never resolved.
Mockito.verify(spyNS, Mockito.never()).lookupAllHostAddr(Mockito.eq(logicalHost));
}
示例7: testFileContextDoesntDnsResolveLogicalURI
import sun.net.spi.nameservice.NameService; //导入依赖的package包/类
/**
* Same test as above, but for FileContext.
*/
@Test
public void testFileContextDoesntDnsResolveLogicalURI() throws Exception {
NameService spyNS = spyOnNameService();
FileSystem fs = HATestUtil.configureFailoverFs(cluster, conf);
String logicalHost = fs.getUri().getHost();
Configuration haClientConf = fs.getConf();
FileContext fc = FileContext.getFileContext(haClientConf);
Path root = new Path("/");
fc.listStatus(root);
fc.listStatus(fc.makeQualified(root));
fc.getDefaultFileSystem().getCanonicalServiceName();
// Ensure that the logical hostname was never resolved.
Mockito.verify(spyNS, Mockito.never()).lookupAllHostAddr(Mockito.eq(logicalHost));
}
示例8: createNameService
import sun.net.spi.nameservice.NameService; //导入依赖的package包/类
@Override
public NameService createNameService() throws Exception {
NameService ns = new NameService() {
@Override
public InetAddress[] lookupAllHostAddr(String host)
throws UnknownHostException {
// Everything is localhost
return new InetAddress[]{
InetAddress.getByAddress(host, new byte[]{127,0,0,1})
};
}
@Override
public String getHostByAddr(byte[] addr)
throws UnknownHostException {
// No reverse lookup, PrincipalName use original string
throw new UnknownHostException();
}
};
return ns;
}
示例9: getKnownNameServices
import sun.net.spi.nameservice.NameService; //导入依赖的package包/类
private List<NameService> getKnownNameServices() {
try {
Field field = InetAddress.class.getDeclaredField("nameServices");
field.setAccessible(true);
return (List<NameService>) field.get(null);
} catch (IllegalAccessException | IllegalArgumentException | NoSuchFieldException | SecurityException e) {
//throw Throwables.propagate(e);
}
return null;
}
示例10: testDoesntDnsResolveLogicalURI
import sun.net.spi.nameservice.NameService; //导入依赖的package包/类
/**
* Test that the client doesn't ever try to DNS-resolve the logical URI.
* Regression test for HADOOP-9150.
*/
@Test
public void testDoesntDnsResolveLogicalURI() throws Exception {
FileSystem fs = HATestUtil.configureFailoverFs(cluster, conf);
NameService spyNS = spyOnNameService();
String logicalHost = fs.getUri().getHost();
Path qualifiedRoot = fs.makeQualified(new Path("/"));
// Make a few calls against the filesystem.
fs.getCanonicalServiceName();
fs.listStatus(qualifiedRoot);
// Ensure that the logical hostname was never resolved.
Mockito.verify(spyNS, Mockito.never()).lookupAllHostAddr(Mockito.eq(logicalHost));
}
示例11: createNameService
import sun.net.spi.nameservice.NameService; //导入依赖的package包/类
@Override
public NameService
createNameService()
throws Exception
{
if ( proxy_name_service == null ){
throw( new Exception( "Failed to create proxy name service" ));
}
return( proxy_name_service );
}
示例12: createNameService
import sun.net.spi.nameservice.NameService; //导入依赖的package包/类
/**
* Intended for use by java.net.InetAddress static initializer.
*
* @return a reference to the name service provider that was already created
* as class load time
*/
@Override
public NameService createNameService()
{
LOG.debug("NetlibNameServiceDescriptor.createNameService() called");
return nameService;
}