+++ /dev/null
-/*******************************************************************************\r
- * ============LICENSE_START====================================================\r
- * * org.onap.aaf\r
- * * ===========================================================================\r
- * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
- * * ===========================================================================\r
- * * Licensed under the Apache License, Version 2.0 (the "License");\r
- * * you may not use this file except in compliance with the License.\r
- * * You may obtain a copy of the License at\r
- * * \r
- * * http://www.apache.org/licenses/LICENSE-2.0\r
- * * \r
- * * Unless required by applicable law or agreed to in writing, software\r
- * * distributed under the License is distributed on an "AS IS" BASIS,\r
- * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * * See the License for the specific language governing permissions and\r
- * * limitations under the License.\r
- * * ============LICENSE_END====================================================\r
- * *\r
- * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
- * *\r
- ******************************************************************************/\r
-package org.onap.aaf.cadi.locator;\r
-\r
-import java.io.IOException;\r
-import java.net.InetAddress;\r
-import java.net.InetSocketAddress;\r
-import java.net.Socket;\r
-import java.net.URI;\r
-import java.net.URISyntaxException;\r
-import java.net.UnknownHostException;\r
-import java.util.ArrayList;\r
-import java.util.List;\r
-import java.security.SecureRandom;\r
-import java.util.Timer;\r
-import java.util.TimerTask;\r
-\r
-import org.onap.aaf.cadi.Locator;\r
-import org.onap.aaf.cadi.LocatorException;\r
-\r
-import org.onap.aaf.inno.env.util.Split;\r
-\r
-public class PropertyLocator implements Locator<URI> {\r
- private final URI [] orig;\r
- private PLItem[] current;\r
- private int end;\r
- private final SecureRandom random;\r
- private URI[] resolved;\r
- private long lastRefreshed=0L;\r
- private long minRefresh;\r
- private long backgroundRefresh;\r
-\r
- public PropertyLocator(String locList) throws LocatorException {\r
- this(locList,10000L, 1000*60*20); // defaults, do not refresh more than once in 10 seconds, Refresh Locator every 20 mins.\r
- }\r
- /**\r
- * comma delimited root url list\r
- * \r
- * @param locList\r
- * @throws LocatorException\r
- */\r
- public PropertyLocator(String locList, long minRefreshMillis, long backgroundRefreshMillis) throws LocatorException {\r
- minRefresh = minRefreshMillis;\r
- backgroundRefresh = backgroundRefreshMillis;\r
- if(locList==null) {\r
- throw new LocatorException("No Location List given for PropertyLocator");\r
- }\r
- String[] locarray = Split.split(',',locList);\r
- List<URI> uriList = new ArrayList<URI>();\r
- \r
- random = new SecureRandom();\r
- \r
- for(int i=0;i<locarray.length;++i) {\r
- try {\r
- int range = locarray[i].indexOf(":[");\r
- if(range<0) {\r
- uriList.add(new URI(locarray[i]));\r
- } else {\r
- int dash = locarray[i].indexOf('-',range+2);\r
- int brac = locarray[i].indexOf(']',dash+1);\r
- int start = Integer.parseInt(locarray[i].substring(range+2, dash));\r
- int end = Integer.parseInt(locarray[i].substring(dash+1, brac));\r
- for(int port=start;port<=end;++port) {\r
- uriList.add(new URI(locarray[i].substring(0, range+1)+port));\r
- }\r
- }\r
- } catch (NumberFormatException nf) {\r
- throw new LocatorException("Invalid URI format: " + locarray[i]);\r
- } catch (URISyntaxException e) {\r
- throw new LocatorException(e);\r
- }\r
- }\r
- orig = new URI[uriList.size()];\r
- uriList.toArray(orig);\r
-\r
- refresh();\r
- new Timer("PropertyLocator Refresh Timer",true).scheduleAtFixedRate(new TimerTask() {\r
- @Override\r
- public void run() {\r
- refresh();\r
- }\r
- }, backgroundRefresh,backgroundRefresh);\r
-\r
- }\r
-\r
- @Override\r
- public URI get(Item item) throws LocatorException {\r
- synchronized(orig) {\r
- if(item==null) {\r
- return null;\r
- } else {\r
- return resolved[((PLItem)item).idx];\r
- }\r
- }\r
- }\r
-\r
- @Override\r
- public Item first() throws LocatorException {\r
- return end>0?current[0]:null;\r
- }\r
-\r
- @Override\r
- public boolean hasItems() {\r
- return end>0;\r
- }\r
-\r
- @Override\r
- public Item next(Item item) throws LocatorException {\r
- if(item==null) {\r
- return null;\r
- } else {\r
- int spot;\r
- if((spot=(((PLItem)item).order+1))>=end)return null;\r
- return current[spot];\r
- }\r
- }\r
-\r
- @Override\r
- public synchronized void invalidate(Item item) throws LocatorException {\r
- if(--end<=0) {\r
- refresh();\r
- return;\r
- }\r
- PLItem pli = (PLItem)item;\r
- int i,order;\r
- for(i=0;i<end;++i) {\r
- if(pli==current[i])break;\r
- }\r
- order = current[i].order;\r
- for(;i<end;++i) {\r
- current[i]=current[i+1];\r
- current[i].order=order++;\r
- }\r
- current[end]=pli;\r
- }\r
-\r
- @Override\r
- public Item best() throws LocatorException {\r
- if(current.length==0) {\r
- refresh();\r
- }\r
- switch(current.length) {\r
- case 0:\r
- return null;\r
- case 1:\r
- return current[0];\r
- default:\r
- return current[Math.abs(random.nextInt())%current.length];\r
- }\r
- }\r
-\r
- @Override\r
- public synchronized boolean refresh() {\r
- if(System.currentTimeMillis()>lastRefreshed) {\r
- // Build up list\r
- List<URI> resolve = new ArrayList<URI>();\r
- String realname;\r
- for(int i = 0; i < orig.length ; ++i) {\r
- try {\r
- InetAddress ia[] = InetAddress.getAllByName(orig[i].getHost());\r
-\r
- URI o,n;\r
- for(int j=0;j<ia.length;++j) {\r
- o = orig[i];\r
- Socket socket = new Socket();\r
- try {\r
- realname=ia[j].getCanonicalHostName();\r
- socket.connect(new InetSocketAddress(realname,o.getPort()),3000);\r
- if(socket.isConnected()) {\r
- n = new URI(\r
- o.getScheme(),\r
- o.getUserInfo(),\r
- realname,\r
- o.getPort(),\r
- o.getPath(),\r
- o.getQuery(),\r
- o.getFragment()\r
- );\r
- resolve.add(n);\r
- }\r
- } catch (IOException e) {\r
- } finally {\r
- if(!socket.isClosed()) {\r
- try {\r
- socket.close();\r
- } catch (IOException e) {\r
- // nothing to do.\r
- }\r
- }\r
- }\r
- }\r
- } catch (UnknownHostException | URISyntaxException e) {\r
- // Note: Orig Name already known as valid, based on constructor\r
- }\r
- }\r
- end=resolve.size();\r
- PLItem[] newCurrent;\r
- if(current==null || current.length!=end) {\r
- newCurrent = new PLItem[end];\r
- } else {\r
- newCurrent = current;\r
- }\r
- \r
- for(int i=0; i< end; ++i) {\r
- if(newCurrent[i]==null){\r
- newCurrent[i]=new PLItem(i);\r
- } else {\r
- newCurrent[i].idx=newCurrent[i].order=i;\r
- }\r
- }\r
- synchronized(orig) {\r
- resolved = new URI[end];\r
- resolve.toArray(resolved);\r
- current = newCurrent;\r
- }\r
- lastRefreshed = System.currentTimeMillis()+minRefresh;\r
- return !resolve.isEmpty();\r
- } else {\r
- return false;\r
- }\r
- }\r
- \r
- private class PLItem implements Item {\r
- public int idx,order;\r
- \r
- public PLItem(int i) {\r
- idx = order =i;\r
- }\r
- \r
- public String toString() {\r
- return "Item: " + idx + " order: " + order;\r
- }\r
- }\r
-\r
- public String toString() {\r
- StringBuilder sb = new StringBuilder();\r
- boolean first = true;\r
- for(URI uri : orig) {\r
- boolean isResolved=false;\r
- if(uri!=null) {\r
- if(first) {\r
- first = false;\r
- } else {\r
- sb.append(", ");\r
- }\r
- sb.append(uri.toString());\r
- sb.append(" [");\r
- for(URI u2 : resolved) {\r
- if(uri.equals(u2)) {\r
- isResolved = true;\r
- break;\r
- }\r
- }\r
- sb.append(isResolved?"X]\n":" ]");\r
- }\r
- }\r
- return sb.toString();\r
- }\r
- \r
- public void destroy() {\r
- }\r
-}\r