/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.tm.plugins.tyrex;

import java.lang.reflect.Method;
import java.lang.reflect.InvocationHandler;

import org.omg.CosTransactions.Coordinator;
import org.omg.CosTransactions.Resource;

import java.lang.reflect.Method;
import java.lang.reflect.InvocationHandler;

import java.io.Externalizable;
import java.io.IOException;

import org.apache.log4j.Category;


/**
 *   This is the InvocationHandler for the Proxy we hand over to the
 *   originator's Coordinator to control our subordinate transaction
 *
 *   @see org.omg.CosTransactions.Resource,
 *        java.lang.reflect.Proxy,
 *        java.lang.reflect.InvocationHandler,
 *        ResourceRemote,
 *        CoordinatorRemote
 *   @author <a href="mailto:akkerman@cs.nyu.edu">Anatoly Akkerman</a>
 *   @version $Revision: 1.3.4.1 $
 */

public class ResourceInvoker implements InvocationHandler, Externalizable {

  private static Method prepare;
  private static Method rollback;
  private static Method commit;
  private static Method commit_one_phase;
  private static Method forget;

  static {
    try {
      // get all the methods through which we may get called
      prepare = Resource.class.getMethod("prepare", null);
      rollback = Resource.class.getMethod("rollback", null);
      commit = Resource.class.getMethod("commit", null);
      commit_one_phase = Resource.class.getMethod("commit_one_phase", null);
      forget = Resource.class.getMethod("forget", null);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  private Category log = Category.getInstance(this.getClass());
  private ResourceRemoteInterface remoteResource;

  public ResourceInvoker() {
    // for externalization to work
  }

  public ResourceInvoker(Resource res) {
    try {
      remoteResource = new ResourceRemote(res);
    } catch (Exception e) {
      e.printStackTrace();
      remoteResource = null;
      log.warn("ResourceInvoker did not initialize properly! Ther will be problems with this transaction!");
    }
  }

  public void writeExternal(java.io.ObjectOutput out) throws IOException {
    out.writeObject(remoteResource);
  }

  public void readExternal(java.io.ObjectInput in) throws IOException, ClassNotFoundException {
    this.remoteResource = (ResourceRemoteInterface) in.readObject();
  }

  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

    // Just call the appropriate method on our RemoteCoordinator

    if (method.equals(prepare)) {
      // DEBUG      log.debug("ResourceInvoker: calling prepare()");
      return remoteResource.prepare();
    }
    else if (method.equals(rollback)) {
      // DEBUG      log.debug("ResourceInvoker: calling rollback()");
      remoteResource.rollback();
      return null;
    }
    else if (method.equals(commit)) {
      // DEBUG      log.debug("ResourceInvoker: calling commit()");
      remoteResource.commit();
      return null;
    }
    else if (method.equals(commit_one_phase)) {
      // DEBUG      log.debug("ResourceInvoker: calling commit_one_phase()");
      remoteResource.commit_one_phase();
      return null;
    }
    else if (method.equals(forget)) {
      // DEBUG      log.debug("ResourceInvoker: calling forget()");
      remoteResource.forget();
      return null;
    }
    else {
      throw new Exception("ResourceInvoker: called through an unknown method!");
    }
  }
}
