-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved reader/writer methods into separate classes
- Loading branch information
1 parent
e7ab6c3
commit c5039d0
Showing
6 changed files
with
344 additions
and
143 deletions.
There are no files selected for viewing
164 changes: 164 additions & 0 deletions
164
src/main/java/org/fuin/utils4j/JandexIndexFileReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
package org.fuin.utils4j; | ||
|
||
import org.jboss.jandex.CompositeIndex; | ||
import org.jboss.jandex.IndexReader; | ||
import org.jboss.jandex.IndexView; | ||
|
||
import java.io.*; | ||
import java.net.URL; | ||
import java.util.*; | ||
|
||
/** | ||
* Helps to read Jandex index files. | ||
*/ | ||
public final class JandexIndexFileReader { | ||
|
||
private final List<File> files; | ||
|
||
private final List<String> resources; | ||
|
||
private JandexIndexFileReader() { | ||
this.files = new ArrayList<>(); | ||
this.resources = new ArrayList<>(); | ||
} | ||
|
||
/** | ||
* Loads the configured index files. | ||
* | ||
* @return Index created from all index files. | ||
* @throws IOException Failed to read index files or resources. | ||
*/ | ||
public IndexView load() throws IOException { | ||
final List<IndexView> indexes = new ArrayList<>(); | ||
for (final File file : files) { | ||
indexes.add(loadFile(file)); | ||
} | ||
for (final String resource : resources) { | ||
indexes.add(loadResources(resource)); | ||
} | ||
return CompositeIndex.create(indexes); | ||
} | ||
|
||
/** | ||
* Loads the configured index files. | ||
* Wraps the possible {@link IOException} into a {@link RuntimeException}. | ||
* | ||
* @return Index created from all index files. | ||
*/ | ||
public IndexView loadR() { | ||
try { | ||
return load(); | ||
} catch (final IOException ex) { | ||
throw new RuntimeException("Failed to read index files or resources", ex); | ||
} | ||
} | ||
|
||
private IndexView loadFile(final File file) throws IOException { | ||
try (final InputStream input = new FileInputStream(file)) { | ||
return new IndexReader(input).read(); | ||
} | ||
} | ||
|
||
private IndexView loadResources(final String indexFilePathAndName) throws IOException { | ||
final Enumeration<URL> enu = Thread.currentThread().getContextClassLoader().getResources(indexFilePathAndName); | ||
final List<IndexView> indexes = new ArrayList<>(); | ||
final List<URL> resources = Collections.list(enu); | ||
for (final URL url : resources) { | ||
try (final InputStream input = url.openStream()) { | ||
indexes.add(new IndexReader(input).read()); | ||
} | ||
} | ||
return CompositeIndex.create(indexes); | ||
} | ||
|
||
/** | ||
* Creates a new instance of the outer class. | ||
*/ | ||
public static final class Builder { | ||
|
||
private JandexIndexFileReader delegate; | ||
|
||
/** | ||
* Default constructor. | ||
*/ | ||
public Builder() { | ||
this.delegate = new JandexIndexFileReader(); | ||
} | ||
|
||
/** | ||
* Adds one or more files. | ||
* | ||
* @param files Index files to read (must exist). | ||
* @return Builder. | ||
*/ | ||
public Builder addFiles(final File...files) { | ||
return addFiles(Arrays.asList(files)); | ||
} | ||
|
||
/** | ||
* Adds one or more files. | ||
* | ||
* @param files Index files to read (must exist). | ||
* @return Builder. | ||
*/ | ||
public Builder addFiles(final List<File> files) { | ||
for (final File file : Objects.requireNonNull(files, "files==null")) { | ||
if (!file.exists()) { | ||
throw new IllegalStateException("Index file does not exist: " + file); | ||
} | ||
if (!file.isFile()) { | ||
throw new IllegalStateException("No file: " + file); | ||
} | ||
delegate.files.add(Objects.requireNonNull(file, "file==null")); | ||
} | ||
return this; | ||
} | ||
|
||
/** | ||
* Adds a default resource named "META-INF/jandex.idx". | ||
* | ||
* @return Builder. | ||
*/ | ||
public Builder addDefaultResource() { | ||
addResources("META-INF/jandex.idx"); | ||
return this; | ||
} | ||
|
||
/** | ||
* Adds one or more resources. | ||
* | ||
* @return Builder. | ||
*/ | ||
public Builder addResources(final String...resources) { | ||
return addResources(Arrays.asList(resources)); | ||
} | ||
|
||
/** | ||
* Adds one or more resources. | ||
* | ||
* @return Builder. | ||
*/ | ||
public Builder addResources(final List<String> resources) { | ||
for (final String resource : Objects.requireNonNull(resources, "resources==null")) { | ||
delegate.resources.add(Objects.requireNonNull(resource, "resource==null")); | ||
} | ||
return this; | ||
} | ||
|
||
/** | ||
* Builds an instance of the outer class. | ||
* | ||
* @return New instance. | ||
*/ | ||
public JandexIndexFileReader build() { | ||
if (delegate.resources.isEmpty() && delegate.files.isEmpty()) { | ||
addDefaultResource(); | ||
} | ||
final JandexIndexFileReader tmp = delegate; | ||
delegate = new JandexIndexFileReader(); | ||
return tmp; | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.fuin.utils4j; | ||
|
||
import org.jboss.jandex.*; | ||
|
||
import java.io.*; | ||
import java.net.URL; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Enumeration; | ||
import java.util.List; | ||
|
||
/** | ||
* Helps to write Jandex index files. | ||
*/ | ||
public final class JandexIndexFileWriter { | ||
|
||
/** | ||
* Writes the index to a file. | ||
* Wraps the possible {@link IOException} into a {@link RuntimeException}. | ||
* | ||
* @param file File to write to. | ||
* @param index Index to save. | ||
*/ | ||
public void writeIndexR(final File file, final Index index) { | ||
try { | ||
writeIndex(file, index); | ||
} catch (final IOException ex) { | ||
throw new RuntimeException("Failed to write index to: " + file, ex); | ||
} | ||
} | ||
|
||
/** | ||
* Writes the index to a file. | ||
* | ||
* @param file File to write to. | ||
* @param index Index to save. | ||
* @throws IOException Failed to write to the file. | ||
*/ | ||
public void writeIndex(final File file, final Index index) throws IOException { | ||
try (final OutputStream out = new FileOutputStream(file)) { | ||
new IndexWriter(out).write(index); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/test/java/org/fuin/utils4j/JandexIndexFileReaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* Copyright (C) 2015 Michael Schnell. All rights reserved. | ||
* http://www.fuin.org/ | ||
* <p> | ||
* This library 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 3 of the License, or (at your option) any | ||
* later version. | ||
* <p> | ||
* This library 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. | ||
* <p> | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this library. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package org.fuin.utils4j; | ||
|
||
import org.fuin.utils4j.JandexIndexFileReader.Builder; | ||
|
||
import org.jboss.jandex.IndexView; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for {@link JandexIndexFileReader} class. | ||
*/ | ||
public class JandexIndexFileReaderTest { | ||
|
||
@Test | ||
public final void loadIndexFile() throws IOException { | ||
final IndexView index = new Builder().addFiles(new File("src/test/resources/sample.index")).build().load(); | ||
assertThat(index.getClassByName(JandexUtils.class.getName())).isNotNull(); | ||
assertThat(index.getClassByName(JandexUtils.class.getName()).name().toString()).isEqualTo(JandexUtils.class.getName()); | ||
} | ||
|
||
@Test | ||
public final void loadIndexResource() throws IOException { | ||
final IndexView index = new Builder().addResources("sample.index").build().load(); | ||
assertThat(index.getClassByName(JandexUtils.class.getName())).isNotNull(); | ||
assertThat(index.getClassByName(JandexUtils.class.getName()).name().toString()).isEqualTo(JandexUtils.class.getName()); | ||
} | ||
|
||
@Test | ||
public final void loadIndexResourceR() { | ||
final IndexView index = new Builder().addResources("sample.index").build().loadR(); | ||
assertThat(index.getClassByName(JandexUtils.class.getName())).isNotNull(); | ||
assertThat(index.getClassByName(JandexUtils.class.getName()).name().toString()).isEqualTo(JandexUtils.class.getName()); | ||
} | ||
|
||
@Test | ||
public final void loadIndexResourceFailure() throws IOException { | ||
assertThat(new Builder().addResources("does-not-exist.index").build().load().getKnownClasses()).isEmpty(); | ||
} | ||
|
||
@Test | ||
public final void loadIndexResourceRFailure() { | ||
assertThat(new Builder().addResources("does-not-exist.index").build().loadR().getKnownClasses()).isEmpty(); | ||
} | ||
|
||
} |
Oops, something went wrong.