1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * Copyright © 2017 Amdocs
\r
7 * * ===========================================================================
\r
8 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
9 * * you may not use this file except in compliance with the License.
\r
10 * * You may obtain a copy of the License at
\r
12 * * http://www.apache.org/licenses/LICENSE-2.0
\r
14 * * Unless required by applicable law or agreed to in writing, software
\r
15 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
16 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 * * See the License for the specific language governing permissions and
\r
18 * * limitations under the License.
\r
19 * * ============LICENSE_END====================================================
\r
21 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
23 ******************************************************************************/
\r
24 package com.att.cadi.client;
\r
26 import java.net.URI;
\r
27 import java.net.URISyntaxException;
\r
28 import java.util.Random;
\r
30 import com.att.cadi.Locator;
\r
31 import com.att.cadi.LocatorException;
\r
33 public class PropertyLocator implements Locator {
\r
34 private final URI [] orig;
\r
35 private PLItem[] current;
\r
37 private final Random random;
\r
40 * comma delimited root url list
\r
43 * @throws LocatorException
\r
45 public PropertyLocator(String locList) throws LocatorException {
\r
46 if(locList==null)throw new LocatorException("No Location List given for PropertyLocator");
\r
47 String[] locarray = locList.split("\\s*,\\s*");
\r
48 orig = new URI[locarray.length];
\r
50 random = new Random();
\r
52 for(int i=0;i<locarray.length;++i) {
\r
54 orig[i] = new URI(locarray[i]);
\r
55 } catch (URISyntaxException e) {
\r
56 throw new LocatorException(e);
\r
60 current = new PLItem[orig.length];
\r
65 public URI get(Item item) throws LocatorException {
\r
66 return orig[((PLItem)item).idx];
\r
70 public Item first() throws LocatorException {
\r
71 return end>0?current[0]:null;
\r
75 public boolean hasItems() {
\r
80 public Item next(Item item) throws LocatorException {
\r
82 if((spot=(((PLItem)item).order+1))>=end)return null;
\r
83 return current[spot];
\r
87 public synchronized void invalidate(Item item) throws LocatorException {
\r
89 PLItem pli = (PLItem)item;
\r
91 for(i=0;i<end;++i) {
\r
92 if(pli==current[i])break;
\r
94 order = current[i].order;
\r
96 current[i]=current[i+1];
\r
97 current[i].order=order++;
\r
103 public Item best() throws LocatorException {
\r
104 switch(current.length) {
\r
110 return current[Math.abs(random.nextInt())%end];
\r
115 public synchronized boolean refresh() {
\r
119 for(int i = 0; i < end ; ++i) {
\r
120 if(current[i]==null)current[i]=new PLItem(i);
\r
121 else current[i].idx=current[i].order=i;
\r
126 private class PLItem implements Item {
\r
127 public int idx,order;
\r
129 public PLItem(int i) {
\r
133 public String toString() {
\r
134 return "Item: " + idx + " order: " + order;
\r
139 public void destroy() {
\r
140 // TODO Auto-generated method stub
\r