Mass whitespace changes (Style Warnings)
[aaf/authz.git] / misc / env / src / main / java / org / onap / aaf / misc / env / StoreImpl.java
1 /**\r
2  * ============LICENSE_START====================================================\r
3  * org.onap.aaf\r
4  * ===========================================================================\r
5  * Copyright (c) 2018 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  */\r
21 \r
22 package org.onap.aaf.misc.env;\r
23 \r
24 import java.io.File;\r
25 import java.io.FileInputStream;\r
26 import java.io.IOException;\r
27 import java.lang.reflect.GenericArrayType;\r
28 import java.util.ArrayList;\r
29 import java.util.HashMap;\r
30 import java.util.List;\r
31 import java.util.Map.Entry;\r
32 \r
33 import org.onap.aaf.misc.env.util.Split;\r
34 \r
35 import java.util.Properties;\r
36 \r
37 \r
38 public class StoreImpl implements Store {\r
39     /*\r
40      * The re-adjustment factor for growing the Static State array. \r
41      */\r
42     private static final int growSize = 10;\r
43     \r
44     /*\r
45      * The index reference for Slot assignment.\r
46      */\r
47     private int local;\r
48     \r
49     /*\r
50      * The index reference for StaticSlot assignment. \r
51      */\r
52     private int stat;\r
53     \r
54     /*\r
55      * The name/slot map for local (transaction specific) State.\r
56      */\r
57     private HashMap<String, Slot> localMap;\r
58     \r
59     /*\r
60      * The name/slot map for Static State.\r
61      */\r
62     private HashMap<String, StaticSlot> staticMap;\r
63 \r
64     private Object[] staticState;\r
65     \r
66     public StoreImpl() {\r
67          staticState = new Object[growSize];\r
68          staticMap = new HashMap<>();\r
69          localMap = new HashMap<>();\r
70     }\r
71     \r
72     public StoreImpl(String tag) {\r
73          staticState = new Object[growSize];\r
74          staticMap = new HashMap<>();\r
75          localMap = new HashMap<>();\r
76     }\r
77 \r
78     \r
79     public StoreImpl(String tag, String[] args) {\r
80          staticState = new Object[growSize];\r
81          staticMap = new HashMap<>();\r
82          localMap = new HashMap<>();\r
83 \r
84          if (tag!=null) {\r
85             String tequals = tag + '=';\r
86             for (String arg : args) {\r
87                 if (arg.startsWith(tequals) && !arg.equals(tequals)) { // needs to have something after =\r
88                     Properties props = new Properties();\r
89                     for (String f : Split.split(File.pathSeparatorChar,arg.substring(tequals.length()))) {\r
90                         moreProps(new File(f),props);\r
91                     }\r
92                     for (Entry<Object, Object> es : props.entrySet()) {\r
93                         put(staticSlot(es.getKey().toString()),es.getValue());\r
94                     }\r
95                 }\r
96             }\r
97          }\r
98 \r
99         // Make sure properties on command line override those in Props\r
100         propsFromArgs(tag,args);\r
101     }\r
102     \r
103     public StoreImpl(String tag, Properties props) {\r
104          staticState = new Object[growSize];\r
105          staticMap = new HashMap<>();\r
106          localMap = new HashMap<>();\r
107          \r
108          if (tag!=null) {\r
109              String fname = props.getProperty(tag);\r
110              if (fname!=null) {\r
111                  for (String f : Split.split(File.pathSeparatorChar,fname)) {\r
112                      if (!moreProps(new File(f),props)) {\r
113                         System.err.println("Unable to load Properties from " + f); \r
114                      }\r
115                  }\r
116              }\r
117          }\r
118 \r
119          for (Entry<Object, Object> es : props.entrySet()) {\r
120              put(staticSlot(es.getKey().toString()),es.getValue());\r
121          }\r
122     }\r
123 \r
124     public void propsFromArgs(String tag, String[] args) {\r
125         if (tag!=null) {\r
126             for (String arg : args) {\r
127                 String sarg[] = Split.split('=',arg);\r
128                 if (sarg.length==2) {\r
129                     if (tag.equals(sarg[0])) {\r
130                         for (String fname : Split.split(File.pathSeparatorChar,sarg[1])) {\r
131                             moreProps(new File(fname),null /* no target */);\r
132                         }\r
133                     }\r
134                     put(staticSlot(sarg[0]),sarg[1]);\r
135                 }\r
136             }\r
137         }\r
138     }\r
139 \r
140     private boolean moreProps(File f, Properties target) {\r
141          if (f.exists()) {\r
142              Properties props = new Properties();\r
143              try {\r
144                  FileInputStream fis = new FileInputStream(f);\r
145                  try {\r
146                      props.load(fis);\r
147                      if (target!=null) {\r
148                          target.load(fis);\r
149                      }\r
150                  } finally {\r
151                      fis.close();\r
152                  }\r
153              } catch (IOException e) {\r
154                  System.err.println(e);\r
155              }\r
156              for (Entry<Object, Object> es : props.entrySet()) {\r
157                  put(staticSlot(es.getKey().toString()),es.getValue());\r
158              }\r
159              return true;\r
160          } else {\r
161              return false;\r
162          }\r
163     }\r
164 \r
165     public Object[] newTransState() {\r
166         return new Object[local];\r
167     }\r
168 \r
169     /* (non-Javadoc)\r
170      * @see com.att.env.Store#slot(java.lang.String)\r
171      */\r
172     public synchronized Slot slot(String name) {\r
173         name = name == null ? "" : name.trim();\r
174         Slot slot = localMap.get(name);\r
175         if (slot == null)  {\r
176             slot = new Slot(local++, name);\r
177             localMap.put(name, slot);\r
178         }\r
179         return slot;\r
180     }\r
181     \r
182     \r
183     /* (non-Javadoc)\r
184      * @see com.att.env.Store#existingSlot(java.lang.String)\r
185      */\r
186     public Slot existingSlot(String name) {\r
187         return localMap.get(name);\r
188     }\r
189     \r
190     /* (non-Javadoc)\r
191      * @see com.att.env.Store#existingSlotNames()\r
192      */\r
193     public List<String> existingSlotNames() {\r
194         return new ArrayList<>(localMap.keySet());\r
195     }\r
196 \r
197     /* (non-Javadoc)\r
198      * @see com.att.env.Store#staticSlot(java.lang.String)\r
199      */\r
200     public synchronized StaticSlot staticSlot(String name) {\r
201         name = name == null ? "" : name.trim();\r
202         StaticSlot slot = staticMap.get(name);\r
203         if (slot == null)  {\r
204             if (stat%growSize == 0) {\r
205                 Object[] temp = staticState;\r
206                 staticState = new Object[temp.length+growSize];\r
207                 System.arraycopy(temp, 0, staticState, 0, temp.length);\r
208             }\r
209             slot = new StaticSlot(stat++, name);\r
210             staticMap.put(name, slot);\r
211         }\r
212         return slot;\r
213     }\r
214     \r
215     /* (non-Javadoc)\r
216      * @see com.att.env.Store#put(com.att.env.StaticSlot, java.lang.Object)\r
217      */\r
218     public void put(StaticSlot slot, Object value) {\r
219         staticState[slot.slot] = value;\r
220     }\r
221     \r
222     /* (non-Javadoc)\r
223      * @see com.att.env.Store#get(com.att.env.StaticSlot T defaultObject)\r
224      */\r
225     @SuppressWarnings("unchecked")\r
226     public<T> T get(StaticSlot sslot,T dflt) {\r
227         T t = (T)staticState[sslot.slot];\r
228         return t==null?dflt:t;\r
229     }\r
230 \r
231     @SuppressWarnings("unchecked")\r
232     public <T> T get(StaticSlot sslot) {\r
233         return (T)staticState[sslot.slot];\r
234     }\r
235 \r
236     public List<String> existingStaticSlotNames() {\r
237         return new ArrayList<>(staticMap.keySet());\r
238     }\r
239 }\r
240 \r