-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[MSHADE-353] adding a generic relocation friendly transformer #39
Open
rmannibucau
wants to merge
1
commit into
master
Choose a base branch
from
rmannibucau/MSHADE-353-ensure-relocations-can-be-applied-to-any-transformer
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
src/main/java/org/apache/maven/plugins/shade/resource/BaseRelocatingTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.apache.maven.plugins.shade.resource; | ||
|
||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import java.util.List; | ||
|
||
import org.apache.maven.plugins.shade.relocation.Relocator; | ||
|
||
/** | ||
* Abstract class providing the needed logic to relocate any content. | ||
*/ | ||
public abstract class BaseRelocatingTransformer implements ResourceTransformer | ||
{ | ||
protected String relocate( String originalValue, List<Relocator> relocators ) | ||
{ | ||
String newValue = originalValue; | ||
for ( Relocator relocator : relocators ) | ||
{ | ||
String value; | ||
do | ||
{ | ||
value = newValue; | ||
newValue = relocator.relocateClass( value ); | ||
} | ||
while ( !value.equals( newValue ) ); | ||
} | ||
return newValue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/main/java/org/apache/maven/plugins/shade/resource/RelocationTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package org.apache.maven.plugins.shade.resource; | ||
|
||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.jar.JarOutputStream; | ||
|
||
import org.apache.maven.plugins.shade.relocation.Relocator; | ||
import org.codehaus.plexus.util.IOUtil; | ||
|
||
/** | ||
* Trivial transformer applying relocators on resources content. | ||
*/ | ||
public class RelocationTransformer extends BaseRelocatingTransformer | ||
{ | ||
private Collection<ResourceTransformer> delegates; | ||
private boolean transformed; | ||
|
||
@Override | ||
public boolean canTransformResource( String resource ) | ||
{ | ||
if ( delegates == null ) | ||
{ | ||
return false; | ||
} | ||
for ( ResourceTransformer transformer : delegates ) | ||
{ | ||
if ( transformer.canTransformResource( resource ) ) | ||
{ | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public void processResource( String resource, InputStream is, List<Relocator> relocators ) throws IOException | ||
{ | ||
byte[] relocated = null; | ||
for ( ResourceTransformer transformer : delegates ) | ||
{ | ||
if ( transformer.canTransformResource( resource ) ) | ||
{ | ||
transformed = true; | ||
if ( relocated == null ) | ||
{ | ||
relocated = relocate( IOUtil.toString( is ), relocators ) | ||
.getBytes( StandardCharsets.UTF_8 ); | ||
} | ||
transformer.processResource( | ||
resource, | ||
new ByteArrayInputStream( relocated ), | ||
relocators ); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public boolean hasTransformedResource() | ||
{ | ||
return transformed; | ||
} | ||
|
||
@Override | ||
public void modifyOutputStream( JarOutputStream os ) throws IOException | ||
{ | ||
if ( !transformed ) | ||
{ | ||
return; | ||
} | ||
for ( ResourceTransformer transformer : delegates ) | ||
{ | ||
if ( transformer.hasTransformedResource() ) | ||
{ | ||
transformer.modifyOutputStream( os ); | ||
} | ||
} | ||
} | ||
|
||
public void setDelegates( Collection<ResourceTransformer> delegates ) | ||
{ | ||
this.delegates = delegates; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/test/java/org/apache/maven/plugins/shade/resource/RelocationTransformerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package org.apache.maven.plugins.shade.resource; | ||
|
||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Collections; | ||
import java.util.jar.JarEntry; | ||
import java.util.jar.JarInputStream; | ||
import java.util.jar.JarOutputStream; | ||
|
||
import org.apache.maven.plugins.shade.relocation.Relocator; | ||
import org.apache.maven.plugins.shade.relocation.SimpleRelocator; | ||
import org.codehaus.plexus.util.IOUtil; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Ensure the relation transformer uses relocators. | ||
*/ | ||
public class RelocationTransformerTest | ||
{ | ||
@Test | ||
public void relocate() throws IOException | ||
{ | ||
AppendingTransformer delegate = new AppendingTransformer(); | ||
delegate.resource = "foo/bar.txt"; | ||
|
||
RelocationTransformer resourceTransformer = new RelocationTransformer(); | ||
resourceTransformer.setDelegates( Collections.<ResourceTransformer>singletonList( delegate ) ); | ||
|
||
assertTrue( resourceTransformer.canTransformResource( "foo/bar.txt" ) ); | ||
resourceTransformer.processResource( | ||
"foo/bar.txt", | ||
new ByteArrayInputStream("a=javax.foo.bar".getBytes( StandardCharsets.UTF_8 )), | ||
Collections.<Relocator>singletonList( new SimpleRelocator( | ||
"javax", "jakarta", null, null ) )); | ||
final ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
try ( JarOutputStream jarOutputStream = new JarOutputStream(out) ) | ||
{ | ||
resourceTransformer.modifyOutputStream(jarOutputStream); | ||
} | ||
try ( JarInputStream jarInputStream = new JarInputStream( new ByteArrayInputStream( out.toByteArray() ) )) | ||
{ | ||
final JarEntry entry = jarInputStream.getNextJarEntry(); | ||
assertNotNull( entry ); | ||
assertEquals( "foo/bar.txt", entry.getName() ); | ||
assertEquals( "a=jakarta.foo.bar", IOUtil.toString( jarInputStream ).trim() ); | ||
assertNull( jarInputStream.getNextJarEntry() ); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe I'm missing an important detail, but in this example is no relocation. So why do it like this?
And I would have expected that defining both transformers after each other would work, but that requires defining the resource twice. That's probably the thing you're trying to solve.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This example is about composing transformers which is not something obvious for most people.
Defining each one after the other does not work because the relocation transformer is a delagate pattern which only works if there is another transformer saying it what it should handle. Goal is not to not define resource twice but to be able to decorate any transformer with this feature, including the ones with dynamic resource handling (pattern in canTransform for ex).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nested transformers looks more like a XML representation of the Java solution, and it might also expose a design flaw in the plugin.
It probably makes more sense to start with the resources, and define its transformers:
I understand that this is quite a different approach, but this looks way more clear to what's happening for the end user. Maybe the code can stay almost the same, but I would map this configuration to the actual transformer calls.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry @rfscholte , I don't get this proposal at all. You assume the resource configuration is an input of a transformer but a transformer can actually compute its resources and even create them on the fly without taking any input from the project sources so I don't see how it can work :s.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on your test the input is
foo/bar.txt
, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, yes and no. It is foo/bar.txt cause it is an AppendingTransformer but the RelocatingTransformer is not aware of that detail. Think to a GraalVM transformer, it will create resources depending what is in the build so the files will not be configured statically. The RelocatingTransformer still works.