test_class.declarator: context
test_class.original_string: DefaultManagedContext context;
test_class.type: DefaultManagedContext
test_class.var_name: context
test_class.file: cosmic-common/src/test/java/com/cloud/common/managed/context/DefaultManagedContextTest.java
test_class.identifier: DefaultManagedContextTest
test_case.body: @Test
      public void testCallable() throws Exception {
          assertEquals(5, this.context.callWithContext(new Callable<Integer>() {
              @Override
              public Integer call() throws Exception {
                  return 5;
              }
          }).intValue());
      }
test_case.class_method_signature: DefaultManagedContextTest.testCallable()
test_case.full_signature: @Test public void testCallable()
test_case.identifier: testCallable
test_case.invocations: assertEquals intValue callWithContext
test_case.modifiers: @Test public
test_case.parameters: ()
test_case.return: void
test_case.signature: void testCallable()
focal_class.declarator: log = LoggerFactory.getLogger(DefaultManagedContext.class)
focal_class.modifier: private static final
focal_class.original_string: private static final Logger log = LoggerFactory.getLogger(DefaultManagedContext.class);
focal_class.type: Logger
focal_class.var_name: log
focal_class.declarator: listeners = new CopyOnWriteArrayList<>()
focal_class.original_string: List<ManagedContextListener<?>> listeners = new CopyOnWriteArrayList<>();
focal_class.type: List<ManagedContextListener<?>>
focal_class.var_name: listeners
focal_class.file: cosmic-common/src/main/java/com/cloud/common/managed/context/DefaultManagedContext.java
focal_class.identifier: DefaultManagedContext
focal_class.interfaces: implements ManagedContext
focal_class.class_method_signature: DefaultManagedContext.registerListener(final ManagedContextListener<?> listener)
focal_class.full_signature: @Override public void registerListener(final ManagedContextListener<?> listener)
focal_class.identifier: registerListener
focal_class.modifiers: @Override public
focal_class.parameters: (final ManagedContextListener<?> listener)
focal_class.return: void
focal_class.signature: void registerListener(final ManagedContextListener<?> listener)
focal_class.class_method_signature: DefaultManagedContext.unregisterListener(final ManagedContextListener<?> listener)
focal_class.full_signature: @Override public void unregisterListener(final ManagedContextListener<?> listener)
focal_class.identifier: unregisterListener
focal_class.modifiers: @Override public
focal_class.parameters: (final ManagedContextListener<?> listener)
focal_class.return: void
focal_class.signature: void unregisterListener(final ManagedContextListener<?> listener)
focal_class.class_method_signature: DefaultManagedContext.runWithContext(final Runnable run)
focal_class.full_signature: @Override public void runWithContext(final Runnable run)
focal_class.identifier: runWithContext
focal_class.modifiers: @Override public
focal_class.parameters: (final Runnable run)
focal_class.return: void
focal_class.signature: void runWithContext(final Runnable run)
focal_class.class_method_signature: DefaultManagedContext.callWithContext(final Callable<T> callable)
focal_class.full_signature: @Override public T callWithContext(final Callable<T> callable)
focal_class.identifier: callWithContext
focal_class.modifiers: @Override public
focal_class.parameters: (final Callable<T> callable)
focal_class.return: T
focal_class.signature: T callWithContext(final Callable<T> callable)
focal_class.class_method_signature: DefaultManagedContext.throwException(final Throwable t)
focal_class.full_signature: protected void throwException(final Throwable t)
focal_class.identifier: throwException
focal_class.modifiers: protected
focal_class.parameters: (final Throwable t)
focal_class.return: void
focal_class.signature: void throwException(final Throwable t)
focal_class.class_method_signature: DefaultManagedContext.getListeners()
focal_class.full_signature: public List<ManagedContextListener<?>> getListeners()
focal_class.identifier: getListeners
focal_class.modifiers: public
focal_class.parameters: ()
focal_class.return: List<ManagedContextListener<?>>
focal_class.signature: List<ManagedContextListener<?>> getListeners()
focal_class.class_method_signature: DefaultManagedContext.setListeners(final List<ManagedContextListener<?>> listeners)
focal_class.full_signature: public void setListeners(final List<ManagedContextListener<?>> listeners)
focal_class.identifier: setListeners
focal_class.modifiers: public
focal_class.parameters: (final List<ManagedContextListener<?>> listeners)
focal_class.return: void
focal_class.signature: void setListeners(final List<ManagedContextListener<?>> listeners)
focal_method.body: @Override
      public <T> T callWithContext(final Callable<T> callable) throws Exception {
          final Object owner = new Object();
  
          final Stack<ListenerInvocation> invocations = new Stack<>();
          final boolean reentry = !ManagedContextUtils.setAndCheckOwner(owner);
          RuntimeException firstError = null;
  
          try {
              for (final ManagedContextListener<?> listener : listeners) {
                  Object data = null;
  
                  try {
                      data = listener.onEnterContext(reentry);
                  } catch (final RuntimeException e) {
                      /* If one listener fails, still call all other listeners
                       * and then we will call onLeaveContext for all
                       */
                      if (firstError == null) {
                          firstError = e;
                      }
                      log.error("Failed onEnterContext for listener [{}]", listener, e);
                  }
  
                  /* Stack data structure is used because in between onEnter and onLeave
                   * the listeners list could have changed
                   */
                  invocations.push(new ListenerInvocation((ManagedContextListener<Object>) listener, data));
              }
  
              try {
                  if (firstError == null) {
                      /* Only call if all the listeners didn't blow up on onEnterContext */
                      return callable.call();
                  } else {
                      throwException(firstError);
                      return null;
                  }
              } finally {
                  Throwable lastError = null;
  
                  while (!invocations.isEmpty()) {
                      final ListenerInvocation invocation = invocations.pop();
                      try {
                          invocation.listener.onLeaveContext(invocation.data, reentry);
                      } catch (final RuntimeException e) {
                          lastError = e;
                          log.error("Failed onLeaveContext for listener [{}]", invocation.listener, e);
                      }
                  }
  
                  if (firstError == null && lastError != null) {
                      throwException(lastError);
                  }
              }
          } finally {
              if (ManagedContextUtils.clearOwner(owner)) {
                  ManagedThreadLocal.reset();
              }
          }
      }
focal_method.class_method_signature: DefaultManagedContext.callWithContext(final Callable<T> callable)
focal_method.full_signature: @Override public T callWithContext(final Callable<T> callable)
focal_method.identifier: callWithContext
focal_method.invocations: setAndCheckOwner onEnterContext error push call throwException isEmpty pop onLeaveContext error throwException clearOwner reset
focal_method.modifiers: @Override public
focal_method.parameters: (final Callable<T> callable)
focal_method.return: T
focal_method.signature: T callWithContext(final Callable<T> callable)