Search in sources :

Example 11 with PathFilter

use of org.jboss.modules.filter.PathFilter in project jboss-modules by jboss-modules.

the class ModuleXmlParser method parseModuleDependency.

private static void parseModuleDependency(final XmlPullParser reader, final ArrayList<DependencySpec> dependencies) throws XmlPullParserException, IOException {
    String name = null;
    String slot = null;
    boolean export = false;
    boolean optional = false;
    boolean noSlots = atLeast1_6(reader);
    String services = D_NONE;
    final Set<String> required = new HashSet<>(LIST_A_NAME);
    final int count = reader.getAttributeCount();
    for (int i = 0; i < count; i++) {
        validateAttributeNamespace(reader, i);
        final String attribute = reader.getAttributeName(i);
        required.remove(attribute);
        switch(attribute) {
            case A_NAME:
                name = reader.getAttributeValue(i);
                break;
            case A_SLOT:
                if (noSlots)
                    throw unknownAttribute(reader, i);
                else
                    slot = reader.getAttributeValue(i);
                break;
            case A_EXPORT:
                export = Boolean.parseBoolean(reader.getAttributeValue(i));
                break;
            case A_OPTIONAL:
                optional = Boolean.parseBoolean(reader.getAttributeValue(i));
                break;
            case A_SERVICES:
                {
                    services = reader.getAttributeValue(i);
                    switch(services) {
                        case D_NONE:
                        case D_IMPORT:
                        case D_EXPORT:
                            break;
                        default:
                            throw unknownAttributeValue(reader, i);
                    }
                    break;
                }
            default:
                throw unknownAttribute(reader, i);
        }
    }
    if (!required.isEmpty()) {
        throw missingAttributes(reader, required);
    }
    final MultiplePathFilterBuilder importBuilder = PathFilters.multiplePathFilterBuilder(true);
    final MultiplePathFilterBuilder exportBuilder = PathFilters.multiplePathFilterBuilder(export);
    int eventType;
    for (; ; ) {
        eventType = reader.nextTag();
        switch(eventType) {
            case END_TAG:
                {
                    assert services.equals(D_NONE) || services.equals(D_EXPORT) || services.equals(D_IMPORT);
                    if (services.equals(D_EXPORT)) {
                        // If services are to be re-exported, add META-INF/services -> true near the end of the list
                        exportBuilder.addFilter(PathFilters.getMetaInfServicesFilter(), true);
                    }
                    if (export) {
                        // If re-exported, add META-INF/** -> false at the end of the list (require explicit override)
                        exportBuilder.addFilter(PathFilters.getMetaInfSubdirectoriesFilter(), false);
                        exportBuilder.addFilter(PathFilters.getMetaInfFilter(), false);
                    }
                    final PathFilter exportFilter = exportBuilder.create();
                    final PathFilter importFilter;
                    if (importBuilder.isEmpty()) {
                        importFilter = services.equals(D_NONE) ? PathFilters.getDefaultImportFilter() : PathFilters.getDefaultImportFilterWithServices();
                    } else {
                        if (!services.equals(D_NONE)) {
                            importBuilder.addFilter(PathFilters.getMetaInfServicesFilter(), true);
                        }
                        importBuilder.addFilter(PathFilters.getMetaInfSubdirectoriesFilter(), false);
                        importBuilder.addFilter(PathFilters.getMetaInfFilter(), false);
                        importFilter = importBuilder.create();
                    }
                    dependencies.add(new ModuleDependencySpecBuilder().setImportFilter(importFilter).setExportFilter(exportFilter).setName(noSlots ? name : ModuleIdentifier.create(name, slot).toString()).setOptional(optional).build());
                    return;
                }
            case START_TAG:
                {
                    validateNamespace(reader);
                    switch(reader.getName()) {
                        case E_EXPORTS:
                            parseFilterList(reader, exportBuilder);
                            break;
                        case E_IMPORTS:
                            parseFilterList(reader, importBuilder);
                            break;
                        default:
                            throw unexpectedContent(reader);
                    }
                    break;
                }
            default:
                {
                    throw unexpectedContent(reader);
                }
        }
    }
}
Also used : PathFilter(org.jboss.modules.filter.PathFilter) MultiplePathFilterBuilder(org.jboss.modules.filter.MultiplePathFilterBuilder) ModuleDependencySpecBuilder(org.jboss.modules.ModuleDependencySpecBuilder) HashSet(java.util.HashSet)

Example 12 with PathFilter

use of org.jboss.modules.filter.PathFilter in project jboss-modules by jboss-modules.

the class ModuleIteratorTest method testMetaInfServicesIterator.

@Test
public void testMetaInfServicesIterator() throws Exception {
    TestModuleLoader moduleLoader = new TestModuleLoader();
    ModuleSpec.Builder builder = ModuleSpec.build(MODULE_A);
    builder.addDependency(new ModuleDependencySpecBuilder().setName(MODULE_B.toString()).build());
    PathFilter importFilter = PathFilters.getMetaInfServicesFilter();
    builder.addDependency(new ModuleDependencySpecBuilder().setImportFilter(importFilter).setExport(true).setModuleLoader(moduleLoader).setName(MODULE_B.toString()).build());
    moduleLoader.addModuleSpec(builder.create());
    builder = ModuleSpec.build(MODULE_B);
    ResourceLoader resB = new JarFileResourceLoader("jarB", toJarFile(getModuleB()));
    builder.addResourceRoot(ResourceLoaderSpec.createResourceLoaderSpec(resB));
    builder.addDependency(DependencySpec.createLocalDependencySpec());
    moduleLoader.addModuleSpec(builder.create());
    Module moduleA = moduleLoader.loadModule(MODULE_A);
    Iterator<Resource> itres = moduleA.iterateResources(PathFilters.getMetaInfServicesFilter());
    Assert.assertTrue("Found a resource", itres.hasNext());
    Assert.assertEquals("META-INF/services/org/apache/camel/component/jms", itres.next().getName());
    Assert.assertFalse("No other resource", itres.hasNext());
}
Also used : PathFilter(org.jboss.modules.filter.PathFilter) TestModuleLoader(org.jboss.modules.util.TestModuleLoader) Test(org.junit.Test)

Example 13 with PathFilter

use of org.jboss.modules.filter.PathFilter in project jboss-modules by jboss-modules.

the class ClassFilteringTest method testClassFilter.

@Test
public void testClassFilter() throws Exception {
    final ModuleIdentifier identifierA = ModuleIdentifier.create(getClass().getSimpleName());
    ModuleSpec.Builder specBuilderA = ModuleSpec.build(identifierA);
    // Export-Package: com.acme.foo; include:="Qux*,BarImpl";exclude:=QuxImpl
    String packagePath = QuxBar.class.getPackage().getName().replace('.', '/');
    PathFilter inA = PathFilters.match(packagePath + "/Qux*.class");
    PathFilter inB = PathFilters.match(packagePath + "/BarImpl.class");
    PathFilter exA = PathFilters.match(packagePath + "/QuxImpl.class");
    // A class is only visible if it is:
    // Matched with an entry in the included list, and
    // Not matched with an entry in the excluded list.
    PathFilter in = PathFilters.any(inA, inB);
    PathFilter ex = PathFilters.not(PathFilters.any(exA));
    final PathFilter filter = PathFilters.all(in, ex);
    ClassFilter classImportFilter = ClassFilters.acceptAll();
    ClassFilter classExportFilter = ClassFilters.fromResourcePathFilter(filter);
    specBuilderA.addResourceRoot(createResourceLoaderSpec(getTestResourceLoader()));
    PathFilter importFilter = PathFilters.acceptAll();
    PathFilter exportFilter = PathFilters.acceptAll();
    PathFilter resourceImportFilter = PathFilters.acceptAll();
    PathFilter resourceExportFilter = PathFilters.acceptAll();
    specBuilderA.addDependency(new LocalDependencySpecBuilder().setImportFilter(importFilter).setExportFilter(exportFilter).setResourceImportFilter(resourceImportFilter).setResourceExportFilter(resourceExportFilter).setClassImportFilter(classImportFilter).setClassExportFilter(classExportFilter).build());
    addModuleSpec(specBuilderA.create());
    ModuleIdentifier identifierB = ModuleIdentifier.create("moduleB");
    ModuleSpec.Builder specBuilderB = ModuleSpec.build(identifierB);
    specBuilderB.addDependency(new ModuleDependencySpecBuilder().setName(identifierA.toString()).build());
    addModuleSpec(specBuilderB.create());
    assertLoadClass(identifierA, QuxFoo.class.getName());
    assertLoadClass(identifierA, QuxBar.class.getName());
    assertLoadClass(identifierA, QuxImpl.class.getName());
    assertLoadClass(identifierA, BarImpl.class.getName());
    assertLoadClass(identifierB, QuxFoo.class.getName());
    assertLoadClass(identifierB, QuxBar.class.getName());
    assertLoadClassFail(identifierB, QuxImpl.class.getName());
    assertLoadClass(identifierB, BarImpl.class.getName());
}
Also used : PathFilter(org.jboss.modules.filter.PathFilter) QuxFoo(org.jboss.modules.test.QuxFoo) ClassFilter(org.jboss.modules.filter.ClassFilter) QuxImpl(org.jboss.modules.test.QuxImpl) BarImpl(org.jboss.modules.test.BarImpl) QuxBar(org.jboss.modules.test.QuxBar) Test(org.junit.Test)

Example 14 with PathFilter

use of org.jboss.modules.filter.PathFilter in project jboss-modules by jboss-modules.

the class PathFilterTest method testMatch.

@Test
public void testMatch() throws Exception {
    PathFilter pathFilter = PathFilters.match("foo/**");
    assertFalse(pathFilter.accept("foo"));
    assertTrue(pathFilter.accept("foo/bar"));
    assertTrue(pathFilter.accept("foo/bar/baz"));
    pathFilter = PathFilters.match("foo/*");
    assertFalse(pathFilter.accept("foo"));
    assertTrue(pathFilter.accept("foo/bar"));
    assertTrue(pathFilter.accept("foo/bar/baz"));
    pathFilter = PathFilters.match("foo");
    assertTrue(pathFilter.accept("foo"));
    assertTrue(pathFilter.accept("foo/bar"));
    assertTrue(pathFilter.accept("foo/bar/baz"));
    pathFilter = PathFilters.match("**/bar/**");
    assertFalse(pathFilter.accept("foo"));
    assertFalse(pathFilter.accept("foo/bar"));
    assertTrue(pathFilter.accept("foo/bar/baz"));
    assertTrue(pathFilter.accept("foo/baz/bar/biff"));
}
Also used : PathFilter(org.jboss.modules.filter.PathFilter) Test(org.junit.Test)

Example 15 with PathFilter

use of org.jboss.modules.filter.PathFilter in project ceylon by eclipse.

the class CeylonModuleLoader method findModule.

@Override
protected ModuleSpec findModule(ModuleIdentifier moduleIdentifier) throws ModuleLoadException {
    try {
        final ArtifactResult artifact = findArtifact(moduleIdentifier);
        if (artifact == null)
            return null;
        if (!artifact.version().equals(moduleIdentifier.getSlot())) {
            AliasModuleSpec alias = (AliasModuleSpec) ModuleSpec.buildAlias(moduleIdentifier, ModuleIdentifier.create(artifact.name(), artifact.version())).create();
            return alias;
        }
        final File moduleFile = artifact.artifact();
        final boolean isDefault = RepositoryManager.DEFAULT_MODULE.equals(moduleIdentifier.getName());
        boolean isMaven = artifact.type() == ArtifactResultType.MAVEN;
        final List<DependencySpec> deps = new ArrayList<>();
        ModuleSpec.Builder builder = ModuleSpec.build(moduleIdentifier);
        if (!ModuleUtil.isMavenJarlessModule(moduleFile)) {
            // add module's jar
            ResourceLoader resourceLoader = ResourceLoaderProvider.getResourceLoader(moduleIdentifier, repository, moduleFile);
            // filter
            PathFilter filter = (artifact.filter() != null ? new CMRPathFilter(artifact.filter()) : PathFilters.acceptAll());
            // module resource root
            ResourceLoaderSpec rls = ResourceLoaderSpec.createResourceLoaderSpec(resourceLoader, filter);
            builder.addResourceRoot(rls);
        }
        // add potential native lib lookup
        ResourceLoader nativeLoader = new NativeLibraryResourceLoader(new File(moduleFile.getParent(), "lib"));
        builder.addResourceRoot(ResourceLoaderSpec.createResourceLoaderSpec(nativeLoader));
        Graph.Vertex<ModuleIdentifier, Boolean> vertex = graph.createVertex(moduleIdentifier, moduleIdentifier);
        DependencySpec lds = DependencySpec.createLocalDependencySpec();
        // local resources
        builder.addDependency(lds);
        deps.add(lds);
        if (isDefault == false) {
            Node<ArtifactResult> root = new Node<>();
            for (ArtifactResult i : artifact.dependencies()) {
                // Skip test scopes
                if (i.moduleScope() == ModuleScope.TEST)
                    continue;
                final String name = i.name();
                // route logging to JBoss LogManager
                if (isLogging(deps, builder, i)) {
                    continue;
                }
                // skip JDK modules
                if (JDK_MODULE_NAMES.contains(name)) {
                    continue;
                }
                boolean isDepMaven = MavenArtifactContext.NAMESPACE.equals(i.namespace());
                if (i.optional()) {
                    Node<ArtifactResult> current = root;
                    String[] tokens = name.split("\\.");
                    for (String token : tokens) {
                        Node<ArtifactResult> child = current.getChild(token);
                        if (child == null)
                            child = current.addChild(token);
                        current = child;
                    }
                    current.setValue(i);
                } else {
                    DependencySpec mds = createModuleDependency(i, exportMavenImports && isMaven && isDepMaven);
                    builder.addDependency(mds);
                    deps.add(mds);
                }
                ModuleIdentifier mi = createModuleIdentifier(i);
                Graph.Vertex<ModuleIdentifier, Boolean> dv = graph.createVertex(mi, mi);
                Graph.Edge.create(i.exported() || (exportMavenImports && isMaven && isDepMaven), vertex, dv);
            }
            if (root.isEmpty() == false) {
                LocalLoader onDemandLoader = new OnDemandLocalLoader(moduleIdentifier, this, root);
                builder.setFallbackLoader(onDemandLoader);
            }
        }
        // automagically import the JDK module
        builder.addDependency(JDK_DEPENDENCY);
        // no need to track system deps -- cannot be updated anyway
        createModuleDependency(vertex, deps, builder, LANGUAGE, false);
        // add runtime utils
        final DependencySpec sds = DependencySpec.createModuleDependencySpec(PathFilters.match(CEYLON_RUNTIME_PATH), PathFilters.rejectAll(), this, RUNTIME, true);
        builder.addDependency(sds);
        deps.add(sds);
        Graph.Vertex<ModuleIdentifier, Boolean> sdsv = graph.createVertex(RUNTIME, RUNTIME);
        Graph.Edge.create(false, vertex, sdsv);
        dependencies.put(moduleIdentifier, deps);
        // Stef: enable back when we upgrade jboss modules
        // index(artifact.artifact(), moduleIdentifier);
        UtilRegistryTransformer transformer = new UtilRegistryTransformer(moduleIdentifier, artifact);
        builder.setClassFileTransformer(transformer);
        // make sure we set our own class loader factory so we can find the transformer back from the class loader
        // this is used in the language module to force module metamodel registration
        builder.setModuleClassLoaderFactory(new CeylonModuleClassLoader.CeylonModuleClassLoaderFactory(transformer));
        return builder.create();
    } catch (Exception e) {
        throw new ModuleLoadException(e);
    }
}
Also used : ModuleLoadException(org.jboss.modules.ModuleLoadException) NativeLibraryResourceLoader(org.jboss.modules.NativeLibraryResourceLoader) ResourceLoader(org.jboss.modules.ResourceLoader) PathFilter(org.jboss.modules.filter.PathFilter) ArrayList(java.util.ArrayList) AliasModuleSpec(org.jboss.modules.AliasModuleSpec) ModuleSpec(org.jboss.modules.ModuleSpec) AliasModuleSpec(org.jboss.modules.AliasModuleSpec) DependencySpec(org.jboss.modules.DependencySpec) ModuleIdentifier(org.jboss.modules.ModuleIdentifier) LocalLoader(org.jboss.modules.LocalLoader) Builder(org.jboss.modules.ModuleSpec.Builder) ModuleLoadException(org.jboss.modules.ModuleLoadException) ArtifactResult(org.eclipse.ceylon.model.cmr.ArtifactResult) ResourceLoaderSpec(org.jboss.modules.ResourceLoaderSpec) NativeLibraryResourceLoader(org.jboss.modules.NativeLibraryResourceLoader) File(java.io.File)

Aggregations

PathFilter (org.jboss.modules.filter.PathFilter)15 ArrayList (java.util.ArrayList)5 HashSet (java.util.HashSet)4 List (java.util.List)4 Test (org.junit.Test)4 ClassFilter (org.jboss.modules.filter.ClassFilter)3 MultiplePathFilterBuilder (org.jboss.modules.filter.MultiplePathFilterBuilder)3 File (java.io.File)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 IdentityHashMap (java.util.IdentityHashMap)2 Set (java.util.Set)2 ModuleLoadException (org.jboss.modules.ModuleLoadException)2 ResourceLoader (org.jboss.modules.ResourceLoader)2 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 PrivilegedActionException (java.security.PrivilegedActionException)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1 JarFile (java.util.jar.JarFile)1