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();
}
}
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));
}
}
}
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);
}
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();
}
}
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);
}
Aggregations