001: /*
002: * ============================================================================
003: * The Apache Software License, Version 1.1
004: * ============================================================================
005: *
006: * Copyright (C) 2000-2003 Lucas Bruand. All
007: * rights reserved.
008: *
009: * Redistribution and use in source and binary forms, with or without modifica-
010: * tion, are permitted provided that the following conditions are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright notice,
013: * this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright notice,
016: * this list of conditions and the following disclaimer in the documentation
017: * and/or other materials provided with the distribution.
018: *
019: * 3. The end-user documentation included with the redistribution, if any, must
020: * include the following acknowledgment: "This product includes software
021: * developed by the Apache Software Foundation (http://www.apache.org/)."
022: * Alternately, this acknowledgment may appear in the software itself, if
023: * and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. The names "Just4Log" and "Apache Software Foundation" must not be used to
026: * endorse or promote products derived from this software without prior
027: * written permission. For written permission, please contact
028: * apache@apache.org.
029: *
030: * 5. Products derived from this software may not be called "Apache", nor may
031: * "Apache" appear in their name, without prior written permission of the
032: * Apache Software Foundation.
033: *
034: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
035: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
036: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
037: * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
038: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
039: * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
040: * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
041: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
042: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
043: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
044: *
045: * This software consists of voluntary contributions made by many individuals
046: * on behalf of the Apache Software Foundation. For more information on the
047: * Apache Software Foundation, please see <http://www.apache.org/>.
048: *
049: */
050: package net.sf.just4log.transform;
051:
052: import org.apache.bcel.Constants;
053: import org.apache.bcel.generic.ConstantPoolGen;
054: import org.apache.bcel.generic.IFEQ;
055: import org.apache.bcel.generic.INVOKEINTERFACE;
056: import org.apache.bcel.generic.InstructionHandle;
057: import org.apache.bcel.generic.InstructionList;
058: import org.apache.bcel.generic.MethodGen;
059: import org.apache.bcel.generic.ObjectType;
060: import org.apache.bcel.generic.PUSH;
061: import org.apache.bcel.generic.Type;
062: import org.apache.commons.logging.Log;
063: import org.apache.commons.logging.LogFactory;
064:
065: /**
066: * @author Lucas Bruand
067: */
068:
069: public class ApacheCommonTransform extends Transform {
070: private static final String INTERFACE = "org.apache.commons.logging.Log";
071: /* (non-Javadoc)
072: * @see net.sf.just4log.transform.Transform#getLogType()
073: */
074: private static final ObjectType logType = new ObjectType(INTERFACE);
075:
076: public ObjectType getLogType() {
077:
078: return logType;
079: }
080:
081: private static Log logger = LogFactory
082: .getLog(ApacheCommonTransform.class);
083: private static String CLASSID = "$Id: ApacheCommonTransform.java,v 1.6 2003/07/22 23:38:37 lbruand Exp $";
084:
085: public static void register() {
086: Transform.register(new ApacheCommonTransform());
087: }
088:
089: public String getClassname() {
090: return INTERFACE;
091: }
092:
093: /**
094: *
095: */
096: private ApacheCommonTransform() {
097: super ();
098:
099: }
100:
101: public InstructionHandle insertFork(InstructionList il,
102: InstructionHandle getStaticHandle,
103: InstructionHandle invokeInterfaceHandle, ConstantPoolGen cp) {
104: logger.info("Inserting GETSTATIC");
105: InstructionHandle insertHandle = il.insert(getStaticHandle,
106: getStaticHandle.getInstruction().copy());
107: String isEnabled = getIsEnabled(((INVOKEINTERFACE) invokeInterfaceHandle
108: .getInstruction()).getMethodName(cp));
109: logger.info("Inserting INVOKEINTERFACE call to " + isEnabled);
110: il.insert(getStaticHandle, instFact.createInvoke(INTERFACE,
111: isEnabled, Type.BOOLEAN, Type.NO_ARGS,
112: Constants.INVOKEINTERFACE));
113: logger.info("Inserting IFEQ");
114: il.insert(getStaticHandle, new IFEQ(invokeInterfaceHandle
115: .getNext()));
116: return insertHandle;
117: }
118:
119: /**
120: * Obtained the equivalent isXXXXEnabled method to the logmethod provided.
121: * @param originalMethod the methodname. (e.g. "debug", "info", "warn", "error", "fatal")
122: * @return the equivalent isXXXXEnabled method
123: */
124: private static final String getIsEnabled(String originalMethod) {
125: return "is" + originalMethod.substring(0, 1).toUpperCase()
126: + originalMethod.substring(1) + "Enabled";
127: }
128:
129: /* (non-Javadoc)
130: * @see net.sf.just4log.transform.Transform#insertEnter(org.apache.bcel.generic.InstructionList, org.apache.bcel.generic.InstructionHandle, org.apache.bcel.generic.ConstantPoolGen)
131: */
132: public InstructionHandle insertEnter(MethodGen orig,
133: InstructionList il,
134: InstructionHandle firstInstructionHandle, ConstantPoolGen cp) {
135: logger.info("Inserting Enter code.");
136: InstructionHandle backup = il.insert(firstInstructionHandle,
137: instFact.createGetStatic(clazz.getClassName(),
138: loggerAttribute.getName(), loggerAttribute
139: .getType()));
140:
141: il.insert(firstInstructionHandle, new PUSH(cp,
142: Transform.ENTER_STRING + getMethodRepr(orig)));
143:
144: il.insert(firstInstructionHandle, instFact.createInvoke(
145: INTERFACE, "trace", Type.VOID,
146: new Type[] { Type.OBJECT }, Constants.INVOKEINTERFACE));
147:
148: return backup;
149: }
150:
151: /* (non-Javadoc)
152: * @see net.sf.just4log.transform.Transform#insertExit(org.apache.bcel.generic.InstructionList, org.apache.bcel.generic.InstructionHandle, org.apache.bcel.generic.ConstantPoolGen)
153: */
154: public InstructionHandle insertExit(MethodGen orig,
155: InstructionList il,
156: InstructionHandle returnInstructionHandle,
157: ConstantPoolGen cp) {
158: logger.info("Inserting Exit code.");
159: InstructionHandle backup = il.insert(returnInstructionHandle,
160: instFact.createGetStatic(clazz.getClassName(),
161: loggerAttribute.getName(), loggerAttribute
162: .getType()));
163:
164: il.insert(returnInstructionHandle, new PUSH(cp,
165: Transform.EXIT_STRING + getMethodRepr(orig)));
166:
167: il.insert(returnInstructionHandle, instFact.createInvoke(
168: INTERFACE, "trace", Type.VOID,
169: new Type[] { Type.OBJECT }, Constants.INVOKEINTERFACE));
170:
171: return backup;
172: }
173:
174: }
|