Java Code Examples for org.jboss.modules.ModuleSpec#Builder

The following examples show how to use org.jboss.modules.ModuleSpec#Builder . 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: ModuleSpecProcessor.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void addResourceRoot(final ModuleSpec.Builder specBuilder, final ResourceRoot resource, final List<PermissionFactory> permFactories)
        throws DeploymentUnitProcessingException {
    try {
        final VirtualFile root = resource.getRoot();
        if (resource.getExportFilters().isEmpty()) {
            specBuilder.addResourceRoot(ResourceLoaderSpec.createResourceLoaderSpec(new VFSResourceLoader(resource
                    .getRootName(), root, resource.isUsePhysicalCodeSource())));
        } else {
            final MultiplePathFilterBuilder filterBuilder = PathFilters.multiplePathFilterBuilder(true);
            for (final FilterSpecification filter : resource.getExportFilters()) {
                filterBuilder.addFilter(filter.getPathFilter(), filter.isInclude());
            }
            specBuilder.addResourceRoot(ResourceLoaderSpec.createResourceLoaderSpec(new VFSResourceLoader(resource
                    .getRootName(), root, resource.isUsePhysicalCodeSource()), filterBuilder.create()));
        }
        // start with the root
        permFactories.add(new ImmediatePermissionFactory(
                new VirtualFilePermission(root.getPathName(), VirtualFilePermission.FLAG_READ)));
        // also include all children, recursively
        permFactories.add(new ImmediatePermissionFactory(
                new VirtualFilePermission(root.getChild("-").getPathName(), VirtualFilePermission.FLAG_READ)));
    } catch (IOException e) {
        throw ServerLogger.ROOT_LOGGER.failedToCreateVFSResourceLoader(resource.getRootName(), e);
    }
}
 
Example 2
Source File: SQLServerDriverInfo.java    From thorntail with Apache License 2.0 5 votes vote down vote up
@Override
protected void addModuleDependencies(ModuleSpec.Builder builder) {
    // JDBC driver for SQL Server 6.0 - 6.2 is using javax.xml.bind.DatatypeConverter
    builder.addDependency(new ModuleDependencySpecBuilder()
            .setName("javax.xml.bind.api")
            .build());
}
 
Example 3
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 4
Source File: ApplicationModuleFinderTest.java    From thorntail with Apache License 2.0 5 votes vote down vote up
@Test
public void testDependency() {
    // Mocks
    ApplicationEnvironment env = mock(ApplicationEnvironment.class);
    when(env.getDependencies()).thenReturn(Collections.singleton("org.jboss.forge.addon:ui-spi:jar:3.4.0.Final"));

    ModuleSpec.Builder builder = mock(ModuleSpec.Builder.class);

    ApplicationModuleFinder sut = new ApplicationModuleFinder();
    sut.addDependencies(builder, env);

    verify(builder, times(1)).addResourceRoot(any());
}
 
Example 5
Source File: ScriptModuleLoader.java    From Nicobar with Apache License 2.0 5 votes vote down vote up
/**
 * Create a JBoss module spec for an about to be created script module.
 * @param archive the script archive being converted to a module.
 * @param moduleId the JBoss module identifier.
 * @param moduleIdMap a map of loaded script module IDs to jboss module identifiers
 * @param moduleCompilationRoot a path to a directory that will hold compiled classes for this module.
 * @throws ModuleLoadException
 */
protected ModuleSpec createModuleSpec(ScriptArchive archive,
        ModuleIdentifier moduleId,
        Map<ModuleId, ModuleIdentifier> moduleIdMap,
        Path moduleCompilationRoot) throws ModuleLoadException {
    ScriptModuleSpec archiveSpec = archive.getModuleSpec();
    // create the jboss module pre-cursor artifact
    ModuleSpec.Builder moduleSpecBuilder = ModuleSpec.build(moduleId);

    JBossModuleUtils.populateModuleSpecWithResources(moduleSpecBuilder, archive);
    JBossModuleUtils.populateModuleSpecWithCoreDependencies(moduleSpecBuilder, archive);
    JBossModuleUtils.populateModuleSpecWithAppImports(moduleSpecBuilder,
            appClassLoader, archiveSpec.getAppImportFilterPaths() == null ? appPackagePaths : archiveSpec.getAppImportFilterPaths());
    // Allow compiled class files to fetched as resources later on.
    JBossModuleUtils.populateModuleSpecWithCompilationRoot(moduleSpecBuilder, moduleCompilationRoot);

    // Populate the modulespec with the scriptArchive dependencies
    for (ModuleId dependencyModuleId : archiveSpec.getModuleDependencies()) {
        ScriptModule dependencyModule = getScriptModule(dependencyModuleId);
        Set<String> exportPaths = dependencyModule.getSourceArchive().getModuleSpec().getModuleExportFilterPaths();

        JBossModuleUtils.populateModuleSpecWithModuleDependency(moduleSpecBuilder,
            archiveSpec.getModuleImportFilterPaths(), exportPaths, moduleIdMap.get(dependencyModuleId));
    }

    return moduleSpecBuilder.create();
}
 
Example 6
Source File: ApplicationModuleFinder.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 5 votes vote down vote up
protected void handleWildFlySwarmApplicationConf(ModuleSpec.Builder builder) throws Exception {
    InputStream appConf = getClass().getClassLoader().getResourceAsStream(WildFlySwarmApplicationConf.CLASSPATH_LOCATION);
    if (appConf != null) {
        WildFlySwarmApplicationConf conf = new WildFlySwarmApplicationConf(appConf);
        conf.apply(builder);
    }
}
 
Example 7
Source File: AbstractSingleModuleFinder.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 5 votes vote down vote up
@Override
public ModuleSpec findModule(ModuleIdentifier identifier, ModuleLoader delegateLoader) throws ModuleLoadException {
    if (!identifier.getName().equals(this.moduleName) || !identifier.getSlot().equals(this.moduleSlot)) {
        return null;
    }

    ModuleSpec.Builder builder = ModuleSpec.build(identifier);
    buildModule(builder, delegateLoader);
    return builder.create();
}
 
Example 8
Source File: ExternalModuleSpecService.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static void addEEDependencies(ModuleSpec.Builder specBuilder) {
    DependencySpec dependencySpec = new ModuleDependencySpecBuilder()
            .setName(ServerRootResourceDefinition.WILDFLY_EE_API)
            .setOptional(true)
            .build();

    specBuilder.addDependency(dependencySpec);
}
 
Example 9
Source File: BootstrapModuleFinder.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 5 votes vote down vote up
protected void handleWildFlySwarmBootstrapConf(ModuleSpec.Builder builder) throws IOException {
    if (LOG.isTraceEnabled()) {
        LOG.trace("Loading conf from " + WildFlySwarmBootstrapConf.CLASSPATH_LOCATION);
    }
    InputStream bootstrapTxt = getClass().getClassLoader().getResourceAsStream(WildFlySwarmBootstrapConf.CLASSPATH_LOCATION);

    if (bootstrapTxt != null) {
        WildFlySwarmBootstrapConf conf = new WildFlySwarmBootstrapConf(bootstrapTxt);
        conf.apply(builder);
    }
}
 
Example 10
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 11
Source File: JBossModuleUtilsTest.java    From Nicobar with Apache License 2.0 5 votes vote down vote up
/**
 * Verify that the module creates the expected set of dependencies for a {@link JarScriptArchive}
 */
@Test
public void testJarResources() throws Exception {
    Path jarPath = CoreTestResourceUtil.getResourceAsPath(TEST_TEXT_JAR);
    ScriptArchive jarScriptArchive = new JarScriptArchive.Builder(jarPath)
            .setModuleSpec(new ScriptModuleSpec.Builder(ModuleId.create("testModuleId"))
            .addMetadata(METADATA_NAME, METADATA_VALUE)
            .build())
        .build();

    ModuleIdentifier revisionId = JBossModuleUtils.createRevisionId(TEST_TEXT_JAR.getModuleId(), 1);
    ModuleSpec.Builder moduleSpecBuilder = ModuleSpec.build(revisionId);
    JBossModuleLoader moduleLoader = new JBossModuleLoader();
    JBossModuleUtils.populateModuleSpecWithCoreDependencies(moduleSpecBuilder, jarScriptArchive);
    JBossModuleUtils.populateModuleSpecWithResources(moduleSpecBuilder, jarScriptArchive);

    moduleLoader.addModuleSpec(moduleSpecBuilder.create());
    Module module = moduleLoader.loadModule(revisionId);
    ModuleClassLoader moduleClassLoader = module.getClassLoader();

    // verify the metadata was transfered
    assertEquals(module.getProperty(METADATA_NAME), METADATA_VALUE);
    // verify that the archive resource match exactly the module resources
    Set<String> actualPaths = getResourcePaths(moduleClassLoader);

    assertEquals(actualPaths, TEST_TEXT_JAR.getContentPaths());
}
 
Example 12
Source File: JBossModuleUtils.java    From Nicobar with Apache License 2.0 5 votes vote down vote up
/**
 * Populates a module spec builder with core dependencies on JRE, Nicobar, itself, and compiler plugins.
 *
 * @param moduleSpecBuilder builder to populate
 * @param scriptArchive {@link ScriptArchive} to copy from
 */
public static void populateModuleSpecWithCoreDependencies(ModuleSpec.Builder moduleSpecBuilder, ScriptArchive scriptArchive) throws ModuleLoadException {
    Objects.requireNonNull(moduleSpecBuilder, "moduleSpecBuilder");
    Objects.requireNonNull(scriptArchive, "scriptArchive");

    Set<String> compilerPlugins = scriptArchive.getModuleSpec().getCompilerPluginIds();
    for (String compilerPluginId : compilerPlugins) {
        moduleSpecBuilder.addDependency(DependencySpec.createModuleDependencySpec(getPluginModuleId(compilerPluginId), false));
    }
    moduleSpecBuilder.addDependency(JRE_DEPENDENCY_SPEC);
    // TODO: Why does a module need a dependency on Nicobar itself?
    moduleSpecBuilder.addDependency(NICOBAR_CORE_DEPENDENCY_SPEC);
    moduleSpecBuilder.addDependency(DependencySpec.createLocalDependencySpec());
}
 
Example 13
Source File: DriverInfo.java    From thorntail with Apache License 2.0 4 votes vote down vote up
protected void addModuleDependencies(ModuleSpec.Builder builder) {
    // no-op, but overridable
}
 
Example 14
Source File: ModuleSpecProcessor.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
private ServiceName createModuleService(final DeploymentPhaseContext phaseContext, final DeploymentUnit deploymentUnit,
                                        final List<ResourceRoot> resourceRoots, final List<ResourceRoot> parentResourceRoots,
                                        final ModuleSpecification moduleSpecification, final ModuleIdentifier moduleIdentifier) throws DeploymentUnitProcessingException {
    logger.debugf("Creating module: %s", moduleIdentifier);
    final ModuleSpec.Builder specBuilder = ModuleSpec.build(moduleIdentifier);
    for (final DependencySpec dep : moduleSpecification.getModuleSystemDependencies()) {
        specBuilder.addDependency(dep);
    }
    final List<ModuleDependency> dependencies = moduleSpecification.getSystemDependencies();
    final List<ModuleDependency> localDependencies = moduleSpecification.getLocalDependencies();
    final List<ModuleDependency> userDependencies = moduleSpecification.getUserDependencies();

    final List<PermissionFactory> permFactories = moduleSpecification.getPermissionFactories();

    installAliases(moduleSpecification, moduleIdentifier, deploymentUnit, phaseContext);

    // add additional resource loaders first
    for (final ResourceLoaderSpec resourceLoaderSpec : moduleSpecification.getResourceLoaders()) {
        logger.debugf("Adding resource loader %s to module %s", resourceLoaderSpec, moduleIdentifier);
        specBuilder.addResourceRoot(resourceLoaderSpec);
    }

    for (final ResourceRoot resourceRoot : resourceRoots) {
        logger.debugf("Adding resource %s to module %s", resourceRoot.getRoot(), moduleIdentifier);
        addResourceRoot(specBuilder, resourceRoot, permFactories);
    }

    createDependencies(specBuilder, dependencies, false);
    createDependencies(specBuilder, userDependencies, false);

    if (moduleSpecification.isLocalLast()) {
        createDependencies(specBuilder, localDependencies, moduleSpecification.isLocalDependenciesTransitive());
        specBuilder.addDependency(DependencySpec.createLocalDependencySpec());
    } else {
        specBuilder.addDependency(DependencySpec.createLocalDependencySpec());
        createDependencies(specBuilder, localDependencies, moduleSpecification.isLocalDependenciesTransitive());
    }

    final Enumeration<Permission> e = DEFAULT_PERMISSIONS.elements();
    while (e.hasMoreElements()) {
        permFactories.add(new ImmediatePermissionFactory(e.nextElement()));
    }
    // TODO: servlet context temp dir FilePermission

    // add file permissions for parent roots
    for (ResourceRoot additionalParentRoot : parentResourceRoots) {
        final VirtualFile root = additionalParentRoot.getRoot();
        // start with the root
        permFactories.add(new ImmediatePermissionFactory(
                new VirtualFilePermission(root.getPathName(), VirtualFilePermission.FLAG_READ)));
        // also include all children, recursively
        permFactories.add(new ImmediatePermissionFactory(
                new VirtualFilePermission(root.getChild("-").getPathName(), VirtualFilePermission.FLAG_READ)));
    }

    FactoryPermissionCollection permissionCollection = new FactoryPermissionCollection(permFactories.toArray(new PermissionFactory[permFactories.size()]));

    specBuilder.setPermissionCollection(permissionCollection);
    deploymentUnit.putAttachment(Attachments.MODULE_PERMISSIONS, permissionCollection);

    final DelegatingClassFileTransformer delegatingClassFileTransformer = new DelegatingClassFileTransformer();
    specBuilder.setClassFileTransformer(delegatingClassFileTransformer);
    deploymentUnit.putAttachment(DelegatingClassFileTransformer.ATTACHMENT_KEY, delegatingClassFileTransformer);
    final ModuleSpec moduleSpec = specBuilder.create();
    final ServiceName moduleSpecServiceName = ServiceModuleLoader.moduleSpecServiceName(moduleIdentifier);

    ModuleDefinition moduleDefinition = new ModuleDefinition(moduleIdentifier, new HashSet<>(moduleSpecification.getAllDependencies()), moduleSpec);

    final ValueService<ModuleDefinition> moduleSpecService = new ValueService<>(new ImmediateValue<>(moduleDefinition));
    final ServiceBuilder sb = phaseContext.getServiceTarget().addService(moduleSpecServiceName, moduleSpecService);
    sb.requires(deploymentUnit.getServiceName());
    sb.requires(phaseContext.getPhaseServiceName());
    sb.setInitialMode(Mode.ON_DEMAND);
    sb.install();

    ModuleResolvePhaseService.installService(phaseContext.getServiceTarget(), moduleDefinition);

    return ModuleLoadService.install(phaseContext.getServiceTarget(), moduleIdentifier, dependencies, localDependencies, userDependencies);
}
 
Example 15
Source File: WildFlySwarmBootstrapConf.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 4 votes vote down vote up
public void apply(ModuleSpec.Builder builder) throws IOException {
    for (MavenArtifactDescriptor entry : this.entries) {
        apply(builder, entry);
    }
}
 
Example 16
Source File: ExternalModuleSpecService.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
private static void addPathResourceRoot(final ModuleSpec.Builder specBuilder, Path path) {
    specBuilder.addResourceRoot(ResourceLoaderSpec.createResourceLoaderSpec(ResourceLoaders.createPathResourceLoader(path)));
}
 
Example 17
Source File: ExternalModuleSpecService.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
private static void addResourceRoot(final ModuleSpec.Builder specBuilder, final JarFile file) {
    specBuilder.addResourceRoot(ResourceLoaderSpec.createResourceLoaderSpec(ResourceLoaders.createJarResourceLoader(file)));
}
 
Example 18
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 19
Source File: AbstractSingleModuleFinder.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 votes vote down vote up
public abstract void buildModule(ModuleSpec.Builder builder, ModuleLoader delegateLoader) throws ModuleLoadException; 
Example 20
Source File: AbstractSingleModuleFinder.java    From thorntail with Apache License 2.0 votes vote down vote up
public abstract void buildModule(ModuleSpec.Builder builder, ModuleLoader delegateLoader) throws ModuleLoadException;