Search in sources :

Example 1 with IndexReader

use of org.jboss.jandex.IndexReader in project core by weld.

the class JandexIndexBeanArchiveHandler method getIndex.

private Index getIndex(final File beanArchiveFile) {
    Preconditions.checkArgumentNotNull(beanArchiveFile, "beanArchiveFile");
    logger.debugv("Try to get Jandex index for: {0}", beanArchiveFile);
    Index index = null;
    try (ZipFile zip = new ZipFile(beanArchiveFile)) {
        // Open the bean archive and try to find the index file
        ZipEntry entry = zip.getEntry(JANDEX_INDEX_NAME);
        if (entry != null) {
            index = new IndexReader(zip.getInputStream(entry)).read();
        }
    } catch (IllegalArgumentException e) {
        CommonLogger.LOG.warnv("Jandex index is not valid: {0}", beanArchiveFile);
    } catch (UnsupportedVersion e) {
        CommonLogger.LOG.warnv("Version of Jandex index is not supported: {0}", beanArchiveFile);
    } catch (IOException e) {
        CommonLogger.LOG.warnv("Cannot get Jandex index from: {0}", beanArchiveFile);
        CommonLogger.LOG.catchingDebug(e);
    }
    logger.debugv("Jandex index {0}found: {1}", index == null ? "NOT " : "", beanArchiveFile);
    return index;
}
Also used : ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) IndexReader(org.jboss.jandex.IndexReader) Index(org.jboss.jandex.Index) IOException(java.io.IOException) UnsupportedVersion(org.jboss.jandex.UnsupportedVersion)

Example 2 with IndexReader

use of org.jboss.jandex.IndexReader in project wildfly-swarm by wildfly-swarm.

the class DeploymentProducer method index.

private void index(Archive<?> archive, List<IndexView> indexes) throws IOException {
    LOGGER.debugv("Indexing archive: {0}", archive.getName());
    // First try to load attached index
    Node indexNode = archive.get(ArchivePaths.create(INDEX_LOCATION));
    if (indexNode != null) {
        try (InputStream indexStream = indexNode.getAsset().openStream()) {
            LOGGER.debugv("Loading attached index from archive: {0}", archive.getName());
            indexes.add(new IndexReader(indexStream).read());
        }
    } else {
        // No index found - index all classes found
        Indexer indexer = new Indexer();
        for (Map.Entry<ArchivePath, Node> entry : archive.getContent(this::isClass).entrySet()) {
            try (InputStream contentStream = entry.getValue().getAsset().openStream()) {
                LOGGER.debugv("Indexing asset: {0} from archive: {1}", entry.getKey().get(), archive.getName());
                indexer.index(contentStream);
            } catch (IOException indexerIOException) {
                LOGGER.warnv(indexerIOException, "Failed parsing: {0} from archive: {1}", entry.getKey().get(), archive.getName());
            }
        }
        Index index = indexer.complete();
        indexes.add(index);
    }
    if (archive instanceof LibraryContainer) {
        for (Map.Entry<ArchivePath, Node> entry : archive.getContent(a -> a.get().endsWith(JAR_SUFFIX)).entrySet()) {
            if (entry.getValue().getAsset() instanceof ArchiveAsset) {
                ArchiveAsset archiveAsset = (ArchiveAsset) entry.getValue().getAsset();
                index(archiveAsset.getArchive(), indexes);
            } else {
                try (InputStream contentStream = entry.getValue().getAsset().openStream()) {
                    JARArchive jarArchive = ShrinkWrap.create(JARArchive.class, entry.getKey().get()).as(ZipImporter.class).importFrom(contentStream).as(JARArchive.class);
                    index(jarArchive, indexes);
                }
            }
        }
    }
}
Also used : Produces(javax.enterprise.inject.Produces) DeploymentScoped(org.wildfly.swarm.spi.runtime.annotations.DeploymentScoped) Logger(org.jboss.logging.Logger) Node(org.jboss.shrinkwrap.api.Node) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Indexer(org.jboss.jandex.Indexer) ArchivePath(org.jboss.shrinkwrap.api.ArchivePath) ArchivePaths(org.jboss.shrinkwrap.api.ArchivePaths) CompositeIndex(org.jboss.jandex.CompositeIndex) Map(java.util.Map) Index(org.jboss.jandex.Index) DeploymentContext(org.wildfly.swarm.container.runtime.cdi.DeploymentContext) IndexView(org.jboss.jandex.IndexView) ZipImporter(org.jboss.shrinkwrap.api.importer.ZipImporter) ShrinkWrap(org.jboss.shrinkwrap.api.ShrinkWrap) IndexReader(org.jboss.jandex.IndexReader) JARArchive(org.wildfly.swarm.spi.api.JARArchive) IOException(java.io.IOException) Archive(org.jboss.shrinkwrap.api.Archive) List(java.util.List) LibraryContainer(org.jboss.shrinkwrap.api.container.LibraryContainer) ApplicationScoped(javax.enterprise.context.ApplicationScoped) InputStream(java.io.InputStream) ArchiveAsset(org.jboss.shrinkwrap.api.asset.ArchiveAsset) InputStream(java.io.InputStream) Node(org.jboss.shrinkwrap.api.Node) CompositeIndex(org.jboss.jandex.CompositeIndex) Index(org.jboss.jandex.Index) ArchiveAsset(org.jboss.shrinkwrap.api.asset.ArchiveAsset) IOException(java.io.IOException) ArchivePath(org.jboss.shrinkwrap.api.ArchivePath) Indexer(org.jboss.jandex.Indexer) IndexReader(org.jboss.jandex.IndexReader) LibraryContainer(org.jboss.shrinkwrap.api.container.LibraryContainer) JARArchive(org.wildfly.swarm.spi.api.JARArchive) Map(java.util.Map)

Example 3 with IndexReader

use of org.jboss.jandex.IndexReader in project wildfly-swarm by wildfly-swarm.

the class FractionProducingExtension method uninstalledFractionClasses.

@SuppressWarnings("unchecked")
private Set<Class<? extends Fraction>> uninstalledFractionClasses(Set<Type> installedClasses) throws ModuleLoadException, IOException, ClassNotFoundException {
    Set<String> installedClassNames = installedClasses.stream().map(Type::getTypeName).collect(Collectors.toSet());
    List<String> moduleNames = ApplicationEnvironment.get().bootstrapModules();
    ClassLoader cl = Module.getBootModuleLoader().loadModule("swarm.container").getClassLoader();
    Set<Class<? extends Fraction>> fractionClasses = new HashSet<>();
    for (String moduleName : moduleNames) {
        Module module = Module.getBootModuleLoader().loadModule(moduleName);
        InputStream indexStream = module.getClassLoader().getResourceAsStream("META-INF/jandex.idx");
        if (indexStream != null) {
            IndexReader reader = new IndexReader(indexStream);
            Index index = reader.read();
            Set<ClassInfo> impls = index.getAllKnownImplementors(DotName.createSimple(Fraction.class.getName()));
            for (ClassInfo impl : impls) {
                if (!installedClassNames.contains(impl.name().toString())) {
                    Class<? extends Fraction> fractionClass = (Class<? extends Fraction>) cl.loadClass(impl.name().toString());
                    fractionClasses.add(fractionClass);
                }
            }
        }
    }
    return fractionClasses;
}
Also used : InputStream(java.io.InputStream) Fraction(org.wildfly.swarm.spi.api.Fraction) Index(org.jboss.jandex.Index) IndexReader(org.jboss.jandex.IndexReader) Module(org.jboss.modules.Module) HashSet(java.util.HashSet) ClassInfo(org.jboss.jandex.ClassInfo)

Example 4 with IndexReader

use of org.jboss.jandex.IndexReader in project scout.rt by eclipse.

the class JandexInventoryBuilder method readIndex.

/**
 * @param indexUri
 * @return index or null
 */
protected Index readIndex(URI indexUri, InputStream in) {
    try {
        Index index = new IndexReader(in).read();
        LOG.debug("Found pre-built {}", indexUri);
        return index;
    } catch (Exception ex) {
        throw new PlatformException("Error reading index '{}'", indexUri, ex);
    }
}
Also used : PlatformException(org.eclipse.scout.rt.platform.exception.PlatformException) IndexReader(org.jboss.jandex.IndexReader) CompositeIndex(org.jboss.jandex.CompositeIndex) Index(org.jboss.jandex.Index) URISyntaxException(java.net.URISyntaxException) PlatformException(org.eclipse.scout.rt.platform.exception.PlatformException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

Index (org.jboss.jandex.Index)4 IndexReader (org.jboss.jandex.IndexReader)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)2 CompositeIndex (org.jboss.jandex.CompositeIndex)2 FileNotFoundException (java.io.FileNotFoundException)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 ZipEntry (java.util.zip.ZipEntry)1 ZipFile (java.util.zip.ZipFile)1 ApplicationScoped (javax.enterprise.context.ApplicationScoped)1 Produces (javax.enterprise.inject.Produces)1 Inject (javax.inject.Inject)1 PlatformException (org.eclipse.scout.rt.platform.exception.PlatformException)1 ClassInfo (org.jboss.jandex.ClassInfo)1 IndexView (org.jboss.jandex.IndexView)1 Indexer (org.jboss.jandex.Indexer)1