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());
}
}
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() + "]");
}
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);
}
}
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;
}
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);
}
}
Aggregations