/**
   * Returns the array of components bound the "multicastItfName" multicast interface, or null if it
   * fails to getting them.
   *
   * @param component The client component
   * @param multicastItfName The name of the multicast interface
   * @return Array of bound components, or null if it fails
   */
  public static Component[] getMulticastBindComponenents(
      Component component, String multicastItfName) {
    try {
      Object[] destinationItfs =
          Utils.getPAMulticastController(component).lookupGCMMulticast(multicastItfName);
      Component[] destinationComps = new Component[destinationItfs.length];

      for (int i = 0; i < destinationItfs.length; i++) {
        destinationComps[i] = ((PAInterface) destinationItfs[i]).getFcItfOwner();
      }

      return destinationComps;

    } catch (NoSuchInterfaceException e) {
      e.printStackTrace();
      return null;
    }
  }
  private void registerMethods() {
    PAActiveObject.setImmediateService(
        "getGCMStatistics",
        new Class[] {String.class, String.class, (new Class<?>[] {}).getClass()});
    PAActiveObject.setImmediateService("getAllGCMStatistics");

    statistics = Collections.synchronizedMap(new HashMap<String, Object>());
    keysList = new HashMap<String, String>();
    NameController nc = null;
    try {
      nc = GCM.getNameController(owner);
    } catch (NoSuchInterfaceException e) {
      e.printStackTrace();
    }
    String name = nc.getFcName();
    Object[] itfs = owner.getFcInterfaces();
    for (int i = 0; i < itfs.length; i++) {
      Interface itf = (Interface) itfs[i];
      InterfaceType itfType = (InterfaceType) itf.getFcItfType();
      try {
        if (!Utils.isControllerItfName(itf.getFcItfName()) && (!itfType.isFcClientItf())) {
          List<MonitorController> subcomponentMonitors = new ArrayList<MonitorController>();
          if (isComposite()) {
            Iterator<Component> bindedComponentsIterator = null;
            if (!((GCMInterfaceType) itfType).isGCMMulticastItf()) {
              List<Component> bindedComponent = new ArrayList<Component>();
              bindedComponent.add(
                  ((PAInterface) ((PAInterface) itf).getFcItfImpl()).getFcItfOwner());
              bindedComponentsIterator = bindedComponent.iterator();
            } else {
              try {
                PAMulticastControllerImpl multicastController =
                    (PAMulticastControllerImpl)
                        ((PAInterface) GCM.getMulticastController(owner)).getFcItfImpl();
                Iterator<PAInterface> delegatee =
                    multicastController.getDelegatee(itf.getFcItfName()).iterator();
                List<Component> bindedComponents = new ArrayList<Component>();
                while (delegatee.hasNext()) {
                  bindedComponents.add(delegatee.next().getFcItfOwner());
                }
                bindedComponentsIterator = bindedComponents.iterator();
              } catch (NoSuchInterfaceException e) {
                e.printStackTrace();
              }
            }
            try {
              while (bindedComponentsIterator.hasNext()) {
                MonitorController monitor =
                    GCM.getMonitorController(bindedComponentsIterator.next());
                monitor.startGCMMonitoring();
                subcomponentMonitors.add(monitor);
              }
            } catch (NoSuchInterfaceException e) {
              e.printStackTrace();
            }
          }
          Class<?> klass =
              ClassLoader.getSystemClassLoader().loadClass(itfType.getFcItfSignature());
          Method[] methods = klass.getDeclaredMethods();
          for (Method m : methods) {
            Class<?>[] parametersTypes = m.getParameterTypes();
            String key =
                PAMonitorControllerHelper.generateKey(
                    itf.getFcItfName(), m.getName(), parametersTypes);
            keysList.put(m.getName(), key);
            if (subcomponentMonitors.isEmpty()) {
              statistics.put(
                  key,
                  new MethodStatisticsPrimitiveImpl(
                      itf.getFcItfName(), m.getName(), parametersTypes));
            } else {
              statistics.put(
                  key,
                  new MethodStatisticsCompositeImpl(
                      itf.getFcItfName(), m.getName(), parametersTypes, subcomponentMonitors));
            }
            controllerLogger.debug(
                m.getName() + " (server) added to monitoring on component " + name + "!!!");
          }
        }
      } catch (ClassNotFoundException e) {
        throw new ProActiveRuntimeException("The interface " + itfType + "cannot be found", e);
      }
    }
  }
  /**
   * Runs the activity as defined in @see ComponentRunActive. The default behaviour is to serve
   * non-functional requests in FIFO order, until the component is started. Then the functional
   * activity (as defined in @see InitActive, @see RunActive and @see EndActive) begins.
   *
   * <p>When redefining the @see RunActive#runActivity(Body) method, the @see Body#isActive()
   * returns true as long as the lifecycle of the component is @see LifeCycleController#STARTED.
   * When the lifecycle of the component is @see LifeCycleController#STOPPED, @see Body#isActive()
   * returns false.
   */
  @Override
  public void runActivity(Body body) {
    if ((componentRunActive != null) && (componentRunActive != this)) {
      componentRunActive.runActivity(body);
    } else {
      // this is the default activity of the active object
      // the activity of the component has been initialized and started, now
      // what we have to do is to manage the life cycle, i.e. start and stop the
      // activity
      // that can be redefined on the reified object.
      try {
        Service componentService = new Service(body);
        NFRequestFilterImpl nfRequestFilter = new NFRequestFilterImpl();
        MembraneControllerRequestFilter memRequestFilter = new MembraneControllerRequestFilter();
        while (body.isActive()) {
          ComponentBody componentBody = (ComponentBody) body;

          /*
           * While the membrane is stopped, serve calls only on the Membrane Controller
           */
          while (Utils.getPAMembraneController(componentBody.getPAComponentImpl())
              .getMembraneState()
              .equals(PAMembraneController.MEMBRANE_STOPPED)) {
            componentService.blockingServeOldest(memRequestFilter);
          }

          while (LifeCycleController.STOPPED.equals(
              GCM.getGCMLifeCycleController(componentBody.getPAComponentImpl()).getFcState())) {
            PriorityController pc = GCM.getPriorityController(componentBody.getPAComponentImpl());
            NF3RequestFilter nf3RequestFilter = new NF3RequestFilter(pc);
            if (componentService.getOldest(nf3RequestFilter) != null) {
              // NF3 bypass all other request
              // System.err.println(
              //   "STOPPED ComponentActivity : NF3");
              componentService.blockingServeOldest(nf3RequestFilter);
            } else {
              componentService.blockingServeOldest(nfRequestFilter);
            }

            if (!body.isActive()) {
              // in case of a migration
              break;
            }
          }
          if (!body.isActive()) {
            // in case of a migration
            break;
          }

          // 3.1. init object Activity
          // life cycle started : starting activity of the object
          if (functionalInitActive != null) {
            functionalInitActive.initActivity(body);
            // functionalInitActive = null; // we won't do it again
          }

          ((ComponentBody) body).startingFunctionalActivity();
          // 3.2 while object activity
          // componentServe (includes filter on priority)
          functionalRunActive.runActivity(body);
          ((ComponentBody) body).finishedFunctionalActivity();
          if (functionalEndActive != null) {
            functionalEndActive.endActivity(body);
          }

          /*
           * While the membrane is started, serve non-functional calls with priority (the
           * same as for Lifecycle Stopped)
           */
          while (Utils.getPAMembraneController(componentBody.getPAComponentImpl())
              .getMembraneState()
              .equals(PAMembraneController.MEMBRANE_STARTED)) {
            PriorityController pc = GCM.getPriorityController(componentBody.getPAComponentImpl());
            NF3RequestFilter nf3RequestFilter = new NF3RequestFilter(pc);
            if (componentService.getOldest(nf3RequestFilter) != null) {
              // NF3 bypass all other request
              // System.err.println(
              //   "STOPPED ComponentActivity : NF3");
              componentService.blockingServeOldest(nf3RequestFilter);
            } else {
              componentService.blockingServeOldest(nfRequestFilter);
            }
            if (!body.isActive()) { // Don't know if this is OK
              // in case of a migration
              break;
            }
          }
        }
      } catch (NoSuchInterfaceException e) {
        logger.error(
            "could not retreive an interface, probably the life cycle controller of this component; terminating the component. Error message is : "
                + e.getMessage());
      }
    }
  }