+++ /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.client;\r
-\r
-import java.net.URI;\r
-import java.net.URISyntaxException;\r
-import java.util.Random;\r
-\r
-import org.onap.aaf.cadi.Locator;\r
-import org.onap.aaf.cadi.LocatorException;\r
-\r
-public class PropertyLocator implements Locator {\r
- private final URI [] orig;\r
- private PLItem[] current;\r
- private int end;\r
- private final Random random;\r
- \r
- /**\r
- * comma delimited root url list\r
- * \r
- * @param locList\r
- * @throws LocatorException\r
- */\r
- public PropertyLocator(String locList) throws LocatorException {\r
- if(locList==null)throw new LocatorException("No Location List given for PropertyLocator");\r
- String[] locarray = locList.split("\\s*,\\s*");\r
- orig = new URI[locarray.length];\r
- \r
- random = new Random();\r
- \r
- for(int i=0;i<locarray.length;++i) {\r
- try {\r
- orig[i] = new URI(locarray[i]);\r
- } catch (URISyntaxException e) {\r
- throw new LocatorException(e);\r
- }\r
- }\r
-\r
- current = new PLItem[orig.length];\r
- refresh();\r
- }\r
-\r
- @Override\r
- public URI get(Item item) throws LocatorException {\r
- return orig[((PLItem)item).idx];\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
- int spot;\r
- if((spot=(((PLItem)item).order+1))>=end)return null;\r
- return current[spot];\r
- }\r
-\r
- @Override\r
- public synchronized void invalidate(Item item) throws LocatorException {\r
- if(--end<=0)return;\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
- 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())%end];\r
- }\r
- }\r
-\r
- @Override\r
- public synchronized boolean refresh() {\r
- end = orig.length;\r
- \r
- // Build up list\r
- for(int i = 0; i < end ; ++i) {\r
- if(current[i]==null)current[i]=new PLItem(i);\r
- else current[i].idx=current[i].order=i;\r
- }\r
- return true;\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
- @Override\r
- public void destroy() {\r
- // TODO Auto-generated method stub\r
- \r
- }\r
-\r
-}\r