Skip to content

Commit

Permalink
Moved reader/writer methods into separate classes
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-schnell committed Jan 21, 2024
1 parent e7ab6c3 commit c5039d0
Show file tree
Hide file tree
Showing 6 changed files with 344 additions and 143 deletions.
164 changes: 164 additions & 0 deletions src/main/java/org/fuin/utils4j/JandexIndexFileReader.java
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;
}

}

}
45 changes: 45 additions & 0 deletions src/main/java/org/fuin/utils4j/JandexIndexFileWriter.java
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);
}
}

}
97 changes: 6 additions & 91 deletions src/main/java/org/fuin/utils4j/JandexUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
*/
package org.fuin.utils4j;

import org.jboss.jandex.*;
import org.jboss.jandex.DotName;
import org.jboss.jandex.Index;
import org.jboss.jandex.Indexer;

import java.io.*;
import java.net.URL;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
Expand All @@ -32,11 +35,6 @@
*/
public final class JandexUtils {

/**
* Default location of the Jandex index file.
*/
public static final String DEFAULT_JANDEX_INDEX_FILE = "META-INF/jandex.idx";

/**
* Private default constructor.
*/
Expand Down Expand Up @@ -195,88 +193,5 @@ public static Class<?> loadClass(DotName name) {
return Utils4J.loadClass(name.toString());
}

/**
* 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 static 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 static void writeIndex(final File file, final Index index) throws IOException {
try (final OutputStream out = new FileOutputStream(file)) {
new IndexWriter(out).write(index);
}
}

/**
* Loads the index from standard "META-INF/jandex.idx" location.
* Wraps the possible {@link IOException} into a {@link RuntimeException}.
*
* @return Index.
*/
public static Index loadIndexResourceR() {
try {
return loadIndexResource();
} catch (final IOException ex) {
throw new RuntimeException("Failed to load: " + DEFAULT_JANDEX_INDEX_FILE, ex);
}
}

/**
* Loads the index from standard "META-INF/jandex.idx" location.
*
* @return Index.
* @throws IOException Failed to load the resource.
*/
public static Index loadIndexResource() throws IOException {
return loadIndexResource(DEFAULT_JANDEX_INDEX_FILE);
}

/**
* Loads the index from a resource in the classpath.
* Wraps the possible {@link IOException} into a {@link RuntimeException}.
*
* @param indexFilePathAndName Path and name of the index file resource.
* @return Index.
*/
public static Index loadIndexResourceR(final String indexFilePathAndName) {
try {
return loadIndexResource(indexFilePathAndName);
} catch (final IOException ex) {
throw new RuntimeException("Failed to write index to: " + indexFilePathAndName, ex);
}
}

/**
* Loads the index from a resource in the classpath.
*
* @param indexFilePathAndName Path and name of the index file resource.
* @return Index.
* @throws IOException Failed to load the resource.
*/
public static Index loadIndexResource(final String indexFilePathAndName) throws IOException {
final URL url = Thread.currentThread().getContextClassLoader().getResource(indexFilePathAndName);
if (url == null) {
throw new FileNotFoundException("Resource not found: " + indexFilePathAndName);
}
try (final InputStream input = url.openStream()) {
return new IndexReader(input).read();
}
}

}
66 changes: 66 additions & 0 deletions src/test/java/org/fuin/utils4j/JandexIndexFileReaderTest.java
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();
}

}
Loading

0 comments on commit c5039d0

Please sign in to comment.