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