1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * Copyright © 2017 Amdocs
\r
7 * * ===========================================================================
\r
8 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
9 * * you may not use this file except in compliance with the License.
\r
10 * * You may obtain a copy of the License at
\r
12 * * http://www.apache.org/licenses/LICENSE-2.0
\r
14 * * Unless required by applicable law or agreed to in writing, software
\r
15 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
16 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 * * See the License for the specific language governing permissions and
\r
18 * * limitations under the License.
\r
19 * * ============LICENSE_END====================================================
\r
21 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
23 ******************************************************************************/
\r
24 package com.att.cadi.locator;
\r
26 import java.io.IOException;
\r
27 import java.net.InetAddress;
\r
28 import java.net.URI;
\r
29 import java.net.URISyntaxException;
\r
31 import com.att.cadi.Access;
\r
32 import com.att.cadi.Locator;
\r
33 import com.att.cadi.LocatorException;
\r
34 import com.att.cadi.Access.Level;
\r
36 public class DNSLocator implements Locator<URI> {
\r
37 private static enum Status {UNTRIED, OK, INVALID, SLOW};
\r
38 private static final int CHECK_TIME = 3000;
\r
40 private String host, protocol;
\r
41 private Access access;
\r
42 private Host[] hosts;
\r
43 private int startPort, endPort;
\r
44 private String suffix;
\r
46 public DNSLocator(Access access, String protocol, String host, String range) {
\r
48 this.protocol = protocol;
\r
49 this.access = access;
\r
50 int dash = range.indexOf('-');
\r
52 startPort = endPort = Integer.parseInt(range);
\r
54 startPort = Integer.parseInt(range.substring(0,dash));
\r
55 endPort = Integer.parseInt(range.substring(dash + 1));
\r
61 public URI get(Item item) throws LocatorException {
\r
62 return hosts[((DLItem)item).cnt].uri;
\r
66 public boolean hasItems() {
\r
67 for(Host h : hosts) {
\r
68 if(h.status==Status.OK) {
\r
76 public void invalidate(Item item) {
\r
77 DLItem di = (DLItem)item;
\r
78 hosts[di.cnt].status = Status.INVALID;
\r
82 public Item best() throws LocatorException {
\r
83 // not a good "best"
\r
84 for(int i=0;i<hosts.length;++i) {
\r
85 switch(hosts[i].status) {
\r
87 return new DLItem(i);
\r
94 if(hosts[i].ia.isReachable(CHECK_TIME)) {
\r
95 hosts[i].status = Status.OK;
\r
96 return new DLItem(i);
\r
98 } catch (IOException e) {
\r
99 throw new LocatorException(e);
\r
106 throw new LocatorException("No Available URIs for " + host);
\r
110 public Item first() throws LocatorException {
\r
111 return new DLItem(0);
\r
115 public Item next(Item item) throws LocatorException {
\r
116 DLItem di = (DLItem)item;
\r
117 if(++di.cnt<hosts.length) {
\r
125 public boolean refresh() {
\r
127 InetAddress[] ias = InetAddress.getAllByName(host);
\r
128 Host[] temp = new Host[ias.length * (1 + endPort - startPort)];
\r
130 for(int j=startPort; j<=endPort; ++j) {
\r
131 for(int i=0;i<ias.length;++i) {
\r
132 temp[++cnt] = new Host(ias[i], j, suffix);
\r
137 } catch (Exception e) {
\r
138 access.log(Level.ERROR, e);
\r
143 private class Host {
\r
145 private InetAddress ia;
\r
146 private Status status;
\r
148 public Host(InetAddress inetAddress, int port, String suffix) throws URISyntaxException {
\r
150 uri = new URI(protocol,null,inetAddress.getHostAddress(),port,suffix,null,null);
\r
151 status = Status.UNTRIED;
\r
155 private class DLItem implements Item {
\r
156 public DLItem(int i) {
\r
163 public void destroy() {}
\r