KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > attributes > compiler > AttributeIndexer


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.attributes.compiler;
17
18 import java.io.BufferedInputStream;
19 import java.io.BufferedOutputStream;
20 import java.io.File;
21 import java.io.FileOutputStream;
22 import java.io.InputStream;
23 import java.io.PrintWriter;
24 import java.io.PrintStream;
25 import java.net.URL;
26 import java.net.URLClassLoader;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.Enumeration;
30 import java.util.HashMap;
31 import java.util.HashSet;
32 import java.util.Iterator;
33 import java.util.List;
34 import java.util.jar.JarFile;
35 import java.util.jar.JarEntry;
36 import java.util.jar.JarOutputStream;
37
38 import org.apache.commons.attributes.AttributeRepositoryClass;
39 import org.apache.commons.attributes.Attributes;
40 import org.apache.commons.attributes.AttributeUtil;
41 import org.apache.commons.attributes.Indexed;
42 import org.apache.tools.ant.AntClassLoader;
43 import org.apache.tools.ant.BuildException;
44 import org.apache.tools.ant.Task;
45 import org.apache.tools.ant.types.FileSet;
46 import org.apache.tools.ant.types.Path;
47
48 /**
49  * Ant task to compile attribute indexes. Usage:
50  *
51  * <pre><code>
52  * &lt;taskdef resource="org/apache/commons/attributes/anttasks.properties"/&gt;
53  *
54  * &lt;attribute-indexer jarFile="myclasses.jar"&gt;
55  * &lt;classpath&gt;
56  * ...
57  * &lt;/classpath&gt;
58  * &lt;/attribute-indexer&gt;
59  * </code></pre>
60  *
61  * The task will inspect the classes in the given jar and add a <tt>META-INF/attrs.index</tt>
62  * file to it, which contains the index information. The classpath element is required and
63  * must contain all dependencies for the attributes used.
64  */

65 public class AttributeIndexer extends Task {
66     
67     private File jarFile;
68     private List classes = new ArrayList ();
69     private Path classPath;
70     private File baseName;
71     private boolean inMaven = false;
72     
73     private final static String INDEX_FILENAME = "META-INF/attrs.index";
74     
75     public AttributeIndexer () {
76     }
77     
78     public void setJarfile (File jarFile) {
79         this.jarFile = jarFile;
80     }
81     
82     public void setBaseName (File baseName) {
83         inMaven = true;
84         this.baseName = baseName;
85     }
86     
87     public Path createClasspath () {
88         this.classPath = new Path(project);
89         return classPath;
90     }
91     
92     private static final String SUFFIX = "$__attributeRepository.class";
93     
94     protected void copyEntry (JarFile jar, JarEntry entry, JarOutputStream outputStream) throws Exception {
95         outputStream.putNextEntry (entry);
96         
97         if (!entry.isDirectory ()) {
98             InputStream is = new BufferedInputStream (jar.getInputStream (entry));
99             try {
100                 byte[] buffer = new byte[16384];
101                 while (true) {
102                     int numRead = is.read (buffer, 0, 16384);
103                     if (numRead == 0 || numRead == -1) {
104                         break;
105                     }
106                     
107                     outputStream.write (buffer, 0, numRead);
108                 }
109             } finally {
110                 is.close ();
111             }
112         }
113     }
114     
115     protected void findJarFile () throws BuildException {
116         File[] allFiles = baseName.getParentFile ().listFiles ();
117         if (allFiles == null) {
118             throw new BuildException ("Unable to find any file with base name " + baseName.getName ()
119                 + " in " + baseName.getParentFile ().getPath ());
120         }
121         
122         long newestDate = 0;
123         for (int i = 0; i < allFiles.length; i++) {
124             String name = allFiles[i].getName ();
125             if (name.startsWith (baseName.getName ()) && name.endsWith (".jar") &&
126                 allFiles[i].lastModified () > newestDate) {
127                 jarFile = allFiles[i];
128                 newestDate = allFiles[i].lastModified ();
129             }
130         }
131         
132         if (jarFile == null) {
133             throw new BuildException ("Unable to find any file with base name " + baseName.getName ()
134                 + " in " + baseName.getParentFile ().getPath ());
135         }
136     }
137     
138     public void execute () throws BuildException {
139         if (inMaven) {
140             findJarFile ();
141         }
142         if (!jarFile.exists ()) {
143             log ("Can't find " + jarFile.getPath ());
144             return;
145         }
146         try {
147             log ("Creating attribute index for " + jarFile.getPath ());
148             
149             JarFile jar = new JarFile (jarFile);
150             File newJarFile = new File (jarFile.getPath () + ".new");
151             JarOutputStream output = new JarOutputStream (new FileOutputStream (newJarFile));
152             try {
153                 Enumeration enum = jar.entries ();
154                 while (enum.hasMoreElements ()) {
155                     JarEntry entry = (JarEntry) enum.nextElement ();
156                     if (!entry.isDirectory ()) {
157                         String className = entry.getName ();
158                         if (className.endsWith (SUFFIX)) {
159                             className = className.replace ('/', '.').replace ('\\', '.').substring (0, className.length () - SUFFIX.length ());
160                             classes.add (className);
161                         }
162                     }
163                     
164                     if (!entry.getName ().equals (INDEX_FILENAME)) {
165                         copyEntry (jar, entry, output);
166                     }
167                 }
168                 
169                 output.putNextEntry (new JarEntry (INDEX_FILENAME));
170                 
171                 Iterator attrs = classes.iterator ();
172                 while (attrs.hasNext ()) {
173                     String className = (String) attrs.next ();
174                     output.write (("Class: " + className + "\n").getBytes ());
175                 }
176             } finally {
177                 output.close ();
178                 jar.close ();
179             }
180                         
181             jarFile.delete ();
182             newJarFile.renameTo (jarFile);
183         } catch (Exception e) {
184             e.printStackTrace ();
185             throw new BuildException (e.toString ());
186         }
187     }
188 }
Popular Tags