|
| 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.FileUtils; |
| 50 | + |
| 51 | +/** |
| 52 | + * Combines SezPoz annotations from all JAR files on the classpath. |
| 53 | + * |
| 54 | + * @author Curtis Rueden |
| 55 | + */ |
| 56 | +public class CombineAnnotations extends AbstractIndexWriter { |
| 57 | + |
| 58 | + private static final String PREFIX = "META-INF/json/"; |
| 59 | + private static final String LEGACY_PREFIX = "META-INF/annotations/"; |
| 60 | + private final String OUTPUT_DIR; |
| 61 | + |
| 62 | + private final Set<String> annotationFiles; |
| 63 | + |
| 64 | + public CombineAnnotations() throws IOException { |
| 65 | + this(null); |
| 66 | + } |
| 67 | + |
| 68 | + public CombineAnnotations(final String outputDir) throws IOException { |
| 69 | + if (outputDir != null) { |
| 70 | + OUTPUT_DIR = outputDir; |
| 71 | + } else { |
| 72 | + OUTPUT_DIR = "src/main/assembly/all"; |
| 73 | + } |
| 74 | + |
| 75 | + annotationFiles = getAnnotationFiles(); |
| 76 | + } |
| 77 | + |
| 78 | + /** Reads in annotations from all available resources and combines them. */ |
| 79 | + public void combine() throws IOException, ClassNotFoundException { |
| 80 | + final ClassLoader loader = Thread.currentThread().getContextClassLoader(); |
| 81 | + |
| 82 | + log(""); |
| 83 | + log("Writing annotations to " + new File(OUTPUT_DIR).getAbsolutePath()); |
| 84 | + |
| 85 | + new File(OUTPUT_DIR, PREFIX).mkdirs(); |
| 86 | + for (final String annotationFile : annotationFiles) { |
| 87 | + final String annotationName = annotationFile.substring(PREFIX.length()); |
| 88 | + @SuppressWarnings("unchecked") |
| 89 | + final Class<? extends Annotation> annotation = |
| 90 | + (Class<? extends Annotation>) loader.loadClass(annotationName); |
| 91 | + for (IndexItem<? extends Annotation> item : Index.load(annotation, loader)) { |
| 92 | + add(adapt(item.annotation()), annotationName, item.className()); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + write(new StreamFactory() { |
| 97 | + |
| 98 | + @Override |
| 99 | + public InputStream openInput(String annotationName) |
| 100 | + throws IOException { |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public OutputStream openOutput(String annotationName) |
| 106 | + throws IOException { |
| 107 | + final File file = new File(OUTPUT_DIR, PREFIX + annotationName); |
| 108 | + return new FileOutputStream(file); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public boolean isClassObsolete(String className) { |
| 113 | + return false; |
| 114 | + } |
| 115 | + |
| 116 | + }); |
| 117 | + } |
| 118 | + |
| 119 | + /** Scans for annotations files in every resource on the classpath. */ |
| 120 | + public Set<String> getAnnotationFiles() throws IOException { |
| 121 | + final HashSet<String> files = new HashSet<String>(); |
| 122 | + |
| 123 | + for (final String prefix : new String[] { PREFIX, LEGACY_PREFIX }) { |
| 124 | + final Enumeration<URL> directories = Thread.currentThread() |
| 125 | + .getContextClassLoader().getResources(prefix); |
| 126 | + while (directories.hasMoreElements()) { |
| 127 | + final URL url = directories.nextElement(); |
| 128 | + for (final URL annotationIndexURL : FileUtils.listContents(url)) { |
| 129 | + String string = annotationIndexURL.toString(); |
| 130 | + if (string.endsWith("/")) { |
| 131 | + continue; |
| 132 | + } |
| 133 | + final int length = string.length(); |
| 134 | + add(files, PREFIX + string.substring( |
| 135 | + string.lastIndexOf('/', length - 1) + 1, length)); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + return files; |
| 140 | + } |
| 141 | + |
| 142 | + public static void main(final String[] args) throws Exception { |
| 143 | + new CombineAnnotations(args.length > 0 ? args[0] : null).combine(); |
| 144 | + } |
| 145 | + |
| 146 | + // -- Helper methods -- |
| 147 | + |
| 148 | + private void add(final HashSet<String> set, final String item) { |
| 149 | + log("\t" + item); |
| 150 | + set.add(item); |
| 151 | + } |
| 152 | + |
| 153 | + private void log(final String msg) { |
| 154 | + System.out.println(msg); |
| 155 | + } |
| 156 | + |
| 157 | +} |
0 commit comments