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