/**
  * Returns true if a component is associated to the specified address. Components registered with
  * this JVM or other cluster nodes are going to be considered.
  *
  * @param componentJID the address of the component. This is the complete domain.
  * @return true if a component is associated to the specified address.
  */
 public boolean hasComponent(JID componentJID) {
   synchronized (routables) {
     if (componentJID.getNode() != null || componentJID.getResource() != null) {
       return false;
     }
     //        if (componentJID.getDomain().lastIndexOf("." + serverDomain) == -1) {
     //            componentJID = new JID(componentJID.getDomain() + "." + serverDomain);
     //        }
     return routingTable.hasComponentRoute(componentJID);
   }
 }
  private void checkPresences() {
    for (JID prober : presenceMap.keySet()) {
      JID probee = presenceMap.get(prober);

      if (routingTable.hasComponentRoute(probee)) {
        Presence presence = new Presence();
        presence.setFrom(prober);
        presence.setTo(probee);
        routingTable.routePacket(probee, presence, false);

        // No reason to hold onto prober reference.
        presenceMap.remove(prober);
      }
    }
  }
  /**
   * Removes a given component. Unlike {@link #removeComponent(String)} this method will just remove
   * a single component instead of all components associated to the subdomain. External components
   * may connect several times and register for the same subdomain. This method just removes a
   * singled connection not all of them.
   *
   * @param subdomain the subdomain of the component's address.
   * @param component specific component to remove.
   */
  public void removeComponent(String subdomain, Component component) {
    if (component == null) {
      return;
    }
    synchronized (routables) {
      Log.debug("InternalComponentManager: Unregistering component for domain: " + subdomain);
      RoutableComponents routable = routables.get(subdomain);
      routable.removeComponent(component);
      if (routable.numberOfComponents() == 0) {
        routables.remove(subdomain);

        JID componentJID = new JID(subdomain + "." + serverDomain);

        // Remove the route for the service provided by the component
        routingTable.removeComponentRoute(componentJID);

        // Ask the component to shutdown
        component.shutdown();

        if (!routingTable.hasComponentRoute(componentJID)) {
          // Remove the disco item from the server for the component that is being removed
          IQDiscoItemsHandler iqDiscoItemsHandler =
              XMPPServer.getInstance().getIQDiscoItemsHandler();
          if (iqDiscoItemsHandler != null) {
            iqDiscoItemsHandler.removeComponentItem(componentJID.toBareJID());
          }
          removeComponentInfo(componentJID);
          // Notify listeners that an existing component has been unregistered
          notifyComponentUnregistered(componentJID);
          // Alert other nodes of component removed event
          CacheFactory.doClusterTask(new NotifyComponentUnregistered(componentJID));
        }
        Log.debug("InternalComponentManager: Component unregistered for domain: " + subdomain);
      } else {
        Log.debug("InternalComponentManager: Other components still tied to domain: " + subdomain);
      }
    }
  }
  @Override
  public void addComponent(String subdomain, Component component) throws ComponentException {
    synchronized (routables) {
      RoutableComponents routable = routables.get(subdomain);
      if (routable != null && routable.hasComponent(component)) {
        // This component has already registered with this subdomain.
        // TODO: Is this all we should do?  Should we return an error?
        return;
      }
      Log.debug("InternalComponentManager: Registering component for domain: " + subdomain);
      JID componentJID = new JID(subdomain + "." + serverDomain);
      boolean notifyListeners = false;
      if (routable != null) {
        routable.addComponent(component);
      } else {
        routable = new RoutableComponents(componentJID, component);
        routables.put(subdomain, routable);

        if (!routingTable.hasComponentRoute(componentJID)) {
          notifyListeners = true;
        }
        // Add the route to the new service provided by the component
        routingTable.addComponentRoute(componentJID, routable);
      }

      // Initialize the new component
      try {
        component.initialize(componentJID, this);
        component.start();

        if (notifyListeners) {
          // Notify listeners that a new component has been registered
          notifyComponentRegistered(componentJID);
          // Alert other nodes of new registered domain event
          CacheFactory.doClusterTask(new NotifyComponentRegistered(componentJID));
        }

        // Check for potential interested users.
        checkPresences();
        // Send a disco#info request to the new component. If the component provides information
        // then it will be added to the list of discoverable server items.
        checkDiscoSupport(component, componentJID);
        Log.debug("InternalComponentManager: Component registered for domain: " + subdomain);
      } catch (Exception e) {
        // Unregister the component's domain
        routable.removeComponent(component);
        if (e instanceof ComponentException) {
          // Rethrow the exception
          throw (ComponentException) e;
        }
        // Rethrow the exception
        throw new ComponentException(e);
      } finally {
        if (routable.numberOfComponents() == 0) {
          // If there are no more components associated with this subdomain, remove it.
          routables.remove(subdomain);
          // Remove the route
          XMPPServer.getInstance().getRoutingTable().removeComponentRoute(componentJID);
        }
      }
    }
  }