/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2008 Bull S.A.S.
 * Contact: jonas-team@ow2.org
 *
 * Application Versioning System
 * Copyright (C) 2008 France Telecom R&D
 *
 * 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 1any 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.web.tomcat6.versioning;

import org.apache.catalina.connector.CoyoteAdapter;
import org.ow2.jonas.asm.MethodAdapter;
import org.ow2.jonas.asm.MethodVisitor;
import org.objectweb.util.monolog.api.BasicLevel;
import org.objectweb.util.monolog.api.Logger;
import org.ow2.jonas.lib.util.Log;

/**
 * ASM transformation for the Tomcat Coyote Adapter.
 *
 * @author Frederic Germaneau
 * @author S. Ali Tokmen
 */
public class ReplaceNewCoyoteAdapter extends MethodAdapter {

    /**
     * Logger.
     */
    private static Logger logger = Log.getLogger(ReplaceNewCoyoteAdapter.class.getName());

    /**
     * Name of the CoyoteAdapter class (ASM format).
     */
    private String coyoteAdapterName = null;

    /**
     * Name of the CoyoteAdapterWithDelegatedContextSearch class (ASM format).
     */
    private String modifiedCoyoteAdapterName = null;

    /**
     * Calls parent.
     *
     * @param mv  MethodVisitor to use?
     */
    public ReplaceNewCoyoteAdapter(final MethodVisitor mv) {
        super(mv);
        coyoteAdapterName = CoyoteAdapter.class.getName().replace('.', '/');
        modifiedCoyoteAdapterName = CoyoteAdapterWithDelegatedContextSearch.class.getName().replace('.', '/');
    }

    /**
     * Changes a "new CoyoteAdapter" into
     * "new CoyoteAdapterWithDelegatedContextSearch".
     *
     * @param opcode  Operation code.
     * @param owner   Owner.
     */
    @Override
    public void visitTypeInsn(final int opcode, String owner) {
        logger.log(BasicLevel.DEBUG, "Inside visitTypeInsn with owner : " + owner);
        if(coyoteAdapterName.equals(owner)) {
            logger.log(BasicLevel.DEBUG, "    Changing TypeInsn for owner: " + owner);
            owner = modifiedCoyoteAdapterName;
        }

        mv.visitTypeInsn(opcode, owner);
    }

    /**
     * Changes a "new CoyoteAdapter" into
     * "new CoyoteAdapterWithDelegatedContextSearch".
     *
     * @param opcode  Operation code.
     * @param owner   Owner.
     */
    @Override
    public void visitMethodInsn(final int opcode, String owner, final String name, final String desc) {
        logger.log(BasicLevel.DEBUG, "Inside visitMethodInsn with owner : " + owner);
        if(coyoteAdapterName.equals(owner)) {
            logger.log(BasicLevel.DEBUG, "    Changing MethodInsn for method: " + name + ", owner " + owner);
            owner = modifiedCoyoteAdapterName;
        }

        mv.visitMethodInsn(opcode, owner, name, desc);
    }
}
