forked from bndtools/bnd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiffPluginImpl.java
291 lines (246 loc) · 8.48 KB
/
DiffPluginImpl.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package aQute.bnd.differ;
import static aQute.bnd.osgi.Jar.METAINF_SIGNING_P;
import static aQute.bnd.service.diff.Delta.CHANGED;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.jar.Attributes.Name;
import java.util.jar.Manifest;
import aQute.bnd.header.Attrs;
import aQute.bnd.header.OSGiHeader;
import aQute.bnd.header.Parameters;
import aQute.bnd.osgi.About;
import aQute.bnd.osgi.Analyzer;
import aQute.bnd.osgi.Clazz;
import aQute.bnd.osgi.Constants;
import aQute.bnd.osgi.Descriptors.PackageRef;
import aQute.bnd.osgi.Descriptors.TypeRef;
import aQute.bnd.osgi.Instructions;
import aQute.bnd.osgi.Jar;
import aQute.bnd.osgi.Resource;
import aQute.bnd.service.diff.Differ;
import aQute.bnd.service.diff.Tree;
import aQute.bnd.service.diff.Tree.Data;
import aQute.bnd.service.diff.Type;
import aQute.bnd.unmodifiable.Sets;
import aQute.bnd.version.Version;
import aQute.lib.hex.Hex;
import aQute.lib.io.IO;
import aQute.lib.strings.Strings;
import aQute.libg.cryptography.Digester;
import aQute.libg.cryptography.SHA1;
/**
* This Diff Plugin Implementation will compare JARs for their API (based on the
* Bundle Class Path and exported packages), the Manifest, and the resources.
* The differences are represented in a {@link aQute.bnd.service.diff.Diff Diff}
* tree.
*/
public class DiffPluginImpl implements Differ {
/**
* Headers that are considered major enough to parse according to spec and
* compare their constituents
*/
final static Set<String> MAJOR_HEADERS = Sets.of( //
Constants.EXPORT_PACKAGE, //
Constants.IMPORT_PACKAGE, //
Constants.REQUIRE_BUNDLE, //
Constants.FRAGMENT_HOST, //
Constants.BUNDLE_SYMBOLICNAME, //
Constants.BUNDLE_LICENSE, //
Constants.BUNDLE_NATIVECODE, //
Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT, //
Constants.DYNAMICIMPORT_PACKAGE, //
Constants.BUNDLE_VERSION);
/**
* Headers that are considered not major enough to be considered
*/
final static Set<String> IGNORED_HEADERS = Sets.of( //
Constants.TOOL, //
Constants.BND_LASTMODIFIED, //
Constants.CREATED_BY);
/**
* Headers that have values that should be sorted
*/
final static Set<String> ORDERED_HEADERS = Sets.of( //
Constants.SERVICE_COMPONENT, //
Constants.TESTCASES);
Instructions localIgnore = null;
/**
* @see aQute.bnd.service.diff.Differ#tree(aQute.bnd.osgi.Jar)
*/
public Tree tree(File newer) throws Exception {
try (Jar jnewer = new Jar(newer)) {
return tree(jnewer);
}
}
/**
* @see aQute.bnd.service.diff.Differ#tree(aQute.bnd.osgi.Jar)
*/
@Override
public Tree tree(Jar newer) throws Exception {
try (Analyzer anewer = new Analyzer(newer)) {
return tree(anewer);
}
}
@Override
public Tree tree(Analyzer newer) throws Exception {
return bundleElement(newer);
}
/**
* Create an element representing a bundle from the Jar.
*/
private Element bundleElement(Analyzer analyzer) throws Exception {
List<Element> result = new ArrayList<>();
Manifest manifest = analyzer.getJar()
.getManifest();
if (manifest != null) {
result.add(JavaElement.getAPI(analyzer));
result.add(manifestElement(manifest));
}
result.add(resourcesElement(analyzer));
return new Element(Type.BUNDLE, analyzer.getJar()
.getName(), result, CHANGED, CHANGED, null);
}
/**
* Create an element representing all resources in the JAR
*/
private Element resourcesElement(Analyzer analyzer) throws Exception {
Jar jar = analyzer.getJar();
List<Element> resources = new ArrayList<>();
for (Map.Entry<String, Resource> entry : jar.getResources()
.entrySet()) {
String path = entry.getKey();
//
// The manifest and other (signer) files are ignored
// since they are extremely sensitive to time
//
if (jar.getManifestName()
.equals(path)
|| METAINF_SIGNING_P.matcher(path)
.matches()) {
continue;
}
if (localIgnore != null && localIgnore.matches(path))
continue;
//
// #794 Use sources for shas of classes in baselining
// Since the compilers generate different bytecodes the
// resource comparison by sha is very awkward for classes.
// This code will not create an element for classes if a
// directory with source code can be found.
//
if (path.endsWith(Constants.EMPTY_HEADER))
continue;
if (analyzer.since(About._3_0)) {
//
// Skip resources that have a source component in the same
// Jar. Changes will be reported on that component
//
if (hasSource(analyzer, path))
continue;
}
Resource resource = entry.getValue();
try (InputStream in = resource.openInputStream(); Digester<SHA1> digester = SHA1.getDigester()) {
IO.copy(in, digester);
String value = Hex.toHexString(digester.digest()
.digest());
resources.add(new Element(Type.RESOURCE, entry.getKey(), Arrays.asList(new Element(Type.SHA, value)),
CHANGED, CHANGED, null));
}
}
return new Element(Type.RESOURCES, "<resources>", resources, CHANGED, CHANGED, null);
}
private boolean hasSource(Analyzer analyzer, String path) throws Exception {
if (!path.endsWith(".class"))
return false;
TypeRef type = analyzer.getTypeRefFromPath(path);
PackageRef packageRef = type.getPackageRef();
Clazz clazz = analyzer.findClass(type);
if (clazz == null)
return false;
String sourceFile = clazz.getSourceFile();
if (sourceFile == null)
return false;
String source = "OSGI-OPT/src/" + packageRef.getBinary() + "/" + sourceFile;
Resource sourceResource = analyzer.getJar()
.getResource(source);
if (sourceResource == null)
return false;
return true;
}
/**
* Create an element for each manifest header. There are
* {@link #IGNORED_HEADERS} and {@link #MAJOR_HEADERS} that will be treated
* differently.
*
* @param manifest
* @return the created {@code Element}
*/
private Element manifestElement(Manifest manifest) {
List<Element> result = new ArrayList<>();
for (Object key : manifest.getMainAttributes()
.keySet()) {
String header = key.toString();
String value = manifest.getMainAttributes()
.getValue((Name) key);
if (IGNORED_HEADERS.contains(header))
continue;
if (localIgnore != null && localIgnore.matches(header)) {
continue;
}
if (MAJOR_HEADERS.contains(header)) {
if (header.equalsIgnoreCase(Constants.BUNDLE_VERSION)) {
String v = new Version(value).toStringWithoutQualifier();
result.add(new Element(Type.HEADER, header + ":" + v, null, CHANGED, CHANGED, null));
} else {
Parameters clauses = OSGiHeader.parseHeader(value);
Collection<Element> clausesDef = new ArrayList<>();
for (Map.Entry<String, Attrs> clause : clauses.entrySet()) {
Collection<Element> parameterDef = new ArrayList<>();
for (Map.Entry<String, String> parameter : clause.getValue()
.entrySet()) {
String paramValue = parameter.getValue();
if (Constants.EXPORT_PACKAGE.equals(header)
&& Constants.USES_DIRECTIVE.equals(parameter.getKey())) {
paramValue = Strings.splitAsStream(paramValue)
.sorted()
.collect(Strings.joining());
}
parameterDef.add(new Element(Type.PARAMETER, parameter.getKey() + ":" + paramValue, null,
CHANGED, CHANGED, null));
}
clausesDef.add(new Element(Type.CLAUSE, clause.getKey(), parameterDef, CHANGED, CHANGED, null));
}
result.add(new Element(Type.HEADER, header, clausesDef, CHANGED, CHANGED, null));
}
} else if (ORDERED_HEADERS.contains(header)) {
String sorted = Strings.splitAsStream(value)
.sorted()
.collect(Strings.joining());
result.add(new Element(Type.HEADER, header + ":" + sorted, null, CHANGED, CHANGED, null));
} else {
result.add(new Element(Type.HEADER, header + ":" + value, null, CHANGED, CHANGED, null));
}
}
return new Element(Type.MANIFEST, "<manifest>", result, CHANGED, CHANGED, null);
}
@Override
public Tree deserialize(Data data) throws Exception {
return new Element(data);
}
public void setIgnore(String diffignore) {
setIgnore(new Parameters(diffignore));
}
public void setIgnore(Parameters diffignore) {
if ((diffignore == null) || diffignore.isEmpty()) {
localIgnore = null;
return;
}
localIgnore = new Instructions(diffignore);
}
}