KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > just4log > JustLog


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
51 package net.sf.just4log;
52
53 import java.io.ByteArrayInputStream JavaDoc;
54 import java.io.ByteArrayOutputStream JavaDoc;
55 import java.io.File JavaDoc;
56 import java.io.FileInputStream JavaDoc;
57 import java.io.FileOutputStream JavaDoc;
58 import java.io.IOException JavaDoc;
59 import java.io.InputStream JavaDoc;
60 import java.io.OutputStream JavaDoc;
61 import java.util.zip.ZipEntry JavaDoc;
62 import java.util.zip.ZipInputStream JavaDoc;
63 import java.util.zip.ZipOutputStream JavaDoc;
64
65 import net.sf.just4log.transform.Transform;
66 import net.sf.just4log.util.NotClosingInputStream;
67
68 import org.apache.bcel.Repository;
69 import org.apache.bcel.classfile.ClassFormatException;
70 import org.apache.bcel.classfile.ClassParser;
71 import org.apache.bcel.classfile.JavaClass;
72 import org.apache.bcel.util.ClassPath;
73 import org.apache.bcel.util.SyntheticRepository;
74 import org.apache.commons.logging.Log;
75 import org.apache.commons.logging.LogFactory;
76
77 /**
78  * This is the core class of Just4log. It actually performs all the bytecode transforming.
79  * @author Lucas Bruand
80  * @version $Id: JustLog.java,v 1.11 2003/07/23 23:59:10 lbruand Exp $
81  */

82
83 public class JustLog {
84
85     private static Log logger = LogFactory.getLog(JustLog.class);
86     private static String JavaDoc CLASSID =
87         "$Id: JustLog.java,v 1.11 2003/07/23 23:59:10 lbruand Exp $";
88
89     /**
90      * Constructor private. not used.
91      */

92     private JustLog() {
93         super();
94     }
95
96     public static void setClasspath(String JavaDoc cp) {
97         String JavaDoc extracp =
98             ClassPath.SYSTEM_CLASS_PATH.toString()
99                 + File.pathSeparatorChar
100                 + cp;
101         logger.info("using " + extracp + "as classpath.");
102         System.out.println("using " + extracp + "as classpath.");
103         Repository.setRepository(
104             SyntheticRepository.getInstance(new ClassPath(extracp)));
105     }
106     private static NotClosingInputStream input =
107         new NotClosingInputStream(null);
108     public static void speedup(
109         InputStream JavaDoc in,
110         String JavaDoc filename,
111         OutputStream JavaDoc out)
112         throws ClassFormatException, IOException JavaDoc {
113         input.setInput(in);
114         JavaClass source = (new ClassParser(input, filename)).parse();
115         logger.info("Source size:" + source.getBytes().length);
116         JavaClass target = Transform.speedup(source);
117         logger.info("after optimization: " + target.getBytes().length);
118         byte[] tmp = target.getBytes();
119         out.write(tmp);
120     }
121
122     /**
123      * Optimize a classfile for faster logging.
124      *
125      * @param source A file to read from.
126      * @param target A file to read to.
127      * @throws ClassFormatException The source file is not in the valid JDK1.2 classformat.
128      * @throws IOException An error happened while accessing disks.
129      */

130     public static void speedup(File JavaDoc source, File JavaDoc target)
131         throws ClassFormatException, IOException JavaDoc {
132         FileInputStream JavaDoc in = null;
133         FileOutputStream JavaDoc out = null;
134         byte[] buffer = new byte[BUFFER];
135         int count;
136         try {
137             in = new FileInputStream JavaDoc(source);
138             ByteArrayOutputStream JavaDoc outb = new ByteArrayOutputStream JavaDoc();
139             while ((count = in.read(buffer, 0, BUFFER)) != -1) {
140                 outb.write(buffer, 0, count);
141             }
142             out = new FileOutputStream JavaDoc(target);
143             speedup(
144                 new ByteArrayInputStream JavaDoc(outb.toByteArray()),
145                 source.getName(),
146                 out);
147         } finally {
148             try {
149                 in.close();
150                 out.close();
151             } catch (IOException JavaDoc e) {
152                 logger.warn("problem with IOException " + e);
153             } catch (NullPointerException JavaDoc e) {
154                 logger.warn("problem with Exception " + e);
155             }
156         }
157     }
158     public static final int BUFFER = 4096;
159     public static void speedupZip(InputStream JavaDoc input, OutputStream JavaDoc output)
160         throws IOException JavaDoc {
161         ZipInputStream JavaDoc in = new ZipInputStream JavaDoc(input);
162         ZipOutputStream JavaDoc out = new ZipOutputStream JavaDoc(output);
163         ZipEntry JavaDoc inentry;
164         ZipEntry JavaDoc outentry;
165         int count;
166         byte[] buffer = new byte[BUFFER];
167         while ((inentry = in.getNextEntry()) != null) {
168             outentry = new ZipEntry JavaDoc(inentry.getName());
169             logger.debug("Extra: " + inentry.getExtra());
170             outentry.setExtra(inentry.getExtra());
171             logger.debug("time: " + inentry.getTime());
172             outentry.setTime(inentry.getTime());
173             logger.debug("method: " + inentry.getMethod());
174             outentry.setMethod(inentry.getMethod());
175             logger.debug("comment: " + inentry.getComment());
176             outentry.setComment(inentry.getComment());
177             logger.debug("CRC: " + inentry.getCrc());
178             if (inentry.getCrc() != -1) {
179                 outentry.setCrc(inentry.getCrc());
180             }
181             logger.debug("size: " + inentry.getSize());
182             if (inentry.getSize() != -1) {
183                 outentry.setSize(inentry.getSize());
184             }
185             out.putNextEntry(outentry);
186             if (!inentry.isDirectory()) {
187                 if (inentry.getName().endsWith(".jar")
188                     || inentry.getName().endsWith(".zip")
189                     || inentry.getName().endsWith(".war")
190                     || inentry.getName().endsWith(".ear")) {
191                     speedupZip(in, out);
192                 } else if (inentry.getName().endsWith(".class")) {
193                     speedup(in, inentry.getName(), out);
194                 } else {
195                     while ((count = in.read(buffer, 0, BUFFER)) != -1) {
196                         out.write(buffer, 0, count);
197                     }
198                 }
199
200             }
201             out.closeEntry();
202             in.closeEntry();
203         }
204         out.close();
205         in.close();
206     }
207 }
208
Popular Tags