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