use of java.util.jar.Attributes in project felix by apache.
the class DeploymentInstaller method analyseFile.
private ResolveRequest analyseFile(File file) throws IOException {
log(LogService.LOG_INFO, null, "Resolving bundle archive: %s", file.getAbsolutePath());
String fileUriStr = file.toURI().toString();
String indexUriStr;
String name;
String symbolicName;
String version = "";
List<Requirement> requirements = new LinkedList<>();
try (JarFile jar = new JarFile(file)) {
Attributes manifestAttribs = jar.getManifest().getMainAttributes();
symbolicName = manifestAttribs.getValue(Constants.DEPLOYMENT_SYMBOLIC_NAME);
if (symbolicName == null) {
symbolicName = file.getName();
}
name = manifestAttribs.getValue(Constants.DEPLOYMENT_NAME);
if (name == null) {
name = symbolicName;
}
version = manifestAttribs.getValue(Constants.DEPLOYMENT_VERSION);
if (version == null) {
version = UNKNOWN_DEPLOYMENT_VERSION;
}
requirements.addAll(RequirementParser.parseRequireBundle(manifestAttribs.getValue(org.osgi.framework.Constants.REQUIRE_BUNDLE)));
requirements.addAll(RequirementParser.parseRequireCapability(manifestAttribs.getValue(org.osgi.framework.Constants.REQUIRE_CAPABILITY)));
if (requirements.isEmpty()) {
throw new IllegalArgumentException(String.format("Missing %s or %s header in manifest in %s", org.osgi.framework.Constants.REQUIRE_BUNDLE, org.osgi.framework.Constants.REQUIRE_CAPABILITY, file.getAbsolutePath()));
}
JarEntry indexEntry = findEntry(jar, Constants.INDEX_FILE, Constants.DEFAULT_INDEX_FILE);
if (indexEntry == null) {
throw new IllegalArgumentException("Missing index entry in " + file.getAbsolutePath());
}
indexUriStr = "jar:" + fileUriStr + "!/" + indexEntry.getName();
}
try {
ResolveRequest request = new ResolveRequest(name, symbolicName, version, Collections.singletonList(new URI(indexUriStr)), requirements);
return request;
} catch (URISyntaxException e) {
throw new IOException("Unable to convert index URI " + indexUriStr, e);
}
}
use of java.util.jar.Attributes in project felix by apache.
the class DPSigner method createSignatureFile.
private Manifest createSignatureFile(Manifest manifest) throws IOException {
byte[] mfRawBytes;
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
manifest.write(baos);
mfRawBytes = baos.toByteArray();
}
Manifest sf = new Manifest();
Attributes sfMain = sf.getMainAttributes();
Map<String, Attributes> sfEntries = sf.getEntries();
sfMain.put(Attributes.Name.SIGNATURE_VERSION, "1.0");
sfMain.putValue("Created-By", "Apache Felix DeploymentPackageBuilder");
sfMain.putValue(m_digestAlg + "-Digest-Manifest", calculateDigest(mfRawBytes));
sfMain.putValue(m_digestAlg + "-Digest-Manifest-Main-Attribute", calculateDigest(getRawBytesMainAttributes(manifest)));
for (Entry<String, Attributes> entry : manifest.getEntries().entrySet()) {
String name = entry.getKey();
byte[] entryData = getRawBytesAttributes(entry.getValue());
sfEntries.put(name, getDigestAttributes(entryData));
}
return sf;
}
use of java.util.jar.Attributes in project felix by apache.
the class DeploymentPackageBuilder method createManifest.
private Manifest createManifest(List<ArtifactData> files) throws Exception {
Manifest manifest = new Manifest();
Attributes main = manifest.getMainAttributes();
main.putValue("Manifest-Version", "1.0");
main.putValue("DeploymentPackage-SymbolicName", m_symbolicName);
main.putValue("DeploymentPackage-Version", m_version);
if ((m_fixPackageVersion != null) && !"".equals(m_fixPackageVersion)) {
main.putValue("DeploymentPackage-FixPack", m_fixPackageVersion);
}
Map<String, Attributes> entries = manifest.getEntries();
for (ArtifactData file : files) {
Attributes attrs = new Attributes();
attrs.putValue("Name", file.getFilename());
if (file.isBundle()) {
attrs.putValue("Bundle-SymbolicName", file.getSymbolicName());
attrs.putValue("Bundle-Version", file.getVersion());
if (file.isCustomizer()) {
attrs.putValue("DeploymentPackage-Customizer", "true");
attrs.putValue("Deployment-ProvidesResourceProcessor", file.getProcessorPid());
}
} else if (file.isResourceProcessorNeeded()) {
attrs.putValue("Resource-Processor", file.getProcessorPid());
}
if (file.isMissing()) {
attrs.putValue("DeploymentPackage-Missing", "true");
}
if (isAddSignatures()) {
m_signer.addDigestAttribute(attrs, file);
}
entries.put(file.getFilename(), attrs);
}
return manifest;
}
use of java.util.jar.Attributes in project felix by apache.
the class Utils method merge.
public static void merge(File targetIndex, File target, File sourceIndex, File source) throws IOException {
List targetFiles = readIndex(targetIndex);
List sourceFiles = readIndex(sourceIndex);
List result = new ArrayList(targetFiles);
File manifestFile = new File(source, (String) sourceFiles.remove(0));
Manifest resultManifest = Utils.readManifest(manifestFile);
resultManifest.getMainAttributes().remove(new Name(Constants.DEPLOYMENTPACKAGE_FIXPACK));
for (Iterator i = result.iterator(); i.hasNext(); ) {
String targetFile = (String) i.next();
if (!MANIFEST_NAME.equals(targetFile) && !resultManifest.getEntries().containsKey(targetFile)) {
i.remove();
}
}
for (Iterator iter = sourceFiles.iterator(); iter.hasNext(); ) {
String path = (String) iter.next();
File from = new File(source, path);
File to = new File(target, path);
if (targetFiles.contains(path)) {
if (!to.delete()) {
throw new IOException("Could not delete " + to);
}
} else {
result.add(path);
}
if (!rename(from, to)) {
throw new IOException("Could not rename " + from + " to " + to);
}
}
targetFiles.removeAll(sourceFiles);
for (Iterator iter = resultManifest.getEntries().keySet().iterator(); iter.hasNext(); ) {
String path = (String) iter.next();
Attributes sourceAttribute = (Attributes) resultManifest.getEntries().get(path);
if ("true".equals(sourceAttribute.remove(new Name(Constants.DEPLOYMENTPACKAGE_MISSING)))) {
targetFiles.remove(path);
}
}
for (Iterator iter = targetFiles.iterator(); iter.hasNext(); ) {
String path = (String) iter.next();
File targetFile = new File(target, path);
if (!targetFile.delete()) {
throw new IOException("Could not delete " + targetFile);
}
}
GZIPOutputStream outputStream = new GZIPOutputStream(new FileOutputStream(new File(target, MANIFEST_NAME)));
try {
resultManifest.write(outputStream);
} finally {
outputStream.close();
}
writeIndex(targetIndex, result);
}
use of java.util.jar.Attributes in project felix by apache.
the class DeploymentPackageBuilderTest method testResourceFilterOk.
/**
* Tests that we can filter a resource.
*/
@Test
public void testResourceFilterOk() throws Exception {
DeploymentPackageBuilder dpBuilder = DeploymentPackageBuilder.create("dp-test", "1.0.0");
dpBuilder.add(dpBuilder.createBundleResource().setUrl(getTestBundle("bundle2"))).add(dpBuilder.createBundleResource().setVersion("1.1.0").setFilter(new JarManifestManipulatingFilter("Bundle-Version", "1.1.0", "Foo", "bar")).setUrl(getTestBundle("bundle1")));
JarInputStream jis = new JarInputStream(dpBuilder.generate());
assertNotNull(jis);
Manifest manifest = jis.getManifest();
assertManifestHeader(manifest, "DeploymentPackage-SymbolicName", "dp-test");
assertManifestHeader(manifest, "DeploymentPackage-Version", "1.0.0");
String filename = getBundleName("bundle1");
assertManifestEntry(manifest, filename, "Name", filename);
assertManifestEntry(manifest, filename, "Bundle-SymbolicName", "testbundles.bundle1");
assertManifestEntry(manifest, filename, "Bundle-Version", "1.1.0");
filename = getBundleName("bundle2");
assertManifestEntry(manifest, filename, "Name", filename);
assertManifestEntry(manifest, filename, "Bundle-SymbolicName", "testbundles.bundle2");
assertManifestEntry(manifest, filename, "Bundle-Version", "1.0.0");
try {
byte[] buf = new byte[32 * 1024];
JarEntry entry;
while ((entry = jis.getNextJarEntry()) != null) {
if (entry.getName().endsWith("valid-bundle1.jar")) {
int read = jis.read(buf);
JarInputStream jis2 = new JarInputStream(new ByteArrayInputStream(Arrays.copyOf(buf, read)));
Manifest manifest2 = jis2.getManifest();
Attributes mainAttributes = manifest2.getMainAttributes();
assertEquals("1.1.0", mainAttributes.getValue("Bundle-Version"));
assertEquals("bar", mainAttributes.getValue("Foo"));
jis2.close();
}
jis.closeEntry();
}
} finally {
jis.close();
}
}
Aggregations