Search in sources :

Example 6 with Relocator

use of org.apache.maven.plugins.shade.relocation.Relocator in project maven-plugins by apache.

the class ServiceResourceTransformerTest method concatenation.

@Test
public void concatenation() throws Exception {
    SimpleRelocator relocator = new SimpleRelocator("org.foo", "borg.foo", null, null);
    List<Relocator> relocators = Lists.<Relocator>newArrayList(relocator);
    String content = "org.foo.Service\n";
    byte[] contentBytes = content.getBytes("UTF-8");
    InputStream contentStream = new ByteArrayInputStream(contentBytes);
    String contentResource = "META-INF/services/org.something.another";
    ServicesResourceTransformer xformer = new ServicesResourceTransformer();
    xformer.processResource(contentResource, contentStream, relocators);
    contentStream.close();
    content = "org.blah.Service\n";
    contentBytes = content.getBytes("UTF-8");
    contentStream = new ByteArrayInputStream(contentBytes);
    contentResource = "META-INF/services/org.something.another";
    xformer.processResource(contentResource, contentStream, relocators);
    contentStream.close();
    File tempJar = File.createTempFile("shade.", ".jar");
    tempJar.deleteOnExit();
    FileOutputStream fos = new FileOutputStream(tempJar);
    JarOutputStream jos = new JarOutputStream(fos);
    try {
        xformer.modifyOutputStream(jos);
        jos.close();
        jos = null;
        JarFile jarFile = new JarFile(tempJar);
        JarEntry jarEntry = jarFile.getJarEntry(contentResource);
        assertNotNull(jarEntry);
        InputStream entryStream = jarFile.getInputStream(jarEntry);
        try {
            String xformedContent = IOUtils.toString(entryStream, "utf-8");
            // must be two lines, with our two classes.
            String[] classes = xformedContent.split("\r?\n");
            boolean h1 = false;
            boolean h2 = false;
            for (String name : classes) {
                if ("org.blah.Service".equals(name)) {
                    h1 = true;
                } else if ("borg.foo.Service".equals(name)) {
                    h2 = true;
                }
            }
            assertTrue(h1 && h2);
        } finally {
            IOUtils.closeQuietly(entryStream);
            jarFile.close();
        }
    } finally {
        if (jos != null) {
            IOUtils.closeQuietly(jos);
        }
        tempJar.delete();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JarOutputStream(java.util.jar.JarOutputStream) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) Relocator(org.apache.maven.plugins.shade.relocation.Relocator) SimpleRelocator(org.apache.maven.plugins.shade.relocation.SimpleRelocator) SimpleRelocator(org.apache.maven.plugins.shade.relocation.SimpleRelocator) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) JarFile(java.util.jar.JarFile) File(java.io.File) Test(org.junit.Test)

Example 7 with Relocator

use of org.apache.maven.plugins.shade.relocation.Relocator in project webservices-axiom by apache.

the class AxiomXmlResourceTransformer method processResource.

public void processResource(String resource, InputStream is, List<Relocator> relocators) throws IOException {
    Document axiomXml = DOMUtils.parse(is);
    is.close();
    NodeList implementations = axiomXml.getElementsByTagNameNS("http://ws.apache.org/axiom/", "implementation");
    for (int i = 0; i < implementations.getLength(); i++) {
        Element implementation = (Element) implementations.item(i);
        String loader = implementation.getAttributeNS(null, "loader");
        for (Relocator relocator : relocators) {
            if (relocator.canRelocateClass(loader)) {
                implementation.setAttributeNS(null, "loader", relocator.relocateClass(loader));
                break;
            }
        }
    }
    if (mergedAxiomXml == null) {
        mergedAxiomXml = axiomXml;
    } else {
        for (Node node = axiomXml.getDocumentElement().getFirstChild(); node != null; node = node.getNextSibling()) {
            mergedAxiomXml.getDocumentElement().appendChild(mergedAxiomXml.importNode(node, true));
        }
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) Relocator(org.apache.maven.plugins.shade.relocation.Relocator)

Example 8 with Relocator

use of org.apache.maven.plugins.shade.relocation.Relocator in project storm by apache.

the class ClojureTransformer method processResource.

@Override
public void processResource(String s, InputStream inputStream, List<Relocator> relocators) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int b;
    while ((b = inputStream.read()) != -1) {
        out.write(b);
    }
    String data = out.toString();
    for (Relocator rel : relocators) {
        data = rel.applyToSourceContent(data);
    }
    this.entries.put(s, data);
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) Relocator(org.apache.maven.plugins.shade.relocation.Relocator)

Example 9 with Relocator

use of org.apache.maven.plugins.shade.relocation.Relocator in project hadoop by apache.

the class ServicesResourceTransformer method modifyOutputStream.

public void modifyOutputStream(JarOutputStream jos) throws IOException {
    for (Map.Entry<String, ServiceStream> entry : serviceEntries.entrySet()) {
        String key = entry.getKey();
        ServiceStream 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;
        }
        jos.putNextEntry(new JarEntry(key));
        //read the content of service file for candidate classes for relocation
        //presume everything is UTF8, because Findbugs barfs on default
        //charset and this seems no worse a choice ¯\_(ツ)_/¯
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(jos, StandardCharsets.UTF_8));
        InputStreamReader streamReader = new InputStreamReader(data.toInputStream(), StandardCharsets.UTF_8);
        BufferedReader reader = new BufferedReader(streamReader);
        String className;
        while ((className = reader.readLine()) != null) {
            if (relocators != null) {
                for (Relocator relocator : relocators) {
                    //if the class can be relocated then relocate it
                    if (relocator.canRelocateClass(className)) {
                        className = relocator.applyToSourceContent(className);
                        break;
                    }
                }
            }
            writer.println(className);
            writer.flush();
        }
        reader.close();
        data.reset();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) JarEntry(java.util.jar.JarEntry) HashMap(java.util.HashMap) Map(java.util.Map) Relocator(org.apache.maven.plugins.shade.relocation.Relocator) PrintWriter(java.io.PrintWriter)

Example 10 with Relocator

use of org.apache.maven.plugins.shade.relocation.Relocator in project maven-plugins by apache.

the class DefaultShader method addJavaSource.

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);
    }
    final Writer writer = new OutputStreamWriter(jos, "UTF-8");
    IOUtil.copy(sourceContent, writer);
    writer.flush();
    resources.add(name);
}
Also used : InputStreamReader(java.io.InputStreamReader) OutputStreamWriter(java.io.OutputStreamWriter) JarEntry(java.util.jar.JarEntry) Relocator(org.apache.maven.plugins.shade.relocation.Relocator) ClassWriter(org.objectweb.asm.ClassWriter) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer)

Aggregations

Relocator (org.apache.maven.plugins.shade.relocation.Relocator)15 File (java.io.File)8 SimpleRelocator (org.apache.maven.plugins.shade.relocation.SimpleRelocator)8 LinkedHashSet (java.util.LinkedHashSet)6 Filter (org.apache.maven.plugins.shade.filter.Filter)6 ResourceTransformer (org.apache.maven.plugins.shade.resource.ResourceTransformer)6 ArrayList (java.util.ArrayList)5 JarEntry (java.util.jar.JarEntry)5 ComponentsXmlResourceTransformer (org.apache.maven.plugins.shade.resource.ComponentsXmlResourceTransformer)5 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStreamReader (java.io.InputStreamReader)3 URLClassLoader (java.net.URLClassLoader)3 ShadeRequest (org.apache.maven.plugins.shade.ShadeRequest)3 BufferedReader (java.io.BufferedReader)2 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 PrintWriter (java.io.PrintWriter)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2