Increase coverage for Env module
[aaf/authz.git] / misc / env / src / main / java / org / onap / aaf / misc / env / StoreImpl.java
index 90fb1f2..54b0ce8 100644 (file)
-/**
- * ============LICENSE_START====================================================
- * org.onap.aaf
- * ===========================================================================
- * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
- * ===========================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * 
- *      http://www.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END====================================================
- *
- */
-
-package org.onap.aaf.misc.env;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.lang.reflect.GenericArrayType;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map.Entry;
-
-import org.onap.aaf.misc.env.util.Split;
-
-import java.util.Properties;
-
-
-public class StoreImpl implements Store {
-       /*
-        * The re-adjustment factor for growing the Static State array. 
-        */
-       private static final int growSize = 10;
-       
-       /*
-        * The index reference for Slot assignment.
-        */
-       private int local;
-       
-       /*
-        * The index reference for StaticSlot assignment. 
-        */
-       private int stat;
-       
-       /*
-        * The name/slot map for local (transaction specific) State.
-        */
-       private HashMap<String, Slot> localMap;
-       
-       /*
-        * The name/slot map for Static State.
-        */
-       private HashMap<String, StaticSlot> staticMap;
-
-       private Object[] staticState;
-       
-       public StoreImpl() {
-                staticState = new Object[growSize];
-                staticMap = new HashMap<String,StaticSlot>();
-                localMap = new HashMap<String,Slot>();
-       }
-       
-       public StoreImpl(String tag) {
-                staticState = new Object[growSize];
-                staticMap = new HashMap<String,StaticSlot>();
-                localMap = new HashMap<String,Slot>();
-       }
-
-       
-       public StoreImpl(String tag, String[] args) {
-                staticState = new Object[growSize];
-                staticMap = new HashMap<String,StaticSlot>();
-                localMap = new HashMap<String,Slot>();
-
-                if(tag!=null) {
-                       String tequals = tag + '=';
-                       for(String arg : args) {
-                               if(arg.startsWith(tequals) && !arg.equals(tequals)) { // needs to have something after =
-                                       Properties props = new Properties();
-                                       for(String f : Split.split(File.pathSeparatorChar,arg.substring(tequals.length()))) {
-                                               moreProps(new File(f),props);
-                                       }
-                                       for(Entry<Object, Object> es : props.entrySet()) {
-                                               put(staticSlot(es.getKey().toString()),es.getValue());
-                                       }
-                               }
-                       }
-                }
-
-               // Make sure properties on command line override those in Props
-               propsFromArgs(tag,args);
-       }
-       
-       public StoreImpl(String tag, Properties props) {
-                staticState = new Object[growSize];
-                staticMap = new HashMap<String,StaticSlot>();
-                localMap = new HashMap<String,Slot>();
-                
-                if(tag!=null) {
-                        String fname = props.getProperty(tag);
-                        if(fname!=null) {
-                                for(String f : Split.split(File.pathSeparatorChar,fname)) {
-                                        if(!moreProps(new File(f),props)) {
-                                               System.err.println("Unable to load Properties from " + f); 
-                                        }
-                                }
-                        }
-                }
-
-                for(Entry<Object, Object> es : props.entrySet()) {
-                        put(staticSlot(es.getKey().toString()),es.getValue());
-                }
-       }
-
-       public void propsFromArgs(String tag, String[] args) {
-               if(tag!=null) {
-                       for(String arg : args) {
-                               String sarg[] = Split.split('=',arg);
-                               if(sarg.length==2) {
-                                       if(tag.equals(sarg[0])) {
-                                               for(String fname : Split.split(File.pathSeparatorChar,sarg[1])) {
-                                                       moreProps(new File(fname),null /* no target */);
-                                               }
-                                       }
-                                       put(staticSlot(sarg[0]),sarg[1]);
-                               }
-                       }
-               }
-       }
-
-       private boolean moreProps(File f, Properties target) {
-                if(f.exists()) {
-                        Properties props = new Properties();
-                        try {
-                                FileInputStream fis = new FileInputStream(f);
-                                try {
-                                        props.load(fis);
-                                        if(target!=null) {
-                                                target.load(fis);
-                                        }
-                                } finally {
-                                        fis.close();
-                                }
-                        } catch(IOException e) {
-                                System.err.println(e);
-                        }
-                        for(Entry<Object, Object> es : props.entrySet()) {
-                                put(staticSlot(es.getKey().toString()),es.getValue());
-                        }
-                        return true;
-                } else {
-                        return false;
-                }
-       }
-
-       public Object[] newTransState() {
-               return new Object[local];
-       }
-
-       /* (non-Javadoc)
-        * @see com.att.env.Store#slot(java.lang.String)
-        */
-       public synchronized Slot slot(String name) {
-               name = name == null ? "" : name.trim();
-               Slot slot = localMap.get(name);
-               if (slot == null)  {
-                       slot = new Slot(local++, name);
-                       localMap.put(name, slot);
-               }
-               return slot;
-       }
-       
-       
-       /* (non-Javadoc)
-        * @see com.att.env.Store#existingSlot(java.lang.String)
-        */
-       public Slot existingSlot(String name) {
-               return localMap.get(name);
-       }
-       
-       /* (non-Javadoc)
-        * @see com.att.env.Store#existingSlotNames()
-        */
-       public List<String> existingSlotNames() {
-               return new ArrayList<String>(localMap.keySet());
-       }
-
-       /* (non-Javadoc)
-        * @see com.att.env.Store#staticSlot(java.lang.String)
-        */
-       public synchronized StaticSlot staticSlot(String name) {
-               name = name == null ? "" : name.trim();
-               StaticSlot slot = staticMap.get(name);
-               if (slot == null)  {
-                       if (stat%growSize == 0) {
-                               Object[] temp = staticState;
-                               staticState = new Object[temp.length+growSize];
-                               System.arraycopy(temp, 0, staticState, 0, temp.length);
-                       }
-                       slot = new StaticSlot(stat++, name);
-                       staticMap.put(name, slot);
-               }
-               return slot;
-       }
-       
-       /* (non-Javadoc)
-        * @see com.att.env.Store#put(com.att.env.StaticSlot, java.lang.Object)
-        */
-       public void put(StaticSlot slot, Object value) {
-               staticState[slot.slot] = value;
-       }
-       
-       /* (non-Javadoc)
-        * @see com.att.env.Store#get(com.att.env.StaticSlot T defaultObject)
-        */
-       @SuppressWarnings("unchecked")
-       public<T> T get(StaticSlot sslot,T dflt) {
-               T t = (T)staticState[sslot.slot];
-               return t==null?dflt:t;
-       }
-
-       @SuppressWarnings("unchecked")
-       public <T> T get(StaticSlot sslot) {
-               return (T)staticState[sslot.slot];
-       }
-
-       public List<String> existingStaticSlotNames() {
-               return new ArrayList<String>(staticMap.keySet());
-       }
-}
-
+/**\r
+ * ============LICENSE_START====================================================\r
+ * org.onap.aaf\r
+ * ===========================================================================\r
+ * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.\r
+ * ===========================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ * \r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ * \r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ============LICENSE_END====================================================\r
+ *\r
+ */\r
+\r
+package org.onap.aaf.misc.env;\r
+\r
+import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.io.IOException;\r
+import java.lang.reflect.GenericArrayType;\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+import java.util.Map.Entry;\r
+\r
+import org.onap.aaf.misc.env.util.Split;\r
+\r
+import java.util.Properties;\r
+\r
+\r
+public class StoreImpl implements Store {\r
+       /*\r
+        * The re-adjustment factor for growing the Static State array. \r
+        */\r
+       private static final int growSize = 10;\r
+       \r
+       /*\r
+        * The index reference for Slot assignment.\r
+        */\r
+       private int local;\r
+       \r
+       /*\r
+        * The index reference for StaticSlot assignment. \r
+        */\r
+       private int stat;\r
+       \r
+       /*\r
+        * The name/slot map for local (transaction specific) State.\r
+        */\r
+       private HashMap<String, Slot> localMap;\r
+       \r
+       /*\r
+        * The name/slot map for Static State.\r
+        */\r
+       private HashMap<String, StaticSlot> staticMap;\r
+\r
+       private Object[] staticState;\r
+       \r
+       public StoreImpl() {\r
+                staticState = new Object[growSize];\r
+                staticMap = new HashMap<String,StaticSlot>();\r
+                localMap = new HashMap<String,Slot>();\r
+       }\r
+       \r
+       public StoreImpl(String tag) {\r
+                staticState = new Object[growSize];\r
+                staticMap = new HashMap<String,StaticSlot>();\r
+                localMap = new HashMap<String,Slot>();\r
+       }\r
+\r
+       \r
+       public StoreImpl(String tag, String[] args) {\r
+                staticState = new Object[growSize];\r
+                staticMap = new HashMap<String,StaticSlot>();\r
+                localMap = new HashMap<String,Slot>();\r
+\r
+                if(tag!=null) {\r
+                       String tequals = tag + '=';\r
+                       for(String arg : args) {\r
+                               if(arg.startsWith(tequals) && !arg.equals(tequals)) { // needs to have something after =\r
+                                       Properties props = new Properties();\r
+                                       for(String f : Split.split(File.pathSeparatorChar,arg.substring(tequals.length()))) {\r
+                                               moreProps(new File(f),props);\r
+                                       }\r
+                                       for(Entry<Object, Object> es : props.entrySet()) {\r
+                                               put(staticSlot(es.getKey().toString()),es.getValue());\r
+                                       }\r
+                               }\r
+                       }\r
+                }\r
+\r
+               // Make sure properties on command line override those in Props\r
+               propsFromArgs(tag,args);\r
+       }\r
+       \r
+       public StoreImpl(String tag, Properties props) {\r
+                staticState = new Object[growSize];\r
+                staticMap = new HashMap<String,StaticSlot>();\r
+                localMap = new HashMap<String,Slot>();\r
+                \r
+                if(tag!=null) {\r
+                        String fname = props.getProperty(tag);\r
+                        if(fname!=null) {\r
+                                for(String f : Split.split(File.pathSeparatorChar,fname)) {\r
+                                        if(!moreProps(new File(f),props)) {\r
+                                               System.err.println("Unable to load Properties from " + f); \r
+                                        }\r
+                                }\r
+                        }\r
+                }\r
+\r
+                for(Entry<Object, Object> es : props.entrySet()) {\r
+                        put(staticSlot(es.getKey().toString()),es.getValue());\r
+                }\r
+       }\r
+\r
+       public void propsFromArgs(String tag, String[] args) {\r
+               if(tag!=null) {\r
+                       for(String arg : args) {\r
+                               String sarg[] = Split.split('=',arg);\r
+                               if(sarg.length==2) {\r
+                                       if(tag.equals(sarg[0])) {\r
+                                               for(String fname : Split.split(File.pathSeparatorChar,sarg[1])) {\r
+                                                       moreProps(new File(fname),null /* no target */);\r
+                                               }\r
+                                       }\r
+                                       put(staticSlot(sarg[0]),sarg[1]);\r
+                               }\r
+                       }\r
+               }\r
+       }\r
+\r
+       private boolean moreProps(File f, Properties target) {\r
+                if(f.exists()) {\r
+                        Properties props = new Properties();\r
+                        try {\r
+                                FileInputStream fis = new FileInputStream(f);\r
+                                try {\r
+                                        props.load(fis);\r
+                                        if(target!=null) {\r
+                                                target.load(fis);\r
+                                        }\r
+                                } finally {\r
+                                        fis.close();\r
+                                }\r
+                        } catch(IOException e) {\r
+                                System.err.println(e);\r
+                        }\r
+                        for(Entry<Object, Object> es : props.entrySet()) {\r
+                                put(staticSlot(es.getKey().toString()),es.getValue());\r
+                        }\r
+                        return true;\r
+                } else {\r
+                        return false;\r
+                }\r
+       }\r
+\r
+       public Object[] newTransState() {\r
+               return new Object[local];\r
+       }\r
+\r
+       /* (non-Javadoc)\r
+        * @see com.att.env.Store#slot(java.lang.String)\r
+        */\r
+       public synchronized Slot slot(String name) {\r
+               name = name == null ? "" : name.trim();\r
+               Slot slot = localMap.get(name);\r
+               if (slot == null)  {\r
+                       slot = new Slot(local++, name);\r
+                       localMap.put(name, slot);\r
+               }\r
+               return slot;\r
+       }\r
+       \r
+       \r
+       /* (non-Javadoc)\r
+        * @see com.att.env.Store#existingSlot(java.lang.String)\r
+        */\r
+       public Slot existingSlot(String name) {\r
+               return localMap.get(name);\r
+       }\r
+       \r
+       /* (non-Javadoc)\r
+        * @see com.att.env.Store#existingSlotNames()\r
+        */\r
+       public List<String> existingSlotNames() {\r
+               return new ArrayList<String>(localMap.keySet());\r
+       }\r
+\r
+       /* (non-Javadoc)\r
+        * @see com.att.env.Store#staticSlot(java.lang.String)\r
+        */\r
+       public synchronized StaticSlot staticSlot(String name) {\r
+               name = name == null ? "" : name.trim();\r
+               StaticSlot slot = staticMap.get(name);\r
+               if (slot == null)  {\r
+                       if (stat%growSize == 0) {\r
+                               Object[] temp = staticState;\r
+                               staticState = new Object[temp.length+growSize];\r
+                               System.arraycopy(temp, 0, staticState, 0, temp.length);\r
+                       }\r
+                       slot = new StaticSlot(stat++, name);\r
+                       staticMap.put(name, slot);\r
+               }\r
+               return slot;\r
+       }\r
+       \r
+       /* (non-Javadoc)\r
+        * @see com.att.env.Store#put(com.att.env.StaticSlot, java.lang.Object)\r
+        */\r
+       public void put(StaticSlot slot, Object value) {\r
+               staticState[slot.slot] = value;\r
+       }\r
+       \r
+       /* (non-Javadoc)\r
+        * @see com.att.env.Store#get(com.att.env.StaticSlot T defaultObject)\r
+        */\r
+       @SuppressWarnings("unchecked")\r
+       public<T> T get(StaticSlot sslot,T dflt) {\r
+               T t = (T)staticState[sslot.slot];\r
+               return t==null?dflt:t;\r
+       }\r
+\r
+       @SuppressWarnings("unchecked")\r
+       public <T> T get(StaticSlot sslot) {\r
+               return (T)staticState[sslot.slot];\r
+       }\r
+\r
+       public List<String> existingStaticSlotNames() {\r
+               return new ArrayList<String>(staticMap.keySet());\r
+       }\r
+}\r
+\r