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.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  public class ComponentsXmlResourceTransformer
46      implements ResourceTransformer
47  {
48      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          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              BufferedInputStream bis = new BufferedInputStream( is )
65              {
66                  public void close()
67                      throws IOException
68                  {
69                      // leave ZIP open
70                  }
71              };
72  
73              Reader reader = ReaderFactory.newXmlReader( bis );
74  
75              newDom = Xpp3DomBuilder.build( reader );
76          }
77          catch ( Exception e )
78          {
79              throw (IOException) new IOException( "Error parsing components.xml in " + is ).initCause( e );
80          }
81  
82          // Only try to merge in components if there are some elements in the component-set
83          if ( newDom.getChild( "components" ) == null )
84          {
85              return;
86          }
87  
88          Xpp3Dom[] children = newDom.getChild( "components" ).getChildren( "component" );
89  
90          for (Xpp3Dom component : children) {
91              String role = getValue(component, "role");
92              role = getRelocatedClass(role, relocators);
93              setValue(component, "role", role);
94  
95              String roleHint = getValue(component, "role-hint");
96  
97              String impl = getValue(component, "implementation");
98              impl = getRelocatedClass(impl, relocators);
99              setValue(component, "implementation", impl);
100 
101             String key = role + ':' + roleHint;
102             if (components.containsKey(key)) {
103                 // TODO: use the tools in Plexus to merge these properly. For now, I just need an all-or-nothing
104                 // configuration carry over
105 
106                 Xpp3Dom dom = components.get(key);
107                 if (dom.getChild("configuration") != null) {
108                     component.addChild(dom.getChild("configuration"));
109                 }
110             }
111 
112             Xpp3Dom requirements = component.getChild("requirements");
113             if (requirements != null && requirements.getChildCount() > 0) {
114                 for (int r = requirements.getChildCount() - 1; r >= 0; r--) {
115                     Xpp3Dom requirement = requirements.getChild(r);
116 
117                     String requiredRole = getValue(requirement, "role");
118                     requiredRole = getRelocatedClass(requiredRole, relocators);
119                     setValue(requirement, "role", requiredRole);
120                 }
121             }
122 
123             components.put(key, component);
124         }
125     }
126 
127     public void modifyOutputStream( JarOutputStream jos )
128         throws IOException
129     {
130         byte[] data = getTransformedResource();
131 
132         jos.putNextEntry( new JarEntry( COMPONENTS_XML_PATH ) );
133 
134         IOUtil.copy( data, jos );
135 
136         components.clear();
137     }
138 
139     public boolean hasTransformedResource()
140     {
141         return !components.isEmpty();
142     }
143 
144     byte[] getTransformedResource()
145         throws IOException
146     {
147         ByteArrayOutputStream baos = new ByteArrayOutputStream( 1024 * 4 );
148 
149         Writer writer = WriterFactory.newXmlWriter( baos );
150         try
151         {
152             Xpp3Dom dom = new Xpp3Dom( "component-set" );
153 
154             Xpp3Dom componentDom = new Xpp3Dom( "components" );
155 
156             dom.addChild( componentDom );
157 
158             for ( Xpp3Dom component : components.values() )
159             {
160                 componentDom.addChild( component );
161             }
162 
163             Xpp3DomWriter.write( writer, dom );
164         }
165         finally
166         {
167             IOUtil.close( writer );
168         }
169 
170         return baos.toByteArray();
171     }
172 
173     private String getRelocatedClass( String className, List<Relocator> relocators )
174     {
175         if ( className != null && className.length() > 0 && relocators != null )
176         {
177             for ( Relocator relocator : relocators )
178             {
179                 if ( relocator.canRelocateClass( className ) )
180                 {
181                     return relocator.relocateClass( className );
182                 }
183             }
184         }
185 
186         return className;
187     }
188 
189     private static String getValue( Xpp3Dom dom, String element )
190     {
191         Xpp3Dom child = dom.getChild( element );
192 
193         return ( child != null && child.getValue() != null ) ? child.getValue() : "";
194     }
195 
196     private static void setValue( Xpp3Dom dom, String element, String value )
197     {
198         Xpp3Dom child = dom.getChild( element );
199 
200         if ( child == null || value == null || value.length() <= 0 )
201         {
202             return;
203         }
204 
205         child.setValue( value );
206     }
207 
208 }