KickJava   Java API By Example, From Geeks To Geeks.

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


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 LogJDK14Transform extends Transform {
72
73     private static final String JavaDoc INTERFACE = "java.util.logging.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         return logType;
80     }
81     private static final String JavaDoc LEVEL = "java.util.logging.Level";
82
83     private static final ObjectType LevelType = new ObjectType(LEVEL);
84     private static final ObjectType[] ARGUMENTS_ISENABLEDFOR =
85         new ObjectType[] { new ObjectType(LEVEL)};
86
87     private static Log logger = LogFactory.getLog(LogJDK14Transform.class);
88     private static String JavaDoc CLASSID =
89         "$Id: LogJDK14Transform.java,v 1.6 2003/07/23 21:19:12 lbruand Exp $";
90
91     public static void register() {
92         Transform.register(new LogJDK14Transform());
93     }
94
95     /**
96      *
97      */

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

105     public InstructionHandle insertFork(
106         InstructionList il,
107         InstructionHandle getStaticHandle,
108         InstructionHandle invokeHandle,
109         ConstantPoolGen cp) {
110         String JavaDoc level =
111             getIsEnabled(
112                 ((InvokeInstruction) invokeHandle.getInstruction()),
113                 cp);
114         if (level == null) {
115             // this is not a correct methodname.
116
return null;
117         }
118         logger.info("the level of enabling is: " + level);
119         logger.info("Inserting GETSTATIC");
120         InstructionHandle insertHandle =
121             il.insert(getStaticHandle, getStaticHandle.getInstruction().copy());
122         il.insert(
123             getStaticHandle,
124             instFact.createGetStatic(LEVEL, level, LevelType));
125
126         logger.info("Inserting INVOKE call to isLoggable");
127
128         il.insert(
129             getStaticHandle,
130             instFact.createInvoke(
131                 INTERFACE,
132                 "isLoggable",
133                 Type.BOOLEAN,
134                 ARGUMENTS_ISENABLEDFOR,
135                 Constants.INVOKEVIRTUAL));
136         logger.info("Inserting IFEQ");
137         il.insert(getStaticHandle, new IFEQ(invokeHandle.getNext()));
138         return insertHandle;
139     }
140     private static HashMap JavaDoc mapping = new HashMap JavaDoc();
141     static {
142         mapping.put("finest", "FINEST");
143         mapping.put("finer", "FINER");
144         mapping.put("fine", "FINE");
145         mapping.put("config", "CONFIG");
146         mapping.put("info", "INFO");
147         mapping.put("warning", "WARNING");
148         mapping.put("severe", "SEVERE");
149         mapping.put("entering", "FINER");
150         mapping.put("exiting", "FINER");
151         mapping.put("throwing", "FINER");
152     }
153     public String JavaDoc getIsEnabled(
154         InvokeInstruction invokeInstruction,
155         ConstantPoolGen cp) {
156         return (String JavaDoc) mapping.get(invokeInstruction.getMethodName(cp));
157     }
158
159     public String JavaDoc getClassname() {
160         return INTERFACE;
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             
179             il.insert(firstInstructionHandle,
180                 new PUSH(cp, clazz.getClassName())
181             );
182             il.insert(firstInstructionHandle,
183                 new PUSH(cp, getMethodRepr(orig))
184             );
185             
186         
187             il.insert(
188                 firstInstructionHandle,
189                         instFact.createInvoke(
190                             INTERFACE,
191                             "entering",
192                             Type.VOID,
193                             new Type[] {Type.STRING, Type.STRING},
194                             Constants.INVOKEVIRTUAL));
195
196             return backup;
197     }
198
199     /* (non-Javadoc)
200      * @see net.sf.just4log.transform.Transform#insertExit(org.apache.bcel.generic.InstructionList, org.apache.bcel.generic.InstructionHandle, org.apache.bcel.generic.ConstantPoolGen)
201      */

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