org.jboss.modules.ResourceLoader Java Examples

The following examples show how to use org.jboss.modules.ResourceLoader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WildFlySwarmApplicationConf.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 5 votes vote down vote up
@Override
void apply(ModuleSpec.Builder builder) throws IOException {

    int slashLoc = this.path.lastIndexOf('/');
    String name = this.path;

    if (slashLoc > 0) {
        name = this.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(this.path)) {
        Files.copy(artifactIn, tmp.toPath(), StandardCopyOption.REPLACE_EXISTING);
    }
    final String jarName = tmp.getName().toString();
    final JarFile jarFile = new JarFile(tmp);
    final ResourceLoader jarLoader = ResourceLoaders.createJarResourceLoader(jarName,
            jarFile);
    builder.addResourceRoot(ResourceLoaderSpec.createResourceLoaderSpec(jarLoader));

    if (".war".equals(ext)) {
        final ResourceLoader warLoader = ResourceLoaders.createJarResourceLoader(jarName,
                jarFile,
                "WEB-INF/classes");
        builder.addResourceRoot(ResourceLoaderSpec.createResourceLoaderSpec(warLoader));
    }
}
 
Example #2
Source File: Seam2Processor.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    if (deploymentUnit.getParent() != null) {
        return;
    }

    final List<DeploymentUnit> deploymentUnits = new ArrayList<DeploymentUnit>();
    deploymentUnits.add(deploymentUnit);
    deploymentUnits.addAll(deploymentUnit.getAttachmentList(Attachments.SUB_DEPLOYMENTS));

    for (DeploymentUnit unit : deploymentUnits) {

        final ResourceRoot mainRoot = unit.getAttachment(Attachments.DEPLOYMENT_ROOT);
        if (mainRoot == null)
            continue;

        VirtualFile root = mainRoot.getRoot();
        for (String path : SEAM_FILES) {
            if (root.getChild(path).exists()) {
                final ModuleSpecification moduleSpecification = deploymentUnit
                        .getAttachment(Attachments.MODULE_SPECIFICATION);
                final ModuleLoader moduleLoader = Module.getBootModuleLoader();
                moduleSpecification.addSystemDependency(new ModuleDependency(moduleLoader, VFS_MODULE, false, false, false,
                        false)); // for VFS scanner

                try {
                    ResourceLoader resourceLoader = ResourceLoaders.createJarResourceLoader(SEAM_INT_JAR, new JarFile(
                            getSeamIntResourceRoot().getRoot().getPathName()));
                    moduleSpecification.addResourceLoader(ResourceLoaderSpec.createResourceLoaderSpec(resourceLoader));
                } catch (Exception e) {
                    throw new DeploymentUnitProcessingException(e);
                }

                unit.addToAttachmentList(Attachments.RESOURCE_ROOTS, getSeamIntResourceRoot());
                return;
            }
        }
    }
}
 
Example #3
Source File: JBossModuleUtils.java    From Nicobar with Apache License 2.0 5 votes vote down vote up
/**
 * Populates a builder with a {@link ResourceLoaderSpec} to a filesystem resource root.
 * {@link ScriptArchive}
 * @param moduleSpecBuilder builder to populate
 * @param compilationRoot a path to the compilation resource root directory
 */
public static void populateModuleSpecWithCompilationRoot(ModuleSpec.Builder moduleSpecBuilder, Path compilationRoot) {
    Objects.requireNonNull(moduleSpecBuilder, "moduleSpecBuilder");
    Objects.requireNonNull(compilationRoot, "resourceRoot");
    ResourceLoader resourceLoader = ResourceLoaders.createFileResourceLoader(compilationRoot.toString(), compilationRoot.toFile());
    moduleSpecBuilder.addResourceRoot(ResourceLoaderSpec.createResourceLoaderSpec(resourceLoader));
}
 
Example #4
Source File: JBossModuleUtils.java    From Nicobar with Apache License 2.0 5 votes vote down vote up
/**
 * Populates a {@link ModuleSpec} with runtime resources, dependencies and properties from the
 * {@link ScriptCompilerPluginSpec}
 * Helpful when creating a {@link ModuleSpec} from a ScriptLibPluginSpec
 *
 * @param moduleSpecBuilder builder to populate
 * @param pluginSpec {@link ScriptCompilerPluginSpec} to copy from
 * @param latestRevisionIds used to lookup the latest dependencies. see {@link JBossModuleLoader#getLatestRevisionIds()}
 */
public static void populateCompilerModuleSpec(ModuleSpec.Builder moduleSpecBuilder, ScriptCompilerPluginSpec pluginSpec, Map<ModuleId, ModuleIdentifier> latestRevisionIds) throws ModuleLoadException {
    Objects.requireNonNull(moduleSpecBuilder, "moduleSpecBuilder");
    Objects.requireNonNull(pluginSpec, "pluginSpec");
    Objects.requireNonNull(latestRevisionIds, "latestRevisionIds");
    Set<Path> pluginRuntime = pluginSpec.getRuntimeResources();
    for (Path resourcePath : pluginRuntime) {
        File file = resourcePath.toFile();
        String pathString = resourcePath.toString();
        ResourceLoader rootResourceLoader = null;
        if (file.isDirectory()) {
            rootResourceLoader = ResourceLoaders.createFileResourceLoader(pathString, file);
        } else if (pathString.endsWith(".jar")) {
            try {
                rootResourceLoader = ResourceLoaders.createJarResourceLoader(pathString, new JarFile(file));
            } catch (IOException e) {
                throw new ModuleLoadException(e);
            }
        }
        if (rootResourceLoader != null) {
            moduleSpecBuilder.addResourceRoot(ResourceLoaderSpec.createResourceLoaderSpec(rootResourceLoader));
        }
    }
    moduleSpecBuilder.addDependency(JRE_DEPENDENCY_SPEC);
    moduleSpecBuilder.addDependency(NICOBAR_CORE_DEPENDENCY_SPEC);
    moduleSpecBuilder.addDependency(DependencySpec.createLocalDependencySpec());
    // add dependencies to the module spec
    Set<ModuleId> dependencies = pluginSpec.getModuleDependencies();
    for (ModuleId scriptModuleId : dependencies) {
        ModuleIdentifier latestIdentifier = latestRevisionIds.get(scriptModuleId);
        if (latestIdentifier == null) {
            throw new ModuleLoadException("Cannot find dependent module: " + scriptModuleId);
        }

        moduleSpecBuilder.addDependency(DependencySpec.createModuleDependencySpec(latestIdentifier, true, false));
    }

    Map<String, String> pluginMetadata = pluginSpec.getPluginMetadata();
    addPropertiesToSpec(moduleSpecBuilder, pluginMetadata);
}
 
Example #5
Source File: ApplicationModuleFinder.java    From thorntail with Apache License 2.0 4 votes vote down vote up
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();
    final JarFile jarFile = JarFiles.create(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());

    // SWARM-1473: exploded app artifact is also used to back ShrinkWrap archive used by deployment processors
    TempFileManager.INSTANCE.setExplodedApplicationArtifact(tmpDir);

    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));
    }
}
 
Example #6
Source File: NestedJarResourceLoader.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 4 votes vote down vote up
public static ResourceLoader loaderFor(URL base, String rootPath, String loaderPath, String loaderName) throws IOException {

        //System.err.println( "** " + base + ", " + rootPath + ", " + loaderPath + ", " + loaderName );

        if (base.toExternalForm().startsWith("jar:file:")) {
            int endLoc = base.toExternalForm().indexOf(".jar!");
            if (endLoc > 0) {
                String jarPath = base.toExternalForm().substring(9, endLoc + 4);

                File exp = exploded.get(jarPath);

                if (exp == null) {
                    exp = TempFileManager.INSTANCE.newTempDirectory( "module-jar", ".jar_d" );

                    JarFile jarFile = new JarFile(jarPath);

                    Enumeration<JarEntry> entries = jarFile.entries();

                    while (entries.hasMoreElements()) {
                        JarEntry each = entries.nextElement();

                        if (!each.isDirectory()) {
                            File out = new File(exp, each.getName());
                            out.getParentFile().mkdirs();
                            Files.copy(jarFile.getInputStream(each), out.toPath(), StandardCopyOption.REPLACE_EXISTING);
                        }
                    }
                }

                String relativeRoot = base.toExternalForm().substring(endLoc + 5);
                File resourceRoot = new File(new File(exp, relativeRoot), loaderPath);
                /*
                if ( resourceRoot.listFiles() != null ) {
                    System.err.println("@ " + resourceRoot + " --> " + Arrays.asList(resourceRoot.listFiles()));
                }
                */
                return ResourceLoaders.createFileResourceLoader(loaderName, resourceRoot);
            }
        }

        return ResourceLoaders.createFileResourceLoader(loaderPath, new File(rootPath));
    }
 
Example #7
Source File: MavenArtifactUtil.java    From thorntail with Apache License 2.0 3 votes vote down vote up
/**
 * A utility method to create a Maven artifact resource loader for the given artifact coordinates.
 *
 * @param rootName the resource root name to use (must not be {@code null})
 * @param coordinates the artifact coordinates to use (must not be {@code null})
 * @param mavenResolver the Maven resolver to use (must not be {@code null})
 * @return the resource loader
 * @throws IOException if the artifact could not be resolved
 */
public static ResourceLoader createMavenArtifactLoader(final MavenResolver mavenResolver, final ArtifactCoordinates coordinates, final String rootName) throws IOException {
    File fp = mavenResolver.resolveJarArtifact(coordinates);
    if (fp == null) return null;
    JarFile jarFile = JDKSpecific.getJarFile(fp, true);
    return ResourceLoaders.createJarResourceLoader(rootName, jarFile);
}
 
Example #8
Source File: MavenArtifactUtil.java    From thorntail with Apache License 2.0 2 votes vote down vote up
/**
 * A utility method to create a Maven artifact resource loader for the given artifact name.
 *
 * @param name the artifact name
 * @return the resource loader
 * @throws IOException if the artifact could not be resolved
 */
public static ResourceLoader createMavenArtifactLoader(final String name) throws IOException {
    return createMavenArtifactLoader(MavenResolver.createDefaultResolver(), name);
}
 
Example #9
Source File: MavenArtifactUtil.java    From thorntail with Apache License 2.0 2 votes vote down vote up
/**
 * A utility method to create a Maven artifact resource loader for the given artifact name.
 *
 * @param mavenResolver the Maven resolver to use (must not be {@code null})
 * @param name the artifact name
 * @return the resource loader
 * @throws IOException if the artifact could not be resolved
 */
public static ResourceLoader createMavenArtifactLoader(final MavenResolver mavenResolver, final String name) throws IOException {
    return createMavenArtifactLoader(mavenResolver, ArtifactCoordinates.fromString(name), name);
}