KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.bcel.Constants;
53 import org.apache.bcel.generic.ConstantPoolGen;
54 import org.apache.bcel.generic.IFEQ;
55 import org.apache.bcel.generic.INVOKEINTERFACE;
56 import org.apache.bcel.generic.InstructionHandle;
57 import org.apache.bcel.generic.InstructionList;
58 import org.apache.bcel.generic.MethodGen;
59 import org.apache.bcel.generic.ObjectType;
60 import org.apache.bcel.generic.PUSH;
61 import org.apache.bcel.generic.Type;
62 import org.apache.commons.logging.Log;
63 import org.apache.commons.logging.LogFactory;
64 /**
65  * @author Lucas Bruand
66  */

67
68 public class ApacheCommonTransform extends Transform {
69     private static final String JavaDoc INTERFACE = "org.apache.commons.logging.Log";
70     /* (non-Javadoc)
71      * @see net.sf.just4log.transform.Transform#getLogType()
72      */

73     private static final ObjectType logType = new ObjectType(INTERFACE);
74     public ObjectType getLogType() {
75
76         return logType;
77     }
78     private static Log logger = LogFactory.getLog(ApacheCommonTransform.class);
79     private static String JavaDoc CLASSID =
80         "$Id: ApacheCommonTransform.java,v 1.6 2003/07/22 23:38:37 lbruand Exp $";
81
82     public static void register() {
83         Transform.register(new ApacheCommonTransform());
84     }
85
86     public String JavaDoc getClassname() {
87         return INTERFACE;
88     }
89
90     /**
91      *
92      */

93     private ApacheCommonTransform() {
94         super();
95
96     }
97
98     public InstructionHandle insertFork(
99         InstructionList il,
100         InstructionHandle getStaticHandle,
101         InstructionHandle invokeInterfaceHandle,
102         ConstantPoolGen cp) {
103         logger.info("Inserting GETSTATIC");
104         InstructionHandle insertHandle =
105             il.insert(getStaticHandle, getStaticHandle.getInstruction().copy());
106         String JavaDoc isEnabled =
107             getIsEnabled(
108                 (
109                     (INVOKEINTERFACE) invokeInterfaceHandle
110                         .getInstruction())
111                         .getMethodName(
112                     cp));
113         logger.info("Inserting INVOKEINTERFACE call to " + isEnabled);
114         il.insert(
115             getStaticHandle,
116             instFact.createInvoke(
117                 INTERFACE,
118                 isEnabled,
119                 Type.BOOLEAN,
120                 Type.NO_ARGS,
121                 Constants.INVOKEINTERFACE));
122         logger.info("Inserting IFEQ");
123         il.insert(getStaticHandle, new IFEQ(invokeInterfaceHandle.getNext()));
124         return insertHandle;
125     }
126
127     /**
128              * Obtained the equivalent isXXXXEnabled method to the logmethod provided.
129              * @param originalMethod the methodname. (e.g. "debug", "info", "warn", "error", "fatal")
130              * @return the equivalent isXXXXEnabled method
131              */

132     private static final String JavaDoc getIsEnabled(String JavaDoc originalMethod) {
133         return "is"
134             + originalMethod.substring(0, 1).toUpperCase()
135             + originalMethod.substring(1)
136             + "Enabled";
137     }
138     
139
140     /* (non-Javadoc)
141      * @see net.sf.just4log.transform.Transform#insertEnter(org.apache.bcel.generic.InstructionList, org.apache.bcel.generic.InstructionHandle, org.apache.bcel.generic.ConstantPoolGen)
142      */

143     public InstructionHandle insertEnter(
144         MethodGen orig,
145         InstructionList il,
146         InstructionHandle firstInstructionHandle,
147         ConstantPoolGen cp) {
148         logger.info("Inserting Enter code.");
149         InstructionHandle backup = il.insert(
150             firstInstructionHandle,
151             instFact.createGetStatic(
152                 clazz.getClassName(),
153                 loggerAttribute.getName(),
154                 loggerAttribute.getType()));
155                 
156         il.insert(firstInstructionHandle,
157             new PUSH(cp, Transform.ENTER_STRING+ getMethodRepr(orig))
158         );
159         
160         
161         il.insert(
162             firstInstructionHandle,
163                     instFact.createInvoke(
164                         INTERFACE,
165                         "trace",
166                         Type.VOID,
167                         new Type[] {Type.OBJECT},
168                         Constants.INVOKEINTERFACE));
169
170         return backup;
171     }
172
173     /* (non-Javadoc)
174      * @see net.sf.just4log.transform.Transform#insertExit(org.apache.bcel.generic.InstructionList, org.apache.bcel.generic.InstructionHandle, org.apache.bcel.generic.ConstantPoolGen)
175      */

176     public InstructionHandle insertExit(
177         MethodGen orig,
178         InstructionList il,
179         InstructionHandle returnInstructionHandle,
180         ConstantPoolGen cp) {
181             logger.info("Inserting Exit code.");
182     InstructionHandle backup = il.insert(
183     returnInstructionHandle,
184         instFact.createGetStatic(
185             clazz.getClassName(),
186             loggerAttribute.getName(),
187             loggerAttribute.getType()));
188                 
189     il.insert(returnInstructionHandle,
190         new PUSH(cp, Transform.EXIT_STRING+ getMethodRepr(orig))
191     );
192         
193         
194     il.insert(
195     returnInstructionHandle,
196                 instFact.createInvoke(
197                     INTERFACE,
198                     "trace",
199                     Type.VOID,
200                     new Type[] {Type.OBJECT},
201                     Constants.INVOKEINTERFACE));
202
203     return backup;
204     }
205
206 }
207
Popular Tags