Search in sources :

Example 6 with MultiplePathFilterBuilder

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

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

the class ModuleXmlParser method parseModuleContents.

private static ModuleSpec.Builder parseModuleContents(final MavenResolver mavenResolver, final XmlPullParser reader, final ResourceRootFactory factory, final ModuleLoader moduleLoader, final String moduleName, final String rootPath) throws XmlPullParserException, IOException {
    final int count = reader.getAttributeCount();
    String name = null;
    String slot = null;
    boolean is1_6 = atLeast1_6(reader);
    Version version = null;
    final Set<String> required = is1_6 ? new HashSet<>(LIST_A_NAME) : new HashSet<>(LIST_A_NAME);
    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 (is1_6)
                    throw unknownAttribute(reader, i);
                else
                    slot = reader.getAttributeValue(i);
                break;
            case A_VERSION:
                try {
                    version = Version.parse(reader.getAttributeValue(i));
                    break;
                } catch (IllegalArgumentException ex) {
                    throw new XmlPullParserException(ex.getMessage(), reader, ex);
                }
            default:
                throw unknownAttribute(reader, i);
        }
    }
    if (!required.isEmpty()) {
        throw missingAttributes(reader, required);
    }
    final String realModuleName;
    if (is1_6) {
        realModuleName = name;
    } else {
        realModuleName = ModuleIdentifier.create(name, slot).toString();
    }
    if (moduleName != null && !realModuleName.equals(moduleName)) {
        throw invalidModuleName(reader, realModuleName);
    }
    final ModuleSpec.Builder specBuilder = ModuleSpec.build(realModuleName);
    specBuilder.setVersion(version);
    // xsd:all
    MultiplePathFilterBuilder exportsBuilder = PathFilters.multiplePathFilterBuilder(true);
    ArrayList<DependencySpec> dependencies = new ArrayList<>();
    final boolean is1_8 = atLeast1_8(reader);
    if (!is1_8) {
        // add default system dependencies
        specBuilder.addDependency(DEP_JAVA_SE);
        specBuilder.addDependency(DEP_JDK_UNSUPPORTED);
    }
    Set<String> visited = new HashSet<>();
    int eventType;
    boolean gotPerms = false;
    for (; ; ) {
        eventType = reader.nextTag();
        switch(eventType) {
            case END_TAG:
                {
                    specBuilder.addDependency(new LocalDependencySpecBuilder().setExportFilter(exportsBuilder.create()).build());
                    for (DependencySpec dependency : dependencies) {
                        specBuilder.addDependency(dependency);
                    }
                    if (!gotPerms)
                        specBuilder.setPermissionCollection(DEFAULT_PERMISSION_COLLECTION);
                    return specBuilder;
                }
            case START_TAG:
                {
                    validateNamespace(reader);
                    final String element = reader.getName();
                    if (visited.contains(element)) {
                        throw unexpectedContent(reader);
                    }
                    visited.add(element);
                    switch(element) {
                        case E_EXPORTS:
                            parseFilterList(reader, exportsBuilder);
                            break;
                        case E_DEPENDENCIES:
                            parseDependencies(reader, dependencies);
                            break;
                        case E_MAIN_CLASS:
                            parseMainClass(reader, specBuilder);
                            break;
                        case E_RESOURCES:
                            parseResources(mavenResolver, factory, rootPath, reader, specBuilder);
                            break;
                        case E_PROPERTIES:
                            parseProperties(reader, specBuilder);
                            break;
                        case E_PERMISSIONS:
                            parsePermissions(reader, moduleLoader, realModuleName, specBuilder);
                            gotPerms = true;
                            break;
                        case E_PROVIDES:
                            if (is1_8)
                                parseProvidesType(reader, specBuilder);
                            else
                                throw unexpectedContent(reader);
                            break;
                        default:
                            throw unexpectedContent(reader);
                    }
                    break;
                }
            default:
                {
                    throw unexpectedContent(reader);
                }
        }
    }
}
Also used : LocalDependencySpecBuilder(org.jboss.modules.LocalDependencySpecBuilder) ArrayList(java.util.ArrayList) ModuleSpec(org.jboss.modules.ModuleSpec) Version(org.jboss.modules.Version) DependencySpec(org.jboss.modules.DependencySpec) ModuleDependencySpec(org.jboss.modules.ModuleDependencySpec) MultiplePathFilterBuilder(org.jboss.modules.filter.MultiplePathFilterBuilder) HashSet(java.util.HashSet)

Example 8 with MultiplePathFilterBuilder

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

the class ModuleClassLoaderTest method setupModuleLoader.

@Before
public void setupModuleLoader() throws Exception {
    moduleLoader = new TestModuleLoader();
    final ModuleSpec.Builder moduleWithContentBuilder = ModuleSpec.build(MODULE_WITH_CONTENT_ID);
    moduleWithContentBuilder.addResourceRoot(ResourceLoaderSpec.createResourceLoaderSpec(TestResourceLoader.build().addClass(TestClass.class).addResources(getResource("test/modulecontentloader/rootOne")).create()));
    moduleWithContentBuilder.addDependency(new ModuleDependencySpecBuilder().setName(MODULE_TO_IMPORT_ID.toString()).build());
    moduleWithContentBuilder.addDependency(DependencySpec.createLocalDependencySpec());
    moduleLoader.addModuleSpec(moduleWithContentBuilder.create());
    final ModuleSpec.Builder moduleWithResourceBuilder = ModuleSpec.build(MODULE_WITH_RESOURCE_ID);
    moduleWithResourceBuilder.addResourceRoot(ResourceLoaderSpec.createResourceLoaderSpec(TestResourceLoader.build().addClass(TestClass.class).addResources(getResource("class-resources")).create()));
    moduleWithResourceBuilder.addDependency(new ModuleDependencySpecBuilder().setName(MODULE_TO_IMPORT_ID.toString()).build());
    moduleWithResourceBuilder.addDependency(DependencySpec.createLocalDependencySpec());
    moduleLoader.addModuleSpec(moduleWithResourceBuilder.create());
    final ModuleSpec.Builder moduleToImportBuilder = ModuleSpec.build(MODULE_TO_IMPORT_ID);
    moduleToImportBuilder.addResourceRoot(ResourceLoaderSpec.createResourceLoaderSpec(TestResourceLoader.build().addClass(ImportedClass.class).addClass(ImportedInterface.class).addResources(getResource("test/modulecontentloader/rootTwo")).create()));
    moduleToImportBuilder.addDependency(DependencySpec.createLocalDependencySpec());
    moduleLoader.addModuleSpec(moduleToImportBuilder.create());
    final ModuleSpec.Builder moduleWithExportBuilder = ModuleSpec.build(MODULE_WITH_EXPORT_ID);
    moduleWithExportBuilder.addDependency(new ModuleDependencySpecBuilder().setName(MODULE_TO_IMPORT_ID.toString()).setExport(true).setOptional(false).build());
    moduleWithExportBuilder.addDependency(DependencySpec.createLocalDependencySpec());
    moduleLoader.addModuleSpec(moduleWithExportBuilder.create());
    final MultiplePathFilterBuilder nestedAndOrgJBossExcludingBuilder = PathFilters.multiplePathFilterBuilder(true);
    nestedAndOrgJBossExcludingBuilder.addFilter(PathFilters.match("org/jboss/**"), false);
    nestedAndOrgJBossExcludingBuilder.addFilter(PathFilters.match("nested"), false);
    final ModuleSpec.Builder moduleWithExportFilterBuilder = ModuleSpec.build(MODULE_WITH_FILTERED_EXPORT_ID);
    moduleWithExportFilterBuilder.addDependency(new ModuleDependencySpecBuilder().setExportFilter(nestedAndOrgJBossExcludingBuilder.create()).setModuleLoader(null).setName(MODULE_TO_IMPORT_ID.toString()).setOptional(false).build());
    moduleWithExportFilterBuilder.addDependency(DependencySpec.createLocalDependencySpec());
    moduleLoader.addModuleSpec(moduleWithExportFilterBuilder.create());
    final ModuleSpec.Builder moduleWithImportFilterBuilder = ModuleSpec.build(MODULE_WITH_FILTERED_IMPORT_ID);
    moduleWithImportFilterBuilder.addDependency(new ModuleDependencySpecBuilder().setImportFilter(nestedAndOrgJBossExcludingBuilder.create()).setName(MODULE_TO_IMPORT_ID.toString()).setOptional(false).build());
    moduleWithImportFilterBuilder.addDependency(DependencySpec.createLocalDependencySpec());
    moduleLoader.addModuleSpec(moduleWithImportFilterBuilder.create());
    final ModuleSpec.Builder moduleWithDoubleExportBuilder = ModuleSpec.build(MODULE_WITH_DOUBLE_EXPORT_ID);
    moduleWithDoubleExportBuilder.addDependency(new ModuleDependencySpecBuilder().setName(MODULE_TO_IMPORT_ID.toString()).setExport(true).build());
    moduleWithDoubleExportBuilder.addDependency(new ModuleDependencySpecBuilder().setName(MODULE_WITH_CONTENT_ID.toString()).setExport(true).build());
    moduleWithDoubleExportBuilder.addDependency(DependencySpec.createLocalDependencySpec());
    moduleLoader.addModuleSpec(moduleWithDoubleExportBuilder.create());
    final ModuleSpec.Builder moduleWithInvertedDoubleExportBuilder = ModuleSpec.build(MODULE_WITH_INVERTED_DOUBLE_EXPORT_ID);
    moduleWithInvertedDoubleExportBuilder.addDependency(new ModuleDependencySpecBuilder().setName(MODULE_WITH_CONTENT_ID.toString()).setExport(true).build());
    moduleWithInvertedDoubleExportBuilder.addDependency(new ModuleDependencySpecBuilder().setName(MODULE_TO_IMPORT_ID.toString()).setExport(true).build());
    moduleWithInvertedDoubleExportBuilder.addDependency(DependencySpec.createLocalDependencySpec());
    moduleLoader.addModuleSpec(moduleWithInvertedDoubleExportBuilder.create());
    final ModuleSpec.Builder moduleWithFilteredDoubleExportBuilder = ModuleSpec.build(MODULE_WITH_FILTERED_DOUBLE_EXPORT_ID);
    moduleWithFilteredDoubleExportBuilder.addDependency(new ModuleDependencySpecBuilder().setImportFilter(PathFilters.not(PathFilters.match("nested"))).setExport(true).setName(MODULE_TO_IMPORT_ID.toString()).build());
    moduleWithFilteredDoubleExportBuilder.addDependency(new ModuleDependencySpecBuilder().setName(MODULE_WITH_EXPORT_ID.toString()).setExport(true).build());
    moduleWithFilteredDoubleExportBuilder.addDependency(DependencySpec.createLocalDependencySpec());
    moduleLoader.addModuleSpec(moduleWithFilteredDoubleExportBuilder.create());
}
Also used : ImportedClass(org.jboss.modules.test.ImportedClass) TestModuleLoader(org.jboss.modules.util.TestModuleLoader) MultiplePathFilterBuilder(org.jboss.modules.filter.MultiplePathFilterBuilder) Before(org.junit.Before)

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