1 /*
 2  * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.
 8  *
 9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 package impl;
25 
26 import java.net.InetAddress;
27 import java.net.UnknownHostException;
28 import java.net.spi.InetAddressResolver;
29 import java.net.spi.InetAddressResolver.LookupPolicy;
30 import java.net.spi.InetAddressResolverProvider;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.List;
34 import java.util.logging.Logger;
35 import java.util.stream.Stream;
36 
37 import testlib.ResolutionRegistry;
38 
39 public class SimpleResolverProviderImpl extends InetAddressResolverProvider {
40 
41     public static ResolutionRegistry registry = new ResolutionRegistry();
42     private static List<LookupPolicy> LOOKUP_HISTORY = Collections.synchronizedList(new ArrayList<>());
43     private static volatile long LAST_LOOKUP_TIMESTAMP;
44     private static Logger LOGGER = Logger.getLogger(SimpleResolverProviderImpl.class.getName());
45 
46     @Override
47     public InetAddressResolver get(Configuration configuration) {
48         System.out.println("The following provider will be used by current test:" + this.getClass().getCanonicalName());
49         return new InetAddressResolver() {
50             @Override
51             public Stream<InetAddress> lookupByName(String host, LookupPolicy lookupPolicy) throws UnknownHostException {
52                 LOGGER.info("Looking-up addresses for '" + host + "'. Lookup characteristics:" +
53                         Integer.toString(lookupPolicy.characteristics(), 2));
54                 LOOKUP_HISTORY.add(lookupPolicy);
55                 LAST_LOOKUP_TIMESTAMP = System.nanoTime();
56                 return registry.lookupHost(host, lookupPolicy);
57             }
58 
59             @Override
60             public String lookupByAddress(byte[] addr) throws UnknownHostException {
61                 LOGGER.info("Looking host name for the following address:" + ResolutionRegistry.addressBytesToString(addr));
62                 return registry.lookupAddress(addr);
63             }
64         };
65     }
66 
67     // Utility methods
68     public static LookupPolicy lastLookupPolicy() {
69         return lookupPolicyHistory(0);
70     }
71 
72     public static long getLastLookupTimestamp() {
73         return LAST_LOOKUP_TIMESTAMP;
74     }
75 
76     public static LookupPolicy lookupPolicyHistory(int position) {
77         if (LOOKUP_HISTORY.isEmpty()) {
78             throw new RuntimeException("No registered lookup policies");
79         }
80         if (position >= LOOKUP_HISTORY.size()) {
81             throw new IllegalArgumentException("No element available with provided position");
82         }
83         return LOOKUP_HISTORY.get(LOOKUP_HISTORY.size() - position - 1);
84     }
85 
86 
87     @Override
88     public String name() {
89         return "simpleInetAddressResolver";
90     }
91 }