예제 #1
0
 /**
  * Removes a listener from the multicaster
  *
  * @param listener the listener to remove
  */
 public void removeApplicationListener(ApplicationListener listener) {
   for (Iterator iterator = listeners.iterator(); iterator.hasNext(); ) {
     ApplicationListener applicationListener = (ApplicationListener) iterator.next();
     if (applicationListener instanceof AsynchronousEventListener) {
       if (((AsynchronousEventListener) applicationListener).getListener().equals(listener)) {
         listeners.remove(applicationListener);
         return;
       }
     } else {
       if (applicationListener.equals(listener)) {
         listeners.remove(applicationListener);
         return;
       }
     }
   }
   listeners.remove(listener);
 }
예제 #2
0
  /**
   * Method is used to dispatch events to listeners registered with the EventManager or dispatches
   * events to Mule depending on the type and state of the event received. If the event is not a
   * Mule event it will be dispatched to any listeners registered that are NOT MuleEventListeners.
   * If the event is a Mule event and there is no source event attached to it, it is assumed that
   * the event was dispatched by an object in the context using context.publishEvent() and will be
   * dispatched by Mule. If the event does have a source event attached to it, it is assumed that
   * the event was dispatched by Mule and will be delivered to any listeners subscribed to the
   * event.
   *
   * @param e application event received by the context
   */
  public void multicastEvent(ApplicationEvent e) {
    MuleApplicationEvent muleEvent = null;
    // if the context gets refreshed we need to reinitialise
    if (e instanceof ContextRefreshedEvent) {
      // If the manager is being initialised from another context
      // don't try and initialise Mule
      if (MuleManager.isInstanciated() && !MuleManager.getInstance().isInitialised()) {
        try {
          registerMulticasterDescriptor();
        } catch (UMOException ex) {
          throw new MuleRuntimeException(SpringMessages.failedToReinitMule(), ex);
        }
      } else {
        initMule();
      }
    } else if (e instanceof ContextClosedEvent) {
      MuleManager.getInstance().dispose();
      return;
    } else if (e instanceof MuleApplicationEvent) {
      muleEvent = (MuleApplicationEvent) e;
      // If there is no Mule event the event didn't originate from Mule
      // so its an outbound event
      if (muleEvent.getMuleEventContext() == null) {
        try {
          dispatchEvent(muleEvent);
        } catch (ApplicationEventException e1) {
          exceptionListener.exceptionThrown(e1);
        }
        return;
      }
    }

    for (Iterator iterator = listeners.iterator(); iterator.hasNext(); ) {
      ApplicationListener listener = (ApplicationListener) iterator.next();
      if (muleEvent != null) {
        // As the asynchronous listener wraps the real listener we need to
        // check the
        // type of the wrapped listener, but invoke the Async listener
        if (listener instanceof AsynchronousEventListener) {
          AsynchronousEventListener asyncListener = (AsynchronousEventListener) listener;
          if (asyncListener.getListener() instanceof MuleSubscriptionEventListener) {
            if (isSubscriptionMatch(
                muleEvent.getEndpoint(),
                ((MuleSubscriptionEventListener) asyncListener.getListener()).getSubscriptions())) {
              asyncListener.onApplicationEvent(muleEvent);
            }
          } else if (asyncListener.getListener() instanceof MuleEventListener) {
            asyncListener.onApplicationEvent(muleEvent);
          } else if (!(asyncListener.getListener() instanceof MuleEventListener)) {
            asyncListener.onApplicationEvent(e);
          }
          // Synchronous Event listener Checks
        } else if (listener instanceof MuleSubscriptionEventListener) {
          if (isSubscriptionMatch(
              muleEvent.getEndpoint(),
              ((MuleSubscriptionEventListener) listener).getSubscriptions())) {
            listener.onApplicationEvent(muleEvent);
          }
        } else if (listener instanceof MuleEventListener) {
          listener.onApplicationEvent(muleEvent);
        }
      } else if (listener instanceof AsynchronousEventListener
          && !(((AsynchronousEventListener) listener).getListener() instanceof MuleEventListener)) {
        listener.onApplicationEvent(e);
      } else if (!(listener instanceof MuleEventListener)) {
        listener.onApplicationEvent(e);
      } else {
        // Finally only propagate the Application event if the
        // ApplicationEvent interface is explicitly implemented
        for (int i = 0; i < listener.getClass().getInterfaces().length; i++) {
          if (listener.getClass().getInterfaces()[i].equals(ApplicationListener.class)) {
            listener.onApplicationEvent(e);
            break;
          }
        }
      }
    }
  }