Skip to content

Commit fbafebf

Browse files
committed
Merge branch 'sezpoz-compat' into eclipse
This branch adds backwards-compatibility with the way scijava-common did things previously: it used SezPoz to generate the annotation indexes. We still want to maintain backwards compatibility with those previously generated annotation indexes (mainly so that older artifacts that might still not be updated on one of the ImageJ update sites are still working correctly). Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2 parents 1526660 + f0b106f commit fbafebf

7 files changed

Lines changed: 688 additions & 4 deletions

File tree

src/main/java/org/scijava/annotations/Index.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
import java.net.URL;
3939
import java.util.Enumeration;
4040
import java.util.Iterator;
41+
import java.util.LinkedHashMap;
4142
import java.util.Map;
43+
import java.util.Map.Entry;
4244

4345
/**
4446
* Makes the annotation indexes accessible.
@@ -90,6 +92,7 @@ public static <A extends Annotation> Index<A> load(final Class<A> annotation,
9092
}
9193

9294
static final String INDEX_PREFIX = "META-INF/json/";
95+
private static final String LEGACY_INDEX_PREFIX = "META-INF/annotations/";
9396

9497
private final Class<A> annotation;
9598
private final ClassLoader loader;
@@ -105,8 +108,27 @@ private class IndexItemIterator implements Iterator<IndexItem<A>> {
105108
private IndexReader indexReader;
106109
private IndexItem<A> next;
107110

111+
private Map<String, URL> legacyURLs;
112+
108113
public IndexItemIterator(final Class<A> annotation) {
109114
try {
115+
legacyURLs = new LinkedHashMap<String, URL>();
116+
final Enumeration<URL> legacy =
117+
loader.getResources(LEGACY_INDEX_PREFIX + annotation.getName());
118+
final int legacySuffixLength =
119+
LEGACY_INDEX_PREFIX.length() + annotation.getName().length();
120+
while (legacy.hasMoreElements()) {
121+
final URL url = legacy.nextElement();
122+
final String string = url.toString();
123+
final String key =
124+
string.substring(0, string.length() - legacySuffixLength) +
125+
INDEX_PREFIX + annotation.getName();
126+
legacyURLs.put(key, url);
127+
}
128+
if (legacyURLs.isEmpty()) {
129+
legacyURLs = null;
130+
}
131+
110132
urls = loader.getResources(INDEX_PREFIX + annotation.getName());
111133
readNext();
112134
}
@@ -146,9 +168,18 @@ private IndexReader getNextReader() throws IOException {
146168
return null;
147169
}
148170
else if (!urls.hasMoreElements()) {
171+
if (legacyURLs != null && !legacyURLs.isEmpty()) {
172+
final Entry<String, URL> entry =
173+
legacyURLs.entrySet().iterator().next();
174+
legacyURLs.remove(entry.getKey());
175+
return IndexReader.getLegacyReader(entry.getValue().openStream());
176+
}
149177
return null;
150178
}
151179
final URL url = urls.nextElement();
180+
if (legacyURLs != null) {
181+
legacyURLs.remove(url.toString());
182+
}
152183
return new IndexReader(url.openStream());
153184
}
154185

src/main/java/org/scijava/annotations/IndexReader.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
import java.util.List;
4343
import java.util.Map;
4444

45+
import org.scijava.annotations.legacy.LegacyReader;
46+
4547
/**
4648
* Reads indexed annotations in JSON format.
4749
* <p>
@@ -213,4 +215,24 @@ private void expect(String match) throws IOException {
213215
expect(c);
214216
}
215217
}
218+
219+
private IndexReader() {
220+
this.in = null;
221+
}
222+
223+
static IndexReader getLegacyReader(final InputStream in) throws IOException {
224+
final LegacyReader legacy = new LegacyReader(in);
225+
return new IndexReader() {
226+
227+
@Override
228+
public Object next() throws IOException {
229+
return legacy.readObject();
230+
}
231+
232+
@Override
233+
public void close() throws IOException {
234+
legacy.close();
235+
}
236+
};
237+
}
216238
}

0 commit comments

Comments
 (0)