/* (non-Javadoc)
   * @see org.granite.tide.ejb.EJBServiceContextIntf#postCall(org.granite.messaging.service.ServiceInvocationContext, java.lang.Object, java.lang.String)
   */
  @Override
  public IInvocationResult postCall(
      ServiceInvocationContext context,
      Object result,
      String componentName,
      Class<?> componentClass) {
    try {
      AbstractContext threadContext = AbstractContext.instance();

      List<ContextUpdate> results = new ArrayList<ContextUpdate>(threadContext.size());
      DataContext dataContext = DataContext.get();
      Set<Object[]> dataUpdates = dataContext != null ? dataContext.getDataUpdates() : null;
      Object[][] updates = null;
      if (dataUpdates != null && !dataUpdates.isEmpty())
        updates = dataUpdates.toArray(new Object[dataUpdates.size()][]);

      for (Map.Entry<String, Object> entry : threadContext.entrySet())
        results.add(new ContextUpdate(entry.getKey(), null, entry.getValue(), 3, false));

      InvocationResult ires = new InvocationResult(result, results);
      if (context.getBean() != null) {
        if (context.getBean().getClass().isAnnotationPresent(BypassTideMerge.class))
          ires.setMerge(false);
        else {
          try {
            Method m =
                context
                    .getBean()
                    .getClass()
                    .getMethod(
                        context.getMethod().getName(), context.getMethod().getParameterTypes());
            if (m.isAnnotationPresent(BypassTideMerge.class)) ires.setMerge(false);
          } catch (Exception e) {
            log.warn("Could not find bean method", e);
          }
        }
      }

      ires.setUpdates(updates);
      ires.setEvents(new ArrayList<ContextEvent>(threadContext.getRemoteEvents()));

      if (componentName != null) {
        EjbComponent component = ejbLookupCache.get(componentName);
        if (component != null
            && component.ejbMetadata != null
            && component.ejbMetadata.isStateful()
            && component.ejbMetadata.isRemoveMethod(context.getMethod()))
          ejbLookupCache.remove(componentName);
      }

      return ires;
    } finally {
      AbstractContext.remove();
    }
  }
 /* (non-Javadoc)
  * @see org.granite.tide.ejb.EJBServiceContextIntf#postCallFault(org.granite.messaging.service.ServiceInvocationContext, java.lang.Throwable, java.lang.String)
  */
 @Override
 public void postCallFault(
     ServiceInvocationContext context,
     Throwable t,
     String componentName,
     Class<?> componentClass) {
   try {
     if (componentName != null) {
       EjbComponent component = ejbLookupCache.get(componentName);
       if (t instanceof NoSuchEJBException
           || (component != null
               && component.ejbMetadata != null
               && (component.ejbMetadata.isStateful()
                   && component.ejbMetadata.isRemoveMethod(context.getMethod())
                   && !component.ejbMetadata.getRetainIfException(context.getMethod())))) {
         ejbLookupCache.remove(componentName);
       }
     }
   } finally {
     AbstractContext.remove();
   }
 }