Increase coverage for Env module
[aaf/authz.git] / misc / env / src / main / java / org / onap / aaf / misc / env / util / RefreshableThreadObject.java
index 5615339..56cd54e 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.util;
-
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-
-import org.onap.aaf.misc.env.APIException;
-import org.onap.aaf.misc.env.Creatable;
-import org.onap.aaf.misc.env.Env;
-import org.onap.aaf.misc.env.LifeCycle;
-
-
-/**
- * <h1>RefreshableThreadObject</h1>
- * This is a ThreadLocal like implementation, but it responds to 
- * the {@link LifeCycle} mechanism for configuration refreshes, and 
- * implements {@link Creatable} (for use in destroy, etc).<p>
- * 
- * In addition to the Thread instance semantics, it compares when the object
- * was created versus the last "refresh(env)" call when getting, for the
- * thread, and if necessary to replace the created object, destroying the 
- * previous.<p>
- * 
- * In most cases, it's better to use the new "Pool" mechanism, as it deals with 
- * gaining and returning resources on an as needed basis.  This, however, remains
- * in the cases where specific Objects need to be retained to specific Threads.<p>
- * 
- * There is no way to do this kind of specialized behavior in ThreadLocal.
- * 
- * @author Jonathan
- *
- * @param <T>
- */
-public class RefreshableThreadObject<T extends Creatable<T>> {
-       private Map<Thread,T> objs;
-       private long refreshed;
-       private Constructor<T> cnst;
-       
-       /**
-        * The passed in class <b>must</b> implement the constructor
-        * <pre>
-        *   public MyClass(Env env) {
-        *     ...
-        *   }
-        * </pre>
-        * @param clss
-        * @throws APIException
-        */
-       public RefreshableThreadObject(Class<T> clss) throws APIException {
-               objs = new ConcurrentHashMap<Thread,T>();
-               try {
-                       cnst = clss.getConstructor(new Class[]{Env.class} );
-               } catch (Exception e) {
-                       throw new APIException(e);
-               }
-       }
-       
-       /**
-        * Get the "T" class from the current thread
-        * 
-        * @param env
-        * @return T
-        * @throws APIException
-        */
-       public T get(Env env) throws APIException {
-               Thread t = Thread.currentThread();
-               T obj = objs.get(t);
-               if(obj==null || refreshed>obj.created()) {
-                       try {
-                               obj = cnst.newInstance(new Object[]{env});
-                       } catch (InvocationTargetException e) {
-                               throw new APIException(e.getTargetException());
-                       } catch (Exception e) {
-                               throw new APIException(e);
-                       }
-                       T destroyMe = objs.put(t,obj);
-                       if(destroyMe!=null) {
-                               destroyMe.destroy(env);
-                       }
-               } 
-               return obj;
-       }
-       
-       /**
-        * Mark the timestamp of refreshed.
-        * 
-        * @param env
-        */
-       public void refresh(Env env) {
-               refreshed = System.currentTimeMillis();
-       }
-       
-       /**
-        * Remove the object from the Thread instances
-        * @param env
-        */
-       public void remove(Env env) {
-               T obj = objs.remove(Thread.currentThread());
-               if(obj!=null)
-                       obj.destroy(env);
-       }
-}
+/**\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.util;\r
+\r
+import java.lang.reflect.Constructor;\r
+import java.lang.reflect.InvocationTargetException;\r
+import java.util.Map;\r
+import java.util.concurrent.ConcurrentHashMap;\r
+\r
+import org.onap.aaf.misc.env.APIException;\r
+import org.onap.aaf.misc.env.Creatable;\r
+import org.onap.aaf.misc.env.Env;\r
+import org.onap.aaf.misc.env.LifeCycle;\r
+\r
+\r
+/**\r
+ * <h1>RefreshableThreadObject</h1>\r
+ * This is a ThreadLocal like implementation, but it responds to \r
+ * the {@link LifeCycle} mechanism for configuration refreshes, and \r
+ * implements {@link Creatable} (for use in destroy, etc).<p>\r
+ * \r
+ * In addition to the Thread instance semantics, it compares when the object\r
+ * was created versus the last "refresh(env)" call when getting, for the\r
+ * thread, and if necessary to replace the created object, destroying the \r
+ * previous.<p>\r
+ * \r
+ * In most cases, it's better to use the new "Pool" mechanism, as it deals with \r
+ * gaining and returning resources on an as needed basis.  This, however, remains\r
+ * in the cases where specific Objects need to be retained to specific Threads.<p>\r
+ * \r
+ * There is no way to do this kind of specialized behavior in ThreadLocal.\r
+ * \r
+ * @author Jonathan\r
+ *\r
+ * @param <T>\r
+ */\r
+public class RefreshableThreadObject<T extends Creatable<T>> {\r
+       private Map<Thread,T> objs;\r
+       private long refreshed;\r
+       private Constructor<T> cnst;\r
+       \r
+       /**\r
+        * The passed in class <b>must</b> implement the constructor\r
+        * <pre>\r
+        *   public MyClass(Env env) {\r
+        *     ...\r
+        *   }\r
+        * </pre>\r
+        * @param clss\r
+        * @throws APIException\r
+        */\r
+       public RefreshableThreadObject(Class<T> clss) throws APIException {\r
+               objs = new ConcurrentHashMap<Thread,T>();\r
+               try {\r
+                       cnst = clss.getConstructor(new Class[]{Env.class} );\r
+               } catch (Exception e) {\r
+                       throw new APIException(e);\r
+               }\r
+       }\r
+       \r
+       /**\r
+        * Get the "T" class from the current thread\r
+        * \r
+        * @param env\r
+        * @return T\r
+        * @throws APIException\r
+        */\r
+       public T get(Env env) throws APIException {\r
+               Thread t = Thread.currentThread();\r
+               T obj = objs.get(t);\r
+               if(obj==null || refreshed>obj.created()) {\r
+                       try {\r
+                               obj = cnst.newInstance(new Object[]{env});\r
+                       } catch (InvocationTargetException e) {\r
+                               throw new APIException(e.getTargetException());\r
+                       } catch (Exception e) {\r
+                               throw new APIException(e);\r
+                       }\r
+                       T destroyMe = objs.put(t,obj);\r
+                       if(destroyMe!=null) {\r
+                               destroyMe.destroy(env);\r
+                       }\r
+               } \r
+               return obj;\r
+       }\r
+       \r
+       /**\r
+        * Mark the timestamp of refreshed.\r
+        * \r
+        * @param env\r
+        */\r
+       public void refresh(Env env) {\r
+               refreshed = System.currentTimeMillis();\r
+       }\r
+       \r
+       /**\r
+        * Remove the object from the Thread instances\r
+        * @param env\r
+        */\r
+       public void remove(Env env) {\r
+               T obj = objs.remove(Thread.currentThread());\r
+               if(obj!=null)\r
+                       obj.destroy(env);\r
+       }\r
+}\r