
aQute.bnd.osgi.metainf.MetaInfServiceMerger Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of biz.aQute.bndlib Show documentation
Show all versions of biz.aQute.bndlib Show documentation
bndlib: A Swiss Army Knife for OSGi
The newest version!
package aQute.bnd.osgi.metainf;
import java.io.ByteArrayInputStream;
import java.io.SequenceInputStream;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Collections;
import java.util.Optional;
import aQute.bnd.exceptions.Exceptions;
import aQute.bnd.osgi.EmbeddedResource;
import aQute.bnd.osgi.Resource;
import aQute.bnd.service.merge.MergeResources;
import aQute.bnd.service.tags.Tagged;
import aQute.bnd.service.tags.Tags;
/**
* Knows how to "merge" duplicate files in META-INF/services, by concatenating
* them with a linebreak in between.
*/
public class MetaInfServiceMerger implements MergeResources, Tagged {
private final static Tags META_INF_SERVICES = Tags.of("metainfservices");
@Override
public Optional tryMerge(String path, Resource a, Resource b) {
if (!path.startsWith("META-INF/services/")) {
return Optional.empty();
}
// do something with a and b
try (SequenceInputStream in = new SequenceInputStream(Collections.enumeration(
Arrays.asList(a.openInputStream(), new ByteArrayInputStream("\n".getBytes()), b.openInputStream())));) {
long lastModified = Math.max(a.lastModified(), b.lastModified());
Resource r = new EmbeddedResource(ByteBuffer.wrap(in.readAllBytes()), lastModified);
return Optional.of(r);
} catch (Exception e) {
throw Exceptions.duck(e);
}
}
@Override
public Tags getTags() {
return META_INF_SERVICES;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy