/*
 * 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.resource.connectionmanager;

import javax.management.ObjectName;
import javax.transaction.xa.XAException;

import org.jboss.logging.Logger;
import org.jboss.system.ServiceMBeanSupport;
import org.jboss.tm.XAExceptionFormatter;

/**
 * JBossLocalXAExceptionFormatter
 *
 * @author <a href="mailto:igorfie at yahoo dot com">Igor Fedorenko</a>.
 * @author <a href="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
 * @author <a href="mailto:adrian@jboss.com">Adrian Brock</a>
 * @version $Revision: 1.1.4.3 $
 */
public class JBossLocalXAExceptionFormatter extends ServiceMBeanSupport
      implements
         XAExceptionFormatter,
         JBossLocalXAExceptionFormatterMBean
{

   private ObjectName transactionManagerService;

   public JBossLocalXAExceptionFormatter()
   {
   }

   /**
    * mbean get-set pair for field transactionManagerService
    * Get the value of transactionManagerService
    * @return value of transactionManagerService
    *
    * @jmx:managed-attribute
    */
   public ObjectName getTransactionManagerService()
   {
      return transactionManagerService;
   }

   /**
    * The <code>setTransactionManagerService</code> method 
    *
    * @param transactionManagerService an <code>ObjectName</code> value
    *
    * @jmx.managed-attribute
    */
   public void setTransactionManagerService(ObjectName transactionManagerService)
   {
      this.transactionManagerService = transactionManagerService;
   }

   protected void startService() throws Exception
   {

      getServer().invoke(transactionManagerService, "registerXAExceptionFormatter", new Object[]
      {JBossLocalXAException.class, this}, new String[]
      {Class.class.getName(), XAExceptionFormatter.class.getName()});
   }

   protected void stopService() throws Exception
   {
      getServer().invoke(transactionManagerService, "unregisterXAExceptionFormatter", new Object[]
      {JBossLocalXAException.class}, new String[]
      {Class.class.getName()});

   }

   public void formatXAException(XAException xae, Logger log)
   {
      try
      {
         log.warn("JBoss Local XA wrapper error: ", ((JBossLocalXAException) xae).getCause());
      }
      catch (Exception e)
      {
         log.info("Problem trying to format XAException: ", e);
      } // end of try-catch

   }

}// JBossLocalXAExceptionFormatter
