Example #1
0
  protected void initMule() {
    try {
      // endpointsCache.clear();
      // See if there has been a discriptor explicitly configured
      if (applicationContext.containsBean(EVENT_MULTICASTER_DESCRIPTOR_NAME)) {
        descriptor = (UMODescriptor) applicationContext.getBean(EVENT_MULTICASTER_DESCRIPTOR_NAME);
      }
      // If the mule manager has been initialised in the contain
      // there is not need to do anything here
      if (applicationContext.containsBean("muleManager")) {
        // Register the multicaster descriptor
        registerMulticasterDescriptor();
        return;
      }
      UMOManager manager = MuleManager.getInstance();
      Map map = applicationContext.getBeansOfType(MuleConfiguration.class);
      if (map != null && map.size() > 0) {
        MuleManager.setConfiguration((MuleConfiguration) map.values().iterator().next());
      }
      if (!manager.isStarted()) {
        MuleManager.getConfiguration().setSynchronous(!asynchronous);
        // register any endpointUri mappings
        registerEndpointMappings();
      }
      // tell mule to load component definitions from spring
      SpringContainerContext containerContext = new SpringContainerContext();
      containerContext.setBeanFactory(applicationContext);
      manager.setContainerContext(null);
      manager.setContainerContext(containerContext);

      // see if there are any UMOConnectors to register
      registerConnectors();

      // Next see if there are any UMOTransformers to register
      registerTransformers();

      registerGlobalEndpoints();

      // Register the multicaster descriptor
      registerMulticasterDescriptor();

      if (!manager.isStarted()) {
        manager.start();
      }
    } catch (UMOException e) {
      throw new MuleRuntimeException(SpringMessages.failedToReinitMule(), e);
    }
  }
Example #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;
          }
        }
      }
    }
  }