KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > just4log > transform > Log4jTransform


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

50 package net.sf.just4log.transform;
51
52 import java.util.HashMap JavaDoc;
53
54 import org.apache.bcel.Constants;
55 import org.apache.bcel.generic.ConstantPoolGen;
56 import org.apache.bcel.generic.IFEQ;
57 import org.apache.bcel.generic.InstructionHandle;
58 import org.apache.bcel.generic.InstructionList;
59 import org.apache.bcel.generic.InvokeInstruction;
60 import org.apache.bcel.generic.MethodGen;
61 import org.apache.bcel.generic.ObjectType;
62 import org.apache.bcel.generic.PUSH;
63 import org.apache.bcel.generic.Type;
64 import org.apache.commons.logging.Log;
65 import org.apache.commons.logging.LogFactory;
66
67 /**
68  * @author Lucas Bruand
69  */

70
71 public class Log4jTransform extends Transform {
72
73     private static final String JavaDoc INTERFACE = "org.apache.log4j.Logger";
74     /* (non-Javadoc)
75          * @see net.sf.just4log.transform.Transform#getLogType()
76          */

77     private static final ObjectType logType = new ObjectType(INTERFACE);
78     public ObjectType getLogType() {
79
80         return logType;
81     }
82     private static final String JavaDoc LEVEL = "org.apache.log4j.Level";
83     private static final String JavaDoc PRIORITY = "org.apache.log4j.Priority";
84     private static final ObjectType LevelType = new ObjectType(LEVEL);
85     private static final ObjectType[] ARGUMENTS_ISENABLEDFOR =
86         new ObjectType[] { new ObjectType(PRIORITY)};
87
88     private static Log logger = LogFactory.getLog(Log4jTransform.class);
89     private static String JavaDoc CLASSID =
90         "$Id: Log4jTransform.java,v 1.8 2003/07/22 23:38:37 lbruand Exp $";
91
92     public static void register() {
93         Transform.register(new Log4jTransform());
94     }
95
96     /**
97      *
98      */

99     public Log4jTransform() {
100         super();
101     }
102
103     /* (non-Javadoc)
104      * @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)
105      */

106     public InstructionHandle insertFork(
107         InstructionList il,
108         InstructionHandle getStaticHandle,
109         InstructionHandle invokeHandle,
110         ConstantPoolGen cp) {
111         String JavaDoc level =
112             getIsEnabled(
113                 ((InvokeInstruction) invokeHandle.getInstruction()),
114                 cp);
115         if (level == null) {
116             // this is not a correct methodname.
117
return null;
118         }
119         logger.info("the level of enabling is: " + level);
120         logger.info("Inserting GETSTATIC");
121         InstructionHandle insertHandle =
122             il.insert(getStaticHandle, getStaticHandle.getInstruction().copy());
123         il.insert(
124             getStaticHandle,
125             instFact.createGetStatic(LEVEL, level, LevelType));
126
127         logger.info("Inserting INVOKE call to isEnabledFor");
128
129         il.insert(
130             getStaticHandle,
131             instFact.createInvoke(
132                 INTERFACE,
133                 "isEnabledFor",
134                 Type.BOOLEAN,
135                 ARGUMENTS_ISENABLEDFOR,
136                 Constants.INVOKEVIRTUAL));
137         logger.info("Inserting IFEQ");
138         il.insert(getStaticHandle, new IFEQ(invokeHandle.getNext()));
139         return insertHandle;
140     }
141     private static HashMap JavaDoc mapping = new HashMap JavaDoc();
142     static {
143         mapping.put("debug", "DEBUG");
144         mapping.put("error", "ERROR");
145         mapping.put("fatal", "FATAL");
146         mapping.put("info", "INFO");
147         mapping.put("warn", "WARN");
148     }
149     public String JavaDoc getIsEnabled(
150         InvokeInstruction invokeInstruction,
151         ConstantPoolGen cp) {
152         return (String JavaDoc) mapping.get(invokeInstruction.getMethodName(cp));
153     }
154
155     /* (non-Javadoc)
156      * @see net.sf.just4log.transform.Transform#getClassname()
157      */

158     public String JavaDoc getClassname() {
159         return INTERFACE;
160     }
161
162     /* (non-Javadoc)
163      * @see net.sf.just4log.transform.Transform#insertEnter(org.apache.bcel.generic.InstructionList, org.apache.bcel.generic.InstructionHandle, org.apache.bcel.generic.ConstantPoolGen)
164      */

165     public InstructionHandle insertEnter(
166         MethodGen orig,
167         InstructionList il,
168         InstructionHandle firstInstructionHandle,
169         ConstantPoolGen cp) {
170             logger.info("Inserting Enter code.");
171             InstructionHandle backup = il.insert(
172                 firstInstructionHandle,
173                 instFact.createGetStatic(
174                     clazz.getClassName(),
175                     loggerAttribute.getName(),
176                     loggerAttribute.getType()));
177                 
178             il.insert(firstInstructionHandle,
179                 new PUSH(cp, Transform.ENTER_STRING+ getMethodRepr(orig))
180             );
181         
182         
183             il.insert(
184                 firstInstructionHandle,
185                         instFact.createInvoke(
186                             INTERFACE,
187                             "debug",
188                             Type.VOID,
189                             new Type[] {Type.OBJECT},
190                             Constants.INVOKEVIRTUAL));
191
192             return backup;
193
194     }
195
196     /* (non-Javadoc)
197      * @see net.sf.just4log.transform.Transform#insertExit(org.apache.bcel.generic.InstructionList, org.apache.bcel.generic.InstructionHandle, org.apache.bcel.generic.ConstantPoolGen)
198      */

199     public InstructionHandle insertExit(
200         MethodGen orig,
201         InstructionList il,
202         InstructionHandle returnInstructionHandle,
203         ConstantPoolGen cp) {
204             logger.info("Inserting Exit code.");
205             InstructionHandle backup = il.insert(
206             returnInstructionHandle,
207                 instFact.createGetStatic(
208                     clazz.getClassName(),
209                     loggerAttribute.getName(),
210                     loggerAttribute.getType()));
211                 
212             il.insert(returnInstructionHandle,
213                 new PUSH(cp, Transform.EXIT_STRING+ getMethodRepr(orig))
214             );
215         
216         
217             il.insert(
218             returnInstructionHandle,
219                         instFact.createInvoke(
220                             INTERFACE,
221                             "debug",
222                             Type.VOID,
223                             new Type[] {Type.OBJECT},
224                             Constants.INVOKEVIRTUAL));
225
226             return backup;
227
228     }
229
230 }
231
Popular Tags