Search in sources :

Example 1 with PathFilter

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

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

the class PathFilterTest method testDelegating.

@Test
public void testDelegating() throws Exception {
    final MultiplePathFilterBuilder builder = PathFilters.multiplePathFilterBuilder(true);
    builder.addFilter(PathFilters.match("foo/*"), false);
    builder.addFilter(PathFilters.match("**/bar/**"), false);
    builder.addFilter(PathFilters.match("baz/**"), false);
    PathFilter pathFilter = builder.create();
    assertTrue(pathFilter.accept("foo"));
    assertFalse(pathFilter.accept("foo/bar"));
    assertFalse(pathFilter.accept("foo/bar/baz"));
    assertFalse(pathFilter.accept("baz/foo/bar"));
}
Also used : PathFilter(org.jboss.modules.filter.PathFilter) MultiplePathFilterBuilder(org.jboss.modules.filter.MultiplePathFilterBuilder) Test(org.junit.Test)

Example 3 with PathFilter

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

the class DependencyTreeViewer method print.

private static void print(PrintWriter out, String prefix, DependencySpec spec, FastCopyHashSet<String> visited, final boolean last, final File... roots) {
    if (spec instanceof ModuleDependencySpec) {
        final ModuleDependencySpec moduleDependencySpec = (ModuleDependencySpec) spec;
        final String name = moduleDependencySpec.getName();
        out.print(prefix);
        out.print(last ? '└' : '├');
        out.print('─');
        out.print(' ');
        out.print(name);
        if (moduleDependencySpec.isOptional()) {
            out.print(" (optional)");
        }
        final PathFilter exportFilter = moduleDependencySpec.getExportFilter();
        if (!exportFilter.equals(PathFilters.rejectAll())) {
            out.print(" (exported)");
        }
        if (visited.add(name)) {
            print(out, prefix + (last ? "   " : "│  "), name, visited, roots);
        } else {
            out.println();
        }
    }
}
Also used : PathFilter(org.jboss.modules.filter.PathFilter)

Example 4 with PathFilter

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

the class KernelDeploymentModuleProcessor method deploy.

/**
 * Add POJO module if we have any bean factories.
 *
 * @param phaseContext the deployment unit context
 * @throws org.jboss.as.server.deployment.DeploymentUnitProcessingException
 */
@Override
public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    final DeploymentUnit unit = phaseContext.getDeploymentUnit();
    final List<KernelDeploymentXmlDescriptor> kdXmlDescriptors = unit.getAttachment(KernelDeploymentXmlDescriptor.ATTACHMENT_KEY);
    if (kdXmlDescriptors == null || kdXmlDescriptors.isEmpty())
        return;
    for (KernelDeploymentXmlDescriptor kdxd : kdXmlDescriptors) {
        if (kdxd.getBeanFactoriesCount() > 0) {
            final ModuleSpecification moduleSpecification = unit.getAttachment(Attachments.MODULE_SPECIFICATION);
            final ModuleLoader moduleLoader = Module.getBootModuleLoader();
            ModuleDependency dependency = new ModuleDependency(moduleLoader, POJO_MODULE, false, false, false, false);
            PathFilter filter = PathFilters.isChildOf(BaseBeanFactory.class.getPackage().getName());
            dependency.addImportFilter(filter, true);
            dependency.addImportFilter(PathFilters.rejectAll(), false);
            moduleSpecification.addSystemDependency(dependency);
            return;
        }
    }
}
Also used : ModuleLoader(org.jboss.modules.ModuleLoader) PathFilter(org.jboss.modules.filter.PathFilter) ModuleDependency(org.jboss.as.server.deployment.module.ModuleDependency) ModuleSpecification(org.jboss.as.server.deployment.module.ModuleSpecification) KernelDeploymentXmlDescriptor(org.jboss.as.pojo.descriptor.KernelDeploymentXmlDescriptor) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit)

Example 5 with PathFilter

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

the class BootstrapModuleFinder method buildModule.

@Override
public void buildModule(ModuleSpec.Builder builder, ModuleLoader delegateLoader) throws ModuleLoadException {
    try (AutoCloseable handle = Performance.accumulate("module: Bootstrap")) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("Loading module");
        }
        ApplicationEnvironment env = ApplicationEnvironment.get();
        PathFilter filter = new PathFilter() {

            @Override
            public boolean accept(String path) {
                return path.endsWith("/module.xml");
            }
        };
        env.bootstrapArtifactsAsCoordinates().forEach((coords) -> {
            try {
                File artifact = MavenResolvers.get().resolveJarArtifact(coords);
                if (artifact == null) {
                    throw new RuntimeException("Unable to resolve artifact from coordinates: " + coords);
                }
                JarFile jar = JarFileManager.INSTANCE.addJarFile(artifact);
                ResourceLoader originaloader = ResourceLoaders.createJarResourceLoader(artifact.getName(), jar);
                builder.addResourceRoot(ResourceLoaderSpec.createResourceLoaderSpec(ResourceLoaders.createFilteredResourceLoader(filter, originaloader)));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
        builder.addDependency(DependencySpec.createLocalDependencySpec());
        builder.addDependency(DependencySpec.createModuleDependencySpec("org.jboss.modules"));
        builder.addDependency(DependencySpec.createModuleDependencySpec("org.jboss.shrinkwrap"));
        builder.addDependency(DependencySpec.createModuleDependencySpec("org.yaml.snakeyaml"));
        HashSet<String> paths = new HashSet<>();
        paths.add("org/wildfly/swarm/bootstrap/env");
        paths.add("org/wildfly/swarm/bootstrap/logging");
        paths.add("org/wildfly/swarm/bootstrap/modules");
        paths.add("org/wildfly/swarm/bootstrap/performance");
        paths.add("org/wildfly/swarm/bootstrap/util");
        builder.addDependency(DependencySpec.createSystemDependencySpec(paths, true));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ResourceLoader(org.jboss.modules.ResourceLoader) PathFilter(org.jboss.modules.filter.PathFilter) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) ApplicationEnvironment(org.wildfly.swarm.bootstrap.env.ApplicationEnvironment) JarFile(java.util.jar.JarFile) File(java.io.File) IOException(java.io.IOException) ModuleLoadException(org.jboss.modules.ModuleLoadException) HashSet(java.util.HashSet)

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