Search in sources :

Example 1 with ResourceLoader

use of org.jboss.modules.ResourceLoader 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 2 with ResourceLoader

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

the class ApplicationModuleFinder method addAsset.

protected void addAsset(ModuleSpec.Builder builder, ApplicationEnvironment env) throws IOException {
    String path = env.getAsset();
    if (path == null) {
        return;
    }
    int slashLoc = path.lastIndexOf('/');
    String name = path;
    if (slashLoc > 0) {
        name = path.substring(slashLoc + 1);
    }
    String ext = ".jar";
    int dotLoc = name.lastIndexOf('.');
    if (dotLoc > 0) {
        ext = name.substring(dotLoc);
        name = name.substring(0, dotLoc);
    }
    File tmp = TempFileManager.INSTANCE.newTempFile(name, ext);
    try (InputStream artifactIn = getClass().getClassLoader().getResourceAsStream(path)) {
        Files.copy(artifactIn, tmp.toPath(), StandardCopyOption.REPLACE_EXISTING);
    }
    final String jarName = tmp.getName().toString();
    final JarFile jarFile = new JarFile(tmp);
    File tmpDir = TempFileManager.INSTANCE.newTempDirectory(name, ext);
    // Explode jar due to some issues in Windows on stopping (JarFiles cannot be deleted)
    BootstrapUtil.explodeJar(jarFile, tmpDir.getAbsolutePath());
    jarFile.close();
    tmp.delete();
    final ResourceLoader jarLoader = ResourceLoaders.createFileResourceLoader(jarName, tmpDir);
    builder.addResourceRoot(ResourceLoaderSpec.createResourceLoaderSpec(jarLoader));
    if (".war".equalsIgnoreCase(ext)) {
        final ResourceLoader warLoader = ResourceLoaders.createFileResourceLoader(jarName + "WEBINF", new File(tmpDir.getAbsolutePath() + File.separator + "WEB-INF" + File.separator + "classes"));
        builder.addResourceRoot(ResourceLoaderSpec.createResourceLoaderSpec(warLoader));
    }
}
Also used : ResourceLoader(org.jboss.modules.ResourceLoader) InputStream(java.io.InputStream) JarFile(java.util.jar.JarFile) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 3 with ResourceLoader

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

Example 4 with ResourceLoader

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

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

ResourceLoader (org.jboss.modules.ResourceLoader)5 File (java.io.File)3 HashSet (java.util.HashSet)3 NativeLibraryResourceLoader (org.jboss.modules.NativeLibraryResourceLoader)3 IOException (java.io.IOException)2 JarFile (java.util.jar.JarFile)2 ModuleLoadException (org.jboss.modules.ModuleLoadException)2 MultiplePathFilterBuilder (org.jboss.modules.filter.MultiplePathFilterBuilder)2 PathFilter (org.jboss.modules.filter.PathFilter)2 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)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 ModuleSpec (org.jboss.modules.ModuleSpec)1 Builder (org.jboss.modules.ModuleSpec.Builder)1 ResourceLoaderSpec (org.jboss.modules.ResourceLoaderSpec)1 ArtifactCoordinates (org.jboss.modules.maven.ArtifactCoordinates)1