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