Search in sources :

Example 1 with MultiplePathFilterBuilder

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

the class JarModuleFinder method findModule.

public ModuleSpec findModule(final String name, final ModuleLoader delegateLoader) throws ModuleLoadException {
    if (name.equals(myName)) {
        // special root JAR module
        Manifest manifest;
        try {
            manifest = jarFile.getManifest();
        } catch (IOException e) {
            throw new ModuleLoadException("Failed to load MANIFEST from JAR", e);
        }
        ModuleSpec.Builder builder = ModuleSpec.build(name);
        Attributes mainAttributes = manifest.getMainAttributes();
        String mainClass = mainAttributes.getValue(Attributes.Name.MAIN_CLASS);
        if (mainClass != null) {
            builder.setMainClass(mainClass);
        }
        String classPath = mainAttributes.getValue(Attributes.Name.CLASS_PATH);
        String dependencies = mainAttributes.getValue(DEPENDENCIES);
        MultiplePathFilterBuilder pathFilterBuilder = PathFilters.multiplePathFilterBuilder(true);
        pathFilterBuilder.addFilter(PathFilters.is(MODULES_DIR), false);
        pathFilterBuilder.addFilter(PathFilters.isChildOf(MODULES_DIR), false);
        builder.addResourceRoot(ResourceLoaderSpec.createResourceLoaderSpec(new JarFileResourceLoader("", jarFile), pathFilterBuilder.create()));
        String[] classPathEntries = classPath == null ? Utils.NO_STRINGS : classPath.split("\\s+");
        for (String entry : classPathEntries) {
            if (!entry.isEmpty()) {
                if (entry.startsWith("../") || entry.startsWith("./") || entry.startsWith("/") || entry.contains("/../")) {
                    // invalid
                    continue;
                }
                File root;
                try {
                    File path = new File(new URI(entry));
                    if (path.isAbsolute()) {
                        root = path;
                    } else {
                        root = new File(jarFile.getName(), path.getPath());
                    }
                } catch (URISyntaxException e) {
                    // invalid, will probably fail anyway
                    root = new File(jarFile.getName(), entry);
                }
                if (entry.endsWith("/")) {
                    // directory reference
                    builder.addResourceRoot(ResourceLoaderSpec.createResourceLoaderSpec(new PathResourceLoader(entry, root.toPath(), context)));
                } else {
                    // assume a JAR
                    JarFile childJarFile;
                    try {
                        childJarFile = JDKSpecific.getJarFile(root, true);
                    } catch (IOException e) {
                        // ignore and continue
                        continue;
                    }
                    builder.addResourceRoot(ResourceLoaderSpec.createResourceLoaderSpec(new JarFileResourceLoader(entry, childJarFile)));
                }
            }
        }
        String[] dependencyEntries = dependencies == null ? Utils.NO_STRINGS : dependencies.split("\\s*,\\s*");
        for (String dependencyEntry : dependencyEntries) {
            boolean optional = false;
            boolean export = false;
            dependencyEntry = dependencyEntry.trim();
            if (!dependencyEntry.isEmpty()) {
                String[] fields = dependencyEntry.split("\\s+");
                if (fields.length < 1) {
                    continue;
                }
                String moduleName = fields[0];
                for (int i = 1; i < fields.length; i++) {
                    String field = fields[i];
                    if (field.equals(OPTIONAL)) {
                        optional = true;
                    } else if (field.equals(EXPORT)) {
                        export = true;
                    }
                // else ignored
                }
                builder.addDependency(new ModuleDependencySpecBuilder().setName(moduleName).setExport(export).setOptional(optional).build());
            }
        }
        builder.addDependency(DependencySpec.createSystemDependencySpec(JDKPaths.JDK));
        builder.addDependency(DependencySpec.createLocalDependencySpec());
        return builder.create();
    } else {
        final String path = PathUtils.basicModuleNameToPath(name);
        if (path == null) {
            // not valid, so not found
            return null;
        }
        String basePath = MODULES_DIR + "/" + path;
        JarEntry moduleXmlEntry = jarFile.getJarEntry(basePath + "/" + MODULE_FILE);
        if (moduleXmlEntry == null) {
            return null;
        }
        ModuleSpec moduleSpec;
        try {
            try (final InputStream inputStream = jarFile.getInputStream(moduleXmlEntry)) {
                moduleSpec = ModuleXmlParser.parseModuleXml((rootPath, loaderPath, loaderName) -> new JarFileResourceLoader(loaderName, jarFile, loaderPath), basePath, inputStream, moduleXmlEntry.getName(), delegateLoader, name);
            }
        } catch (IOException e) {
            throw new ModuleLoadException("Failed to read " + MODULE_FILE + " file", e);
        }
        return moduleSpec;
    }
}
Also used : AccessControlContext(java.security.AccessControlContext) Manifest(java.util.jar.Manifest) URISyntaxException(java.net.URISyntaxException) MODULE_FILE(org.jboss.modules.Utils.MODULE_FILE) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) MultiplePathFilterBuilder(org.jboss.modules.filter.MultiplePathFilterBuilder) Attributes(java.util.jar.Attributes) File(java.io.File) DEPENDENCIES(org.jboss.modules.Utils.DEPENDENCIES) PathFilters(org.jboss.modules.filter.PathFilters) JarEntry(java.util.jar.JarEntry) MODULES_DIR(org.jboss.modules.Utils.MODULES_DIR) ModuleXmlParser(org.jboss.modules.xml.ModuleXmlParser) URI(java.net.URI) AccessController(java.security.AccessController) InputStream(java.io.InputStream) EXPORT(org.jboss.modules.Utils.EXPORT) OPTIONAL(org.jboss.modules.Utils.OPTIONAL) InputStream(java.io.InputStream) Attributes(java.util.jar.Attributes) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) Manifest(java.util.jar.Manifest) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) URI(java.net.URI) MultiplePathFilterBuilder(org.jboss.modules.filter.MultiplePathFilterBuilder) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 2 with MultiplePathFilterBuilder

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

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

the class ModuleXmlParser method parseArtifact.

private static Version parseArtifact(final MavenResolver mavenResolver, final XmlPullParser reader, final ModuleSpec.Builder specBuilder) throws XmlPullParserException, IOException {
    String name = null;
    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;
            default:
                throw unknownAttribute(reader, i);
        }
    }
    if (!required.isEmpty()) {
        throw missingAttributes(reader, required);
    }
    final MultiplePathFilterBuilder filterBuilder = PathFilters.multiplePathFilterBuilder(true);
    final SystemPropertyConditionBuilder conditionBuilder = new SystemPropertyConditionBuilder();
    final ResourceLoader resourceLoader;
    final Set<String> encountered = new HashSet<>();
    int eventType;
    for (; ; ) {
        eventType = reader.nextTag();
        switch(eventType) {
            case END_TAG:
                {
                    if (conditionBuilder.resolve()) {
                        final ArtifactCoordinates coordinates;
                        try {
                            coordinates = ArtifactCoordinates.fromString(name);
                            resourceLoader = MavenArtifactUtil.createMavenArtifactLoader(mavenResolver, coordinates, name);
                        } catch (IOException | IllegalArgumentException e) {
                            throw new XmlPullParserException(String.format("Failed to add artifact '%s'", name), reader, e);
                        }
                        if (resourceLoader == null)
                            throw new XmlPullParserException(String.format("Failed to resolve artifact '%s'", name), reader, null);
                        specBuilder.addResourceRoot(ResourceLoaderSpec.createResourceLoaderSpec(resourceLoader, filterBuilder.create()));
                        final String version = coordinates.getVersion();
                        try {
                            return Version.parse(version);
                        } catch (IllegalArgumentException ignored) {
                            return null;
                        }
                    }
                    return null;
                }
            case START_TAG:
                {
                    validateNamespace(reader);
                    final String element = reader.getName();
                    if (!encountered.add(element))
                        throw unexpectedContent(reader);
                    switch(element) {
                        case E_FILTER:
                            parseFilterList(reader, filterBuilder);
                            break;
                        case E_CONDITIONS:
                            parseConditions(reader, conditionBuilder);
                            break;
                        default:
                            throw unexpectedContent(reader);
                    }
                    break;
                }
            default:
                {
                    throw unexpectedContent(reader);
                }
        }
    }
}
Also used : NativeLibraryResourceLoader(org.jboss.modules.NativeLibraryResourceLoader) ResourceLoader(org.jboss.modules.ResourceLoader) ArtifactCoordinates(org.jboss.modules.maven.ArtifactCoordinates) MultiplePathFilterBuilder(org.jboss.modules.filter.MultiplePathFilterBuilder) HashSet(java.util.HashSet)

Example 4 with MultiplePathFilterBuilder

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

the class ModuleXmlParser method parseResourceRoot.

private static Version parseResourceRoot(final ResourceRootFactory factory, final String rootPath, final XmlPullParser reader, final ModuleSpec.Builder specBuilder) throws XmlPullParserException, IOException {
    String name = null;
    String path = null;
    final Set<String> required = new HashSet<>(LIST_A_PATH);
    final int count = reader.getAttributeCount();
    final boolean is1_8 = atLeast1_8(reader);
    for (int i = 0; i < count; i++) {
        validateAttributeNamespace(reader, i);
        final String attribute = reader.getAttributeName(i);
        required.remove(attribute);
        switch(attribute) {
            case A_NAME:
                if (!is1_8)
                    name = reader.getAttributeValue(i);
                else
                    throw unknownAttribute(reader, i);
                break;
            case A_PATH:
                path = reader.getAttributeValue(i);
                break;
            default:
                throw unknownAttribute(reader, i);
        }
    }
    if (!required.isEmpty()) {
        throw missingAttributes(reader, required);
    }
    if (name == null)
        name = is1_8 ? "unnamed" : path;
    final MultiplePathFilterBuilder filterBuilder = PathFilters.multiplePathFilterBuilder(true);
    final SystemPropertyConditionBuilder conditionBuilder = new SystemPropertyConditionBuilder();
    final ResourceLoader resourceLoader;
    final Set<String> encountered = new HashSet<>();
    int eventType;
    for (; ; ) {
        eventType = reader.nextTag();
        switch(eventType) {
            case END_TAG:
                {
                    if (conditionBuilder.resolve()) {
                        try {
                            resourceLoader = factory.createResourceLoader(rootPath, path, name);
                        } catch (IOException e) {
                            throw new XmlPullParserException(String.format("Failed to add resource root '%s' at path '%s'", name, path), reader, e);
                        }
                        specBuilder.addResourceRoot(ResourceLoaderSpec.createResourceLoaderSpec(resourceLoader, filterBuilder.create()));
                        if (specBuilder.getVersion() == null) {
                            return VersionDetection.detectVersion(resourceLoader);
                        } else {
                            return null;
                        }
                    } else {
                        return null;
                    }
                }
            case START_TAG:
                {
                    validateNamespace(reader);
                    final String element = reader.getName();
                    if (!encountered.add(element))
                        throw unexpectedContent(reader);
                    switch(element) {
                        case E_FILTER:
                            parseFilterList(reader, filterBuilder);
                            break;
                        case E_CONDITIONS:
                            parseConditions(reader, conditionBuilder);
                            break;
                        default:
                            throw unexpectedContent(reader);
                    }
                    break;
                }
            default:
                {
                    throw unexpectedContent(reader);
                }
        }
    }
}
Also used : NativeLibraryResourceLoader(org.jboss.modules.NativeLibraryResourceLoader) ResourceLoader(org.jboss.modules.ResourceLoader) IOException(java.io.IOException) MultiplePathFilterBuilder(org.jboss.modules.filter.MultiplePathFilterBuilder) HashSet(java.util.HashSet)

Example 5 with MultiplePathFilterBuilder

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

the class ModuleXmlParser method parseSystemDependency.

private static void parseSystemDependency(final XmlPullParser reader, final ArrayList<DependencySpec> dependencies) throws XmlPullParserException, IOException {
    boolean export = false;
    final int count = reader.getAttributeCount();
    for (int i = 0; i < count; i++) {
        validateAttributeNamespace(reader, i);
        final String attribute = reader.getAttributeName(i);
        switch(attribute) {
            case A_EXPORT:
                export = Boolean.parseBoolean(reader.getAttributeValue(i));
                break;
            default:
                throw unexpectedContent(reader);
        }
    }
    Set<String> paths = Collections.emptySet();
    final MultiplePathFilterBuilder exportBuilder = PathFilters.multiplePathFilterBuilder(export);
    int eventType;
    for (; ; ) {
        eventType = reader.nextTag();
        switch(eventType) {
            case END_TAG:
                {
                    final PathFilter exportFilter = exportBuilder.create();
                    dependencies.add(DependencySpec.createSystemDependencySpec(PathFilters.acceptAll(), exportFilter, paths));
                    return;
                }
            case START_TAG:
                {
                    validateNamespace(reader);
                    switch(reader.getName()) {
                        case E_PATHS:
                            {
                                paths = parseSet(reader);
                                break;
                            }
                        case E_EXPORTS:
                            {
                                parseFilterList(reader, exportBuilder);
                                break;
                            }
                        default:
                            {
                                throw unexpectedContent(reader);
                            }
                    }
                }
        }
    }
}
Also used : PathFilter(org.jboss.modules.filter.PathFilter) MultiplePathFilterBuilder(org.jboss.modules.filter.MultiplePathFilterBuilder)

Aggregations

MultiplePathFilterBuilder (org.jboss.modules.filter.MultiplePathFilterBuilder)8 HashSet (java.util.HashSet)4 PathFilter (org.jboss.modules.filter.PathFilter)3 IOException (java.io.IOException)2 NativeLibraryResourceLoader (org.jboss.modules.NativeLibraryResourceLoader)2 ResourceLoader (org.jboss.modules.ResourceLoader)2 File (java.io.File)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 AccessControlContext (java.security.AccessControlContext)1 AccessController (java.security.AccessController)1 ArrayList (java.util.ArrayList)1 Attributes (java.util.jar.Attributes)1 JarEntry (java.util.jar.JarEntry)1 JarFile (java.util.jar.JarFile)1 Manifest (java.util.jar.Manifest)1 DependencySpec (org.jboss.modules.DependencySpec)1 LocalDependencySpecBuilder (org.jboss.modules.LocalDependencySpecBuilder)1 ModuleDependencySpec (org.jboss.modules.ModuleDependencySpec)1