Search in sources :

Example 41 with Index

use of org.jboss.jandex.Index in project wildfly by wildfly.

the class AnnotationsTestCase method testProcessConnectorManualSpecConnector.

/**
 * Process: Connector -- verification of the processConnector method
 *
 * @throws Throwable throwable exception
 */
public void testProcessConnectorManualSpecConnector() throws Throwable {
    // Test prep
    AnnotationRepository ar = null;
    try {
        File srcRars = new File("./target/test-classes/org/jboss/as/connector/deployers/spec/rars");
        File srcRa16 = new File("./target/test-classes/org/jboss/as/connector/deployers/spec/rars/ra16inoutmultianno");
        final String destRars = MULTIANNO_TARGET + "/org/jboss/as/connector/deployers/spec/rars";
        final String destRa16 = MULTIANNO_TARGET + "/org/jboss/as/connector/deployers/spec/rars/ra16inoutmultianno";
        for (File srcFile : srcRars.listFiles()) {
            if (!srcFile.isDirectory()) {
                Files.copy(srcFile, new File(destRars + "/" + srcFile.getName()));
            }
        }
        for (File srcFile : srcRa16.listFiles()) {
            if (!srcFile.isDirectory() && !"TestResourceAdapter.class".equals(srcFile.getName())) {
                Files.copy(srcFile, new File(destRa16 + "/" + srcFile.getName()));
            }
        }
        URI uri = getURI("/ra16inoutmultianno.rar");
        final VirtualFile virtualFile = VFS.getChild(uri);
        final Indexer indexer = new Indexer();
        final List<VirtualFile> classChildren = virtualFile.getChildren(new SuffixMatchFilter(".class", VisitorAttributes.RECURSE_LEAVES_ONLY));
        for (VirtualFile classFile : classChildren) {
            System.out.println("testProcessConnectorFailNoConnector: adding " + classFile.getPathName());
            InputStream inputStream = null;
            try {
                inputStream = classFile.openStream();
                indexer.index(inputStream);
            } finally {
                VFSUtils.safeClose(inputStream);
            }
        }
        final Index index = indexer.complete();
        ar = new JandexAnnotationRepositoryImpl(index, Thread.currentThread().getContextClassLoader());
    } catch (Throwable e) {
        e.printStackTrace();
        fail("Test preparation error " + e.getMessage());
    }
    Collection<Annotation> values = ar.getAnnotation(javax.resource.spi.Connector.class);
    assertNull(values);
    // Test run
    annotations.process(ar, "org.jboss.as.connector.deployers.spec.rars.BaseResourceAdapter", Thread.currentThread().getContextClassLoader());
}
Also used : VirtualFile(org.jboss.vfs.VirtualFile) InputStream(java.io.InputStream) Index(org.jboss.jandex.Index) URI(java.net.URI) Annotation(org.jboss.jca.common.spi.annotations.repository.Annotation) Indexer(org.jboss.jandex.Indexer) AnnotationRepository(org.jboss.jca.common.spi.annotations.repository.AnnotationRepository) SuffixMatchFilter(org.jboss.vfs.util.SuffixMatchFilter) VirtualFile(org.jboss.vfs.VirtualFile) File(java.io.File)

Example 42 with Index

use of org.jboss.jandex.Index in project wildfly by wildfly.

the class AnnotationsTestCase method testProcessConnectorFailTooManyConnectors.

/**
 * Process: Connector -- verification of the processConnector method
 *
 * @throws Throwable throwable exception
 */
@Test(expected = ValidateException.class)
public void testProcessConnectorFailTooManyConnectors() throws Throwable {
    // Test prep
    AnnotationRepository ar = null;
    try {
        File srcRars = new File("./target/test-classes/org/jboss/as/connector/deployers/spec/rars");
        File srcRa16 = new File("./target/test-classes/org/jboss/as/connector/deployers/spec/rars/ra16inoutmultianno");
        File srcStandalone = new File("./target/test-classes/org/jboss/as/connector/deployers/spec/rars/rastandalone");
        final String destRars = MULTIANNO_TARGET + "/org/jboss/as/connector/deployers/spec/rars";
        final String destRa16 = MULTIANNO_TARGET + "/org/jboss/as/connector/deployers/spec/rars/ra16inoutmultianno";
        final String destStandalone = MULTIANNO_TARGET + "/org/jboss/as/connector/deployers/spec/rars/rastandalone";
        Files.createParentDirs(new File(destStandalone + "/sample.file"));
        for (File srcFile : srcRars.listFiles()) {
            if (!srcFile.isDirectory()) {
                Files.copy(srcFile, new File(destRars + "/" + srcFile.getName()));
            }
        }
        for (File srcFile : srcRa16.listFiles()) {
            if (!srcFile.isDirectory()) {
                Files.copy(srcFile, new File(destRa16 + "/" + srcFile.getName()));
            }
        }
        for (File srcFile : srcStandalone.listFiles()) {
            if (!srcFile.isDirectory()) {
                Files.copy(srcFile, new File(destStandalone + "/" + srcFile.getName()));
            }
        }
        URI uri = getURI("/ra16inoutmultianno.rar");
        final VirtualFile virtualFile = VFS.getChild(uri);
        final Indexer indexer = new Indexer();
        final List<VirtualFile> classChildren = virtualFile.getChildren(new SuffixMatchFilter(".class", VisitorAttributes.RECURSE_LEAVES_ONLY));
        for (VirtualFile classFile : classChildren) {
            System.out.println("testProcessConnectorFailTooManyConnectors: adding " + classFile.getPathName());
            InputStream inputStream = null;
            try {
                inputStream = classFile.openStream();
                indexer.index(inputStream);
            } finally {
                VFSUtils.safeClose(inputStream);
            }
        }
        final Index index = indexer.complete();
        ar = new JandexAnnotationRepositoryImpl(index, Thread.currentThread().getContextClassLoader());
    } catch (Throwable e) {
        e.printStackTrace();
        fail("Test preparation error " + e.getMessage());
    }
    Collection<Annotation> values = ar.getAnnotation(javax.resource.spi.Connector.class);
    assertNotNull(values);
    assertEquals(2, values.size());
    // Test run
    annotations.process(ar, null, Thread.currentThread().getContextClassLoader());
}
Also used : VirtualFile(org.jboss.vfs.VirtualFile) InputStream(java.io.InputStream) Index(org.jboss.jandex.Index) URI(java.net.URI) Annotation(org.jboss.jca.common.spi.annotations.repository.Annotation) Indexer(org.jboss.jandex.Indexer) AnnotationRepository(org.jboss.jca.common.spi.annotations.repository.AnnotationRepository) SuffixMatchFilter(org.jboss.vfs.util.SuffixMatchFilter) VirtualFile(org.jboss.vfs.VirtualFile) File(java.io.File) Test(org.junit.Test)

Example 43 with Index

use of org.jboss.jandex.Index in project wildfly by wildfly.

the class AnnotationsTestCase method testProcessConnector.

/**
 * Process: Connector -- verification of the processConnector method
 *
 * @throws Throwable throwable exception
 */
@Test
public void testProcessConnector() throws Throwable {
    // Test prep
    File srcRars = new File("./target/test-classes/org/jboss/as/connector/deployers/spec/rars");
    File srcRa16 = new File("./target/test-classes/org/jboss/as/connector/deployers/spec/rars/ra16inoutmultianno");
    final String destRars = MULTIANNO_TARGET + "/org/jboss/as/connector/deployers/spec/rars";
    final String destRa16 = MULTIANNO_TARGET + "/org/jboss/as/connector/deployers/spec/rars/ra16inoutmultianno";
    for (File srcFile : srcRars.listFiles()) {
        if (!srcFile.isDirectory()) {
            Files.copy(srcFile, new File(destRars + "/" + srcFile.getName()));
        }
    }
    for (File srcFile : srcRa16.listFiles()) {
        if (!srcFile.isDirectory()) {
            Files.copy(srcFile, new File(destRa16 + "/" + srcFile.getName()));
        }
    }
    URI uri = getURI("/ra16inoutmultianno.rar");
    final VirtualFile virtualFile = VFS.getChild(uri);
    final Indexer indexer = new Indexer();
    final List<VirtualFile> classChildren = virtualFile.getChildren(new SuffixMatchFilter(".class", VisitorAttributes.RECURSE_LEAVES_ONLY));
    for (VirtualFile classFile : classChildren) {
        System.out.println("testProcessConnector: adding " + classFile.getPathName());
        InputStream inputStream = null;
        try {
            inputStream = classFile.openStream();
            indexer.index(inputStream);
        } finally {
            VFSUtils.safeClose(inputStream);
        }
    }
    final Index index = indexer.complete();
    AnnotationRepository ar = new JandexAnnotationRepositoryImpl(index, Thread.currentThread().getContextClassLoader());
    Collection<Annotation> values = ar.getAnnotation(javax.resource.spi.Connector.class);
    assertNotNull(values);
    assertEquals(1, values.size());
    // Test run
    try {
        annotations.process(ar, null, Thread.currentThread().getContextClassLoader());
    } catch (Throwable t) {
        t.printStackTrace();
        fail(t.getMessage());
    }
}
Also used : VirtualFile(org.jboss.vfs.VirtualFile) InputStream(java.io.InputStream) Index(org.jboss.jandex.Index) URI(java.net.URI) Annotation(org.jboss.jca.common.spi.annotations.repository.Annotation) Indexer(org.jboss.jandex.Indexer) SuffixMatchFilter(org.jboss.vfs.util.SuffixMatchFilter) AnnotationRepository(org.jboss.jca.common.spi.annotations.repository.AnnotationRepository) VirtualFile(org.jboss.vfs.VirtualFile) File(java.io.File) Test(org.junit.Test)

Example 44 with Index

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

the class DeploymentIndexTest method createIndexAsset.

private Asset createIndexAsset(Class<?>... classes) throws IOException {
    Indexer indexer = new Indexer();
    for (Class<?> clazz : classes) {
        try (InputStream stream = DeploymentIndexTest.class.getClassLoader().getResourceAsStream(clazz.getName().replace(".", "/") + ".class")) {
            if (stream != null) {
                indexer.index(stream);
            }
        }
    }
    Index index = indexer.complete();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    new IndexWriter(out).write(index);
    return new ByteArrayAsset(out.toByteArray());
}
Also used : Indexer(org.jboss.jandex.Indexer) ByteArrayAsset(org.jboss.shrinkwrap.api.asset.ByteArrayAsset) IndexWriter(org.jboss.jandex.IndexWriter) InputStream(java.io.InputStream) Index(org.jboss.jandex.Index) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 45 with Index

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

the class Swarm method initializeConfigFiltersClassPath.

private void initializeConfigFiltersClassPath() throws IOException, ClassNotFoundException {
    String classpath = System.getProperty("java.class.path");
    String[] locations = classpath.split(System.getProperty("path.separator"));
    Indexer indexer = new Indexer();
    for (String location : locations) {
        File file = new File(location);
        JavaArchive archive = null;
        if (file.exists()) {
            if (file.isDirectory()) {
                archive = ShrinkWrap.create(ExplodedImporter.class).importDirectory(file).as(JavaArchive.class);
            } else {
                archive = ShrinkWrap.create(ZipImporter.class).importFrom(file).as(JavaArchive.class);
            }
            Map<ArchivePath, Node> content = archive.getContent();
            for (ArchivePath path : content.keySet()) {
                if (path.get().endsWith(".class") && !path.get().endsWith("module-info.class")) {
                    Node node = content.get(path);
                    try {
                        indexer.index(node.getAsset().openStream());
                    } catch (IOException e) {
                    // ignore
                    }
                }
            }
        }
    }
    Index index = indexer.complete();
    Set<ClassInfo> impls = index.getAllKnownImplementors(DotName.createSimple(ConfigurationFilter.class.getName()));
    for (ClassInfo each : impls) {
        String name = each.name().toString();
        Class<? extends ConfigurationFilter> cls = (Class<? extends ConfigurationFilter>) Class.forName(name);
        try {
            ConfigurationFilter filter = cls.newInstance();
            this.configView.withFilter(filter);
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
Also used : ConfigurationFilter(org.wildfly.swarm.spi.api.ConfigurationFilter) Node(org.jboss.shrinkwrap.api.Node) Index(org.jboss.jandex.Index) IOException(java.io.IOException) ExplodedImporter(org.jboss.shrinkwrap.api.importer.ExplodedImporter) JavaArchive(org.jboss.shrinkwrap.api.spec.JavaArchive) ArchivePath(org.jboss.shrinkwrap.api.ArchivePath) Indexer(org.jboss.jandex.Indexer) ZipImporter(org.jboss.shrinkwrap.api.importer.ZipImporter) JarFile(java.util.jar.JarFile) File(java.io.File) ClassInfo(org.jboss.jandex.ClassInfo)

Aggregations

Index (org.jboss.jandex.Index)50 Indexer (org.jboss.jandex.Indexer)24 InputStream (java.io.InputStream)21 Test (org.junit.Test)18 File (java.io.File)16 URI (java.net.URI)15 AnnotationRepository (org.jboss.jca.common.spi.annotations.repository.AnnotationRepository)13 VirtualFile (org.jboss.vfs.VirtualFile)13 URL (java.net.URL)11 SuffixMatchFilter (org.jboss.vfs.util.SuffixMatchFilter)11 ClassInfo (org.jboss.jandex.ClassInfo)10 ResourceRoot (org.jboss.as.server.deployment.module.ResourceRoot)8 IOException (java.io.IOException)7 HashMap (java.util.HashMap)6 DeploymentUnit (org.jboss.as.server.deployment.DeploymentUnit)6 ArrayList (java.util.ArrayList)5 HashSet (java.util.HashSet)5 Map (java.util.Map)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 CompositeIndex (org.jboss.jandex.CompositeIndex)4