Coverage Report - org.apache.maven.plugins.scmpublish.ScmPublishInventory
 
Classes in this File Line Coverage Branch Coverage Complexity
ScmPublishInventory
0 %
0/31
0 %
0/6
3,167
ScmPublishInventory$IgnoreFilter
0 %
0/5
0 %
0/4
3,167
 
 1  
 package org.apache.maven.plugins.scmpublish;
 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 java.io.File;
 23  
 import java.io.IOException;
 24  
 import java.util.ArrayList;
 25  
 import java.util.Collections;
 26  
 import java.util.HashSet;
 27  
 import java.util.List;
 28  
 import java.util.Set;
 29  
 
 30  
 import org.apache.commons.io.FileUtils;
 31  
 import org.apache.commons.io.filefilter.IOFileFilter;
 32  
 import org.apache.maven.plugin.MojoFailureException;
 33  
 import org.codehaus.jackson.JsonEncoding;
 34  
 import org.codehaus.jackson.JsonGenerator;
 35  
 import org.codehaus.jackson.JsonParser;
 36  
 import org.codehaus.jackson.JsonProcessingException;
 37  
 import org.codehaus.jackson.map.MappingJsonFactory;
 38  
 
 39  
 /**
 40  
  * A class designed for json serialization to store the existing inventory, if any. In this version, there's no attempt
 41  
  * to account for directories as managed items.
 42  
  */
 43  0
 public class ScmPublishInventory
 44  
 {
 45  0
     private static class IgnoreFilter
 46  
         implements IOFileFilter
 47  
     {
 48  
         private final String ignoreFile;
 49  
 
 50  
         public IgnoreFilter( String ignoreFile )
 51  0
         {
 52  0
             this.ignoreFile = ignoreFile;
 53  0
         }
 54  
 
 55  
         public boolean accept( File file )
 56  
         {
 57  0
             return !file.getName().equals( ignoreFile );
 58  
         }
 59  
 
 60  
         public boolean accept( File dir, String name )
 61  
         {
 62  0
             return !name.equals( ignoreFile );
 63  
         }
 64  
 
 65  
     }
 66  
 
 67  
     public static List<File> listInventoryFiles( File basedir, String ignoreFile )
 68  
     {
 69  0
         List<File> inventory = new ArrayList<File>();
 70  0
         inventory.addAll( FileUtils.listFiles( basedir, new IgnoreFilter( ignoreFile ), new IgnoreFilter( ignoreFile ) ) );
 71  0
         Collections.sort( inventory );
 72  0
         return inventory;
 73  
     }
 74  
 
 75  
     /**
 76  
      * Create a list of all the files in the checkout (which we will presently remove). For now, duck anything that
 77  
      * starts with a ., since the site plugin won't make any and it will dodge metadata I'm familiar with. None if this
 78  
      * is really good enough for safe usage with exotics like clearcase. Perhaps protest if anything other than svn or
 79  
      * git? Or use http://plexus.codehaus.org/plexus-utils/apidocs/org/codehaus/plexus/util/AbstractScanner.html#DEFAULTEXCLUDES?
 80  
      * @throws MojoFailureException 
 81  
      */
 82  
     public static List<File> writeInventory( List<File> inventory, File inventoryFile )
 83  
         throws MojoFailureException
 84  
     {
 85  0
         Set<String> paths = new HashSet<String>();
 86  
 
 87  
         /*
 88  
          * It might be cleverer to store paths relative to the checkoutDirectory, but this really should work.
 89  
          */
 90  0
         for ( File f : inventory )
 91  
         {
 92  
             // See below. We only bother about files.
 93  0
             if ( f.isFile() )
 94  
             {
 95  0
                 paths.add( f.getAbsolutePath() );
 96  
             }
 97  
         }
 98  
         try
 99  
         {
 100  0
             MappingJsonFactory factory = new MappingJsonFactory();
 101  0
             JsonGenerator gen = factory.createJsonGenerator( inventoryFile, JsonEncoding.UTF8 );
 102  0
             gen.writeObject( paths );
 103  0
             gen.close();
 104  0
             return inventory;
 105  
         }
 106  0
         catch ( JsonProcessingException e )
 107  
         {
 108  0
             throw new MojoFailureException( "Failed to write inventory to " + inventoryFile.getAbsolutePath(), e );
 109  
         }
 110  0
         catch ( IOException e )
 111  
         {
 112  0
             throw new MojoFailureException( "Failed to write inventory to " + inventoryFile.getAbsolutePath(), e );
 113  
         }
 114  
     }
 115  
 
 116  
     public static List<File> readInventory( File inventoryFile )
 117  
         throws MojoFailureException
 118  
     {
 119  
         try
 120  
         {
 121  0
             MappingJsonFactory factory = new MappingJsonFactory();
 122  0
             JsonParser parser = factory.createJsonParser( inventoryFile );
 123  
             @SuppressWarnings( "unchecked" )
 124  0
             Set<String> storedInventory = parser.readValueAs( HashSet.class );
 125  0
             List<File> inventory = new ArrayList<File>();
 126  0
             for ( String p : storedInventory )
 127  
             {
 128  0
                 inventory.add( new File( p ) );
 129  
             }
 130  0
             parser.close();
 131  0
             return inventory;
 132  
         }
 133  0
         catch ( JsonProcessingException e )
 134  
         {
 135  0
             throw new MojoFailureException( "Failed to write inventory to " + inventoryFile.getAbsolutePath(), e );
 136  
         }
 137  0
         catch ( IOException e )
 138  
         {
 139  0
             throw new MojoFailureException( "Failed to write inventory to " + inventoryFile.getAbsolutePath(), e );
 140  
         }  
 141  
     }
 142  
 }