Skip to content

Commit

Permalink
[MSHADE-401] Improve ServiceResourceTransformer
Browse files Browse the repository at this point in the history
Changes:
- radically simplify ServiceResourceTransformer
- fix MavenJDOMWriter to be consistent (do not mix) line endings in XML
  • Loading branch information
cstamas committed Aug 9, 2021
1 parent 4d03156 commit c18d3d3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.apache.maven.model.ActivationFile;
import org.apache.maven.model.ActivationOS;
Expand Down Expand Up @@ -90,17 +91,22 @@ public class MavenJDOMWriter
/**
* Field factory
*/
private DefaultJDOMFactory factory;
private final DefaultJDOMFactory factory;

/**
* Field lineSeparator
*/
private String lineSeparator;
private final String lineSeparator;

public MavenJDOMWriter()
{
factory = new DefaultJDOMFactory();
lineSeparator = "\n";
this( "\n" );
}

public MavenJDOMWriter( final String lineSeparator )
{
this.factory = new DefaultJDOMFactory();
this.lineSeparator = Objects.requireNonNull( lineSeparator );
}

/**
Expand Down Expand Up @@ -2126,7 +2132,7 @@ public void write( Model project, Document document, OutputStream stream )
updateModel( project, "project", new Counter( 0 ), document.getRootElement() );
XMLOutputter outputter = new XMLOutputter();
Format format = Format.getPrettyFormat();
format.setIndent( " " ).setLineSeparator( System.getProperty( "line.separator" ) );
format.setIndent( " " ).setLineSeparator( lineSeparator );
outputter.setFormat( format );
outputter.output( document, stream );
}
Expand All @@ -2143,7 +2149,7 @@ public void write( Model project, Document document, OutputStreamWriter writer )
throws IOException
{
Format format = Format.getRawFormat();
format.setEncoding( writer.getEncoding() ).setLineSeparator( System.getProperty( "line.separator" ) );
format.setEncoding( writer.getEncoding() ).setLineSeparator( lineSeparator );
write( project, document, writer, format );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,14 @@
* under the License.
*/

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;

Expand All @@ -51,7 +46,7 @@ public class ServicesResourceTransformer

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

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

private List<Relocator> relocators;

Expand All @@ -65,28 +60,25 @@ public boolean canTransformResource( String resource )
public void processResource( String resource, InputStream is, final List<Relocator> relocators, long time )
throws IOException
{
ServiceStream out = serviceEntries.get( resource );
ArrayList<String> out = serviceEntries.get( resource );
if ( out == null )
{
out = new ServiceStream();
out = new ArrayList<>();
serviceEntries.put( resource, out );
}

final String content = IOUtils.toString( is, StandardCharsets.UTF_8 );
StringReader reader = new StringReader( content );
BufferedReader lineReader = new BufferedReader( reader );
String line;
while ( ( line = lineReader.readLine() ) != null )
Scanner scanner = new Scanner( is, StandardCharsets.UTF_8.name() );
while ( scanner.hasNextLine() )
{
String relContent = line;
String relContent = scanner.nextLine();
for ( Relocator relocator : relocators )
{
if ( relocator.canRelocateClass( relContent ) )
{
relContent = relocator.applyToSourceContent( relContent );
}
}
out.append( relContent + "\n" );
out.add( relContent );
}

if ( this.relocators == null )
Expand All @@ -102,16 +94,16 @@ public void processResource( String resource, InputStream is, final List<Relocat

public boolean hasTransformedResource()
{
return serviceEntries.size() > 0;
return !serviceEntries.isEmpty();
}

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

if ( relocators != null )
{
Expand All @@ -132,52 +124,9 @@ public void modifyOutputStream( JarOutputStream jos )
jarEntry.setTime( time );
jos.putNextEntry( jarEntry );


// read the content of service file for candidate classes for relocation.
// Specification requires that this file is encoded in UTF-8.
Writer writer = new OutputStreamWriter( jos, StandardCharsets.UTF_8 );
InputStreamReader streamReader = new InputStreamReader( data.toInputStream() );
BufferedReader reader = new BufferedReader( streamReader );
String className;

while ( ( className = reader.readLine() ) != null )
{
writer.write( className );
writer.write( System.lineSeparator() );
writer.flush();
}

reader.close();
data.reset();
}
}

static class ServiceStream
extends ByteArrayOutputStream
{

ServiceStream()
{
super( 1024 );
IOUtils.writeLines( data, "\n", jos, StandardCharsets.UTF_8 );
jos.flush();
data.clear();
}

public void append( String content )
throws IOException
{
if ( count > 0 && buf[count - 1] != '\n' && buf[count - 1] != '\r' )
{
write( '\n' );
}

byte[] contentBytes = content.getBytes( StandardCharsets.UTF_8 );
this.write( contentBytes );
}

public InputStream toInputStream()
{
return new ByteArrayInputStream( buf, 0, count );
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
* Test for handling META-INF/service/...
*/
public class ServiceResourceTransformerTest {

private final String NEWLINE = "\n";

private List<Relocator> relocators = new ArrayList<Relocator>();

@Test
Expand Down Expand Up @@ -75,8 +76,8 @@ public void relocatedClasses() throws Exception {
assertNotNull( jarEntry );
try ( InputStream entryStream = jarFile.getInputStream( jarEntry ) ) {
String xformedContent = IOUtils.toString( entryStream, "utf-8" );
assertEquals( "borg.foo.Service" + System.getProperty( "line.separator" )
+ "org.foo.exclude.OtherService" + System.getProperty( "line.separator" ), xformedContent );
assertEquals( "borg.foo.Service" + NEWLINE
+ "org.foo.exclude.OtherService" + NEWLINE, xformedContent );
} finally {
jarFile.close();
}
Expand Down Expand Up @@ -111,8 +112,8 @@ public void concatanationAppliedMultipleTimes() throws Exception {
JarEntry jarEntry = jarFile.getJarEntry( contentResource );
assertNotNull( jarEntry );
try ( InputStream entryStream = jarFile.getInputStream( jarEntry ) ) {
String xformedContent = IOUtils.toString(entryStream, "utf-8");
assertEquals( "org.eclipse1234.osgi.launch.EquinoxFactory" + System.getProperty( "line.separator" ), xformedContent );
String xformedContent = IOUtils.toString(entryStream, StandardCharsets.UTF_8);
assertEquals( "org.eclipse1234.osgi.launch.EquinoxFactory" + NEWLINE, xformedContent );
} finally {
jarFile.close();
}
Expand Down

0 comments on commit c18d3d3

Please sign in to comment.