forked from arquillian/arquillian-extension-liferay
/
ManifestManagerImpl.java
94 lines (67 loc) · 2.56 KB
/
ManifestManagerImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package org.arquillian.container.osgi.remote.processor.service;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.Node;
import org.jboss.shrinkwrap.api.asset.Asset;
import org.jboss.shrinkwrap.api.asset.ByteArrayAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
/**
* @author Cristina González
*/
public class ManifestManagerImpl implements ManifestManager {
public Manifest getManifest(JavaArchive javaArchive) throws IOException {
Node manifestNode = javaArchive.get(JarFile.MANIFEST_NAME);
Asset manifestAsset = manifestNode.getAsset();
return new Manifest(manifestAsset.openStream());
}
public Manifest putAttributeValue(
Manifest manifest, String attributeName, String... attributeValue)
throws IOException {
Attributes mainAttributes = manifest.getMainAttributes();
String attributeValues = mainAttributes.getValue(attributeName);
Set<String> attributeValueSet = new HashSet<>();
if (attributeValues != null) {
attributeValueSet.addAll(Arrays.asList(attributeValues.split(",")));
}
attributeValueSet.addAll(Arrays.asList(attributeValue));
StringBuilder sb = new StringBuilder();
for (String value : attributeValueSet) {
sb.append(value);
sb.append(",");
}
if (!attributeValueSet.isEmpty()) {
sb.setLength(sb.length() - 1);
}
attributeValues = sb.toString();
mainAttributes.putValue(attributeName, attributeValues);
return manifest;
}
public void replaceManifest(Archive archive, Manifest manifest )
throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
manifest.write(baos);
ByteArrayAsset byteArrayAsset = new ByteArrayAsset(baos.toByteArray());
archive.delete(JarFile.MANIFEST_NAME);
archive.add(byteArrayAsset, JarFile.MANIFEST_NAME);
}
}