Coverage Report - org.apache.maven.plugin.eclipse.writers.myeclipse.MyEclipseSpringBeansWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
MyEclipseSpringBeansWriter
0%
0/50
0%
0/8
2,75
MyEclipseSpringBeansWriter$1
0%
0/2
N/A
2,75
 
 1  
 package org.apache.maven.plugin.eclipse.writers.myeclipse;
 2  
 
 3  
 import java.io.File;
 4  
 import java.io.FileFilter;
 5  
 import java.io.FileWriter;
 6  
 import java.io.IOException;
 7  
 import java.util.ArrayList;
 8  
 import java.util.Collection;
 9  
 import java.util.Collections;
 10  
 import java.util.Iterator;
 11  
 import java.util.Map;
 12  
 
 13  
 import org.apache.maven.plugin.MojoExecutionException;
 14  
 import org.apache.maven.plugin.eclipse.Messages;
 15  
 import org.apache.maven.plugin.eclipse.writers.AbstractEclipseWriter;
 16  
 import org.codehaus.plexus.util.FileUtils;
 17  
 import org.codehaus.plexus.util.IOUtil;
 18  
 import org.codehaus.plexus.util.StringUtils;
 19  
 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
 20  
 import org.codehaus.plexus.util.xml.XMLWriter;
 21  
 
 22  
 /**
 23  
  * MyEclipse .springBeans configuration file writer
 24  
  * 
 25  
  * @author Olivier Jacob
 26  
  */
 27  
 public class MyEclipseSpringBeansWriter
 28  
     extends AbstractEclipseWriter
 29  
 {
 30  
     private static final String MYECLIPSE_SPRING_CONFIGURATION_FILENAME = ".springBeans";
 31  
 
 32  
     private static final String MYECLIPSE_SPRING_BEANS_PROJECT_DESCRIPTION = "beansProjectDescription";
 33  
 
 34  
     private static final String MYECLIPSE_SPRING_CONFIG_EXTENSIONS = "configExtensions";
 35  
 
 36  
     private static final String MYECLIPSE_SPRING_CONFIG_EXTENSION = "configExtension";
 37  
 
 38  
     private static final String MYECLIPSE_SPRING_CONFIGS = "configs";
 39  
 
 40  
     private static final String MYECLIPSE_SPRING_CONFIG = "config";
 41  
 
 42  
     private static final String MYECLIPSE_SPRING_CONFIGSETS = "configSets";
 43  
 
 44  
     private static final String MYECLIPSE_SPRING_VERSION = "springVersion";
 45  
 
 46  
     /**
 47  
      * Spring configuration filenames (injected by the plugin)
 48  
      */
 49  
     private Map springConfig;
 50  
 
 51  
     /**
 52  
      * Allow injection of Spring configuration filenames through constructor
 53  
      * 
 54  
      * @param springConfig a map holding Spring configuration properties
 55  
      */
 56  
     public MyEclipseSpringBeansWriter( Map springConfig )
 57  0
     {
 58  0
         this.springConfig = springConfig;
 59  0
     }
 60  
 
 61  
     /**
 62  
      * Write MyEclipse .springBeans configuration file
 63  
      * 
 64  
      * @throws MojoExecutionException
 65  
      */
 66  
     public void write()
 67  
         throws MojoExecutionException
 68  
     {
 69  
         FileWriter springFileWriter;
 70  
         try
 71  
         {
 72  0
             springFileWriter =
 73  
                 new FileWriter( new File( config.getEclipseProjectDirectory(), MYECLIPSE_SPRING_CONFIGURATION_FILENAME ) );
 74  
         }
 75  0
         catch ( IOException ex )
 76  
         {
 77  0
             throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); //$NON-NLS-1$
 78  0
         }
 79  
 
 80  0
         XMLWriter writer = new PrettyPrintXMLWriter( springFileWriter, "UTF-8", null );
 81  
 
 82  0
         writer.startElement( MYECLIPSE_SPRING_BEANS_PROJECT_DESCRIPTION );
 83  
         // Configuration extension
 84  0
         writer.startElement( MYECLIPSE_SPRING_CONFIG_EXTENSIONS );
 85  0
         writer.startElement( MYECLIPSE_SPRING_CONFIG_EXTENSION );
 86  0
         writer.writeText( "xml" );
 87  0
         writer.endElement();
 88  0
         writer.endElement();
 89  
 
 90  
         // Configuration files
 91  0
         writer.startElement( MYECLIPSE_SPRING_CONFIGS );
 92  
 
 93  
         // maven's cwd stays at the top of hierarchical projects so we
 94  
         // do this with full path so it works as we descend through various modules (projects)
 95  0
         String absolutePrefix = config.getEclipseProjectDirectory() + "/";
 96  
         // convert hem all to forward slashes
 97  0
         absolutePrefix = StringUtils.replace( absolutePrefix, "\\", "/" );
 98  0
         int absolutePrefixLength = absolutePrefix.length();
 99  0
         Iterator onConfigFiles =
 100  
             getConfigurationFilesList( absolutePrefix + (String) springConfig.get( "basedir" ),
 101  
                                        (String) springConfig.get( "file-pattern" ) ).iterator();
 102  
 
 103  0
         while ( onConfigFiles.hasNext() )
 104  
         {
 105  0
             writer.startElement( MYECLIPSE_SPRING_CONFIG );
 106  
             // convert out any back slashes
 107  0
             String processedFileName = StringUtils.replace( (String) onConfigFiles.next(), "\\", "/" );
 108  
             // write out the file name minus the absolute path to get to the top of the project
 109  0
             writer.writeText( processedFileName.substring( absolutePrefixLength ) );
 110  0
             writer.endElement();
 111  0
         }
 112  0
         writer.endElement();
 113  
 
 114  
         // Configuration sets
 115  0
         writer.startElement( MYECLIPSE_SPRING_CONFIGSETS );
 116  0
         writer.endElement();
 117  
 
 118  
         // Spring version
 119  0
         writer.startElement( MYECLIPSE_SPRING_VERSION );
 120  0
         writer.writeText( (String) springConfig.get( "version" ) );
 121  0
         writer.endElement();
 122  
 
 123  0
         writer.endElement();
 124  
 
 125  0
         IOUtil.close( springFileWriter );
 126  0
     }
 127  
 
 128  
     /**
 129  
      * Retrieve the list of Spring configuration files recursively from the <code>basedir</code> directory, considering
 130  
      * only filenames matching the <code>pattern</code> given
 131  
      * 
 132  
      * @param basedir the path to the base directory to search in
 133  
      * @param pattern file include pattern
 134  
      * @return the list of filenames matching the given pattern
 135  
      */
 136  
     private Collection getConfigurationFilesList( String basedir, String pattern )
 137  
     {
 138  0
         ArrayList configFiles = new ArrayList();
 139  
 
 140  
         try
 141  
         {
 142  0
             File directory = new File( basedir );
 143  0
             if ( directory.exists() )
 144  
             {
 145  0
                 log.debug( "Scanning " + basedir + " for spring definition files" );
 146  0
                 File[] subdirs = directory.listFiles( new FileFilter()
 147  
                 {
 148  0
                     public boolean accept( File pathname )
 149  
                     {
 150  0
                         return pathname.isDirectory();
 151  
                     }
 152  
                 } );
 153  
 
 154  0
                 if ( subdirs != null )
 155  
                 {
 156  0
                     for ( int i = 0; i < subdirs.length; i++ )
 157  
                     {
 158  0
                         configFiles.addAll( getConfigurationFilesList( subdirs[i].getPath(), pattern ) );
 159  
                     }
 160  
                 }
 161  
 
 162  0
                 configFiles.addAll( FileUtils.getFileNames( directory, pattern, null, true ) );
 163  0
             }
 164  
             else
 165  
             {
 166  
                 // This isn't fatal because sometimes we run this in a nested set of
 167  
                 // projects where some of the projects may not have spring configuration
 168  0
                 log.warn( Messages.getString( "MyEclipseSpringBeansWriter.baseDirDoesNotExist",
 169  
                                               new Object[] { basedir } ) );
 170  
             }
 171  
         }
 172  0
         catch ( IOException ioe )
 173  
         {
 174  0
             log.error( "Error while retrieving Spring configuration files. Returning list in current state" );
 175  0
         }
 176  
         // Sort them to have something constant
 177  0
         Collections.sort( configFiles );
 178  0
         return configFiles;
 179  
     }
 180  
 }