KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > just4log > integration > ant > Just4LogTask


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.integration.ant;
52
53 import java.io.File JavaDoc;
54 import java.io.FileInputStream JavaDoc;
55 import java.io.FileNotFoundException JavaDoc;
56 import java.io.FileOutputStream JavaDoc;
57 import java.io.IOException JavaDoc;
58
59 import net.sf.just4log.JustLog;
60 import net.sf.just4log.transform.Transform;
61
62 import org.apache.bcel.classfile.ClassFormatException;
63 import org.apache.tools.ant.BuildException;
64 import org.apache.tools.ant.DirectoryScanner;
65 import org.apache.tools.ant.Project;
66 import org.apache.tools.ant.taskdefs.MatchingTask;
67 import org.apache.tools.ant.types.Path;
68 import org.apache.tools.ant.types.Reference;
69
70 /**
71  * @author Lucas Bruand
72  * @version $Id: Just4LogTask.java,v 1.8 2003/10/20 17:32:12 lbruand Exp $
73  */

74
75 public class Just4LogTask extends MatchingTask {
76     private static String JavaDoc CLASSID = "$Id: Just4LogTask.java,v 1.8 2003/10/20 17:32:12 lbruand Exp $";
77     /**
78      *
79      */

80     public Just4LogTask() {
81         super();
82     }
83     private Path src;
84     public Path createSrc() {
85         if (src == null) {
86             src = new Path(getProject());
87         }
88         return src.createPath();
89     }
90
91     public void setEnters(boolean level) {
92         Transform.level_enters = level ? Transform.SIMPLEENTER : Transform.NOENTER;
93     }
94     
95     public boolean getEnters() {
96         return (Transform.level_enters == Transform.SIMPLEENTER);
97     }
98     
99     public void setExits(boolean level) {
100         Transform.level_exits = level ? Transform.SIMPLEEXIT : Transform.NOEXIT;
101     }
102     
103     public boolean getExits() {
104         return (Transform.level_exits == Transform.SIMPLEEXIT);
105     }
106     
107     
108     private Path compileClasspath;
109     /**
110      * Set the classpath to be used for this compilation.
111      *
112      * @param classpath an Ant Path object containing the compilation classpath.
113      */

114     public void setClasspath(Path classpath) {
115         if (compileClasspath == null) {
116             compileClasspath = classpath;
117         } else {
118             compileClasspath.append(classpath);
119         }
120     }
121     
122     /** Gets the classpath to be used for this compilation. */
123     public Path getClasspath() {
124         return compileClasspath;
125     }
126     
127     /**
128      * Adds a path to the classpath.
129      */

130     public Path createClasspath() {
131         if (compileClasspath == null) {
132             compileClasspath = new Path(getProject());
133         }
134         return compileClasspath.createPath();
135     }
136
137     /**
138      * Adds a reference to a classpath defined elsewhere.
139      */

140     public void setClasspathRef(Reference r) {
141         createClasspath().setRefid(r);
142     }
143
144
145     private File JavaDoc destDir;
146
147     /**
148      * @param file
149      */

150     public void setDestDir(File JavaDoc file) {
151         destDir = file;
152     }
153     /**
154      * @return
155      */

156     public File JavaDoc getDestDir() {
157         return destDir;
158     }
159     /**
160      * Check that all required attributes have been set and nothing
161      * silly has been entered.
162      *
163      * @since Ant 1.5
164      */

165     protected void checkParameters() throws BuildException {
166         if (destDir != null && !destDir.isDirectory()) {
167             throw new BuildException(
168                 "destination directory \""
169                     + destDir
170                     + "\" does not exist "
171                     + "or is not a directory",
172                 getLocation());
173         }
174     }
175
176     /* (non-Javadoc)
177      * @see org.apache.tools.ant.Task#execute()
178      */

179     public void execute() throws BuildException {
180         super.execute();
181         checkParameters();
182         // Setting the classpath.
183
if (compileClasspath != null) {
184             JustLog.setClasspath(compileClasspath.toString());
185         }
186         String JavaDoc[] list = src.list();
187         for (int i = 0; i < list.length; i++) {
188             File JavaDoc srcDir = getProject().resolveFile(list[i]);
189             if (!srcDir.exists()) {
190                 throw new BuildException(
191                     "srcdir \"" + srcDir.getPath() + "\" does not exist!",
192                     getLocation());
193             }
194
195             DirectoryScanner ds = this.getDirectoryScanner(srcDir);
196             String JavaDoc[] files = ds.getIncludedFiles();
197             int j;
198             for (j = 0; j < files.length; j++) {
199                 String JavaDoc one = files[j];
200
201                 File JavaDoc source = new File JavaDoc(srcDir, one);
202                 File JavaDoc destination = new File JavaDoc(getDestDir(), one);
203                 if (!destination.getParentFile().exists()) {
204                     log("Making dir: "+destination.getParentFile(), Project.MSG_VERBOSE);
205                     destination.getParentFile().mkdirs();
206                 }
207                 
208         
209                 if (source.lastModified() <= destination.lastModified()) {
210                     log(source+" omitted as "+destination+" is up to date", Project.MSG_VERBOSE);
211                     continue;
212                 }
213                 if (one.endsWith(".class")) {
214                     try {
215                         log("source: "+source + " to "+destination, Project.MSG_VERBOSE);
216                         JustLog.speedup(source, destination);
217                         
218                         log("Payload: " + 100*(destination.length()-source.length())/source.length()+ "%");
219                     } catch (ClassFormatException e) {
220                         throw new BuildException(one + " is not a class file", e);
221                     } catch (IOException JavaDoc e) {
222                         throw new BuildException(one + " has an IO problem", e);
223                     }
224                 }
225                 if (one.endsWith(".jar")
226                     || one.endsWith(".ear")
227                     || one.endsWith(".zip")
228                     || one.endsWith(".war")) {
229                     try {
230                         JustLog.speedupZip(new FileInputStream JavaDoc(source), new FileOutputStream JavaDoc(destination));
231                     } catch (FileNotFoundException JavaDoc e) {
232                         throw new BuildException(one + " is not found", e);
233                     } catch (IOException JavaDoc e) {
234                         StackTraceElement JavaDoc[] st=e.getStackTrace();
235                         StringBuffer JavaDoc sb= new StringBuffer JavaDoc();
236                         for(int h=0; h<st.length; h++) {
237                             sb.append(st[h].toString());
238                             sb.append("\n");
239                         }
240                         throw new BuildException(one + " has an IO problem: "+sb.toString(), e);
241                     }
242                 }
243             }
244
245         }
246
247     }
248
249 }
250
Popular Tags