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