Coverage Report - org.apache.maven.plugins.repository.BundleUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
BundleUtils
80%
40/50
77%
23/30
0
BundleUtils$1
100%
2/2
75%
3/4
0
BundleUtils$2
100%
6/6
75%
3/4
0
 
 1  
 package org.apache.maven.plugins.repository;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import org.apache.maven.plugin.MojoExecutionException;
 23  
 import org.apache.maven.plugin.logging.Log;
 24  
 import org.codehaus.plexus.components.interactivity.InputHandler;
 25  
 
 26  
 import java.io.File;
 27  
 import java.io.FilenameFilter;
 28  
 import java.io.IOException;
 29  
 import java.util.ArrayList;
 30  
 import java.util.Arrays;
 31  
 import java.util.Collections;
 32  
 import java.util.Comparator;
 33  
 import java.util.List;
 34  
 import java.util.StringTokenizer;
 35  
 
 36  
 final class BundleUtils
 37  
 {
 38  
     private BundleUtils()
 39  0
     {
 40  0
     }
 41  
     
 42  
     public static List<File> selectProjectFiles( final File dir, final InputHandler inputHandler, final String finalName,
 43  
                                            final File pom, final Log log, final boolean batchMode )
 44  
         throws MojoExecutionException
 45  
     {
 46  15
         File[] projectFiles = dir.listFiles( new FilenameFilter()
 47  
         {
 48  15
             public boolean accept( File dir, String name )
 49  
             {
 50  56
                 return new File( dir, name ).isFile() && name.startsWith( finalName );
 51  
             }
 52  
         } );
 53  
         
 54  15
         List<File> result = new ArrayList<File>();
 55  
         
 56  15
         if ( projectFiles == null )
 57  
         {
 58  0
             return result;
 59  
         }
 60  
         
 61  65
         for ( int i = 0; i < projectFiles.length; i++ )
 62  
         {
 63  50
             if ( projectFiles[i].getName().endsWith( ".pom" ) )
 64  
             {
 65  6
                 if ( !projectFiles[i].equals( pom ) )
 66  
                 {
 67  0
                     log.info( "Detected POM file will be excluded:\n" + projectFiles[i]
 68  
                                                                                      + "\n\nInstead, the bundle will include the POM from:\n" + pom );
 69  
                 }
 70  
             }
 71  44
             else if ( projectFiles[i].getName().endsWith( "-bundle.jar" ) )
 72  
             {
 73  9
                 log.warn( "Skipping project file which collides with repository bundle filename:\n" + projectFiles[i] );
 74  
             }
 75  
             else
 76  
             {
 77  35
                 result.add( projectFiles[i] );
 78  
             }
 79  
         }
 80  
         
 81  15
         if ( result.isEmpty() )
 82  
         {
 83  2
             return result;
 84  
         }
 85  
         
 86  13
         Collections.sort( result, new Comparator<File>()
 87  
         {
 88  45
             public int compare( File first, File second )
 89  
             {
 90  32
                 String f = first.getName();
 91  32
                 String s = second.getName();
 92  
                 
 93  32
                 if ( f.length() == s.length() )
 94  
                 {
 95  10
                     return f.compareTo( s );
 96  
                 }
 97  
                 
 98  22
                 return f.length() < s.length() ? -1 : 1;
 99  
             }
 100  
         } );
 101  
         
 102  13
         result = reviseFileList( result, inputHandler, log, batchMode );
 103  
         
 104  13
         return result;
 105  
     }
 106  
 
 107  
     public static List<File> reviseFileList( List<File> input, InputHandler inputHandler, Log log, boolean batchMode )
 108  
         throws MojoExecutionException
 109  
     {
 110  13
         List<File> result = new ArrayList<File>( input );
 111  
         
 112  13
         if ( batchMode )
 113  
         {
 114  0
             return result;
 115  
         }
 116  
         
 117  
         while( true )
 118  
         {
 119  21
             StringBuffer message = new StringBuffer();
 120  21
             message.append( "The following files are marked for inclusion in the repository bundle:\n" );
 121  21
             message.append( "\n0.) Done" );
 122  
             
 123  21
             int i = 1;
 124  21
             for ( File f : result )
 125  
             {
 126  45
                 message.append( "\n" ).append( (i++) ).append( ".) " ).append( f.getName() );
 127  
             }
 128  
             
 129  21
             message.append( "\n\nPlease select the number(s) for any files you wish to exclude, " +
 130  
                     "or '0' when you're done.\nSeparate the numbers for multiple files with a " +
 131  
                     "comma (',').\n\nSelection: " );
 132  
             
 133  21
             log.info( message );
 134  21
             String response = null;
 135  
             try
 136  
             {
 137  21
                 response = inputHandler.readLine();
 138  
             }
 139  0
             catch ( IOException e )
 140  
             {
 141  0
                 throw new MojoExecutionException( "Project file selection failed with an I/O exception: " + e.getMessage(), e );
 142  21
             }
 143  
             
 144  21
             if ( response == null || "0".equals( response ) )
 145  
             {
 146  0
                 break;
 147  
             }
 148  
             
 149  8
             StringTokenizer st = new StringTokenizer( response, "," );
 150  
             
 151  8
             if ( st.countTokens() > 0 )
 152  
             {
 153  8
                 int[] idxs = new int[st.countTokens()];
 154  22
                 for ( int j = 0; j < idxs.length; j++ )
 155  
                 {
 156  14
                     idxs[j] = Integer.parseInt( st.nextToken().trim() );
 157  
                 }
 158  
                 
 159  8
                 Arrays.sort( idxs );
 160  
                 
 161  22
                 for( int k = idxs.length - 1; k > -1; k-- )
 162  
                 {
 163  14
                     if ( idxs[k] < 1 || idxs[k] > result.size() )
 164  
                     {
 165  0
                         log.warn( "NOT removing: " + idxs[k] + "; no such file." );
 166  0
                         continue;
 167  
                     }
 168  
                     
 169  14
                     File removed = (File) result.remove( idxs[k] -1 );
 170  14
                     log.info( "Removed: " + removed.getName() );
 171  
                 }
 172  
             }
 173  
             else
 174  
             {
 175  
                 break;
 176  
             }
 177  8
         }
 178  
         
 179  13
         return result;
 180  
     }
 181  
 
 182  
 }