Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.jboss.windup.rules.apps.java.config;
  
  import java.io.File;
  import java.util.List;
  import java.util.Map;
  
  import  org.apache.commons.io.IOUtils;
 import  org.apache.commons.io.LineIterator;
 import  org.jboss.forge.furnace.util.Predicate;
 import  org.jboss.forge.furnace.util.Visitor;
 import  org.jboss.windup.config.AbstractRuleProvider;
 import  org.jboss.windup.config.GraphRewrite;
 import  org.jboss.windup.config.metadata.MetadataBuilder;
 import  org.jboss.windup.config.operation.GraphOperation;
 import  org.jboss.windup.config.phase.InitializationPhase;
 import  org.jboss.windup.graph.GraphContext;
 import  org.jboss.windup.util.PathUtil;
 import  org.jboss.windup.util.exception.WindupException;
 import  org.jboss.windup.util.file.FileSuffixPredicate;
 import  org.jboss.windup.util.file.FileVisit;
 import  org.ocpsoft.rewrite.config.Configuration;
 import  org.ocpsoft.rewrite.config.ConfigurationBuilder;
 import  org.ocpsoft.rewrite.config.Rule;
 import  org.ocpsoft.rewrite.context.EvaluationContext;

Copies configuration data from GraphContext.getOptionMap() to the graph itself for easy use by other Rules.
 
 public class CopyJavaConfigToGraphRuleProvider extends AbstractRuleProvider
 {
     {
         super(MetadataBuilder.forProvider(CopyJavaConfigToGraphRuleProvider.class)
                     .setPhase(InitializationPhase.class).setHaltOnException(true));
     }
 
     @Override
     public Configuration getConfiguration(GraphContext context)
     {
         GraphOperation copyConfigToGraph = new GraphOperation()
         {
             @Override
             public void perform(GraphRewrite event, EvaluationContext context)
             {
                 Map<StringObjectconfig = event.getGraphContext().getOptionMap();
                 Boolean sourceMode = (Booleanconfig.get(.);
 
                 @SuppressWarnings("unchecked")
                 List<StringincludeJavaPackages = (List<String>) config.get(.);
 
                 @SuppressWarnings("unchecked")
                 final List<StringexcludeJavaPackages;
                 if (config.get(.) == null)
                     excludeJavaPackages = new ArrayList<String>();
                 else
                     excludeJavaPackages = new ArrayList<>((List<String>) config.get(.));
 
                 Predicate<Filepredicate = new FileSuffixPredicate("\\.package-ignore\\.txt");
                 Visitor<Filevisitor = new Visitor<File>()
                 {
                     @Override
                     public void visit(File file)
                     {
                         try (FileInputStream inputStream = new FileInputStream(file))
                         {
                             LineIterator it = IOUtils.lineIterator(inputStream"UTF-8");
                             while (it.hasNext())
                             {
                                 String line = it.next();
                                 if (!line.startsWith("#") && !line.trim().isEmpty())
                                 {
                                     excludeJavaPackages.add(line);
                                 }
                             }
                         }
                         catch (Exception e)
                         {
                             throw new WindupException("Failed loading package ignore patterns from [" + file.toString() + "]"e);
                         }
                     }
                 };
 
                 FileVisit.visit(PathUtil.getUserIgnoreDir().toFile(), predicatevisitor);
                 FileVisit.visit(PathUtil.getWindupIgnoreDir().toFile(), predicatevisitor);
 
                 WindupJavaConfigurationModel javaCfg = WindupJavaConfigurationService.getJavaConfigurationModel(event
                             .getGraphContext());
                 javaCfg.setSourceMode(sourceMode == null ? false : sourceMode);
                 javaCfg.setScanJavaPackageList(includeJavaPackages);
                 javaCfg.setExcludeJavaPackageList(excludeJavaPackages);
             }
         };
 
         return ConfigurationBuilder.begin()
                    .addRule()
                    .perform(copyConfigToGraph);
    }
New to GrepCode? Check out our FAQ X