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  
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.util.ArrayList;
27  import java.util.Collection;
28  import java.util.Collections;
29  import java.util.Iterator;
30  import java.util.List;
31  import java.util.Properties;
32  import java.util.jar.JarEntry;
33  import java.util.jar.JarOutputStream;
34  
35  /**
36   * Aggregate Apache Groovy extension modules descriptors
37   */
38  public class GroovyResourceTransformer
39      implements ResourceTransformer
40  {
41  
42      static final String EXT_MODULE_NAME = "META-INF/services/org.codehaus.groovy.runtime.ExtensionModule";
43  
44      private List<String> extensionClassesList = new ArrayList<String>();
45  
46      private List<String> staticExtensionClassesList = new ArrayList<String>();
47  
48      private String extModuleName = "no-module-name";
49  
50      private String extModuleVersion = "1.0";
51  
52      @Override
53      public boolean canTransformResource( String resource )
54      {
55          return EXT_MODULE_NAME.equals( resource );
56      }
57  
58      @Override
59      public void processResource( String resource, InputStream is, List<Relocator> relocators )
60          throws IOException
61      {
62          Properties out = new Properties();
63          try
64          {
65              out.load( is );
66          }
67          finally
68          {
69              is.close();
70          }
71          String extensionClasses = out.getProperty( "extensionClasses", "" ).trim();
72          if ( extensionClasses.length() > 0 )
73          {
74              append( extensionClasses, extensionClassesList );
75          }
76          String staticExtensionClasses = out.getProperty( "staticExtensionClasses", "" ).trim();
77          if ( staticExtensionClasses.length() > 0 )
78          {
79              append( staticExtensionClasses, staticExtensionClassesList );
80          }
81      }
82  
83      private void append( String entry, List<String> list )
84      {
85          if ( entry != null )
86          {
87              Collections.addAll( list, entry.split( "\\s*,\\s*" ) );
88          }
89      }
90  
91      @Override
92      public boolean hasTransformedResource()
93      {
94          return extensionClassesList.size() > 0 && staticExtensionClassesList.size() > 0;
95      }
96  
97      @Override
98      public void modifyOutputStream( JarOutputStream os )
99          throws IOException
100     {
101         if ( hasTransformedResource() )
102         {
103             os.putNextEntry( new JarEntry( EXT_MODULE_NAME ) );
104             Properties desc = new Properties();
105             desc.put( "moduleName", extModuleName );
106             desc.put( "moduleVersion", extModuleVersion );
107             if ( extensionClassesList.size() > 0 )
108             {
109                 desc.put( "extensionClasses", join( extensionClassesList ) );
110             }
111             if ( staticExtensionClassesList.size() > 0 )
112             {
113                 desc.put( "staticExtensionClasses", join( staticExtensionClassesList ) );
114             }
115             desc.store( os, null );
116         }
117     }
118 
119     private String join( Collection<String> strings )
120     {
121         Iterator<String> it = strings.iterator();
122         switch ( strings.size() )
123         {
124             case 0:
125                 return "";
126             case 1:
127                 return it.next();
128             default:
129                 StringBuilder buff = new StringBuilder( it.next() );
130                 while ( it.hasNext() )
131                 {
132                     buff.append( "," ).append( it.next() );
133                 }
134                 return buff.toString();
135         }
136     }
137 
138     public void setExtModuleName( String extModuleName )
139     {
140         this.extModuleName = extModuleName;
141     }
142 
143     public void setExtModuleVersion( String extModuleVersion )
144     {
145         this.extModuleVersion = extModuleVersion;
146     }
147 }