Coverage Report - org.boretti.drools.integration.drools5.DroolsCopyAndValidate
 
Classes in this File Line Coverage Branch Coverage Complexity
DroolsCopyAndValidate
0 %
0/54
0 %
0/16
2.667
 
 1  
 /*
 2  
     Drools5 Integration Helper
 3  
     Copyright (C) 2009  Mathieu Boretti mathieu.boretti@gmail.com
 4  
 
 5  
     This program is free software: you can redistribute it and/or modify
 6  
     it under the terms of the GNU General Public License as published by
 7  
     the Free Software Foundation, either version 3 of the License, or
 8  
     (at your option) any later version.
 9  
 
 10  
     This program is distributed in the hope that it will be useful,
 11  
     but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
     GNU General Public License for more details.
 14  
 
 15  
     You should have received a copy of the GNU General Public License
 16  
     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 17  
 
 18  
  */
 19  
 package org.boretti.drools.integration.drools5;
 20  
 
 21  
 import java.io.File;
 22  
 import java.io.IOException;
 23  
 
 24  
 import org.apache.maven.execution.MavenSession;
 25  
 import org.apache.maven.plugin.AbstractMojo;
 26  
 import org.apache.maven.plugin.MojoExecutionException;
 27  
 import org.apache.maven.plugin.MojoFailureException;
 28  
 import org.apache.maven.project.MavenProject;
 29  
 import org.apache.maven.shared.filtering.MavenResourcesFiltering;
 30  
 import org.codehaus.plexus.util.DirectoryScanner;
 31  
 
 32  
 
 33  
 /**
 34  
  * This goal copy drools file and validate them
 35  
  * @author mbo
 36  
  * @goal drools-copy-validate
 37  
  * @phase compile
 38  
  * @requiresDependencyResolution runtime
 39  
  */
 40  0
 public class DroolsCopyAndValidate extends AbstractMojo {
 41  
 
 42  
         @Override
 43  
         public void execute() throws MojoExecutionException, MojoFailureException {
 44  0
                 if (getLog().isDebugEnabled()) getLog().debug("starting drools copy and validation");
 45  0
                 DirectoryScanner scanner = new DirectoryScanner();
 46  0
                 if (!getInputDirectory().exists()) {
 47  0
                         getLog().warn("Skipping not existing directory "+getInputDirectory());
 48  0
                         return;
 49  
                 }
 50  0
                 scanner.setBasedir(getInputDirectory());
 51  0
                 scanner.setIncludes(new String[] { "**/*"+getExtension(),"*"+getExtension() });
 52  0
                 scanner.scan();
 53  0
                 boolean ok=true;
 54  0
                 getOutputDirectory().mkdirs();
 55  0
                 for (String file : scanner.getIncludedFiles()) {
 56  0
                         File sfile = new File(getInputDirectory(), file);
 57  0
                         File dfile = new File(getOutputDirectory(),file);
 58  0
                         dfile.getParentFile().mkdirs();
 59  0
                         if (!DroolsHelper.validateSourceFile(getLog(), project, sfile,false)) ok=false;
 60  
                         try {
 61  0
                                 FileHelper.copyFile(sfile, dfile);
 62  0
                         } catch (IOException e) {
 63  0
                                 getLog().error("Unable to copy file :"+e.getMessage(),e);
 64  0
                                 throw new MojoExecutionException("Unable to coyp file :"+e.getMessage(),e);
 65  0
                         }
 66  
                 }
 67  0
                 scanner = new DirectoryScanner();
 68  0
                 if (!getInputDirectory().exists()) {
 69  0
                         getLog().warn("Skipping not existing directory "+getInputDirectory());
 70  0
                         return;
 71  
                 }
 72  0
                 scanner.setBasedir(getInputDirectory());
 73  0
                 scanner.setIncludes(new String[] { "**/*"+getXmlExtension(),"*"+getXmlExtension() });
 74  0
                 scanner.scan();
 75  0
                 getOutputDirectory().mkdirs();
 76  0
                 for (String file : scanner.getIncludedFiles()) {
 77  0
                         File sfile = new File(getInputDirectory(), file);
 78  0
                         File dfile = new File(getOutputDirectory(),file);
 79  0
                         dfile.getParentFile().mkdirs();
 80  0
                         if (!DroolsHelper.validateSourceFile(getLog(), project, sfile,true)) ok=false;
 81  
                         try {
 82  0
                                 FileHelper.copyFile(sfile, dfile);
 83  0
                         } catch (IOException e) {
 84  0
                                 getLog().error("Unable to copy file :"+e.getMessage(),e);
 85  0
                                 throw new MojoExecutionException("Unable to coyp file :"+e.getMessage(),e);
 86  0
                         }
 87  
                 }
 88  0
                 if (!ok) {
 89  0
                         throw new MojoExecutionException("Validation error of the drools");
 90  
                 }
 91  0
         }
 92  
 
 93  
         /**
 94  
          * @parameter expression="${session}"
 95  
          * 
 96  
          * @readonly
 97  
          * @required
 98  
          */
 99  
         protected MavenSession session;
 100  
         
 101  
         /**
 102  
      * @parameter expression="${project}"
 103  
      * @required
 104  
      * @readonly
 105  
      */
 106  
     protected MavenProject project;
 107  
     
 108  
     /**
 109  
      * The output directory into which to copy the rules.
 110  
      *
 111  
      * @parameter expression="${project.basedir}/target/classes"
 112  
      * @required
 113  
      */
 114  
     private File outputDirectory;
 115  
     
 116  
     /**
 117  
      * The input directory from where to copy the rules.
 118  
      * 
 119  
      * @parameter expression="${project.basedir}/src/main/drools"
 120  
      * @required
 121  
      */
 122  
     private File inputDirectory;
 123  
     
 124  
     /**
 125  
      * The default extension for drools file
 126  
      * 
 127  
      * @parameter expression=".drl"
 128  
      * @required
 129  
      */
 130  
     private String extension;
 131  
     
 132  
     /**
 133  
      * The default extension for xml drools file
 134  
      * 
 135  
      * @parameter expression=".xml"
 136  
      * @required
 137  
      */
 138  
     private String xmlExtension;
 139  
     
 140  
     /**
 141  
      * 
 142  
      * @component role="org.apache.maven.shared.filtering.MavenResourcesFiltering" role-hint="default"
 143  
      * @required
 144  
      */    
 145  
     protected MavenResourcesFiltering mavenResourcesFiltering;
 146  
 
 147  
         public File getOutputDirectory() {
 148  0
                 return outputDirectory;
 149  
         }
 150  
 
 151  
         public void setOutputDirectory(File outputDirectory) {
 152  0
                 this.outputDirectory = outputDirectory;
 153  0
         }
 154  
 
 155  
         public File getInputDirectory() {
 156  0
                 return inputDirectory;
 157  
         }
 158  
 
 159  
         public void setInputDirectory(File inputDirectory) {
 160  0
                 this.inputDirectory = inputDirectory;
 161  0
         }
 162  
 
 163  
         public String getExtension() {
 164  0
                 return extension;
 165  
         }
 166  
 
 167  
         public void setExtension(String extension) {
 168  0
                 this.extension = extension;
 169  0
         }
 170  
 
 171  
         /**
 172  
          * @return the xmlExtension
 173  
          */
 174  
         public String getXmlExtension() {
 175  0
                 return xmlExtension;
 176  
         }
 177  
 
 178  
         /**
 179  
          * @param xmlExtension the xmlExtension to set
 180  
          */
 181  
         public void setXmlExtension(String xmlExtension) {
 182  0
                 this.xmlExtension = xmlExtension;
 183  0
         } 
 184  
 
 185  
 }