From 6b30c6cc3afc45f16d9a3887b4579e4214cf3311 Mon Sep 17 00:00:00 2001 From: Vladimir Mutafov Date: Wed, 12 Jan 2022 23:58:11 +0200 Subject: [PATCH] chore: format code --- .../api/context/ThreadContextFacade.java | 384 +++++++++--------- 1 file changed, 187 insertions(+), 197 deletions(-) diff --git a/modules/commons/commons-api/src/main/java/org/eclipse/dirigible/commons/api/context/ThreadContextFacade.java b/modules/commons/commons-api/src/main/java/org/eclipse/dirigible/commons/api/context/ThreadContextFacade.java index 5e993755b60..17f96e1de52 100644 --- a/modules/commons/commons-api/src/main/java/org/eclipse/dirigible/commons/api/context/ThreadContextFacade.java +++ b/modules/commons/commons-api/src/main/java/org/eclipse/dirigible/commons/api/context/ThreadContextFacade.java @@ -27,213 +27,203 @@ */ public class ThreadContextFacade { - private static final Logger logger = LoggerFactory.getLogger(ThreadContextFacade.class); - - private static final ThreadLocal>> STACKED_CONTEXT = new ThreadLocal<>(); - - private static final ThreadLocal>> STACKED_CLOSEABLES = new ThreadLocal<>(); - - private static final AtomicLong UUID_GENERATOR = new AtomicLong(Long.MIN_VALUE); - - private static final ThreadLocal STACK_ID = new ThreadLocal<>(); - - /** - * Initializes the context. This has to be called at the very first (as possible) place at the service entry point - * - */ - public static void setUp() { - if (stackedContextIsEmpty()) { - STACKED_CONTEXT.set(new HashMap<>()); - } - if (stackedCloseablesIsEmpty()) { - STACKED_CLOSEABLES.set(new HashMap<>()); - } - - Integer currentStackId = STACK_ID.get(); - if (currentStackId == null) { - STACK_ID.set(0); - } else { - STACK_ID.set(currentStackId + 1); - - } - - STACKED_CONTEXT.get().put(STACK_ID.get(), collectParentObjects()); - STACKED_CLOSEABLES.get().put(STACK_ID.get(), new HashMap<>()); - - logger.trace("Scripting context {} has been set up", Thread.currentThread().hashCode()); - } - - private static Map collectParentObjects() { - Map allParentObjects = new HashMap<>(); - - for (int parentStackId = STACK_ID.get() - 1; parentStackId >= 0; parentStackId -= 1) { - Map parentStackObjects = STACKED_CONTEXT.get().get(parentStackId); - allParentObjects.putAll(parentStackObjects); - } - - return allParentObjects; - } - - /** - * IMPORTANT! This have to be added at the finally block to clean up objects after the execution of the service. - * - */ - public static void tearDown() { + private static final Logger logger = LoggerFactory.getLogger(ThreadContextFacade.class); + + private static final ThreadLocal>> STACKED_CONTEXT = new ThreadLocal<>(); + + private static final ThreadLocal>> STACKED_CLOSEABLES = new ThreadLocal<>(); + + private static final AtomicLong UUID_GENERATOR = new AtomicLong(Long.MIN_VALUE); + + private static final ThreadLocal STACK_ID = new ThreadLocal<>(); + + /** + * Initializes the context. This has to be called at the very first (as possible) place at the service entry point + */ + public static void setUp() { + if (stackedContextIsEmpty()) { + STACKED_CONTEXT.set(new HashMap<>()); + } + if (stackedCloseablesIsEmpty()) { + STACKED_CLOSEABLES.set(new HashMap<>()); + } + + Integer currentStackId = STACK_ID.get(); + if (currentStackId == null) { + STACK_ID.set(0); + } else { + STACK_ID.set(currentStackId + 1); + + } + + STACKED_CONTEXT.get().put(STACK_ID.get(), collectParentObjects()); + STACKED_CLOSEABLES.get().put(STACK_ID.get(), new HashMap<>()); + + logger.trace("Scripting context {} has been set up", Thread.currentThread().hashCode()); + } + + private static Map collectParentObjects() { + Map allParentObjects = new HashMap<>(); + + for (int parentStackId = STACK_ID.get() - 1; parentStackId >= 0; parentStackId -= 1) { + Map parentStackObjects = STACKED_CONTEXT.get().get(parentStackId); + allParentObjects.putAll(parentStackObjects); + } + + return allParentObjects; + } + + /** + * IMPORTANT! This have to be added at the finally block to clean up objects after the execution of the service. + */ + public static void tearDown() { Integer stackId = STACK_ID.get(); - if (stackId == null) { + if (stackId == null) { return; } - if (stackedContextIsNotEmpty()) { - STACKED_CONTEXT.get().get(stackId).clear(); - } - - if (stackedCloseablesIsNotEmpty()) { - Map CLOSEABLES = STACKED_CLOSEABLES.get().get(stackId); - for (Entry closeable : CLOSEABLES.entrySet()) { - try { - logger.error("Object of type {} from the context {} has not been closed properly.", closeable.getValue().getClass().getCanonicalName(), Thread.currentThread().hashCode()); - closeable.getValue().close(); - } catch (Exception e) { - logger.error(e.getMessage(), e); - } - } - STACKED_CLOSEABLES.get().get(stackId).clear(); - } - - if (stackId == 0) { - STACKED_CONTEXT.remove(); - STACKED_CLOSEABLES.remove(); - } - STACK_ID.set(stackId - 1); - - logger.trace("Scripting context {} has been torn down", Thread.currentThread().hashCode()); - } - - /** - * Get a context scripting object. - * - * @param key - * the key - * @return the value by this key - * @throws ContextException - * in case of an error - */ - public static Object get(String key) throws ContextException { - checkContext(); - Map> stackedContext = STACKED_CONTEXT.get(); - Map currentStack = stackedContext.get(STACK_ID.get()); - return currentStack.get(key); - } - - /** - * Set a context scripting object. - * - * @param value - * the value - * @return the UUID of the object - * @throws ContextException - * in case of an error - */ - public static String set(Object value) throws ContextException { - final String uuid = generateObjectId(); - set(uuid, value); - return uuid; - } - - /** - * Set a context scripting object. If object with - * with this key exists, it will be replaced with - * the new object - * - * @param key - * the key - * @param value - * the value - * @throws ContextException - * in case of an error - */ - public static void set(String key, Object value) throws ContextException { - checkContext(); - STACKED_CONTEXT.get().get(STACK_ID.get()).put(key, value); - logger.trace("Context object has been added to {} with key {}", Thread.currentThread().hashCode(), key); - } - - /** - * Remove a context scripting object. - * - * @param key the key - * @throws ContextException in case of an error - */ - public static void remove(String key) throws ContextException { - checkContext(); - STACKED_CONTEXT.get().get(STACK_ID.get()).remove(key); - logger.trace("Context object has been removed - key {}", key); - } - - - /** - * Check context. - * - * @throws ContextException - * the context exception - */ - private static void checkContext() throws ContextException { - if (STACKED_CONTEXT.get() == null) { - throw new ContextException("Context has not been initialized"); - } - } - - /** - * Check whether the facade is valid. - * - * @return yes, if it is valid - */ - public static boolean isValid() { - return (STACKED_CONTEXT.get() != null); - } - - /** - * Generate object id. - * - * @return the string - */ - private static String generateObjectId() { - return Long.toString(UUID_GENERATOR.incrementAndGet(), Character.MAX_RADIX); - } - - /** - * Add a closeable object to the map - * - * @param closeable the closeable object - */ - public static void addCloseable(AutoCloseable closeable) { - if (STACKED_CLOSEABLES.get() != null) { - STACKED_CLOSEABLES.get().get(STACK_ID.get()).put(STACK_ID.get() + "_" + closeable.hashCode(), closeable); - logger.trace("Closeable object has been added to {} with hash {}", Thread.currentThread().hashCode(), closeable.hashCode()); - } - } - - /** - * Remove a closeable object. - * - * @param closeable the closeable object - */ - public static void removeCloseable(AutoCloseable closeable) { - if (STACKED_CLOSEABLES.get() != null) { - STACKED_CLOSEABLES.get().get(STACK_ID.get()).remove(STACK_ID.get() + "_" + closeable.hashCode()); - logger.trace("Closeable object has been removed - hash {}", closeable.hashCode()); - } - } + if (stackedContextIsNotEmpty()) { + STACKED_CONTEXT.get().get(stackId).clear(); + } + + if (stackedCloseablesIsNotEmpty()) { + Map CLOSEABLES = STACKED_CLOSEABLES.get().get(stackId); + for (Entry closeable : CLOSEABLES.entrySet()) { + try { + logger.error("Object of type {} from the context {} has not been closed properly.", closeable.getValue().getClass().getCanonicalName(), Thread.currentThread().hashCode()); + closeable.getValue().close(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + } + } + STACKED_CLOSEABLES.get().get(stackId).clear(); + } + + if (stackId == 0) { + STACKED_CONTEXT.remove(); + STACKED_CLOSEABLES.remove(); + } + STACK_ID.set(stackId - 1); + + logger.trace("Scripting context {} has been torn down", Thread.currentThread().hashCode()); + } + + /** + * Get a context scripting object. + * + * @param key the key + * @return the value by this key + * @throws ContextException in case of an error + */ + public static Object get(String key) throws ContextException { + checkContext(); + Map> stackedContext = STACKED_CONTEXT.get(); + Map currentStack = stackedContext.get(STACK_ID.get()); + return currentStack.get(key); + } + + /** + * Set a context scripting object. + * + * @param value the value + * @return the UUID of the object + * @throws ContextException in case of an error + */ + public static String set(Object value) throws ContextException { + final String uuid = generateObjectId(); + set(uuid, value); + return uuid; + } + + /** + * Set a context scripting object. If object with + * with this key exists, it will be replaced with + * the new object + * + * @param key the key + * @param value the value + * @throws ContextException in case of an error + */ + public static void set(String key, Object value) throws ContextException { + checkContext(); + STACKED_CONTEXT.get().get(STACK_ID.get()).put(key, value); + logger.trace("Context object has been added to {} with key {}", Thread.currentThread().hashCode(), key); + } + + /** + * Remove a context scripting object. + * + * @param key the key + * @throws ContextException in case of an error + */ + public static void remove(String key) throws ContextException { + checkContext(); + STACKED_CONTEXT.get().get(STACK_ID.get()).remove(key); + logger.trace("Context object has been removed - key {}", key); + } + + + /** + * Check context. + * + * @throws ContextException the context exception + */ + private static void checkContext() throws ContextException { + if (STACKED_CONTEXT.get() == null) { + throw new ContextException("Context has not been initialized"); + } + } + + /** + * Check whether the facade is valid. + * + * @return yes, if it is valid + */ + public static boolean isValid() { + return (STACKED_CONTEXT.get() != null); + } + + /** + * Generate object id. + * + * @return the string + */ + private static String generateObjectId() { + return Long.toString(UUID_GENERATOR.incrementAndGet(), Character.MAX_RADIX); + } + + /** + * Add a closeable object to the map + * + * @param closeable the closeable object + */ + public static void addCloseable(AutoCloseable closeable) { + if (STACKED_CLOSEABLES.get() != null) { + STACKED_CLOSEABLES.get().get(STACK_ID.get()).put(STACK_ID.get() + "_" + closeable.hashCode(), closeable); + logger.trace("Closeable object has been added to {} with hash {}", Thread.currentThread().hashCode(), closeable.hashCode()); + } + } + + /** + * Remove a closeable object. + * + * @param closeable the closeable object + */ + public static void removeCloseable(AutoCloseable closeable) { + if (STACKED_CLOSEABLES.get() != null) { + STACKED_CLOSEABLES.get().get(STACK_ID.get()).remove(STACK_ID.get() + "_" + closeable.hashCode()); + logger.trace("Closeable object has been removed - hash {}", closeable.hashCode()); + } + } private static boolean stackedContextIsEmpty() { Map> stackedContext = STACKED_CONTEXT.get(); return stackedContext == null || stackedContext.isEmpty(); } - private static boolean stackedContextIsNotEmpty() { - Map> stackedContext = STACKED_CONTEXT.get(); - return stackedContext != null && !stackedContext.isEmpty(); + private static boolean stackedContextIsNotEmpty() { + Map> stackedContext = STACKED_CONTEXT.get(); + return stackedContext != null && !stackedContext.isEmpty(); } private static boolean stackedCloseablesIsEmpty() {