Search in sources :

Example 1 with ClassFilter

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

the class Module method addExportedPaths.

private long addExportedPaths(Dependency[] dependencies, Map<String, List<LocalLoader>> map, FastCopyHashSet<PathFilter> filterStack, FastCopyHashSet<ClassFilter> classFilterStack, final FastCopyHashSet<PathFilter> resourceFilterStack, Set<Visited> visited) throws ModuleLoadException {
    if (!visited.add(new Visited(this, filterStack, classFilterStack, resourceFilterStack))) {
        return 0L;
    }
    long subtract = 0L;
    moduleLoader.incScanCount();
    for (Dependency dependency : dependencies) {
        final PathFilter exportFilter = dependency.getExportFilter();
        // skip non-exported dependencies altogether
        if (exportFilter != PathFilters.rejectAll()) {
            if (dependency instanceof ModuleDependency) {
                final ModuleDependency moduleDependency = (ModuleDependency) dependency;
                final ModuleLoader moduleLoader = moduleDependency.getModuleLoader();
                final String name = moduleDependency.getName();
                final Module module;
                try {
                    long pauseStart = Metrics.getCurrentCPUTime();
                    try {
                        module = moduleLoader.preloadModule(name);
                    } finally {
                        subtract += Metrics.getCurrentCPUTime() - pauseStart;
                    }
                } catch (ModuleLoadException ex) {
                    if (moduleDependency.isOptional()) {
                        continue;
                    } else {
                        log.trace("Module %s, dependency %s preload failed: %s", getIdentifier(), moduleDependency.getName(), ex);
                        throw ex;
                    }
                }
                if (module == null) {
                    if (!moduleDependency.isOptional()) {
                        throw new ModuleNotFoundException(name);
                    }
                    continue;
                }
                final PathFilter importFilter = dependency.getImportFilter();
                final FastCopyHashSet<PathFilter> nestedFilters;
                final FastCopyHashSet<ClassFilter> nestedClassFilters;
                final FastCopyHashSet<PathFilter> nestedResourceFilters;
                if (filterStack.contains(importFilter) && filterStack.contains(exportFilter)) {
                    nestedFilters = filterStack;
                } else {
                    nestedFilters = filterStack.clone();
                    nestedFilters.add(importFilter);
                    nestedFilters.add(exportFilter);
                }
                final ClassFilter classImportFilter = dependency.getClassImportFilter();
                final ClassFilter classExportFilter = dependency.getClassExportFilter();
                if ((classImportFilter == ClassFilters.acceptAll() || classFilterStack.contains(classImportFilter)) && (classExportFilter == ClassFilters.acceptAll() || classFilterStack.contains(classExportFilter))) {
                    nestedClassFilters = classFilterStack;
                } else {
                    nestedClassFilters = classFilterStack.clone();
                    if (classImportFilter != ClassFilters.acceptAll())
                        nestedClassFilters.add(classImportFilter);
                    if (classExportFilter != ClassFilters.acceptAll())
                        nestedClassFilters.add(classExportFilter);
                }
                final PathFilter resourceImportFilter = dependency.getResourceImportFilter();
                final PathFilter resourceExportFilter = dependency.getResourceExportFilter();
                if ((resourceImportFilter == PathFilters.acceptAll() || resourceFilterStack.contains(resourceImportFilter)) && (resourceExportFilter == PathFilters.acceptAll() || resourceFilterStack.contains(resourceExportFilter))) {
                    nestedResourceFilters = resourceFilterStack;
                } else {
                    nestedResourceFilters = resourceFilterStack.clone();
                    if (resourceImportFilter != PathFilters.acceptAll())
                        nestedResourceFilters.add(resourceImportFilter);
                    if (resourceExportFilter != PathFilters.acceptAll())
                        nestedResourceFilters.add(resourceExportFilter);
                }
                subtract += module.addExportedPaths(module.getDependenciesInternal(), map, nestedFilters, nestedClassFilters, nestedResourceFilters, visited);
            } else if (dependency instanceof ModuleClassLoaderDependency) {
                final ModuleClassLoaderDependency classLoaderDependency = (ModuleClassLoaderDependency) dependency;
                LocalLoader localLoader = classLoaderDependency.getLocalLoader();
                for (Object filter : classFilterStack.getRawArray()) {
                    if (filter != null && filter != ClassFilters.acceptAll()) {
                        localLoader = createClassFilteredLocalLoader((ClassFilter) filter, localLoader);
                    }
                }
                for (Object filter : resourceFilterStack.getRawArray()) {
                    if (filter != null && filter != PathFilters.acceptAll()) {
                        localLoader = createPathFilteredLocalLoader((PathFilter) filter, localLoader);
                    }
                }
                ClassFilter classImportFilter = classLoaderDependency.getClassImportFilter();
                if (classImportFilter != ClassFilters.acceptAll()) {
                    localLoader = createClassFilteredLocalLoader(classImportFilter, localLoader);
                }
                ClassFilter classExportFilter = classLoaderDependency.getClassExportFilter();
                if (classExportFilter != ClassFilters.acceptAll()) {
                    localLoader = createClassFilteredLocalLoader(classExportFilter, localLoader);
                }
                PathFilter resourceImportFilter = classLoaderDependency.getResourceImportFilter();
                if (resourceImportFilter != PathFilters.acceptAll()) {
                    localLoader = createPathFilteredLocalLoader(resourceImportFilter, localLoader);
                }
                PathFilter resourceExportFilter = classLoaderDependency.getResourceExportFilter();
                if (resourceExportFilter != PathFilters.acceptAll()) {
                    localLoader = createPathFilteredLocalLoader(resourceExportFilter, localLoader);
                }
                final PathFilter importFilter = classLoaderDependency.getImportFilter();
                final Set<String> paths = classLoaderDependency.getPaths();
                for (String path : paths) {
                    boolean accept = !"_private".equals(path);
                    if (accept)
                        for (Object filter : filterStack.getRawArray()) {
                            if (filter != null && !((PathFilter) filter).accept(path)) {
                                accept = false;
                                break;
                            }
                        }
                    if (accept && importFilter.accept(path) && exportFilter.accept(path)) {
                        List<LocalLoader> list = map.get(path);
                        if (list == null) {
                            map.put(path, list = new ArrayList<LocalLoader>(1));
                            list.add(localLoader);
                        } else if (!list.contains(localLoader)) {
                            list.add(localLoader);
                        }
                    }
                }
            } else if (dependency instanceof LocalDependency) {
                final LocalDependency localDependency = (LocalDependency) dependency;
                LocalLoader localLoader = localDependency.getLocalLoader();
                for (Object filter : classFilterStack.getRawArray()) {
                    if (filter != null && filter != ClassFilters.acceptAll()) {
                        localLoader = createClassFilteredLocalLoader((ClassFilter) filter, localLoader);
                    }
                }
                for (Object filter : resourceFilterStack.getRawArray()) {
                    if (filter != null && filter != PathFilters.acceptAll()) {
                        localLoader = createPathFilteredLocalLoader((PathFilter) filter, localLoader);
                    }
                }
                ClassFilter classFilter = localDependency.getClassExportFilter();
                if (classFilter != ClassFilters.acceptAll()) {
                    localLoader = createClassFilteredLocalLoader(classFilter, localLoader);
                }
                classFilter = localDependency.getClassImportFilter();
                if (classFilter != ClassFilters.acceptAll()) {
                    localLoader = createClassFilteredLocalLoader(classFilter, localLoader);
                }
                PathFilter resourceFilter = localDependency.getResourceExportFilter();
                if (resourceFilter != PathFilters.acceptAll()) {
                    localLoader = createPathFilteredLocalLoader(resourceFilter, localLoader);
                }
                resourceFilter = localDependency.getResourceImportFilter();
                if (resourceFilter != PathFilters.acceptAll()) {
                    localLoader = createPathFilteredLocalLoader(resourceFilter, localLoader);
                }
                final Set<String> paths = localDependency.getPaths();
                for (String path : paths) {
                    boolean accept = true;
                    for (Object filter : filterStack.getRawArray()) {
                        if (filter != null && !((PathFilter) filter).accept(path)) {
                            accept = false;
                            break;
                        }
                    }
                    if (accept && localDependency.getImportFilter().accept(path) && localDependency.getExportFilter().accept(path)) {
                        List<LocalLoader> list = map.get(path);
                        if (list == null) {
                            map.put(path, list = new ArrayList<LocalLoader>(1));
                            list.add(localLoader);
                        } else if (!list.contains(localLoader)) {
                            list.add(localLoader);
                        }
                    }
                }
            }
        // else unknown dep type so just skip
        }
    }
    return subtract;
}
Also used : PathFilter(org.jboss.modules.filter.PathFilter) HashSet(java.util.HashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) ClassFilter(org.jboss.modules.filter.ClassFilter) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with ClassFilter

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

the class Module method addPaths.

private long addPaths(Dependency[] dependencies, Map<String, List<LocalLoader>> map, FastCopyHashSet<PathFilter> filterStack, FastCopyHashSet<ClassFilter> classFilterStack, final FastCopyHashSet<PathFilter> resourceFilterStack, Set<Visited> visited) throws ModuleLoadException {
    long subtract = 0L;
    moduleLoader.incScanCount();
    for (Dependency dependency : dependencies) {
        if (dependency instanceof ModuleDependency) {
            final ModuleDependency moduleDependency = (ModuleDependency) dependency;
            final ModuleLoader moduleLoader = moduleDependency.getModuleLoader();
            final String name = moduleDependency.getName();
            final Module module;
            try {
                long pauseStart = Metrics.getCurrentCPUTime();
                try {
                    module = moduleLoader.preloadModule(name);
                } finally {
                    subtract += Metrics.getCurrentCPUTime() - pauseStart;
                }
            } catch (ModuleLoadException ex) {
                if (moduleDependency.isOptional()) {
                    continue;
                } else {
                    log.trace("Module %s, dependency %s preload failed: %s", getIdentifier(), moduleDependency.getName(), ex);
                    throw ex;
                }
            }
            if (module == null) {
                if (!moduleDependency.isOptional()) {
                    throw new ModuleNotFoundException(name);
                }
                continue;
            }
            final PathFilter importFilter = dependency.getImportFilter();
            final FastCopyHashSet<PathFilter> nestedFilters;
            final FastCopyHashSet<ClassFilter> nestedClassFilters;
            final FastCopyHashSet<PathFilter> nestedResourceFilters;
            if (filterStack.contains(importFilter)) {
                nestedFilters = filterStack;
            } else {
                nestedFilters = filterStack.clone();
                nestedFilters.add(importFilter);
            }
            final ClassFilter classImportFilter = dependency.getClassImportFilter();
            if (classImportFilter == ClassFilters.acceptAll() || classFilterStack.contains(classImportFilter)) {
                nestedClassFilters = classFilterStack;
            } else {
                nestedClassFilters = classFilterStack.clone();
                if (classImportFilter != ClassFilters.acceptAll())
                    nestedClassFilters.add(classImportFilter);
            }
            final PathFilter resourceImportFilter = dependency.getResourceImportFilter();
            if (resourceImportFilter == PathFilters.acceptAll() || resourceFilterStack.contains(resourceImportFilter)) {
                nestedResourceFilters = resourceFilterStack;
            } else {
                nestedResourceFilters = resourceFilterStack.clone();
                if (resourceImportFilter != PathFilters.acceptAll())
                    nestedResourceFilters.add(resourceImportFilter);
            }
            subtract += module.addExportedPaths(module.getDependenciesInternal(), map, nestedFilters, nestedClassFilters, nestedResourceFilters, visited);
        } else if (dependency instanceof ModuleClassLoaderDependency) {
            final ModuleClassLoaderDependency classLoaderDependency = (ModuleClassLoaderDependency) dependency;
            LocalLoader localLoader = classLoaderDependency.getLocalLoader();
            for (Object filter : classFilterStack.getRawArray()) {
                if (filter != null && filter != ClassFilters.acceptAll()) {
                    localLoader = createClassFilteredLocalLoader((ClassFilter) filter, localLoader);
                }
            }
            for (Object filter : resourceFilterStack.getRawArray()) {
                if (filter != null && filter != PathFilters.acceptAll()) {
                    localLoader = createPathFilteredLocalLoader((PathFilter) filter, localLoader);
                }
            }
            ClassFilter classFilter = classLoaderDependency.getClassImportFilter();
            if (classFilter != ClassFilters.acceptAll()) {
                localLoader = createClassFilteredLocalLoader(classFilter, localLoader);
            }
            PathFilter resourceFilter = classLoaderDependency.getResourceImportFilter();
            if (resourceFilter != PathFilters.acceptAll()) {
                localLoader = createPathFilteredLocalLoader(resourceFilter, localLoader);
            }
            final PathFilter importFilter = classLoaderDependency.getImportFilter();
            final Set<String> paths = classLoaderDependency.getPaths();
            for (String path : paths) {
                if (importFilter.accept(path)) {
                    List<LocalLoader> list = map.get(path);
                    if (list == null) {
                        map.put(path, list = new ArrayList<LocalLoader>());
                        list.add(localLoader);
                    } else if (!list.contains(localLoader)) {
                        list.add(localLoader);
                    }
                }
            }
        } else if (dependency instanceof LocalDependency) {
            final LocalDependency localDependency = (LocalDependency) dependency;
            LocalLoader localLoader = localDependency.getLocalLoader();
            for (Object filter : classFilterStack.getRawArray()) {
                if (filter != null && filter != ClassFilters.acceptAll()) {
                    localLoader = createClassFilteredLocalLoader((ClassFilter) filter, localLoader);
                }
            }
            for (Object filter : resourceFilterStack.getRawArray()) {
                if (filter != null && filter != PathFilters.acceptAll()) {
                    localLoader = createPathFilteredLocalLoader((PathFilter) filter, localLoader);
                }
            }
            final ClassFilter classFilter = localDependency.getClassImportFilter();
            if (classFilter != ClassFilters.acceptAll()) {
                localLoader = createClassFilteredLocalLoader(classFilter, localLoader);
            }
            final PathFilter resourceFilter = localDependency.getResourceImportFilter();
            if (resourceFilter != PathFilters.acceptAll()) {
                localLoader = createPathFilteredLocalLoader(resourceFilter, localLoader);
            }
            final PathFilter importFilter = localDependency.getImportFilter();
            final Set<String> paths = localDependency.getPaths();
            for (String path : paths) {
                if (importFilter.accept(path)) {
                    List<LocalLoader> list = map.get(path);
                    if (list == null) {
                        map.put(path, list = new ArrayList<LocalLoader>());
                        list.add(localLoader);
                    } else if (!list.contains(localLoader)) {
                        list.add(localLoader);
                    }
                }
            }
        }
    // else unknown dep type so just skip
    }
    return subtract;
}
Also used : PathFilter(org.jboss.modules.filter.PathFilter) HashSet(java.util.HashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) ClassFilter(org.jboss.modules.filter.ClassFilter) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with ClassFilter

use of org.jboss.modules.filter.ClassFilter 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)

Aggregations

ClassFilter (org.jboss.modules.filter.ClassFilter)3 PathFilter (org.jboss.modules.filter.PathFilter)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Set (java.util.Set)2 BarImpl (org.jboss.modules.test.BarImpl)1 QuxBar (org.jboss.modules.test.QuxBar)1 QuxFoo (org.jboss.modules.test.QuxFoo)1 QuxImpl (org.jboss.modules.test.QuxImpl)1 Test (org.junit.Test)1