Search in sources :

Example 1 with RuleProvider

use of org.jboss.windup.config.RuleProvider in project windup by windup.

the class LoadGroovyRulesTest method testGroovyUserDirectoryRuleProvider.

@Test
public void testGroovyUserDirectoryRuleProvider() throws Exception {
    // create a user path
    Path userRulesPath = Paths.get(FileUtils.getTempDirectory().toString(), "WindupGroovyPath");
    try {
        FileUtils.deleteDirectory(userRulesPath.toFile());
        Files.createDirectories(userRulesPath);
        Path exampleGroovyUserDirWindupGroovyFile = userRulesPath.resolve("ExampleUserFile.windup.groovy");
        Path exampleGroovyUserDirRhamtGroovyFile = userRulesPath.resolve("ExampleUserFile.rhamt.groovy");
        Path exampleGroovyUserDirMtaGroovyFile = userRulesPath.resolve("ExampleUserFile.mta.groovy");
        // copy a groovy rule example to it
        try (InputStream is = getClass().getResourceAsStream(EXAMPLE_GROOVY_WINDUP_FILE);
            OutputStream os = new FileOutputStream(exampleGroovyUserDirWindupGroovyFile.toFile())) {
            IOUtils.copy(is, os);
        }
        try (InputStream is = getClass().getResourceAsStream(EXAMPLE_GROOVY_RHAMT_FILE);
            OutputStream os = new FileOutputStream(exampleGroovyUserDirRhamtGroovyFile.toFile())) {
            IOUtils.copy(is, os);
        }
        try (InputStream is = getClass().getResourceAsStream(EXAMPLE_GROOVY_MTA_FILE);
            OutputStream os = new FileOutputStream(exampleGroovyUserDirMtaGroovyFile.toFile())) {
            IOUtils.copy(is, os);
        }
        RuleLoaderContext ruleLoaderContext = new RuleLoaderContext(Collections.singleton(userRulesPath), null);
        Imported<RuleProviderLoader> loaders = furnace.getAddonRegistry().getServices(RuleProviderLoader.class);
        Assert.assertNotNull(loaders);
        List<RuleProvider> allProviders = new ArrayList<>();
        for (RuleProviderLoader loader : loaders) {
            allProviders.addAll(loader.getProviders(ruleLoaderContext));
        }
        boolean foundScriptPath = false;
        boolean foundRhamtScriptPath = false;
        boolean foundMtaScriptPath = false;
        for (RuleProvider provider : allProviders) {
            Rule rule = RuleBuilder.define();
            Context ruleContext = (Context) rule;
            AbstractRuleProvider.enhanceRuleMetadata(provider, rule);
            String origin = ((String) ruleContext.get(RuleMetadataType.ORIGIN));
            if (origin.endsWith("ExampleUserFile.windup.groovy")) {
                // make sure we found the one from the user dir
                foundScriptPath = true;
            } else if (origin.endsWith("ExampleUserFile.rhamt.groovy")) {
                // make sure we found the one from the user dir
                foundRhamtScriptPath = true;
            } else if (origin.endsWith("ExampleUserFile.mta.groovy")) {
                // make sure we found the one from the user dir
                foundMtaScriptPath = true;
            }
        }
        Assert.assertTrue("Script path should have been set in Rule Metatada", foundScriptPath);
        Assert.assertTrue("Script path should have been set in RHAMT Rule Metatada", foundRhamtScriptPath);
        Assert.assertTrue("Script path should have been set in MTA Rule Metatada", foundMtaScriptPath);
        Assert.assertTrue(allProviders.size() > 0);
    } finally {
        FileUtils.deleteDirectory(userRulesPath.toFile());
    }
}
Also used : Path(java.nio.file.Path) Context(org.ocpsoft.rewrite.context.Context) RuleLoaderContext(org.jboss.windup.config.loader.RuleLoaderContext) RuleProviderLoader(org.jboss.windup.config.loader.RuleProviderLoader) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) AbstractRuleProvider(org.jboss.windup.config.AbstractRuleProvider) RuleProvider(org.jboss.windup.config.RuleProvider) FileOutputStream(java.io.FileOutputStream) Rule(org.ocpsoft.rewrite.config.Rule) RuleLoaderContext(org.jboss.windup.config.loader.RuleLoaderContext) Test(org.junit.Test)

Example 2 with RuleProvider

use of org.jboss.windup.config.RuleProvider in project windup by windup.

the class RuleLoaderImpl method printRulePhases.

/**
 * Prints all of the {@link RulePhase} objects in the order that they should execute. This is primarily for debug purposes and should be called
 * before the entire {@link RuleProvider} list is sorted, as this will allow us to print the {@link RulePhase} list without the risk of
 * user-introduced cycles making the sort impossible.
 */
private void printRulePhases(List<RuleProvider> allProviders) {
    List<RuleProvider> unsortedPhases = new ArrayList<>();
    for (RuleProvider provider : allProviders) {
        if (provider instanceof RulePhase)
            unsortedPhases.add(provider);
    }
    List<RuleProvider> sortedPhases = RuleProviderSorter.sort(unsortedPhases);
    StringBuilder rulePhaseSB = new StringBuilder();
    for (RuleProvider phase : sortedPhases) {
        Class<?> unproxiedClass = Proxies.unwrap(phase).getClass();
        rulePhaseSB.append("\tPhase: ").append(unproxiedClass.getSimpleName()).append(System.lineSeparator());
    }
    LOG.info("Rule Phases: [\n" + rulePhaseSB.toString() + "]");
}
Also used : RulePhase(org.jboss.windup.config.phase.RulePhase) AbstractRuleProvider(org.jboss.windup.config.AbstractRuleProvider) RuleProvider(org.jboss.windup.config.RuleProvider)

Example 3 with RuleProvider

use of org.jboss.windup.config.RuleProvider in project windup by windup.

the class RuleLoaderImpl method checkForDuplicateProviders.

private void checkForDuplicateProviders(List<RuleProvider> providers) {
    /*
         * We are using a map so that we can easily pull out the previous value later (in the case of a duplicate)
         */
    Map<RuleProvider, RuleProvider> duplicates = new HashMap<>(providers.size());
    for (RuleProvider provider : providers) {
        RuleProvider previousProvider = duplicates.get(provider);
        if (previousProvider != null) {
            String typeMessage;
            String currentProviderOrigin = provider.getMetadata().getOrigin();
            String previousProviderOrigin = previousProvider.getMetadata().getOrigin();
            if (previousProvider.getClass().equals(provider.getClass())) {
                typeMessage = " (type: " + previousProviderOrigin + " and " + currentProviderOrigin + ")";
            } else {
                typeMessage = " (types: " + Proxies.unwrapProxyClassName(previousProvider.getClass()) + " at " + previousProviderOrigin + " and " + Proxies.unwrapProxyClassName(provider.getClass()) + " at " + currentProviderOrigin + ")";
            }
            throw new WindupException("Found two providers with the same id: " + provider.getMetadata().getID() + typeMessage);
        }
        duplicates.put(provider, provider);
    }
}
Also used : AbstractRuleProvider(org.jboss.windup.config.AbstractRuleProvider) RuleProvider(org.jboss.windup.config.RuleProvider) WindupException(org.jboss.windup.util.exception.WindupException)

Example 4 with RuleProvider

use of org.jboss.windup.config.RuleProvider in project windup by windup.

the class RuleLoaderImpl method buildRegistry.

private RuleProviderRegistry buildRegistry(RuleLoaderContext ruleLoaderContext) {
    // estimate of how many rules we will likely see TODO: careful with this
    List<Rule> allRules = new ArrayList<>(2000);
    List<RuleProvider> providers = loadProviders(ruleLoaderContext);
    RuleProviderRegistry registry = new RuleProviderRegistry();
    registry.setProviders(providers);
    // Get override rules from override providers (if any)
    Map<RuleKey, Rule> overrideRules = extractOverrideRules(providers);
    // Add provider->rules mappings to the registry and, for each rule, inject parameters if applicable
    for (RuleProvider provider : providers) {
        if (ruleLoaderContext.getRuleProviderFilter() != null) {
            boolean accepted = ruleLoaderContext.getRuleProviderFilter().accept(provider);
            LOG.info((accepted ? "Accepted" : "Skipped") + ": [" + provider + "] by filter [" + ruleLoaderContext.getRuleProviderFilter() + "]");
            if (!accepted)
                continue;
        }
        // these are not used directly... they only override others
        if (provider.getMetadata().isOverrideProvider())
            continue;
        Configuration cfg = provider.getConfiguration(ruleLoaderContext);
        List<Rule> rules = overrideRules(cfg, overrideRules, provider);
        registry.addRulesForProvider(provider, rules);
        for (int i = 0; i < rules.size(); i++) {
            Rule rule = rules.get(i);
            AbstractRuleProvider.enhanceRuleMetadata(provider, rule);
            if (rule instanceof RuleBuilder && isBlank(rule.getId())) {
                ((RuleBuilder) rule).withId(generatedRuleID(provider, i + 1));
            }
            allRules.add(rule);
            if (rule instanceof ParameterizedRule) {
                injectParametersIntoRule(rule);
            }
        }
    }
    ConfigurationBuilder result = ConfigurationBuilder.begin();
    for (Rule rule : allRules) {
        result.addRule(rule);
    }
    registry.setConfiguration(result);
    return registry;
}
Also used : ConfigurationBuilder(org.ocpsoft.rewrite.config.ConfigurationBuilder) Configuration(org.ocpsoft.rewrite.config.Configuration) AbstractRuleProvider(org.jboss.windup.config.AbstractRuleProvider) RuleProvider(org.jboss.windup.config.RuleProvider) RuleProviderRegistry(org.jboss.windup.config.metadata.RuleProviderRegistry) ParameterizedRule(org.ocpsoft.rewrite.param.ParameterizedRule) ParameterizedRule(org.ocpsoft.rewrite.param.ParameterizedRule) Rule(org.ocpsoft.rewrite.config.Rule) RuleBuilder(org.ocpsoft.rewrite.config.RuleBuilder)

Example 5 with RuleProvider

use of org.jboss.windup.config.RuleProvider in project windup by windup.

the class RuleProviderSorter method initializeLookupCaches.

/**
 * Initializes lookup caches that are used during sort to lookup providers by ID or Java {@link Class}.
 */
private void initializeLookupCaches() {
    for (RuleProvider provider : providers) {
        Class<? extends RuleProvider> unproxiedClass = unwrapType(provider.getClass());
        classToProviderMap.put(unproxiedClass, provider);
        idToProviderMap.put(provider.getMetadata().getID(), provider);
    }
}
Also used : AbstractRuleProvider(org.jboss.windup.config.AbstractRuleProvider) RuleProvider(org.jboss.windup.config.RuleProvider)

Aggregations

RuleProvider (org.jboss.windup.config.RuleProvider)40 AbstractRuleProvider (org.jboss.windup.config.AbstractRuleProvider)25 Test (org.junit.Test)18 Path (java.nio.file.Path)17 GraphContext (org.jboss.windup.graph.GraphContext)15 WindupConfiguration (org.jboss.windup.exec.configuration.WindupConfiguration)14 NotPredicate (org.jboss.windup.exec.rulefilters.NotPredicate)13 ArrayList (java.util.ArrayList)10 RuleProviderPhasePredicate (org.jboss.windup.exec.rulefilters.RuleProviderPhasePredicate)9 ReportGenerationPhase (org.jboss.windup.config.phase.ReportGenerationPhase)8 MigrationRulesPhase (org.jboss.windup.config.phase.MigrationRulesPhase)7 AndPredicate (org.jboss.windup.exec.rulefilters.AndPredicate)7 HashSet (java.util.HashSet)6 EnumeratedRuleProviderPredicate (org.jboss.windup.engine.predicates.EnumeratedRuleProviderPredicate)6 RuleProviderWithDependenciesPredicate (org.jboss.windup.engine.predicates.RuleProviderWithDependenciesPredicate)6 RuleLoaderContext (org.jboss.windup.config.loader.RuleLoaderContext)5 ProjectModel (org.jboss.windup.graph.model.ProjectModel)4 Rule (org.ocpsoft.rewrite.config.Rule)4 IOException (java.io.IOException)3 LinkedHashSet (java.util.LinkedHashSet)3