Coverage Report - org.apache.maven.plugins.shade.resource.ComponentsXmlResourceTransformer
 
Classes in this File Line Coverage Branch Coverage Complexity
ComponentsXmlResourceTransformer
92%
59/64
71%
27/38
3.556
ComponentsXmlResourceTransformer$1
100%
2/2
N/A
3.556
 
 1  
 package org.apache.maven.plugins.shade.resource;
 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.plugins.shade.relocation.Relocator;
 23  
 import org.codehaus.plexus.util.IOUtil;
 24  
 import org.codehaus.plexus.util.ReaderFactory;
 25  
 import org.codehaus.plexus.util.WriterFactory;
 26  
 import org.codehaus.plexus.util.xml.Xpp3Dom;
 27  
 import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
 28  
 import org.codehaus.plexus.util.xml.Xpp3DomWriter;
 29  
 
 30  
 import java.io.BufferedInputStream;
 31  
 import java.io.ByteArrayOutputStream;
 32  
 import java.io.IOException;
 33  
 import java.io.InputStream;
 34  
 import java.io.Reader;
 35  
 import java.io.Writer;
 36  
 import java.util.LinkedHashMap;
 37  
 import java.util.List;
 38  
 import java.util.Map;
 39  
 import java.util.jar.JarEntry;
 40  
 import java.util.jar.JarOutputStream;
 41  
 
 42  
 /**
 43  
  * A resource processor that aggregates plexus <code>components.xml</code> files.
 44  
  */
 45  6
 public class ComponentsXmlResourceTransformer
 46  
     implements ResourceTransformer
 47  
 {
 48  6
     private Map<String, Xpp3Dom> components = new LinkedHashMap<String, Xpp3Dom>();
 49  
 
 50  
     public static final String COMPONENTS_XML_PATH = "META-INF/plexus/components.xml";
 51  
 
 52  
     public boolean canTransformResource( String resource )
 53  
     {
 54  55
         return COMPONENTS_XML_PATH.equals( resource );
 55  
     }
 56  
 
 57  
     public void processResource( String resource, InputStream is, List<Relocator> relocators )
 58  
         throws IOException
 59  
     {
 60  
         Xpp3Dom newDom;
 61  
 
 62  
         try
 63  
         {
 64  7
             BufferedInputStream bis = new BufferedInputStream( is )
 65  7
             {
 66  
                 public void close()
 67  
                     throws IOException
 68  
                 {
 69  
                     // leave ZIP open
 70  7
                 }
 71  
             };
 72  
 
 73  7
             Reader reader = ReaderFactory.newXmlReader( bis );
 74  
 
 75  7
             newDom = Xpp3DomBuilder.build( reader );
 76  
         }
 77  0
         catch ( Exception e )
 78  
         {
 79  0
             throw (IOException) new IOException( "Error parsing components.xml in " + is ).initCause( e );
 80  7
         }
 81  
 
 82  
         // Only try to merge in components if there are some elements in the component-set
 83  7
         if ( newDom.getChild( "components" ) == null )
 84  
         {
 85  0
             return;
 86  
         }
 87  
 
 88  7
         Xpp3Dom[] children = newDom.getChild( "components" ).getChildren( "component" );
 89  
 
 90  96
         for ( int i = 0; i < children.length; i++ )
 91  
         {
 92  89
             Xpp3Dom component = children[i];
 93  
 
 94  89
             String role = getValue( component, "role" );
 95  89
             role = getRelocatedClass( role, relocators );
 96  89
             setValue( component, "role", role );
 97  
 
 98  89
             String roleHint = getValue( component, "role-hint" );
 99  
 
 100  89
             String impl = getValue( component, "implementation" );
 101  89
             impl = getRelocatedClass( impl, relocators );
 102  89
             setValue( component, "implementation", impl );
 103  
 
 104  89
             String key = role + ':' + roleHint;
 105  89
             if ( components.containsKey( key ) )
 106  
             {
 107  
                 // TODO: use the tools in Plexus to merge these properly. For now, I just need an all-or-nothing
 108  
                 // configuration carry over
 109  
 
 110  2
                 Xpp3Dom dom = components.get( key );
 111  2
                 if ( dom.getChild( "configuration" ) != null )
 112  
                 {
 113  1
                     component.addChild( dom.getChild( "configuration" ) );
 114  
                 }
 115  
             }
 116  
 
 117  89
             Xpp3Dom requirements = component.getChild( "requirements" );
 118  89
             if ( requirements != null && requirements.getChildCount() > 0 )
 119  
             {
 120  20
                 for ( int r = requirements.getChildCount() - 1; r >= 0; r-- )
 121  
                 {
 122  10
                     Xpp3Dom requirement = requirements.getChild( r );
 123  
 
 124  10
                     String requiredRole = getValue( requirement, "role" );
 125  10
                     requiredRole = getRelocatedClass( requiredRole, relocators );
 126  10
                     setValue( requirement, "role", requiredRole );
 127  
                 }
 128  
             }
 129  
 
 130  89
             components.put( key, component );
 131  
         }
 132  7
     }
 133  
 
 134  
     public void modifyOutputStream( JarOutputStream jos )
 135  
         throws IOException
 136  
     {
 137  5
         byte[] data = getTransformedResource();
 138  
 
 139  5
         jos.putNextEntry( new JarEntry( COMPONENTS_XML_PATH ) );
 140  
 
 141  5
         IOUtil.copy( data, jos );
 142  
 
 143  5
         components.clear();
 144  5
     }
 145  
 
 146  
     public boolean hasTransformedResource()
 147  
     {
 148  5
         return !components.isEmpty();
 149  
     }
 150  
 
 151  
     byte[] getTransformedResource()
 152  
         throws IOException
 153  
     {
 154  6
         ByteArrayOutputStream baos = new ByteArrayOutputStream( 1024 * 4 );
 155  
 
 156  6
         Writer writer = WriterFactory.newXmlWriter( baos );
 157  
         try
 158  
         {
 159  6
             Xpp3Dom dom = new Xpp3Dom( "component-set" );
 160  
 
 161  6
             Xpp3Dom componentDom = new Xpp3Dom( "components" );
 162  
 
 163  6
             dom.addChild( componentDom );
 164  
 
 165  6
             for ( Xpp3Dom component : components.values() )
 166  
             {
 167  87
                 componentDom.addChild( component );
 168  
             }
 169  
 
 170  6
             Xpp3DomWriter.write( writer, dom );
 171  
         }
 172  
         finally
 173  
         {
 174  6
             IOUtil.close( writer );
 175  6
         }
 176  
 
 177  6
         return baos.toByteArray();
 178  
     }
 179  
 
 180  
     private String getRelocatedClass( String className, List<Relocator> relocators )
 181  
     {
 182  188
         if ( className != null && className.length() > 0 && relocators != null )
 183  
         {
 184  188
             for ( Relocator relocator : relocators )
 185  
             {
 186  180
                 if ( relocator.canRelocateClass( className ) )
 187  
                 {
 188  0
                     return relocator.relocateClass( className );
 189  
                 }
 190  
             }
 191  
         }
 192  
 
 193  188
         return className;
 194  
     }
 195  
 
 196  
     private static String getValue( Xpp3Dom dom, String element )
 197  
     {
 198  277
         Xpp3Dom child = dom.getChild( element );
 199  
 
 200  277
         return ( child != null && child.getValue() != null ) ? child.getValue() : "";
 201  
     }
 202  
 
 203  
     private static void setValue( Xpp3Dom dom, String element, String value )
 204  
     {
 205  188
         Xpp3Dom child = dom.getChild( element );
 206  
 
 207  188
         if ( child == null || value == null || value.length() <= 0 )
 208  
         {
 209  0
             return;
 210  
         }
 211  
 
 212  188
         child.setValue( value );
 213  188
     }
 214  
 
 215  
 }