package org.jboss.windup.rules.apps.java.scan.operation.packagemapping;

import java.util.Collections;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.inject.Inject;

import org.jboss.windup.config.GraphRewrite;
import org.jboss.windup.config.RuleSubset;
import org.jboss.windup.config.loader.RuleLoaderContext;
import org.jboss.windup.config.metadata.RuleProviderRegistry;
import org.jboss.windup.config.metadata.RuleProviderRegistryCache;
import org.jboss.windup.graph.GraphContext;
import org.jboss.windup.graph.GraphContextFactory;
import org.jboss.windup.graph.model.WindupConfigurationModel;
import org.jboss.windup.graph.model.resource.FileModel;
import org.jboss.windup.graph.service.FileService;
import org.jboss.windup.graph.service.WindupConfigurationService;
import org.jboss.windup.util.PathUtil;
import org.ocpsoft.rewrite.config.RuleVisit;

/**
 * This registry is conceptually similar to the {@link RuleProviderRegistryCache} except that it is designed for {@link PackageNameMapping}. It allows
 * callers (such as bootstrap) to find packagename mappings without fully running Windup.
 *
 * @author <a href="mailto:jesse.sightler@gmail.com">Jesse Sightler</a>
 */
public class PackageNameMappingRegistry
{
    private static final Logger LOG = Logger.getLogger(PackageNameMappingRegistry.class.getSimpleName());

    @Inject
    private GraphContextFactory graphContextFactory;
    @Inject
    private RuleProviderRegistryCache cache;
    private GraphRewrite event;

    public String getOrganizationForPackage(String packageName)
    {
        return PackageNameMapping.getOrganizationForPackage(this.event, packageName);
    }

    public void loadPackageMappings()
    {
        try (GraphContext graphContext = graphContextFactory.create())
        {
            WindupConfigurationModel configurationModel = WindupConfigurationService.getConfigurationModel(graphContext);
            FileModel windupRulesPath = new FileService(graphContext).createByFilePath(PathUtil.getWindupRulesDir().toString());
            configurationModel.addUserRulesPath(windupRulesPath);
            RuleLoaderContext ruleLoaderContext = new RuleLoaderContext(Collections.singleton(PathUtil.getWindupRulesDir()), null);

            RuleProviderRegistry registry = cache.getRuleProviderRegistry(ruleLoaderContext);
            this.event = new GraphRewrite(graphContext);
            RuleSubset ruleSubset = RuleSubset.create(registry.getConfiguration());
            new RuleVisit(ruleSubset).accept((r) -> {
                if (r instanceof PackageNameMapping)
                {
                    ((PackageNameMapping) r).preRulesetEvaluation(event);
                }
            });

            graphContext.clear();
        }
        catch (Exception e)
        {
            LOG.log(Level.WARNING, "Failed to load rule information due to: " + e.getMessage(), e);
        }
    }
}

@@ -66,16 +66,43 @@
 pping;%0A%0A
+import java.nio.file.Path;%0A
 import j
@@ -1734,32 +1734,156 @@
 appings()%0A    %7B%0A
+        loadPackageMappings(PathUtil.getWindupRulesDir());%0A    %7D%0A%0A    public void loadPackageMappings(Path rulesPath)%0A    %7B%0A
         try (Gra
@@ -2160,36 +2160,17 @@
 ath(
-PathUtil.getWindupRulesDir()
+rulesPath
 .toS
@@ -2343,36 +2343,17 @@
 ton(
-PathUtil.getWindupRulesDir()
+rulesPath
 ), n

Package mapping configurable path (#1022)