@Override public void processResource( String resource, InputStream is, List<Relocator> relocators ) throws IOException { Properties out = new Properties(); try { out.load( is ); } finally { is.close(); } String extensionClasses = out.getProperty( "extensionClasses", "" ).trim(); if ( extensionClasses.length() > 0 ) { append( extensionClasses, extensionClassesList ); } String staticExtensionClasses = out.getProperty( "staticExtensionClasses", "" ).trim(); if ( staticExtensionClasses.length() > 0 ) { append( staticExtensionClasses, staticExtensionClassesList ); } }
private List<Relocator> getRelocators() { List<Relocator> relocators = new ArrayList<Relocator>(); if ( relocations == null ) { return relocators; } for ( PackageRelocation r : relocations ) { relocators.add( new SimpleRelocator( r.getPattern(), r.getShadedPattern(), r.getIncludes(), r.getExcludes(), r.isRawString() ) ); } return relocators; }
private void addJavaSource( Set<String> resources, JarOutputStream jos, String name, InputStream is, List<Relocator> relocators, boolean consistentDates ) throws IOException { JarEntry jarEntry = new ConsistentJarEntry( name, consistentDates ); jos.putNextEntry( jarEntry ); String sourceContent = IOUtil.toString( new InputStreamReader( is, "UTF-8" ) ); for ( Relocator relocator : relocators ) { sourceContent = relocator.applyToSourceContent( sourceContent ); } final Writer writer = new OutputStreamWriter( jos, "UTF-8" ); IOUtil.copy( sourceContent, writer ); writer.flush(); resources.add( name ); }
public String map( String name ) { String value = name; String prefix = ""; String suffix = ""; Matcher m = classPattern.matcher( name ); if ( m.matches() ) { prefix = m.group( 1 ) + "L"; suffix = ";"; name = m.group( 2 ); } for ( Relocator r : relocators ) { if ( r.canRelocatePath( name ) ) { value = prefix + r.relocatePath( name ) + suffix; break; } } return value; }
public void testConcatenation() throws Exception { GroovyResourceTransformer transformer = new GroovyResourceTransformer(); transformer.processResource( GroovyResourceTransformer.EXT_MODULE_NAME, module( "mod1", "1.0", "some.ext1", null ), Collections.<Relocator>emptyList() ); transformer.processResource( GroovyResourceTransformer.EXT_MODULE_NAME, module( "mod2", "1.0", null, "some.staticExt1" ), Collections.<Relocator>emptyList() ); transformer.processResource( GroovyResourceTransformer.EXT_MODULE_NAME, module( "mod3", "1.0", "", "" ), Collections.<Relocator>emptyList() ); transformer.processResource( GroovyResourceTransformer.EXT_MODULE_NAME, module( "mod4", "1.0", "some.ext2", "some.staticExt2" ), Collections.<Relocator>emptyList() ); Properties desc = transform( transformer, false ); assertEquals( "no-module-name", desc.getProperty( "moduleName" ) ); assertEquals( "1.0", desc.getProperty( "moduleVersion" ) ); assertEquals( "some.ext1,some.ext2", desc.getProperty( "extensionClasses" ) ); assertEquals( "some.staticExt1,some.staticExt2", desc.getProperty( "staticExtensionClasses" ) ); }
public void testConfigurationMerging() throws Exception { XMLUnit.setNormalizeWhitespace( true ); InputStream resourceAsStream = getClass().getResourceAsStream( "/components-1.xml" ); transformer.processResource( "components-1.xml", resourceAsStream, Collections.<Relocator> emptyList() ); resourceAsStream.close(); InputStream resourceAsStream1 = getClass().getResourceAsStream( "/components-2.xml" ); transformer.processResource( "components-1.xml", resourceAsStream1, Collections.<Relocator> emptyList() ); resourceAsStream1.close(); final InputStream resourceAsStream2 = getClass().getResourceAsStream( "/components-expected.xml" ); Diff diff = XMLUnit.compareXML( IOUtil.toString( resourceAsStream2, "UTF-8" ), IOUtil.toString( transformer.getTransformedResource(), "UTF-8" ) ); //assertEquals( IOUtil.toString( getClass().getResourceAsStream( "/components-expected.xml" ), "UTF-8" ), // IOUtil.toString( transformer.getTransformedResource(), "UTF-8" ).replaceAll("\r\n", "\n") ); resourceAsStream2.close(); XMLAssert.assertXMLIdentical( diff, true ); }
private void addJavaSource(Set<String> resources, JarOutputStream jos, String name, InputStream is, List<Relocator> relocators) throws IOException { jos.putNextEntry(new JarEntry(name)); String sourceContent = IOUtil.toString(new InputStreamReader(is, "UTF-8")); for (Relocator relocator : relocators) { sourceContent = relocator.applyToSourceContent(sourceContent); } OutputStreamWriter writer = new OutputStreamWriter(jos, "UTF-8"); IOUtil.copy(sourceContent, writer); writer.flush(); resources.add(name); }
@Override public String map(String name) { String value = name; String prefix = ""; String suffix = ""; Matcher m = classPattern.matcher(name); if (m.matches()) { prefix = m.group(1) + "L"; suffix = ";"; name = m.group(2); } for (Relocator r : relocators) { if (r.canRelocatePath(name)) { value = prefix + r.relocatePath(name) + suffix; break; } } return value; }
public void testConfigurationMerging() throws Exception { XMLUnit.setNormalizeWhitespace( true ); transformer.processResource( "components-1.xml", getClass().getResourceAsStream( "/components-1.xml" ), Collections.<Relocator> emptyList() ); transformer.processResource( "components-1.xml", getClass().getResourceAsStream( "/components-2.xml" ), Collections.<Relocator> emptyList() ); Diff diff = XMLUnit.compareXML( IOUtil.toString( getClass().getResourceAsStream( "/components-expected.xml" ), "UTF-8" ), IOUtil.toString( transformer.getTransformedResource(), "UTF-8" ) ); //assertEquals( IOUtil.toString( getClass().getResourceAsStream( "/components-expected.xml" ), "UTF-8" ), // IOUtil.toString( transformer.getTransformedResource(), "UTF-8" ).replaceAll("\r\n", "\n") ); XMLAssert.assertXMLIdentical( diff, true ); }
public void processResource( String resource, InputStream is, List<Relocator> relocators ) throws IOException { // We just want to take the first manifest we come across as that's our project's manifest. This is the behavior // now which is situational at best. Right now there is no context passed in with the processing so we cannot // tell what artifact is being processed. if ( !manifestDiscovered ) { manifest = new Manifest( is ); manifestDiscovered = true; } }
private String getRelocatedClass( String className, List<Relocator> relocators ) { if ( className != null && className.length() > 0 && relocators != null ) { for ( Relocator relocator : relocators ) { if ( relocator.canRelocateClass( className ) ) { return relocator.relocateClass( className ); } } } return className; }
public void processResource( String resource, InputStream is, final List<Relocator> relocators ) throws IOException { ServiceStream out = serviceEntries.get( resource ); if ( out == null ) { out = new ServiceStream(); serviceEntries.put( resource, out ); } final ServiceStream fout = out; final String content = IOUtils.toString( is ); StringReader reader = new StringReader( content ); LineReader lineReader = new LineReader( reader ); String line; while ( ( line = lineReader.readLine() ) != null ) { String relContent = line; for ( Relocator relocator : relocators ) { if ( relocator.canRelocateClass( relContent ) ) { relContent = relocator.applyToSourceContent( relContent ); } } fout.append( relContent + "\n" ); } if ( this.relocators == null ) { this.relocators = relocators; } }
public void processResource( String resource, InputStream is, List<Relocator> relocators ) throws IOException { Document r; try { SAXBuilder builder = new SAXBuilder( false ); builder.setExpandEntities( false ); if ( ignoreDtd ) { builder.setEntityResolver( new EntityResolver() { public InputSource resolveEntity( String publicId, String systemId ) throws SAXException, IOException { return new InputSource( new StringReader( "" ) ); } } ); } r = builder.build( is ); } catch ( JDOMException e ) { throw new RuntimeException( "Error processing resource " + resource + ": " + e.getMessage(), e ); } if ( doc == null ) { doc = r; } else { Element root = r.getRootElement(); for ( @SuppressWarnings( "unchecked" ) Iterator<Attribute> itr = root.getAttributes().iterator(); itr.hasNext(); ) { Attribute a = itr.next(); itr.remove(); Element mergedEl = doc.getRootElement(); Attribute mergedAtt = mergedEl.getAttribute( a.getName(), a.getNamespace() ); if ( mergedAtt == null ) { mergedEl.setAttribute( a ); } } for ( @SuppressWarnings( "unchecked" ) Iterator<Content> itr = root.getChildren().iterator(); itr.hasNext(); ) { Content n = itr.next(); itr.remove(); doc.getRootElement().addContent( n ); } } }
private ShadeRequest createShadeSourcesRequest( Set<File> testArtifacts, File testJar, List<Filter> filters, List<Relocator> relocators, List<ResourceTransformer> resourceTransformers ) { ShadeRequest shadeSourcesRequest = shadeRequest( testArtifacts, testJar, filters, relocators, resourceTransformers, this.consistentDates, this.disableJarFileVerification ); shadeSourcesRequest.setShadeSourcesContent( shadeSourcesContent ); return shadeSourcesRequest; }
public Object mapValue( Object object ) { if ( object instanceof String ) { String name = (String) object; String value = name; String prefix = ""; String suffix = ""; Matcher m = classPattern.matcher( name ); if ( m.matches() ) { prefix = m.group( 1 ) + "L"; suffix = ";"; name = m.group( 2 ); } for ( Relocator r : relocators ) { if ( r.canRelocateClass( name ) ) { value = prefix + r.relocateClass( name ) + suffix; break; } else if ( r.canRelocatePath( name ) ) { value = prefix + r.relocatePath( name ) + suffix; break; } } return value; } return super.mapValue( object ); }
public void testSpecifyModuleName() throws Exception { GroovyResourceTransformer transformer = new GroovyResourceTransformer(); transformer.setExtModuleName( "the-module-name" ); transformer.setExtModuleVersion( "2.0" ); transformer.processResource( GroovyResourceTransformer.EXT_MODULE_NAME, module( "mod1", "1.0", "some.ext", "some.staticExt" ), Collections.<Relocator>emptyList() ); Properties desc = transform( transformer, false ); assertEquals( "the-module-name", desc.getProperty( "moduleName" ) ); assertEquals( "2.0", desc.getProperty( "moduleVersion" ) ); assertEquals( "some.ext", desc.getProperty( "extensionClasses" ) ); assertEquals( "some.staticExt", desc.getProperty( "staticExtensionClasses" ) ); }
private void processAndFailOnNullPointer( final String noticeText ) throws IOException { try { final ByteArrayInputStream noticeInputStream = new ByteArrayInputStream( noticeText.getBytes() ); final List<Relocator> emptyList = Collections.emptyList(); subject.processResource( NOTICE_RESOURCE, noticeInputStream, emptyList ); noticeInputStream.close(); } catch ( NullPointerException e ) { fail( "Null pointer should not be thrown when no parameters are set." ); } }
public void testShaderWithStaticInitializedClass() throws Exception { Shader s = newShader(); Set<File> set = new LinkedHashSet<File>(); set.add( new File( "src/test/jars/test-artifact-1.0-SNAPSHOT.jar" ) ); List<Relocator> relocators = new ArrayList<Relocator>(); relocators.add( new SimpleRelocator( "org.apache.maven.plugins.shade", null, null, null ) ); List<ResourceTransformer> resourceTransformers = new ArrayList<ResourceTransformer>(); List<Filter> filters = new ArrayList<Filter>(); File file = new File( "target/testShaderWithStaticInitializedClass.jar" ); ShadeRequest shadeRequest = new ShadeRequest(); shadeRequest.setJars( set ); shadeRequest.setUberJar( file ); shadeRequest.setFilters( filters ); shadeRequest.setRelocators( relocators ); shadeRequest.setResourceTransformers( resourceTransformers ); s.shade( shadeRequest ); URLClassLoader cl = new URLClassLoader( new URL[] { file.toURI().toURL() } ); Class<?> c = cl.loadClass( "hidden.org.apache.maven.plugins.shade.Lib" ); Object o = c.newInstance(); assertEquals( "foo.bar/baz", c.getDeclaredField( "CONSTANT" ).get( o ) ); }
public void testShaderWithExclusions() throws Exception { File jarFile = new File( getBasedir(), "target/unit/foo-bar.jar" ); Shader s = (Shader) lookup( Shader.ROLE, "default" ); Set<File> set = new LinkedHashSet<File>(); set.add( new File( getBasedir(), "src/test/jars/test-artifact-1.0-SNAPSHOT.jar" ) ); List<Relocator> relocators = new ArrayList<Relocator>(); relocators.add( new SimpleRelocator( "org.codehaus.plexus.util", "hidden", null, Arrays.asList( "org.codehaus.plexus.util.xml.Xpp3Dom", "org.codehaus.plexus.util.xml.pull.*") ) ); List<ResourceTransformer> resourceTransformers = new ArrayList<ResourceTransformer>(); List<Filter> filters = new ArrayList<Filter>(); ShadeRequest shadeRequest = new ShadeRequest(); shadeRequest.setJars( set ); shadeRequest.setUberJar( jarFile ); shadeRequest.setFilters( filters ); shadeRequest.setRelocators( relocators ); shadeRequest.setResourceTransformers( resourceTransformers ); s.shade( shadeRequest ); ClassLoader cl = new URLClassLoader( new URL[]{ jarFile.toURI().toURL() } ); Class<?> c = cl.loadClass( "org.apache.maven.plugins.shade.Lib" ); Field field = c.getDeclaredField( "CLASS_REALM_PACKAGE_IMPORT" ); assertEquals( "org.codehaus.plexus.util.xml.pull", field.get( null ) ); Method method = c.getDeclaredMethod( "getClassRealmPackageImport", new Class[0] ); assertEquals( "org.codehaus.plexus.util.xml.pull", method.invoke( null, new Object[0] ) ); }
@Override public void processResource(String resource, InputStream is, List<Relocator> relocators) throws IOException { Properties properties = new Properties(); properties.load(is); is.close(); for (Entry<Object, Object> entry : properties.entrySet()) { String name = (String) entry.getKey(); String value = (String) entry.getValue(); String existing = this.data.getProperty(name); this.data.setProperty(name, existing == null ? value : existing + "," + value); } }
public void processResource(String resource, InputStream is, List<Relocator> relocators) throws IOException { final File tempFile = File.createTempFile("Log4j2Plugins", "dat"); FileOutputStream fos = new FileOutputStream(tempFile); try { IOUtils.copyLarge(is, fos); } finally { IOUtils.closeQuietly(fos); } tempFiles.add(tempFile); if (relocators != null) { this.relocators.addAll(relocators); } }
void relocatePlugin(PluginCache aggregator, List<Relocator> relocators) { for (Map.Entry<String, Map<String, PluginEntry>> categoryEntry : aggregator.getAllCategories().entrySet()) { for (Map.Entry<String, PluginEntry> pluginMapEntry : categoryEntry.getValue().entrySet()) { PluginEntry pluginEntry = pluginMapEntry.getValue(); String originalClassName = pluginEntry.getClassName(); Relocator matchingRelocator = findFirstMatchingRelocator(originalClassName, relocators); if (matchingRelocator != null) { String newClassName = matchingRelocator.relocateClass(originalClassName); pluginEntry.setClassName(newClassName); } } } }
private Relocator findFirstMatchingRelocator(String originalClassName, List<Relocator> relocators) { for (Relocator relocator : relocators) { if (relocator.canRelocateClass(originalClassName)) { return relocator; } } return null; }
@Test public void test() throws Exception { PluginsCacheFileTransformer t = new PluginsCacheFileTransformer(); final InputStream is = getClass().getClassLoader().getResourceAsStream(PLUGIN_CACHE_FILE); t.processResource(PLUGIN_CACHE_FILE, is, null); assertFalse(t.hasTransformedResource()); List<Relocator> relocators = new ArrayList<Relocator>(); relocators.add(new SimpleRelocator(null, null, null, null)); t.processResource(PLUGIN_CACHE_FILE, is, relocators); assertTrue(t.hasTransformedResource()); }
private void testRelocation(String src, String pattern, String target) throws IOException { PluginsCacheFileTransformer t = new PluginsCacheFileTransformer(); Relocator log4jRelocator = new SimpleRelocator(src, pattern, null, null); PluginCache aggregator = new PluginCache(); aggregator.loadCacheFiles(enumeration(singletonList(pluginUrl))); t.relocatePlugin(aggregator, singletonList(log4jRelocator)); for (Map<String, PluginEntry> pluginEntryMap : aggregator.getAllCategories().values()) { for (PluginEntry entry : pluginEntryMap.values()) { assertTrue(entry.getClassName().startsWith(target)); } } }
@Override public void processResource(String resource, InputStream is, List<Relocator> relocators) throws IOException { Properties properties = new Properties(); properties.load(is); is.close(); for (Object key : properties.keySet()) { String name = (String) key; String value = properties.getProperty(name); String existing = this.data.getProperty(name); this.data.setProperty(name, existing == null ? value : existing + "," + value); } }
public void processResource( String resource, InputStream is, List<Relocator> relocators ) throws IOException { // We just want to take the first manifest we come across as that's our project's manifest. This is the behavior // now which is situational at best. Right now there is no context passed in with the processing so we cannot // tell what artifact is being processed. if ( !manifestDiscovered ) { manifest = new Manifest( is ); manifestDiscovered = true; IOUtil.close( is ); } }
public void processResource( String resource, InputStream is, List<Relocator> relocators ) throws IOException { IOUtil.copy( is, data ); data.write( '\n' ); is.close(); }
public void processResource(String resource, InputStream is, List<Relocator> relocators) throws IOException { Set<String> serviceLines = getServiceLines(resource); for (String line : readAllLines(is)) { if (!line.isEmpty()) { serviceLines.add(relocateIfPossible(relocators, line)); } } is.close(); }
private String relocateIfPossible(List<Relocator> relocators, String line) { for (Relocator relocator : relocators) { if (relocator.canRelocateClass(line)) { return relocator.relocateClass(line); } } return line; }
private boolean resourceTransformed(List<ResourceTransformer> resourceTransformers, String name, InputStream is, List<Relocator> relocators) throws IOException { for (ResourceTransformer transformer : resourceTransformers) { if (transformer.canTransformResource(name)) { getLogger().debug("Transforming " + name + " using " + transformer.getClass().getName()); transformer.processResource(name, is, relocators); return true; } } return false; }
@Override public Object mapValue(Object object) { if (object instanceof String) { String name = (String) object; String value = name; String prefix = ""; String suffix = ""; Matcher m = classPattern.matcher(name); if (m.matches()) { prefix = m.group(1) + "L"; suffix = ";"; name = m.group(2); } for (Relocator r : relocators) { if (r.canRelocateClass(name)) { value = prefix + r.relocateClass(name) + suffix; break; } else if (r.canRelocatePath(name)) { value = prefix + r.relocatePath(name) + suffix; break; } } return value; } return super.mapValue(object); }
private void processAndFailOnNullPointer( final String noticeText ) throws IOException { try { final ByteArrayInputStream noticeInputStream = new ByteArrayInputStream( noticeText.getBytes() ); final List<Relocator> emptyList = Collections.emptyList(); subject.processResource( NOTICE_RESOURCE, noticeInputStream, emptyList ); } catch ( NullPointerException e ) { fail( "Null pointer should not be thrown when no parameters are set." ); } }