/* (non-Javadoc)
   * @see org.jboss.remoting.ServerInvocationHandler#invoke(org.jboss.remoting.InvocationRequest)
   */
  public Object invoke(InvocationRequest invocation) throws Throwable {
    Serializable key = (Serializable) invocation.getRequestPayload().get(OID);
    Remotable remotable = remotables.get(key);
    if (remotable == null)
      throw new IllegalArgumentException("Can't find remotable " + key + " in " + remotables);

    Object parameters[] = (Object[]) invocation.getParameter();
    SerializableMethod method = (SerializableMethod) parameters[0];
    Object args[] = (Object[]) parameters[1];
    ClassLoader loader = remotable.getClassLoader();
    Method realMethod = method.toMethod(loader);
    ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
    try {
      Thread.currentThread().setContextClassLoader(loader);
      return realMethod.invoke(remotable.getTarget(), args);
    } catch (IllegalArgumentException e) {
      throw new IllegalArgumentException(
          "unable to invoke "
              + realMethod
              + " on "
              + remotable.getTarget()
              + " with "
              + Arrays.toString(args),
          e);
    } catch (InvocationTargetException e) {
      throw e.getCause();
    } finally {
      Thread.currentThread().setContextClassLoader(oldLoader);
    }
  }
 @Override
 public Object invoke(InvocationRequest arg0) throws Throwable {
   // TODO Auto-generated method stub
   index.setIndexfile((String) arg0.getParameter());
   return null;
 }
  /**
   * Implementation of the server invoker handler interface. Will take the invocation request and
   * invoke down the interceptor chain.
   *
   * @param invocationReq
   * @return response of the invocation
   * @throws Throwable
   */
  public Object invoke(InvocationRequest invocationReq) throws Throwable {
    Invocation invocation = (Invocation) invocationReq.getParameter();
    Thread currentThread = Thread.currentThread();
    ClassLoader oldCl = currentThread.getContextClassLoader();
    ObjectName mbean = null;
    try {
      mbean = (ObjectName) Registry.lookup(invocation.getObjectName());

      /** Clustering * */
      long clientViewId = ((Long) invocation.getValue("CLUSTER_VIEW_ID")).longValue();
      HATarget target = (HATarget) beanMap.get(invocation.getObjectName());
      if (target == null) {
        // We could throw IllegalStateException but we have a race condition that could occur:
        // when we undeploy a bean, the cluster takes some time to converge
        // and to recalculate a new viewId and list of replicant for each HATarget.
        // Consequently, a client could own an up-to-date list of the replicants
        // (before the cluster has converged) and try to perform an invocation
        // on this node where the HATarget no more exist, thus receiving a
        // wrong exception and no failover is performed with an IllegalStateException
        //
        throw new GenericClusteringException(
            GenericClusteringException.COMPLETED_NO,
            "target is not/no more registered on this node");
      }

      if (!target.invocationsAllowed()) {
        throw new GenericClusteringException(
            GenericClusteringException.COMPLETED_NO,
            "invocations are currently not allowed on this target");
      }
      /** End Clustering * */

      // The cl on the thread should be set in another interceptor
      Object obj =
          getServer()
              .invoke(mbean, "invoke", new Object[] {invocation}, Invocation.INVOKE_SIGNATURE);

      /** Clustering * */
      HARMIResponse haResponse = new HARMIResponse();

      if (clientViewId != target.getCurrentViewId()) {
        haResponse.newReplicants = new ArrayList(target.getReplicants());
        haResponse.currentViewId = target.getCurrentViewId();
      }
      haResponse.response = obj;

      /** End Clustering * */
      return new MarshalledObject(haResponse);
    } catch (Exception e) {
      Throwable th = JMXExceptionDecoder.decode(e);
      if (log.isTraceEnabled()) {
        log.trace("Failed to invoke on mbean: " + mbean, th);
      }

      if (th instanceof Exception) {
        e = (Exception) th;
      }

      throw e;
    } finally {
      currentThread.setContextClassLoader(oldCl);
      Thread.interrupted(); // clear interruption because this thread may be pooled.
    }
  }