Skip to content

Commit

Permalink
Fixes Netty resources to references shaded class names
Browse files Browse the repository at this point in the history
  • Loading branch information
dzou committed Jun 14, 2021
1 parent 91948b2 commit 7387158
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
55 changes: 55 additions & 0 deletions netty/shaded/build.gradle
@@ -1,3 +1,19 @@
import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer
import com.github.jengelman.gradle.plugins.shadow.transformers.TransformerContext
import org.gradle.api.file.FileTreeElement
import shadow.org.apache.tools.zip.ZipOutputStream
import shadow.org.apache.tools.zip.ZipEntry


buildscript {
repositories {
jcenter()
}
dependencies {
classpath "com.github.jengelman.gradle.plugins:shadow:6.1.0"
}
}

plugins {
id "java"
id "maven-publish"
Expand Down Expand Up @@ -41,6 +57,7 @@ shadowJar {
// this includes concatenation of string literals and constants.
relocate 'META-INF/native/libnetty', 'META-INF/native/libio_grpc_netty_shaded_netty'
relocate 'META-INF/native/netty', 'META-INF/native/io_grpc_netty_shaded_netty'
transform(NettyResourceTransformer.class)
mergeServiceFiles()
}

Expand Down Expand Up @@ -76,3 +93,41 @@ compileTestShadowJava.options.compilerArgs = compileTestJava.options.compilerArg
compileTestShadowJava.options.encoding = compileTestJava.options.encoding

test.dependsOn testShadow

/**
* A Transformer which updates the Netty JAR META-INF/ resources to accurately
* reference shaded class names.
*/
class NettyResourceTransformer implements Transformer {

// A map of resource file paths to the modiid
private Map<String, String> resources = [:]

@Override
boolean canTransformResource(FileTreeElement fileTreeElement) {
fileTreeElement.name.startsWith("META-INF/native-image/io.netty")
}

@Override
void transform(TransformerContext context) {
String updatedContent = context.is.getText().replace("io.netty", "io.grpc.netty.shaded.io.netty")
resources.put(context.path, updatedContent)
}

@Override
boolean hasTransformedResource() {
resources.size() > 0
}

@Override
void modifyOutputStream(ZipOutputStream outputStream, boolean preserveFileTimestamps) {
for (resourceEntry in resources) {
ZipEntry entry = new ZipEntry(resourceEntry.key)
entry.time = TransformerContext.getEntryTimestamp(preserveFileTimestamps, entry.time)

outputStream.putNextEntry(entry)
outputStream.write(resourceEntry.value.getBytes())
outputStream.closeEntry()
}
}
}
Expand Up @@ -39,6 +39,11 @@
import io.grpc.testing.protobuf.SimpleServiceGrpc;
import io.grpc.testing.protobuf.SimpleServiceGrpc.SimpleServiceBlockingStub;
import io.grpc.testing.protobuf.SimpleServiceGrpc.SimpleServiceImplBase;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Test;
Expand Down Expand Up @@ -69,6 +74,19 @@ public void noNormalNetty() throws Exception {
Class.forName("io.grpc.netty.NettyServerBuilder");
}

/** Verify that resources under META-INF/native-image reference shaded class names. */
@Test
public void nettyResourcesUpdated() throws IOException {
InputStream inputStream = NettyChannelBuilder.class.getClassLoader()
.getResourceAsStream("META-INF/native-image/io.netty/transport/reflection-config.json");
assertThat(inputStream).isNotNull();

Scanner s = new Scanner(inputStream, StandardCharsets.UTF_8.name()).useDelimiter("\\A");
String reflectionConfig = s.hasNext() ? s.next() : "";

assertThat(reflectionConfig).contains("io.grpc.netty.shaded.io.netty");
}

@Test
public void serviceLoaderFindsNetty() throws Exception {
assertThat(Grpc.newServerBuilderForPort(0, InsecureServerCredentials.create()))
Expand Down

0 comments on commit 7387158

Please sign in to comment.