Search in sources :

Example 1 with NativeLibraryResourceLoader

use of org.jboss.modules.NativeLibraryResourceLoader in project jboss-modules by jboss-modules.

the class ModuleXmlParser method parseResources.

private static void parseResources(final MavenResolver mavenResolver, final ResourceRootFactory factory, final String rootPath, final XmlPullParser reader, final ModuleSpec.Builder specBuilder) throws XmlPullParserException, IOException {
    assertNoAttributes(reader);
    final List<Version> detectedVersions = new ArrayList<>();
    // xsd:choice
    int eventType;
    for (; ; ) {
        eventType = reader.nextTag();
        switch(eventType) {
            case END_TAG:
                {
                    final Version specifiedVersion = specBuilder.getVersion();
                    if (specifiedVersion == null) {
                        final Iterator<Version> iterator = detectedVersions.iterator();
                        if (iterator.hasNext()) {
                            Version guess = iterator.next();
                            while (iterator.hasNext()) {
                                if (!guess.equals(iterator.next())) {
                                    guess = null;
                                    break;
                                }
                            }
                            if (guess != null)
                                specBuilder.setVersion(guess);
                        }
                    }
                    specBuilder.addResourceRoot(ResourceLoaderSpec.createResourceLoaderSpec(new NativeLibraryResourceLoader(new File(rootPath, "lib")), PathFilters.rejectAll()));
                    return;
                }
            case START_TAG:
                {
                    validateNamespace(reader);
                    switch(reader.getName()) {
                        case E_RESOURCE_ROOT:
                            {
                                final Version version = parseResourceRoot(factory, rootPath, reader, specBuilder);
                                if (version != null)
                                    detectedVersions.add(version);
                                break;
                            }
                        case E_ARTIFACT:
                            {
                                final Version version = parseArtifact(mavenResolver, reader, specBuilder);
                                if (version != null)
                                    detectedVersions.add(version);
                                break;
                            }
                        case E_NATIVE_ARTIFACT:
                            {
                                parseNativeArtifact(mavenResolver, reader, specBuilder);
                                break;
                            }
                        default:
                            throw unexpectedContent(reader);
                    }
                    break;
                }
            default:
                {
                    throw unexpectedContent(reader);
                }
        }
    }
}
Also used : Version(org.jboss.modules.Version) NativeLibraryResourceLoader(org.jboss.modules.NativeLibraryResourceLoader) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) JarFile(java.util.jar.JarFile) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 2 with NativeLibraryResourceLoader

use of org.jboss.modules.NativeLibraryResourceLoader in project jboss-modules by jboss-modules.

the class ModuleXmlParser method createMavenNativeArtifactLoader.

private static void createMavenNativeArtifactLoader(final MavenResolver mavenResolver, final String name, final XmlPullParser reader, final ModuleSpec.Builder specBuilder) throws IOException, XmlPullParserException {
    File fp = mavenResolver.resolveJarArtifact(ArtifactCoordinates.fromString(name));
    if (fp == null)
        throw new XmlPullParserException(String.format("Failed to resolve native artifact '%s'", name), reader, null);
    File lib = new File(fp.getParentFile(), "lib");
    if (!lib.exists()) {
        if (!fp.getParentFile().canWrite())
            throw new XmlPullParserException(String.format("Native artifact '%s' cannot be unpacked", name), reader, null);
        unzip(fp, fp.getParentFile());
    }
    specBuilder.addResourceRoot(ResourceLoaderSpec.createResourceLoaderSpec(new NativeLibraryResourceLoader(lib), PathFilters.rejectAll()));
}
Also used : NativeLibraryResourceLoader(org.jboss.modules.NativeLibraryResourceLoader) JarFile(java.util.jar.JarFile) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 3 with NativeLibraryResourceLoader

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

File (java.io.File)3 NativeLibraryResourceLoader (org.jboss.modules.NativeLibraryResourceLoader)3 ArrayList (java.util.ArrayList)2 JarFile (java.util.jar.JarFile)2 ZipFile (java.util.zip.ZipFile)2 Iterator (java.util.Iterator)1 ArtifactResult (org.eclipse.ceylon.model.cmr.ArtifactResult)1 AliasModuleSpec (org.jboss.modules.AliasModuleSpec)1 DependencySpec (org.jboss.modules.DependencySpec)1 LocalLoader (org.jboss.modules.LocalLoader)1 ModuleIdentifier (org.jboss.modules.ModuleIdentifier)1 ModuleLoadException (org.jboss.modules.ModuleLoadException)1 ModuleSpec (org.jboss.modules.ModuleSpec)1 Builder (org.jboss.modules.ModuleSpec.Builder)1 ResourceLoader (org.jboss.modules.ResourceLoader)1 ResourceLoaderSpec (org.jboss.modules.ResourceLoaderSpec)1 Version (org.jboss.modules.Version)1 PathFilter (org.jboss.modules.filter.PathFilter)1