/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2008 Bull S.A.S.
 * Contact: jonas-team@ow2.org
 *
 * This library 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 any later version.
 *
 * This library 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 library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.ow2.jonas.ws.cxf.easybeans;

import java.util.logging.Logger;

import org.apache.cxf.common.i18n.Message;
import org.apache.cxf.common.logging.LogUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Exchange;
import org.apache.cxf.message.FaultMode;
import org.apache.cxf.service.invoker.AbstractInvoker;
import org.ow2.easybeans.api.bean.EasyBeansSLSB;
import org.ow2.util.pool.api.Pool;
import org.ow2.util.pool.api.PoolException;

/**
 * EasyBeans invoker that will take an instance from the pool, call the method
 * and then put it back in the pool.
 * @author Florent Benoit
 */
public class EasyBeansCXFInvoker extends AbstractInvoker {

    /**
     * CXF internal exception logger.
     */
    private static final Logger LOG = LogUtils.getL7dLogger(EasyBeansCXFInvoker.class);

    /**
     * the EJB's Pool.
     */
    private Pool<EasyBeansSLSB, Long> pool = null;

    /**
     * Build a new invoker for the given stateless' pool.
     * @param pool the stateless' pool of the factory
     */
    public EasyBeansCXFInvoker(final Pool<EasyBeansSLSB, Long> pool) {
        this.pool = pool;
    }

    /**
     * Creates and returns a service object depending on the scope.
     * @param context the exchange object.
     * @return a given service object.
     */
    @Override
    public Object getServiceObject(final Exchange context) {
        try {
            return pool.get();
        } catch (PoolException e) {
            context.getInMessage().put(FaultMode.class, FaultMode.UNCHECKED_APPLICATION_FAULT);
            throw createFault(e);
        }
    }

    /**
     * Called when the invoker is done with the object.
     * Replace the bean in pool.
     * @param context CXF Exchange
     * @param obj object to be released
     */
    @Override
    public void releaseServiceObject(final Exchange context, final Object obj) {
        try {
            pool.release((EasyBeansSLSB) obj);
        } catch (PoolException e) {
            context.getInMessage().put(FaultMode.class, FaultMode.UNCHECKED_APPLICATION_FAULT);
            throw createFault(e);
        }
    }

    /**
     * Create a {@link Fault} from an {@link Exception}.
     * @param e Exception to be wrapped.
     * @return the new {@link Fault}
     */
    private static Fault createFault(final Exception e) {
        Message msg = new Message("EXCEPTION_INVOKING_OBJECT",
                                  LOG,
                                  e.getMessage());
        return new Fault(msg, e);
    }

}
