1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package com.att.cadi.dnsloc;
\r
25 import java.io.IOException;
\r
26 import java.net.InetAddress;
\r
27 import java.net.URI;
\r
28 import java.net.URISyntaxException;
\r
30 import com.att.cadi.Access;
\r
31 import com.att.cadi.Access.Level;
\r
32 import com.att.cadi.Locator;
\r
33 import com.att.cadi.LocatorException;
\r
35 public class DNSLocator implements Locator {
\r
36 private static enum Status {UNTRIED, OK, INVALID, SLOW};
\r
37 private static final int CHECK_TIME = 3000;
\r
39 private String host, protocol;
\r
40 private Access access;
\r
41 private Host[] hosts;
\r
42 private int startPort, endPort;
\r
43 private String suffix;
\r
45 public DNSLocator(Access access, String protocol, String host, String range) {
\r
47 this.protocol = protocol;
\r
48 this.access = access;
\r
49 int dash = range.indexOf('-');
\r
51 startPort = endPort = Integer.parseInt(range);
\r
53 startPort = Integer.parseInt(range.substring(0,dash));
\r
54 endPort = Integer.parseInt(range.substring(dash + 1));
\r
60 public URI get(Item item) throws LocatorException {
\r
61 return hosts[((DLItem)item).cnt].uri;
\r
65 public boolean hasItems() {
\r
66 for(Host h : hosts) {
\r
67 if(h.status==Status.OK) {
\r
75 public void invalidate(Item item) {
\r
76 DLItem di = (DLItem)item;
\r
77 hosts[di.cnt].status = Status.INVALID;
\r
81 public Item best() throws LocatorException {
\r
82 // not a good "best"
\r
83 for(int i=0;i<hosts.length;++i) {
\r
84 switch(hosts[i].status) {
\r
86 return new DLItem(i);
\r
93 if(hosts[i].ia.isReachable(CHECK_TIME)) {
\r
94 hosts[i].status = Status.OK;
\r
95 return new DLItem(i);
\r
97 } catch (IOException e) {
\r
98 throw new LocatorException(e);
\r
105 throw new LocatorException("No Available URIs for " + host);
\r
109 public Item first() throws LocatorException {
\r
110 return new DLItem(0);
\r
114 public Item next(Item item) throws LocatorException {
\r
115 DLItem di = (DLItem)item;
\r
116 if(++di.cnt<hosts.length) {
\r
124 public boolean refresh() {
\r
126 InetAddress[] ias = InetAddress.getAllByName(host);
\r
127 Host[] temp = new Host[ias.length * (1 + endPort - startPort)];
\r
129 for(int j=startPort; j<=endPort; ++j) {
\r
130 for(int i=0;i<ias.length;++i) {
\r
131 temp[++cnt] = new Host(ias[i], j, suffix);
\r
136 } catch (Exception e) {
\r
137 access.log(Level.ERROR, e);
\r
142 private class Host {
\r
144 private InetAddress ia;
\r
145 private Status status;
\r
147 public Host(InetAddress inetAddress, int port, String suffix) throws URISyntaxException {
\r
149 uri = new URI(protocol,null,inetAddress.getHostAddress(),port,suffix,null,null);
\r
150 status = Status.UNTRIED;
\r
154 private class DLItem implements Item {
\r
155 public DLItem(int i) {
\r
163 public void destroy() {
\r
164 // TODO Auto-generated method stub
\r