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 java.io.IOException;
23  import java.io.InputStream;
24  import java.nio.charset.StandardCharsets;
25  import java.util.ArrayList;
26  import java.util.List;
27  import java.util.Scanner;
28  import java.util.jar.JarEntry;
29  import java.util.jar.JarOutputStream;
30  
31  import org.apache.commons.io.IOUtils;
32  import org.apache.maven.plugins.shade.relocation.Relocator;
33  
34  /**
35   * Resource transformer that relocates classes in {@code META-INF/sisu/javax.inject.Named} and appends resources
36   * into a single resource.
37   *
38   * @since 3.3.0
39   */
40  public class SisuIndexResourceTransformer
41      extends AbstractCompatibilityTransformer
42  {
43      private static final String SISU_INDEX_PATH = "META-INF/sisu/javax.inject.Named";
44  
45      private final ArrayList<String> indexEntries = new ArrayList<>();
46  
47      private long time = Long.MIN_VALUE;
48  
49      @Override
50      public boolean canTransformResource( final String resource )
51      {
52          return resource.equals( SISU_INDEX_PATH );
53      }
54  
55      @Override
56      public void processResource( final String resource,
57                                   final InputStream is,
58                                   final List<Relocator> relocators,
59                                   long time ) throws IOException
60      {
61          Scanner scanner = new Scanner( is, StandardCharsets.UTF_8.name() );
62          while ( scanner.hasNextLine() )
63          {
64              String relContent = scanner.nextLine();
65              for ( Relocator relocator : relocators )
66              {
67                  if ( relocator.canRelocateClass( relContent ) )
68                  {
69                      relContent = relocator.applyToSourceContent( relContent );
70                  }
71              }
72              indexEntries.add( relContent );
73          }
74  
75          if ( time > this.time )
76          {
77              this.time = time;        
78          }
79      }
80  
81      @Override
82      public boolean hasTransformedResource()
83      {
84          return !indexEntries.isEmpty();
85      }
86  
87      @Override
88      public void modifyOutputStream( final JarOutputStream jos )
89          throws IOException
90      {
91          JarEntry jarEntry = new JarEntry( SISU_INDEX_PATH );
92          jarEntry.setTime( time );
93          jos.putNextEntry( jarEntry );
94          IOUtils.writeLines( indexEntries, "\n", jos, StandardCharsets.UTF_8 );
95          jos.flush();
96          indexEntries.clear();
97     }
98  }