Skip to content

Commit

Permalink
Merge pull request #320 from jsight/userrules_dir_fix
Browse files Browse the repository at this point in the history
Fixes for user rules directory issues
  • Loading branch information
lincolnthree committed Oct 21, 2014
2 parents 27d34fd + 1248913 commit 2b96673
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 30 deletions.
Expand Up @@ -16,9 +16,11 @@
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.inject.Inject;

Expand All @@ -34,6 +36,7 @@
import org.jboss.windup.graph.model.resource.FileModel;
import org.jboss.windup.graph.service.WindupConfigurationService;
import org.jboss.windup.util.FurnaceCompositeClassLoader;
import org.jboss.windup.util.Logging;
import org.jboss.windup.util.exception.WindupException;
import org.jboss.windup.util.furnace.FurnaceClasspathScanner;

Expand All @@ -46,6 +49,8 @@
*/
public class GroovyWindupRuleProviderLoader implements WindupRuleProviderLoader
{
private static final Logger LOG = Logging.get(GroovyWindupRuleProviderLoader.class);

public static final String CURRENT_WINDUP_SCRIPT = "CURRENT_WINDUP_SCRIPT";

private static final String GROOVY_RULES_EXTENSION = "windup.groovy";
Expand Down Expand Up @@ -170,22 +175,34 @@ private Iterable<URL> getScripts(GraphContext context)

private Collection<URL> getScripts(FileModel userRulesFileModel)
{
String userRulesPath = userRulesFileModel == null ? null : userRulesFileModel.getFilePath();
String userRulesDirectory = userRulesFileModel == null ? null : userRulesFileModel.getFilePath();

List<URL> scripts = scanner.scan(GROOVY_RULES_EXTENSION);

// no user dir, so just return the ones that we found in the classpath
if (userRulesPath == null)
if (userRulesDirectory == null)
{
return scripts;
}
Path userRulesPath = Paths.get(userRulesDirectory);

if (!Files.isDirectory(userRulesPath))
{
LOG.warning("Not scanning: " + userRulesPath.normalize().toString() + " for rules as the directory could not be found!");
return Collections.emptyList();
}
if (!Files.isDirectory(userRulesPath))
{
LOG.warning("Not scanning: " + userRulesPath.normalize().toString() + " for rules as the directory could not be read!");
return Collections.emptyList();
}

// create the results as a copy (as we will be adding user groovy files to them)
final List<URL> results = new ArrayList<>(scripts);

try
{
Files.walkFileTree(Paths.get(userRulesPath), new SimpleFileVisitor<Path>()
Files.walkFileTree(userRulesPath, new SimpleFileVisitor<Path>()
{
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException
Expand Down
Expand Up @@ -19,6 +19,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.inject.Inject;
import javax.xml.parsers.DocumentBuilder;
Expand All @@ -32,6 +33,7 @@
import org.jboss.windup.graph.model.WindupConfigurationModel;
import org.jboss.windup.graph.model.resource.FileModel;
import org.jboss.windup.graph.service.WindupConfigurationService;
import org.jboss.windup.util.Logging;
import org.jboss.windup.util.exception.WindupException;
import org.jboss.windup.util.furnace.FileExtensionFilter;
import org.jboss.windup.util.furnace.FurnaceClasspathScanner;
Expand All @@ -46,6 +48,7 @@
*/
public class XMLRuleProviderLoader implements WindupRuleProviderLoader
{
private static final Logger LOG = Logging.get(XMLRuleProviderLoader.class);

public static final String CURRENT_WINDUP_SCRIPT = "CURRENT_WINDUP_SCRIPT";

Expand Down Expand Up @@ -133,20 +136,31 @@ private Map<Addon, List<URL>> getAddonWindupXmlFiles()

private Collection<URL> getWindupUserDirectoryXmlFiles(FileModel userRulesFileModel)
{
String userRulesPath = userRulesFileModel == null ? null : userRulesFileModel.getFilePath();
String userRulesDirectory = userRulesFileModel == null ? null : userRulesFileModel.getFilePath();

// no user dir, so just return the ones that we found in the classpath
if (userRulesPath == null)
if (userRulesDirectory == null)
{
return Collections.emptyList();
}
Path userRulesPath = Paths.get(userRulesDirectory);
if (!Files.isDirectory(userRulesPath))
{
LOG.warning("Not scanning: " + userRulesPath.normalize().toString() + " for rules as the directory could not be found!");
return Collections.emptyList();
}
if (!Files.isDirectory(userRulesPath))
{
LOG.warning("Not scanning: " + userRulesPath.normalize().toString() + " for rules as the directory could not be read!");
return Collections.emptyList();
}

// create the results as a copy (as we will be adding user groovy files to them)
final List<URL> results = new ArrayList<>();

try
{
Files.walkFileTree(Paths.get(userRulesPath), new SimpleFileVisitor<Path>()
Files.walkFileTree(userRulesPath, new SimpleFileVisitor<Path>()
{
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException
Expand All @@ -161,7 +175,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
}
catch (IOException e)
{
throw new WindupException("Failed to search userdir: \"" + userRulesPath + "\" for groovy rules due to: "
throw new WindupException("Failed to search userdir: \"" + userRulesDirectory + "\" for groovy rules due to: "
+ e.getMessage(), e);
}

Expand Down
Expand Up @@ -6,9 +6,11 @@
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.jboss.forge.furnace.addons.Addon;
import org.jboss.forge.furnace.services.Imported;
Expand All @@ -32,6 +34,8 @@
*/
public class WindupConfiguration
{
private static final String DEFAULT_USER_RULES_DIRECTORIES_OPTION = "defaultUserRulesDirectories";

private Predicate<WindupRuleProvider> ruleProviderFilter;
private WindupProgressMonitor progressMonitor = new NullWindupProgressMonitor();
private Map<String, Object> configurationOptions = new HashMap<>();
Expand Down Expand Up @@ -149,39 +153,51 @@ public WindupConfiguration setOutputDirectory(Path outputDirectory)
}

/**
* Contains a list of {@link Path}s with the directory that contains user provided rules.
* Gets all user rule directories. This includes both the ones that they specify (eg, /path/to/rules) as well as ones that Windup provides by
* default (eg, WINDUP_HOME/rules and ~/.windup/rules).
*/
public List<Path> getUserRulesDirectories()
public Iterable<Path> getAllUserRulesDirectories()
{
List<Path> paths = getOptionValue(UserRulesDirectoryOption.NAME);
if (paths == null)
Set<Path> results = new HashSet<>();
results.addAll(getDefaultUserRulesDirectories());
File userSpecifiedFile = getOptionValue(UserRulesDirectoryOption.NAME);
if (userSpecifiedFile != null)
{
return Collections.emptyList();
results.add(userSpecifiedFile.toPath());
}
return paths;
return results;
}

/**
* Contains a list of {@link Path}s with the directory that contains user provided rules.
* Contains a list of {@link Path}s with directories that contains user provided rules.
*/
public WindupConfiguration setUserRulesDirectories(List<Path> userRulesDirectories)
public List<Path> getDefaultUserRulesDirectories()
{
setOptionValue(UserRulesDirectoryOption.NAME, userRulesDirectories);
return this;
List<Path> paths = getOptionValue(DEFAULT_USER_RULES_DIRECTORIES_OPTION);
if (paths == null)
{
return Collections.emptyList();
}
return Collections.unmodifiableList(paths);
}

/**
* Contains a list of {@link Path}s with the directory that contains user provided rules.
*
* This method does guard against duplicate directories.
*/
public WindupConfiguration addUserRulesDirectory(Path path)
public WindupConfiguration addDefaultUserRulesDirectory(Path path)
{
List<Path> paths = getOptionValue(UserRulesDirectoryOption.NAME);
List<Path> paths = getOptionValue(DEFAULT_USER_RULES_DIRECTORIES_OPTION);
if (paths == null)
{
paths = new ArrayList<>();
paths.add(path);
setOptionValue(DEFAULT_USER_RULES_DIRECTORIES_OPTION, paths);
}

File userSpecifiedRulePath = getOptionValue(UserRulesDirectoryOption.NAME);
if (userSpecifiedRulePath != null && userSpecifiedRulePath.toPath().equals(path))
{
return this;
}

Expand All @@ -193,7 +209,6 @@ public WindupConfiguration addUserRulesDirectory(Path path)
}
}
paths.add(path);

return this;
}

Expand Down
Expand Up @@ -55,7 +55,7 @@ public void execute(WindupConfiguration windupConfiguration)
configModel.setInputPath(getFileModel(context, windupConfiguration.getInputPath()));
configModel.setOutputPath(getFileModel(context, windupConfiguration.getOutputDirectory()));
configModel.setOfflineMode(windupConfiguration.isOffline());
for (Path path : windupConfiguration.getUserRulesDirectories())
for (Path path : windupConfiguration.getAllUserRulesDirectories())
{
configModel.addUserRulesPath(getFileModel(context, path));
}
Expand Down
Expand Up @@ -106,7 +106,7 @@ public void testRunWindupSourceMode() throws Exception
// The test-files folder in the project root dir.
List<String> includeList = Collections.emptyList();
List<String> excludeList = Collections.emptyList();
super.runTest(context, "../test-files/src_example", Collections.singletonList(userPath), true, includeList, excludeList);
super.runTest(context, "../test-files/src_example", userPath.toFile(), true, includeList, excludeList);

validateWebXmlReferences(context);
validatePropertiesModels(context);
Expand Down
@@ -1,5 +1,6 @@
package org.jboss.windup.tests.application;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
Expand All @@ -13,6 +14,7 @@
import org.jboss.windup.exec.WindupProcessor;
import org.jboss.windup.exec.WindupProgressMonitor;
import org.jboss.windup.exec.configuration.WindupConfiguration;
import org.jboss.windup.exec.configuration.options.UserRulesDirectoryOption;
import org.jboss.windup.graph.GraphContext;
import org.jboss.windup.graph.GraphContextFactory;
import org.jboss.windup.rules.apps.java.config.ExcludePackagesOption;
Expand Down Expand Up @@ -76,7 +78,7 @@ void runTest(GraphContext graphContext, String inputPath, boolean sourceMode,

void runTest(final GraphContext graphContext,
final String inputPath,
final List<Path> userRulesDirs,
final File userRulesDir,
final boolean sourceMode,
final List<String> includePackages,
final List<String> excludePackages) throws Exception
Expand All @@ -85,9 +87,9 @@ void runTest(final GraphContext graphContext,
WindupConfiguration wpc = new WindupConfiguration().setGraphContext(graphContext);
wpc.setInputPath(Paths.get(inputPath));
wpc.setOutputDirectory(graphContext.getGraphDirectory());
if (userRulesDirs != null)
if (userRulesDir != null)
{
wpc.setUserRulesDirectories(userRulesDirs);
wpc.setOptionValue(UserRulesDirectoryOption.NAME, userRulesDir);
}
wpc.setOptionValue(SourceModeOption.NAME, sourceMode);
wpc.setOptionValue(ScanPackagesOption.NAME, includePackages);
Expand Down
16 changes: 14 additions & 2 deletions ui/src/main/java/org/jboss/windup/ui/WindupCommand.java
@@ -1,6 +1,7 @@
package org.jboss.windup.ui;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -164,10 +165,18 @@ public Result execute(UIExecutionContext context) throws Exception

// add dist/rules/ and ${forge.home}/rules/ to the user rules directory list
Path userRulesDir = WindupPathUtil.getWindupUserRulesDir();
windupConfiguration.addUserRulesDirectory(userRulesDir);
if (!Files.isDirectory(userRulesDir))
{
Files.createDirectories(userRulesDir);
}
windupConfiguration.addDefaultUserRulesDirectory(userRulesDir);

Path windupHomeRulesDir = WindupPathUtil.getWindupHomeRules();
windupConfiguration.addUserRulesDirectory(windupHomeRulesDir);
if (!Files.isDirectory(windupHomeRulesDir))
{
Files.createDirectories(windupHomeRulesDir);
}
windupConfiguration.addDefaultUserRulesDirectory(windupHomeRulesDir);

boolean overwrite = this.overwrite.getValue();
if (!overwrite && pathNotEmpty(windupConfiguration.getOutputDirectory().toFile()))
Expand All @@ -180,6 +189,9 @@ public Result execute(UIExecutionContext context) throws Exception
}
}

// put this in the context for debugging, and unit tests (or anything else that needs it)
context.getUIContext().getAttributeMap().put(WindupConfiguration.class, windupConfiguration);

FileUtils.deleteQuietly(windupConfiguration.getOutputDirectory().toFile());
Path graphPath = windupConfiguration.getOutputDirectory().resolve("graph");
try (GraphContext graphContext = graphContextFactory.create(graphPath))
Expand Down

0 comments on commit 2b96673

Please sign in to comment.