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.locator;
\r
25 import java.io.IOException;
\r
26 import java.net.InetAddress;
\r
27 import java.net.InetSocketAddress;
\r
28 import java.net.Socket;
\r
29 import java.net.URI;
\r
30 import java.net.URISyntaxException;
\r
31 import java.net.UnknownHostException;
\r
32 import java.util.ArrayList;
\r
33 import java.util.List;
\r
34 import java.security.SecureRandom;
\r
35 import java.util.Timer;
\r
36 import java.util.TimerTask;
\r
38 import com.att.cadi.Locator;
\r
39 import com.att.cadi.LocatorException;
\r
40 import com.att.inno.env.util.Split;
\r
42 public class PropertyLocator implements Locator<URI> {
\r
43 private final URI [] orig;
\r
44 private PLItem[] current;
\r
46 private final SecureRandom random;
\r
47 private URI[] resolved;
\r
48 private long lastRefreshed=0L;
\r
49 private long minRefresh;
\r
50 private long backgroundRefresh;
\r
52 public PropertyLocator(String locList) throws LocatorException {
\r
53 this(locList,10000L, 1000*60*20); // defaults, do not refresh more than once in 10 seconds, Refresh Locator every 20 mins.
\r
56 * comma delimited root url list
\r
59 * @throws LocatorException
\r
61 public PropertyLocator(String locList, long minRefreshMillis, long backgroundRefreshMillis) throws LocatorException {
\r
62 minRefresh = minRefreshMillis;
\r
63 backgroundRefresh = backgroundRefreshMillis;
\r
65 throw new LocatorException("No Location List given for PropertyLocator");
\r
67 String[] locarray = Split.split(',',locList);
\r
68 List<URI> uriList = new ArrayList<URI>();
\r
70 random = new SecureRandom();
\r
72 for(int i=0;i<locarray.length;++i) {
\r
74 int range = locarray[i].indexOf(":[");
\r
76 uriList.add(new URI(locarray[i]));
\r
78 int dash = locarray[i].indexOf('-',range+2);
\r
79 int brac = locarray[i].indexOf(']',dash+1);
\r
80 int start = Integer.parseInt(locarray[i].substring(range+2, dash));
\r
81 int end = Integer.parseInt(locarray[i].substring(dash+1, brac));
\r
82 for(int port=start;port<=end;++port) {
\r
83 uriList.add(new URI(locarray[i].substring(0, range+1)+port));
\r
86 } catch (NumberFormatException nf) {
\r
87 throw new LocatorException("Invalid URI format: " + locarray[i]);
\r
88 } catch (URISyntaxException e) {
\r
89 throw new LocatorException(e);
\r
92 orig = new URI[uriList.size()];
\r
93 uriList.toArray(orig);
\r
96 new Timer("PropertyLocator Refresh Timer",true).scheduleAtFixedRate(new TimerTask() {
\r
101 }, backgroundRefresh,backgroundRefresh);
\r
106 public URI get(Item item) throws LocatorException {
\r
107 synchronized(orig) {
\r
111 return resolved[((PLItem)item).idx];
\r
117 public Item first() throws LocatorException {
\r
118 return end>0?current[0]:null;
\r
122 public boolean hasItems() {
\r
127 public Item next(Item item) throws LocatorException {
\r
132 if((spot=(((PLItem)item).order+1))>=end)return null;
\r
133 return current[spot];
\r
138 public synchronized void invalidate(Item item) throws LocatorException {
\r
143 PLItem pli = (PLItem)item;
\r
145 for(i=0;i<end;++i) {
\r
146 if(pli==current[i])break;
\r
148 order = current[i].order;
\r
150 current[i]=current[i+1];
\r
151 current[i].order=order++;
\r
157 public Item best() throws LocatorException {
\r
158 if(current.length==0) {
\r
161 switch(current.length) {
\r
167 return current[Math.abs(random.nextInt())%current.length];
\r
172 public synchronized boolean refresh() {
\r
173 if(System.currentTimeMillis()>lastRefreshed) {
\r
175 List<URI> resolve = new ArrayList<URI>();
\r
177 for(int i = 0; i < orig.length ; ++i) {
\r
179 InetAddress ia[] = InetAddress.getAllByName(orig[i].getHost());
\r
182 for(int j=0;j<ia.length;++j) {
\r
184 Socket socket = new Socket();
\r
186 realname=ia[j].getCanonicalHostName();
\r
187 socket.connect(new InetSocketAddress(realname,o.getPort()),3000);
\r
188 if(socket.isConnected()) {
\r
200 } catch (IOException e) {
\r
202 if(!socket.isClosed()) {
\r
205 } catch (IOException e) {
\r
211 } catch (UnknownHostException | URISyntaxException e) {
\r
212 // Note: Orig Name already known as valid, based on constructor
\r
215 end=resolve.size();
\r
216 PLItem[] newCurrent;
\r
217 if(current==null || current.length!=end) {
\r
218 newCurrent = new PLItem[end];
\r
220 newCurrent = current;
\r
223 for(int i=0; i< end; ++i) {
\r
224 if(newCurrent[i]==null){
\r
225 newCurrent[i]=new PLItem(i);
\r
227 newCurrent[i].idx=newCurrent[i].order=i;
\r
230 synchronized(orig) {
\r
231 resolved = new URI[end];
\r
232 resolve.toArray(resolved);
\r
233 current = newCurrent;
\r
235 lastRefreshed = System.currentTimeMillis()+minRefresh;
\r
236 return !resolve.isEmpty();
\r
242 private class PLItem implements Item {
\r
243 public int idx,order;
\r
245 public PLItem(int i) {
\r
249 public String toString() {
\r
250 return "Item: " + idx + " order: " + order;
\r
254 public String toString() {
\r
255 StringBuilder sb = new StringBuilder();
\r
256 boolean first = true;
\r
257 for(URI uri : orig) {
\r
258 boolean isResolved=false;
\r
265 sb.append(uri.toString());
\r
267 for(URI u2 : resolved) {
\r
268 if(uri.equals(u2)) {
\r
273 sb.append(isResolved?"X]\n":" ]");
\r
276 return sb.toString();
\r
279 public void destroy() {
\r