Skip to content

Commit

Permalink
[MSHADE-425] Relocate services name before add to serviceEntries
Browse files Browse the repository at this point in the history
Signed-off-by: tison <wander4096@gmail.com>
  • Loading branch information
tisonkun authored and slawekjaranowski committed Sep 5, 2022
1 parent 26b5873 commit ad2f6f8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,12 @@
* shading process.
*/
public class ServicesResourceTransformer
extends AbstractCompatibilityTransformer
extends AbstractCompatibilityTransformer
{

private static final String SERVICES_PATH = "META-INF/services";

private final Map<String, ArrayList<String>> serviceEntries = new HashMap<>();

private List<Relocator> relocators;

private long time = Long.MIN_VALUE;

public boolean canTransformResource( String resource )
Expand All @@ -58,14 +55,20 @@ public boolean canTransformResource( String resource )
}

public void processResource( String resource, InputStream is, final List<Relocator> relocators, long time )
throws IOException
throws IOException
{
ArrayList<String> out = serviceEntries.get( resource );
if ( out == null )
resource = resource.substring( SERVICES_PATH.length() + 1 );
for ( Relocator relocator : relocators )
{
out = new ArrayList<>();
serviceEntries.put( resource, out );
if ( relocator.canRelocateClass( resource ) )
{
resource = relocator.relocateClass( resource );
break;
}
}
resource = SERVICES_PATH + '/' + resource;

ArrayList<String> out = serviceEntries.computeIfAbsent( resource, k -> new ArrayList<>() );

Scanner scanner = new Scanner( is, StandardCharsets.UTF_8.name() );
while ( scanner.hasNextLine() )
Expand All @@ -81,14 +84,9 @@ public void processResource( String resource, InputStream is, final List<Relocat
out.add( relContent );
}

if ( this.relocators == null )
{
this.relocators = relocators;
}

if ( time > this.time )
{
this.time = time;
this.time = time;
}
}

Expand All @@ -98,28 +96,13 @@ public boolean hasTransformedResource()
}

public void modifyOutputStream( JarOutputStream jos )
throws IOException
throws IOException
{
for ( Map.Entry<String, ArrayList<String>> entry : serviceEntries.entrySet() )
{
String key = entry.getKey();
ArrayList<String> data = entry.getValue();

if ( relocators != null )
{
key = key.substring( SERVICES_PATH.length() + 1 );
for ( Relocator relocator : relocators )
{
if ( relocator.canRelocateClass( key ) )
{
key = relocator.relocateClass( key );
break;
}
}

key = SERVICES_PATH + '/' + key;
}

JarEntry jarEntry = new JarEntry( key );
jarEntry.setTime( time );
jos.putNextEntry( jarEntry );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
Expand Down Expand Up @@ -85,6 +86,49 @@ public void relocatedClasses() throws Exception {
tempJar.delete();
}
}

@Test
public void mergeRelocatedFiles() throws Exception {
SimpleRelocator relocator =
new SimpleRelocator( "org.foo", "borg.foo", null, Collections.singletonList("org.foo.exclude.*"));
relocators.add( relocator );

String content = "org.foo.Service" + NEWLINE + "org.foo.exclude.OtherService" + NEWLINE;
String contentShaded = "borg.foo.Service" + NEWLINE + "org.foo.exclude.OtherService" + NEWLINE;
byte[] contentBytes = content.getBytes( StandardCharsets.UTF_8 );
String contentResource = "META-INF/services/org.foo.something.another";
String contentResourceShaded = "META-INF/services/borg.foo.something.another";

ServicesResourceTransformer xformer = new ServicesResourceTransformer();

try (InputStream contentStream = new ByteArrayInputStream( contentBytes )) {
xformer.processResource(contentResource, contentStream, relocators, 0);
}

try (InputStream contentStream = new ByteArrayInputStream( contentBytes )) {
xformer.processResource(contentResourceShaded, contentStream, relocators, 0);
}

File tempJar = File.createTempFile("shade.", ".jar");
tempJar.deleteOnExit();
FileOutputStream fos = new FileOutputStream( tempJar );
try ( JarOutputStream jos = new JarOutputStream( fos ) ) {
xformer.modifyOutputStream( jos );
jos.close();

JarFile jarFile = new JarFile( tempJar );
JarEntry jarEntry = jarFile.getJarEntry( contentResourceShaded );
assertNotNull( jarEntry );
try ( InputStream entryStream = jarFile.getInputStream( jarEntry ) ) {
String xformedContent = IOUtils.toString( entryStream, StandardCharsets.UTF_8);
assertEquals( contentShaded + contentShaded, xformedContent );
} finally {
jarFile.close();
}
} finally {
tempJar.delete();
}
}

@Test
public void concatanationAppliedMultipleTimes() throws Exception {
Expand Down

0 comments on commit ad2f6f8

Please sign in to comment.