Skip to content

Commit

Permalink
Add ant task
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas committed Feb 25, 2011
1 parent 52b7e74 commit 354559d
Show file tree
Hide file tree
Showing 6 changed files with 314 additions and 92 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
.classpath
.project
.settings
.idea
jandex.iml
target
*~
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,12 @@
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.8.1</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
</dependencies>
</project>
108 changes: 108 additions & 0 deletions src/main/java/org/jboss/jandex/JandexAntTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2010, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.jandex;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.FileSet;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
* Ant Task that indexes jars, and saves the resulting index
* @author Stuart Douglas
*/
public class JandexAntTask extends Task {

private final List<FileSet> filesets = new ArrayList<FileSet>();

private boolean modify = false;

private boolean newJar = false;

private boolean verbose = false;

private boolean run = true;

@Override
public void execute() throws BuildException {
if(!run) {
return;
}
if(modify && newJar) {
throw new BuildException("Specifying both modify and newJar does not make sense.");
}
Indexer indexer = new Indexer();
for(FileSet fileset : filesets) {
String[] files = fileset.getDirectoryScanner(getProject()).getIncludedFiles();
for(String file : files) {
if(file.endsWith(".jar")) {
try {
JarIndexer.createJarIndex(new File(fileset.getDir().getAbsolutePath() + "/" +file), indexer, modify, newJar,verbose);
} catch (IOException e) {
throw new BuildException(e);
}
}
}
}

}

public void addFileset(FileSet fileset) {
filesets.add(fileset);
}

public boolean isModify() {
return modify;
}

public void setModify(boolean modify) {
this.modify = modify;
}

public boolean isVerbose() {
return verbose;
}

public void setVerbose(boolean verbose) {
this.verbose = verbose;
}

public boolean isRun() {
return run;
}

public void setRun(boolean run) {
this.run = run;
}

public boolean isNewJar() {
return newJar;
}

public void setNewJar(boolean newJar) {
this.newJar = newJar;
}
}
102 changes: 102 additions & 0 deletions src/main/java/org/jboss/jandex/JarIndexer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package org.jboss.jandex;

import java.io.*;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
*
* Class which contains utility methods to create an index for a jar file
*
* @author Stuart Douglas
*
*/
public class JarIndexer {

/**
* Indexes a jar file and saves the result. If the modify flag is try it is saved META-INF/jandex.idx.
* Otherwies an external file is created with a similar name to the original file, however the
* <code>.jar</code> extension is replaced with <code>-jar.idx</code>
*
* @param jarFile The file to index
* @param indexer The indexer to use
* @param modify If the original jar should be modified
* @param verbose If we should print what we are doing to standard out
*/
public static Result createJarIndex(File jarFile, Indexer indexer, boolean modify, boolean newJar, boolean verbose) throws IOException {
File tmpCopy = null;
ZipOutputStream zo = null;
OutputStream out = null;
File outputFile = null;

JarFile jar = new JarFile(jarFile);

if (modify) {
tmpCopy = File.createTempFile(jarFile.getName().substring(0, jarFile.getName().lastIndexOf('.')), "jmp");
out = zo = new ZipOutputStream(new FileOutputStream(tmpCopy));
} else if (newJar) {
outputFile = new File(jarFile.getAbsolutePath().replace(".jar", "-jandex.jar"));
out = zo = new ZipOutputStream(new FileOutputStream(outputFile));
} else
{
outputFile = new File(jarFile.getAbsolutePath().replace(".jar", "-jar") + ".idx");
out = new FileOutputStream( outputFile);
}

try {
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
if (modify) {
zo.putNextEntry(entry);
copy(jar.getInputStream(entry), zo);
}

if (entry.getName().endsWith(".class")) {
ClassInfo info = indexer.index(jar.getInputStream(entry));
if (verbose && info != null)
printIndexEntryInfo(info);
}
}

if (modify || newJar) {
zo.putNextEntry(new ZipEntry("META-INF/jandex.idx"));
}

IndexWriter writer = new IndexWriter(out);
Index index = indexer.complete();
int bytes = writer.write(index);


if (modify) {
jarFile.delete();
tmpCopy.renameTo(jarFile);
tmpCopy = null;
}
return new Result(index, modify ? "META-INF/jandex.idx" : outputFile.getPath(), bytes);
} finally {
out.flush();
out.close();
if (tmpCopy != null)
tmpCopy.delete();
}
}

private static void printIndexEntryInfo(ClassInfo info) {
System.out.println("Indexed " + info.name() + " (" + info.annotations().size() + " annotations)");
}
private static void copy(InputStream in, OutputStream out) throws IOException {
byte[] buf = new byte[8192];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.flush();
}
private JarIndexer() {

}
}
94 changes: 2 additions & 92 deletions src/main/java/org/jboss/jandex/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,29 +54,6 @@ public static void main(String[] args) {
main.execute(args);
}

private static class Result {
private int annotations;
private int instances;
private int classes;
private int bytes;
private String name;

private Result(Index index, String name, int bytes) {
annotations = index.annotations.size();
instances = countInstances(index);
classes = index.classes.size();
this.bytes = bytes;
this.name = name;
}

private int countInstances(Index index) {
int c = 0;
for (List<AnnotationInstance> list : index.annotations.values())
c += list.size();

return c;
}
}

private void execute(String[] args) {
try {
Expand All @@ -89,9 +66,9 @@ private void execute(String[] args) {

long start = System.currentTimeMillis();
Indexer indexer = new Indexer();
Result result = (source.isDirectory()) ? indexDirectory(source, indexer) : indexJar(source, indexer);
Result result = (source.isDirectory()) ? indexDirectory(source, indexer) : JarIndexer.createJarIndex(source, indexer,modify,verbose);
double time = (System.currentTimeMillis() - start) / 1000.00;
System.out.printf("Wrote %s in %.4f seconds (%d classes, %d annotations, %d instances, %d bytes)\n", result.name, time, result.classes, result.annotations, result.instances, result.bytes);
System.out.printf("Wrote %s in %.4f seconds (%d classes, %d annotations, %d instances, %d bytes)\n", result.getName(), time, result.getClasses(), result.getAnnotations(), result.getInstances(), result.getBytes());
} catch (Exception e) {
if (!verbose && (e instanceof IllegalArgumentException || e instanceof FileNotFoundException)) {
System.err.println(e.getMessage() == null ? e.getClass().getSimpleName() : "ERROR: " + e.getMessage());
Expand Down Expand Up @@ -142,77 +119,10 @@ private Result indexDirectory(File source, Indexer indexer) throws FileNotFoundE
}
}

private Result indexJar(File source, Indexer indexer) throws IOException {
boolean modify = this.modify;
boolean verbose = this.verbose;
File tmpCopy = null;
ZipOutputStream zo = null;
OutputStream out = null;

JarFile jar = new JarFile(source);

if (modify) {
tmpCopy = File.createTempFile(source.getName().substring(0, source.getName().lastIndexOf('.')), "jmp");
out = zo = new ZipOutputStream(new FileOutputStream(tmpCopy));
} else {
if (outputFile == null) {
outputFile = new File(source.getName().replace('.', '-') + ".idx");
}
out = new FileOutputStream(outputFile);
}

try {
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
if (modify) {
zo.putNextEntry(entry);
copy(jar.getInputStream(entry), zo);
}

if (entry.getName().endsWith(".class")) {
ClassInfo info = indexer.index(jar.getInputStream(entry));
if (verbose && info != null)
printIndexEntryInfo(info);
}
}

if (modify) {
zo.putNextEntry(new ZipEntry("META-INF/jandex.idx"));
}

IndexWriter writer = new IndexWriter(out);
Index index = indexer.complete();
int bytes = writer.write(index);


if (modify) {
source.delete();
tmpCopy.renameTo(source);
tmpCopy = null;
}
return new Result(index, modify ? "META-INF/jandex.idx" : outputFile.getPath(), bytes);
} finally {
out.flush();
out.close();
if (tmpCopy != null)
tmpCopy.delete();
}
}

private void printIndexEntryInfo(ClassInfo info) {
System.out.println("Indexed " + info.name() + " (" + info.annotations().size() + " annotations)");
}

private void copy(InputStream in, OutputStream out) throws IOException {
byte[] buf = new byte[8192];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.flush();
}

private void scanFile(File source, Indexer indexer) throws FileNotFoundException, IOException {
if (source.isDirectory()) {
File[] children = source.listFiles();
Expand Down
Loading

0 comments on commit 354559d

Please # to comment.