Package net.sf.just4log.transform

Source Code of net.sf.just4log.transform.Log4jTransform

/*
* ============================================================================
*                   The Apache Software License, Version 1.1
* ============================================================================
*
*    Copyright (C) 2000-2003 Lucas Bruand. All
*    rights reserved.
*
* Redistribution and use in source and binary forms, with or without modifica-
* tion, are permitted provided that the following conditions are met:
*
* 1. Redistributions of  source code must  retain the above copyright  notice,
*    this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
*    this list of conditions and the following disclaimer in the documentation
*    and/or other materials provided with the distribution.
*
* 3. The end-user documentation included with the redistribution, if any, must
*    include  the following  acknowledgment:  "This product includes  software
*    developed  by the  Apache Software Foundation  (http://www.apache.org/)."
*    Alternately, this  acknowledgment may  appear in the software itself,  if
*    and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Just4Log" and  "Apache Software Foundation"  must not be used to
*    endorse  or promote  products derived  from this  software without  prior
*    written permission. For written permission, please contact
*    apache@apache.org.
*
* 5. Products  derived from this software may not  be called "Apache", nor may
*    "Apache" appear  in their name,  without prior written permission  of the
*    Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
* APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
* DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
* ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
* (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software  consists of voluntary contributions made  by many individuals
* on behalf of the  Apache Software Foundation.  For more  information  on the
* Apache Software Foundation, please see <http://www.apache.org/>.
*
*/
package net.sf.just4log.transform;

import java.util.HashMap;

import org.apache.bcel.Constants;
import org.apache.bcel.generic.ConstantPoolGen;
import org.apache.bcel.generic.IFEQ;
import org.apache.bcel.generic.InstructionHandle;
import org.apache.bcel.generic.InstructionList;
import org.apache.bcel.generic.InvokeInstruction;
import org.apache.bcel.generic.MethodGen;
import org.apache.bcel.generic.ObjectType;
import org.apache.bcel.generic.PUSH;
import org.apache.bcel.generic.Type;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* @author Lucas Bruand
*/

public class Log4jTransform extends Transform {

  private static final String INTERFACE = "org.apache.log4j.Logger";
  /* (non-Javadoc)
     * @see net.sf.just4log.transform.Transform#getLogType()
     */
  private static final ObjectType logType = new ObjectType(INTERFACE);
  public ObjectType getLogType() {

    return logType;
  }
  private static final String LEVEL = "org.apache.log4j.Level";
  private static final String PRIORITY = "org.apache.log4j.Priority";
  private static final ObjectType LevelType = new ObjectType(LEVEL);
  private static final ObjectType[] ARGUMENTS_ISENABLEDFOR =
    new ObjectType[] { new ObjectType(PRIORITY)};

  private static Log logger = LogFactory.getLog(Log4jTransform.class);
  private static String CLASSID =
    "$Id: Log4jTransform.java,v 1.8 2003/07/22 23:38:37 lbruand Exp $";

  public static void register() {
    Transform.register(new Log4jTransform());
  }

  /**
   *
   */
  public Log4jTransform() {
    super();
  }

  /* (non-Javadoc)
   * @see net.sf.just4log.transform.Transform#insertFork(org.apache.bcel.generic.InstructionList, org.apache.bcel.generic.InstructionHandle, org.apache.bcel.generic.InstructionHandle, org.apache.bcel.generic.ConstantPoolGen)
   */
  public InstructionHandle insertFork(
    InstructionList il,
    InstructionHandle getStaticHandle,
    InstructionHandle invokeHandle,
    ConstantPoolGen cp) {
    String level =
      getIsEnabled(
        ((InvokeInstruction) invokeHandle.getInstruction()),
        cp);
    if (level == null) {
      // this is not a correct methodname.
      return null;
    }
    logger.info("the level of enabling is: " + level);
    logger.info("Inserting GETSTATIC");
    InstructionHandle insertHandle =
      il.insert(getStaticHandle, getStaticHandle.getInstruction().copy());
    il.insert(
      getStaticHandle,
      instFact.createGetStatic(LEVEL, level, LevelType));

    logger.info("Inserting INVOKE call to isEnabledFor");

    il.insert(
      getStaticHandle,
      instFact.createInvoke(
        INTERFACE,
        "isEnabledFor",
        Type.BOOLEAN,
        ARGUMENTS_ISENABLEDFOR,
        Constants.INVOKEVIRTUAL));
    logger.info("Inserting IFEQ");
    il.insert(getStaticHandle, new IFEQ(invokeHandle.getNext()));
    return insertHandle;
  }
  private static HashMap mapping = new HashMap();
  static {
    mapping.put("debug", "DEBUG");
    mapping.put("error", "ERROR");
    mapping.put("fatal", "FATAL");
    mapping.put("info", "INFO");
    mapping.put("warn", "WARN");
  }
  public String getIsEnabled(
    InvokeInstruction invokeInstruction,
    ConstantPoolGen cp) {
    return (String) mapping.get(invokeInstruction.getMethodName(cp));
  }

  /* (non-Javadoc)
   * @see net.sf.just4log.transform.Transform#getClassname()
   */
  public String getClassname() {
    return INTERFACE;
  }

  /* (non-Javadoc)
   * @see net.sf.just4log.transform.Transform#insertEnter(org.apache.bcel.generic.InstructionList, org.apache.bcel.generic.InstructionHandle, org.apache.bcel.generic.ConstantPoolGen)
   */
  public InstructionHandle insertEnter(
    MethodGen orig,
    InstructionList il,
    InstructionHandle firstInstructionHandle,
    ConstantPoolGen cp) {
      logger.info("Inserting Enter code.");
      InstructionHandle backup = il.insert(
        firstInstructionHandle,
        instFact.createGetStatic(
          clazz.getClassName(),
          loggerAttribute.getName(),
          loggerAttribute.getType()));
       
      il.insert(firstInstructionHandle,
        new PUSH(cp, Transform.ENTER_STRING+ getMethodRepr(orig))
      );
   
   
      il.insert(
        firstInstructionHandle,
            instFact.createInvoke(
              INTERFACE,
              "debug",
              Type.VOID,
              new Type[] {Type.OBJECT},
              Constants.INVOKEVIRTUAL));

      return backup;

  }

  /* (non-Javadoc)
   * @see net.sf.just4log.transform.Transform#insertExit(org.apache.bcel.generic.InstructionList, org.apache.bcel.generic.InstructionHandle, org.apache.bcel.generic.ConstantPoolGen)
   */
  public InstructionHandle insertExit(
    MethodGen orig,
    InstructionList il,
    InstructionHandle returnInstructionHandle,
    ConstantPoolGen cp) {
      logger.info("Inserting Exit code.");
      InstructionHandle backup = il.insert(
      returnInstructionHandle,
        instFact.createGetStatic(
          clazz.getClassName(),
          loggerAttribute.getName(),
          loggerAttribute.getType()));
       
      il.insert(returnInstructionHandle,
        new PUSH(cp, Transform.EXIT_STRING+ getMethodRepr(orig))
      );
   
   
      il.insert(
      returnInstructionHandle,
            instFact.createInvoke(
              INTERFACE,
              "debug",
              Type.VOID,
              new Type[] {Type.OBJECT},
              Constants.INVOKEVIRTUAL));

      return backup;

  }

}
TOP

Related Classes of net.sf.just4log.transform.Log4jTransform

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.