View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.shade.resource;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.nio.charset.StandardCharsets;
24  import java.util.ArrayList;
25  import java.util.List;
26  import java.util.Scanner;
27  import java.util.jar.JarEntry;
28  import java.util.jar.JarOutputStream;
29  
30  import org.apache.commons.io.IOUtils;
31  import org.apache.maven.plugins.shade.relocation.Relocator;
32  
33  /**
34   * Resource transformer that relocates classes in {@code META-INF/sisu/javax.inject.Named} and appends resources
35   * into a single resource.
36   *
37   * @since 3.3.0
38   */
39  public class SisuIndexResourceTransformer extends AbstractCompatibilityTransformer {
40      private static final String SISU_INDEX_PATH = "META-INF/sisu/javax.inject.Named";
41  
42      private final ArrayList<String> indexEntries = new ArrayList<>();
43  
44      private long time = Long.MIN_VALUE;
45  
46      @Override
47      public boolean canTransformResource(final String resource) {
48          return resource.equals(SISU_INDEX_PATH);
49      }
50  
51      @Override
52      public void processResource(
53              final String resource, final InputStream is, final List<Relocator> relocators, long time)
54              throws IOException {
55          Scanner scanner = new Scanner(is, StandardCharsets.UTF_8.name());
56          while (scanner.hasNextLine()) {
57              String relContent = scanner.nextLine();
58              for (Relocator relocator : relocators) {
59                  if (relocator.canRelocateClass(relContent)) {
60                      relContent = relocator.applyToSourceContent(relContent);
61                  }
62              }
63              indexEntries.add(relContent);
64          }
65  
66          if (time > this.time) {
67              this.time = time;
68          }
69      }
70  
71      @Override
72      public boolean hasTransformedResource() {
73          return !indexEntries.isEmpty();
74      }
75  
76      @Override
77      public void modifyOutputStream(final JarOutputStream jos) throws IOException {
78          JarEntry jarEntry = new JarEntry(SISU_INDEX_PATH);
79          jarEntry.setTime(time);
80          jos.putNextEntry(jarEntry);
81          IOUtils.writeLines(indexEntries, "\n", jos, StandardCharsets.UTF_8);
82          jos.flush();
83          indexEntries.clear();
84      }
85  }