2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.misc.env;
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;
33 import org.onap.aaf.misc.env.util.Split;
35 import java.util.Properties;
38 public class StoreImpl implements Store {
40 * The re-adjustment factor for growing the Static State array.
42 private static final int growSize = 10;
45 * The index reference for Slot assignment.
50 * The index reference for StaticSlot assignment.
55 * The name/slot map for local (transaction specific) State.
57 private HashMap<String, Slot> localMap;
60 * The name/slot map for Static State.
62 private HashMap<String, StaticSlot> staticMap;
64 private Object[] staticState;
67 staticState = new Object[growSize];
68 staticMap = new HashMap<String,StaticSlot>();
69 localMap = new HashMap<String,Slot>();
72 public StoreImpl(String tag) {
73 staticState = new Object[growSize];
74 staticMap = new HashMap<String,StaticSlot>();
75 localMap = new HashMap<String,Slot>();
79 public StoreImpl(String tag, String[] args) {
80 staticState = new Object[growSize];
81 staticMap = new HashMap<String,StaticSlot>();
82 localMap = new HashMap<String,Slot>();
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);
92 for(Entry<Object, Object> es : props.entrySet()) {
93 put(staticSlot(es.getKey().toString()),es.getValue());
99 // Make sure properties on command line override those in Props
100 propsFromArgs(tag,args);
103 public StoreImpl(String tag, Properties props) {
104 staticState = new Object[growSize];
105 staticMap = new HashMap<String,StaticSlot>();
106 localMap = new HashMap<String,Slot>();
109 String fname = props.getProperty(tag);
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);
119 for(Entry<Object, Object> es : props.entrySet()) {
120 put(staticSlot(es.getKey().toString()),es.getValue());
124 public void propsFromArgs(String tag, String[] args) {
126 for(String arg : args) {
127 String sarg[] = Split.split('=',arg);
129 if(tag.equals(sarg[0])) {
130 for(String fname : Split.split(File.pathSeparatorChar,sarg[1])) {
131 moreProps(new File(fname),null /* no target */);
134 put(staticSlot(sarg[0]),sarg[1]);
140 private boolean moreProps(File f, Properties target) {
142 Properties props = new Properties();
144 FileInputStream fis = new FileInputStream(f);
153 } catch(IOException e) {
154 System.err.println(e);
156 for(Entry<Object, Object> es : props.entrySet()) {
157 put(staticSlot(es.getKey().toString()),es.getValue());
165 public Object[] newTransState() {
166 return new Object[local];
170 * @see com.att.env.Store#slot(java.lang.String)
172 public synchronized Slot slot(String name) {
173 name = name == null ? "" : name.trim();
174 Slot slot = localMap.get(name);
176 slot = new Slot(local++, name);
177 localMap.put(name, slot);
184 * @see com.att.env.Store#existingSlot(java.lang.String)
186 public Slot existingSlot(String name) {
187 return localMap.get(name);
191 * @see com.att.env.Store#existingSlotNames()
193 public List<String> existingSlotNames() {
194 return new ArrayList<String>(localMap.keySet());
198 * @see com.att.env.Store#staticSlot(java.lang.String)
200 public synchronized StaticSlot staticSlot(String name) {
201 name = name == null ? "" : name.trim();
202 StaticSlot slot = staticMap.get(name);
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);
209 slot = new StaticSlot(stat++, name);
210 staticMap.put(name, slot);
216 * @see com.att.env.Store#put(com.att.env.StaticSlot, java.lang.Object)
218 public void put(StaticSlot slot, Object value) {
219 staticState[slot.slot] = value;
223 * @see com.att.env.Store#get(com.att.env.StaticSlot T defaultObject)
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;
231 @SuppressWarnings("unchecked")
232 public <T> T get(StaticSlot sslot) {
233 return (T)staticState[sslot.slot];
236 public List<String> existingStaticSlotNames() {
237 return new ArrayList<String>(staticMap.keySet());