acd3b4a7209f7519b06c64acfa4480852a6ad831
[aaf/cadi.git] / client / src / main / java / com / att / cadi / client / PropertyLocator.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\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
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \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
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package com.att.cadi.client;\r
24 \r
25 import java.net.URI;\r
26 import java.net.URISyntaxException;\r
27 import java.util.Random;\r
28 \r
29 import com.att.cadi.Locator;\r
30 import com.att.cadi.LocatorException;\r
31 \r
32 public class PropertyLocator implements Locator {\r
33         private final URI [] orig;\r
34         private PLItem[] current;\r
35         private int end;\r
36         private final Random random;\r
37         \r
38         /**\r
39          * comma delimited root url list\r
40          * \r
41          * @param locList\r
42          * @throws LocatorException\r
43          */\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
48                 \r
49                 random = new Random();\r
50                 \r
51                 for(int i=0;i<locarray.length;++i) {\r
52                         try {\r
53                                 orig[i] = new URI(locarray[i]);\r
54                         } catch (URISyntaxException e) {\r
55                                 throw new LocatorException(e);\r
56                         }\r
57                 }\r
58 \r
59                 current = new PLItem[orig.length];\r
60                 refresh();\r
61         }\r
62 \r
63         @Override\r
64         public URI get(Item item) throws LocatorException {\r
65                 return orig[((PLItem)item).idx];\r
66         }\r
67 \r
68         @Override\r
69         public Item first() throws LocatorException {\r
70                 return end>0?current[0]:null;\r
71         }\r
72 \r
73         @Override\r
74         public boolean hasItems() {\r
75                 return end>0;\r
76         }\r
77 \r
78         @Override\r
79         public Item next(Item item) throws LocatorException {\r
80                 int spot;\r
81                 if((spot=(((PLItem)item).order+1))>=end)return null;\r
82                 return current[spot];\r
83         }\r
84 \r
85         @Override\r
86         public synchronized void invalidate(Item item) throws LocatorException {\r
87                 if(--end<=0)return;\r
88                 PLItem pli = (PLItem)item;\r
89                 int i,order;\r
90                 for(i=0;i<end;++i) {\r
91                         if(pli==current[i])break;\r
92                 }\r
93                 order = current[i].order;\r
94                 for(;i<end;++i) {\r
95                         current[i]=current[i+1];\r
96                         current[i].order=order++;\r
97                 }\r
98                 current[end]=pli;\r
99         }\r
100 \r
101         @Override\r
102         public Item best() throws LocatorException {\r
103                 switch(current.length) {\r
104                         case 0:\r
105                                 return null;\r
106                         case 1:\r
107                                 return current[0];\r
108                         default:\r
109                                 return current[Math.abs(random.nextInt())%end];\r
110                 }\r
111         }\r
112 \r
113         @Override\r
114         public synchronized boolean refresh() {\r
115                 end = orig.length;\r
116                 \r
117                 // Build up list\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
121                 }\r
122                 return true;\r
123         }\r
124         \r
125         private class PLItem implements Item {\r
126                 public int idx,order;\r
127                 \r
128                 public PLItem(int i) {\r
129                         idx = order =i;\r
130                 }\r
131                 \r
132                 public String toString() {\r
133                         return "Item: " + idx + " order: " + order;\r
134                 }\r
135         }\r
136 \r
137         @Override\r
138         public void destroy() {\r
139                 // TODO Auto-generated method stub\r
140                 \r
141         }\r
142 \r
143 }\r