Coverage Report - org.apache.maven.plugin.assembly.filter.ComponentsXmlArchiverFileFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
ComponentsXmlArchiverFileFilter
58%
42/72
43%
13/30
0
 
 1  
 package org.apache.maven.plugin.assembly.filter;
 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.codehaus.plexus.archiver.Archiver;
 23  
 import org.codehaus.plexus.archiver.ArchiverException;
 24  
 import org.codehaus.plexus.archiver.ResourceIterator;
 25  
 import org.codehaus.plexus.archiver.UnArchiver;
 26  
 import org.codehaus.plexus.components.io.fileselectors.FileInfo;
 27  
 import org.codehaus.plexus.util.IOUtil;
 28  
 import org.codehaus.plexus.util.xml.Xpp3Dom;
 29  
 import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
 30  
 import org.codehaus.plexus.util.xml.Xpp3DomWriter;
 31  
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 32  
 
 33  
 import java.io.File;
 34  
 import java.io.FileOutputStream;
 35  
 import java.io.IOException;
 36  
 import java.io.InputStream;
 37  
 import java.io.InputStreamReader;
 38  
 import java.io.OutputStreamWriter;
 39  
 import java.io.Reader;
 40  
 import java.io.Writer;
 41  
 import java.util.Collections;
 42  
 import java.util.Iterator;
 43  
 import java.util.LinkedHashMap;
 44  
 import java.util.List;
 45  
 import java.util.Map;
 46  
 
 47  
 /**
 48  
  * Components XML file filter.
 49  
  * 
 50  
  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
 51  
  * @version $Id: ComponentsXmlArchiverFileFilter.java 999612 2010-09-21 20:34:50Z jdcasey $
 52  
  */
 53  8
 public class ComponentsXmlArchiverFileFilter
 54  
     implements ContainerDescriptorHandler
 55  
 {
 56  
     // [jdcasey] Switched visibility to protected to allow testing. Also, because this class isn't final, it should
 57  
     // allow
 58  
     // some minimal access to the components accumulated for extending classes.
 59  
     protected Map<String, Xpp3Dom> components;
 60  
 
 61  8
     private boolean excludeOverride = false;
 62  
 
 63  
     public static final String COMPONENTS_XML_PATH = "META-INF/plexus/components.xml";
 64  
 
 65  
     protected void addComponentsXml( final Reader componentsReader ) throws XmlPullParserException, IOException
 66  
     {
 67  3
         Xpp3Dom newDom = Xpp3DomBuilder.build( componentsReader );
 68  
 
 69  3
         if ( newDom != null )
 70  
         {
 71  3
             newDom = newDom.getChild( "components" );
 72  
         }
 73  
 
 74  3
         if ( newDom != null )
 75  
         {
 76  3
             final Xpp3Dom[] children = newDom.getChildren();
 77  
 
 78  7
             for ( int i = 0; i < children.length; i++ )
 79  
             {
 80  4
                 final Xpp3Dom component = children[i];
 81  
 
 82  4
                 if ( components == null )
 83  
                 {
 84  3
                     components = new LinkedHashMap<String, Xpp3Dom>();
 85  
                 }
 86  
 
 87  4
                 final String role = component.getChild( "role" )
 88  
                                              .getValue();
 89  4
                 final Xpp3Dom child = component.getChild( "role-hint" );
 90  4
                 final String roleHint = child != null ? child.getValue() : "";
 91  
 
 92  4
                 final String key = role + roleHint;
 93  4
                 if ( !components.containsKey( key ) )
 94  
                 {
 95  4
                     System.out.println( "Adding " + key );
 96  4
                     components.put( key, component );
 97  
                 }
 98  
                 else
 99  
                 {
 100  0
                     System.out.println( "Component: " + key + " is already defined. Skipping." );
 101  
                 }
 102  
             }
 103  
         }
 104  3
     }
 105  
 
 106  
     // public void addComponentsXml( File componentsXml )
 107  
     // throws IOException, XmlPullParserException
 108  
     // {
 109  
     // FileReader fileReader = null;
 110  
     // try
 111  
     // {
 112  
     // fileReader = new FileReader( componentsXml );
 113  
     //
 114  
     // addComponentsXml( fileReader );
 115  
     // }
 116  
     // finally
 117  
     // {
 118  
     // IOUtil.close( fileReader );
 119  
     // }
 120  
     // }
 121  
 
 122  
     private void addToArchive( final Archiver archiver ) throws IOException, ArchiverException
 123  
     {
 124  4
         if ( components != null )
 125  
         {
 126  4
             final File f = File.createTempFile( "maven-assembly-plugin", "tmp" );
 127  4
             f.deleteOnExit();
 128  
 
 129  
             // TODO use WriterFactory.newXmlWriter() when plexus-utils is upgraded to 1.4.5+
 130  4
             final Writer fileWriter = new OutputStreamWriter( new FileOutputStream( f ), "UTF-8" );
 131  
             try
 132  
             {
 133  4
                 final Xpp3Dom dom = new Xpp3Dom( "component-set" );
 134  4
                 final Xpp3Dom componentDom = new Xpp3Dom( "components" );
 135  4
                 dom.addChild( componentDom );
 136  
 
 137  4
                 for ( final Iterator<Xpp3Dom> i = components.values()
 138  10
                                                             .iterator(); i.hasNext(); )
 139  
                 {
 140  6
                     final Xpp3Dom component = i.next();
 141  6
                     componentDom.addChild( component );
 142  6
                 }
 143  
 
 144  4
                 Xpp3DomWriter.write( fileWriter, dom );
 145  
             }
 146  
             finally
 147  
             {
 148  4
                 IOUtil.close( fileWriter );
 149  4
             }
 150  
 
 151  4
             excludeOverride = true;
 152  
 
 153  4
             archiver.addFile( f, COMPONENTS_XML_PATH );
 154  
 
 155  4
             excludeOverride = false;
 156  
         }
 157  4
     }
 158  
 
 159  
     public void finalizeArchiveCreation( final Archiver archiver ) throws ArchiverException
 160  
     {
 161  
         // this will prompt the isSelected() call, below, for all resources added to the archive.
 162  
         // FIXME: This needs to be corrected in the AbstractArchiver, where
 163  
         // runArchiveFinalizers() is called before regular resources are added...
 164  
         // which is done because the manifest needs to be added first, and the
 165  
         // manifest-creation component is a finalizer in the assembly plugin...
 166  4
         for ( final ResourceIterator it = archiver.getResources(); it.hasNext(); )
 167  
         {
 168  0
             it.next();
 169  
         }
 170  
 
 171  
         try
 172  
         {
 173  4
             addToArchive( archiver );
 174  
         }
 175  0
         catch ( final IOException e )
 176  
         {
 177  0
             throw new ArchiverException( "Error finalizing component-set for archive. Reason: " + e.getMessage(), e );
 178  4
         }
 179  4
     }
 180  
 
 181  
     public List<String> getVirtualFiles()
 182  
     {
 183  0
         if ( ( components != null ) && !components.isEmpty() )
 184  
         {
 185  0
             return Collections.singletonList( COMPONENTS_XML_PATH );
 186  
         }
 187  
 
 188  0
         return null;
 189  
     }
 190  
 
 191  
     public boolean isSelected( final FileInfo fileInfo ) throws IOException
 192  
     {
 193  0
         if ( fileInfo.isFile() )
 194  
         {
 195  0
             if ( excludeOverride )
 196  
             {
 197  0
                 return true;
 198  
             }
 199  
 
 200  0
             String entry = fileInfo.getName()
 201  
                                    .replace( '\\', '/' );
 202  
 
 203  0
             if ( entry.startsWith( "/" ) )
 204  
             {
 205  0
                 entry = entry.substring( 1 );
 206  
             }
 207  
 
 208  0
             if ( ComponentsXmlArchiverFileFilter.COMPONENTS_XML_PATH.equals( entry ) )
 209  
             {
 210  0
                 InputStream stream = null;
 211  0
                 InputStreamReader reader = null;
 212  
 
 213  
                 try
 214  
                 {
 215  0
                     stream = fileInfo.getContents();
 216  
                     // TODO use ReaderFactory.newXmlReader() when plexus-utils is upgraded to 1.4.5+
 217  0
                     reader = new InputStreamReader( stream, "UTF-8" );
 218  0
                     addComponentsXml( reader );
 219  
                 }
 220  0
                 catch ( final XmlPullParserException e )
 221  
                 {
 222  0
                     final IOException error =
 223  
                         new IOException( "Error finalizing component-set for archive. Reason: " + e.getMessage() );
 224  0
                     error.initCause( e );
 225  
 
 226  0
                     throw error;
 227  
                 }
 228  
                 finally
 229  
                 {
 230  0
                     IOUtil.close( stream );
 231  0
                     IOUtil.close( reader );
 232  0
                 }
 233  
 
 234  0
                 return false;
 235  
             }
 236  
             else
 237  
             {
 238  0
                 return true;
 239  
             }
 240  
         }
 241  
         else
 242  
         {
 243  0
             return true;
 244  
         }
 245  
     }
 246  
 
 247  
     public void finalizeArchiveExtraction( final UnArchiver unarchiver ) throws ArchiverException
 248  
     {
 249  0
     }
 250  
 
 251  
 }