|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * SciJava Common shared library for SciJava software. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2009 - 2013 Board of Regents of the University of |
| 6 | + * Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck |
| 7 | + * Institute of Molecular Cell Biology and Genetics. |
| 8 | + * %% |
| 9 | + * Redistribution and use in source and binary forms, with or without |
| 10 | + * modification, 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 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 15 | + * this list of conditions and the following disclaimer in the documentation |
| 16 | + * and/or other materials provided with the distribution. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 22 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | + * POSSIBILITY OF SUCH DAMAGE. |
| 29 | + * |
| 30 | + * The views and conclusions contained in the software and documentation are |
| 31 | + * those of the authors and should not be interpreted as representing official |
| 32 | + * policies, either expressed or implied, of any organization. |
| 33 | + * #L% |
| 34 | + */ |
| 35 | + |
| 36 | +package org.scijava.annotations; |
| 37 | + |
| 38 | +import java.io.File; |
| 39 | +import java.io.FileOutputStream; |
| 40 | +import java.io.IOException; |
| 41 | +import java.io.InputStream; |
| 42 | +import java.io.OutputStream; |
| 43 | +import java.lang.annotation.Annotation; |
| 44 | +import java.net.URL; |
| 45 | +import java.util.Enumeration; |
| 46 | +import java.util.HashSet; |
| 47 | +import java.util.Set; |
| 48 | + |
| 49 | +import org.scijava.util.Combiner; |
| 50 | +import org.scijava.util.FileUtils; |
| 51 | + |
| 52 | +/** |
| 53 | + * Combines SezPoz annotations from all JAR files on the classpath. |
| 54 | + * |
| 55 | + * @author Curtis Rueden |
| 56 | + */ |
| 57 | +public class AnnotationCombiner extends AbstractIndexWriter implements Combiner |
| 58 | +{ |
| 59 | + |
| 60 | + private static final String PREFIX = "META-INF/json/"; |
| 61 | + private static final String LEGACY_PREFIX = "META-INF/annotations/"; |
| 62 | + |
| 63 | + /** Reads in annotations from all available resources and combines them. */ |
| 64 | + @Override |
| 65 | + public void combine(File outputDirectory) throws Exception { |
| 66 | + if (outputDirectory == null) { |
| 67 | + outputDirectory = new File("src/main/assembly/all"); |
| 68 | + } |
| 69 | + final Set<String> annotationFiles = getAnnotationFiles(); |
| 70 | + |
| 71 | + final ClassLoader loader = Thread.currentThread().getContextClassLoader(); |
| 72 | + |
| 73 | + log(""); |
| 74 | + log("Writing annotations to " + outputDirectory.getAbsolutePath()); |
| 75 | + |
| 76 | + new File(outputDirectory, PREFIX).mkdirs(); |
| 77 | + for (final String annotationFile : annotationFiles) { |
| 78 | + final String annotationName = annotationFile.substring(PREFIX.length()); |
| 79 | + @SuppressWarnings("unchecked") |
| 80 | + final Class<? extends Annotation> annotation = |
| 81 | + (Class<? extends Annotation>) loader.loadClass(annotationName); |
| 82 | + for (IndexItem<? extends Annotation> item : Index |
| 83 | + .load(annotation, loader)) |
| 84 | + { |
| 85 | + add(adapt(item.annotation()), annotationName, item.className()); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + write(new AnnotationStreamFactory(outputDirectory)); |
| 90 | + } |
| 91 | + |
| 92 | + /** Scans for annotations files in every resource on the classpath. */ |
| 93 | + public Set<String> getAnnotationFiles() throws IOException { |
| 94 | + final HashSet<String> files = new HashSet<String>(); |
| 95 | + |
| 96 | + for (final String prefix : new String[] { PREFIX, LEGACY_PREFIX }) { |
| 97 | + final Enumeration<URL> directories = |
| 98 | + Thread.currentThread().getContextClassLoader().getResources(prefix); |
| 99 | + while (directories.hasMoreElements()) { |
| 100 | + final URL url = directories.nextElement(); |
| 101 | + for (final URL annotationIndexURL : FileUtils.listContents(url)) { |
| 102 | + String string = annotationIndexURL.toString(); |
| 103 | + if (string.endsWith("/")) { |
| 104 | + continue; |
| 105 | + } |
| 106 | + final int length = string.length(); |
| 107 | + add(files, PREFIX + |
| 108 | + string.substring(string.lastIndexOf('/', length - 1) + 1, length)); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + return files; |
| 113 | + } |
| 114 | + |
| 115 | + // -- Helper methods -- |
| 116 | + |
| 117 | + private void add(final HashSet<String> set, final String item) { |
| 118 | + log("\t" + item); |
| 119 | + set.add(item); |
| 120 | + } |
| 121 | + |
| 122 | + private void log(final String msg) { |
| 123 | + System.out.println(msg); |
| 124 | + } |
| 125 | + |
| 126 | + // -- Helper Class -- |
| 127 | + |
| 128 | + /** |
| 129 | + * {@link StreamFactory} implementation for writing an annotation. |
| 130 | + * |
| 131 | + */ |
| 132 | + private static class AnnotationStreamFactory implements StreamFactory { |
| 133 | + |
| 134 | + private final File outputDirectory; |
| 135 | + |
| 136 | + public AnnotationStreamFactory(final File outputDirectory) { |
| 137 | + this.outputDirectory = outputDirectory; |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public InputStream openInput(String annotationName) throws IOException { |
| 142 | + return null; |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public OutputStream openOutput(String annotationName) throws IOException { |
| 147 | + final File file = new File(outputDirectory, PREFIX + annotationName); |
| 148 | + return new FileOutputStream(file); |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public boolean isClassObsolete(String className) { |
| 153 | + return false; |
| 154 | + } |
| 155 | + |
| 156 | + } |
| 157 | + |
| 158 | + // -- Main method -- |
| 159 | + |
| 160 | + public static void main(final String[] args) throws Exception { |
| 161 | + new AnnotationCombiner().combine(args.length > 0 ? new File(args[0]) : null); |
| 162 | + } |
| 163 | +} |
0 commit comments