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 org.onap.aaf.cadi.client;
\r
25 import java.net.URI;
\r
26 import java.net.URISyntaxException;
\r
27 import java.util.Random;
\r
29 import org.onap.aaf.cadi.Locator;
\r
30 import org.onap.aaf.cadi.LocatorException;
\r
32 public class PropertyLocator implements Locator {
\r
33 private final URI [] orig;
\r
34 private PLItem[] current;
\r
36 private final Random random;
\r
39 * comma delimited root url list
\r
42 * @throws LocatorException
\r
44 public PropertyLocator(String locList) throws LocatorException {
\r
45 if(locList==null)throw new LocatorException("No Location List given for PropertyLocator");
\r
46 String[] locarray = locList.split("\\s*,\\s*");
\r
47 orig = new URI[locarray.length];
\r
49 random = new Random();
\r
51 for(int i=0;i<locarray.length;++i) {
\r
53 orig[i] = new URI(locarray[i]);
\r
54 } catch (URISyntaxException e) {
\r
55 throw new LocatorException(e);
\r
59 current = new PLItem[orig.length];
\r
64 public URI get(Item item) throws LocatorException {
\r
65 return orig[((PLItem)item).idx];
\r
69 public Item first() throws LocatorException {
\r
70 return end>0?current[0]:null;
\r
74 public boolean hasItems() {
\r
79 public Item next(Item item) throws LocatorException {
\r
81 if((spot=(((PLItem)item).order+1))>=end)return null;
\r
82 return current[spot];
\r
86 public synchronized void invalidate(Item item) throws LocatorException {
\r
88 PLItem pli = (PLItem)item;
\r
90 for(i=0;i<end;++i) {
\r
91 if(pli==current[i])break;
\r
93 order = current[i].order;
\r
95 current[i]=current[i+1];
\r
96 current[i].order=order++;
\r
102 public Item best() throws LocatorException {
\r
103 switch(current.length) {
\r
109 return current[Math.abs(random.nextInt())%end];
\r
114 public synchronized boolean refresh() {
\r
118 for(int i = 0; i < end ; ++i) {
\r
119 if(current[i]==null)current[i]=new PLItem(i);
\r
120 else current[i].idx=current[i].order=i;
\r
125 private class PLItem implements Item {
\r
126 public int idx,order;
\r
128 public PLItem(int i) {
\r
132 public String toString() {
\r
133 return "Item: " + idx + " order: " + order;
\r
138 public void destroy() {
\r
139 // TODO Auto-generated method stub
\r