blob: 38de87261b15a18b495a29cbfe8115f7c708f5e1 [file] [log] [blame]
Stuart McCullochf3173222012-06-07 21:57:32 +00001package aQute.bnd.differ;
2
3import static aQute.bnd.service.diff.Delta.*;
4
5import java.io.*;
6import java.util.*;
7import java.util.jar.*;
8
Stuart McCulloch42151ee2012-07-16 13:43:38 +00009import aQute.bnd.header.*;
10import aQute.bnd.osgi.*;
Stuart McCullochf3173222012-06-07 21:57:32 +000011import aQute.bnd.service.diff.*;
12import aQute.bnd.service.diff.Tree.Data;
Stuart McCulloch2a0afd62012-09-06 18:28:06 +000013import aQute.lib.collections.*;
Stuart McCullochf3173222012-06-07 21:57:32 +000014import aQute.lib.hex.*;
15import aQute.lib.io.*;
Stuart McCullochf3173222012-06-07 21:57:32 +000016import aQute.libg.cryptography.*;
Stuart McCullochf3173222012-06-07 21:57:32 +000017
Stuart McCullochf3173222012-06-07 21:57:32 +000018/**
19 * This Diff Plugin Implementation will compare JARs for their API (based on the
20 * Bundle Class Path and exported packages), the Manifest, and the resources.
21 * The Differences are represented in a {@link Diff} tree.
22 */
23public class DiffPluginImpl implements Differ {
Stuart McCulloch4482c702012-06-15 13:27:53 +000024
Stuart McCullochf3173222012-06-07 21:57:32 +000025 /**
26 * Headers that are considered major enough to parse according to spec and
27 * compare their constituents
28 */
Stuart McCulloch4482c702012-06-15 13:27:53 +000029 final static Set<String> MAJOR_HEADERS = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
Stuart McCullochf3173222012-06-07 21:57:32 +000030
31 /**
32 * Headers that are considered not major enough to be considered
33 */
Stuart McCulloch4482c702012-06-15 13:27:53 +000034 final static Set<String> IGNORE_HEADERS = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
Stuart McCullochf3173222012-06-07 21:57:32 +000035
36 static {
37 MAJOR_HEADERS.add(Constants.EXPORT_PACKAGE);
38 MAJOR_HEADERS.add(Constants.IMPORT_PACKAGE);
39 MAJOR_HEADERS.add(Constants.REQUIRE_BUNDLE);
40 MAJOR_HEADERS.add(Constants.FRAGMENT_HOST);
41 MAJOR_HEADERS.add(Constants.BUNDLE_SYMBOLICNAME);
42 MAJOR_HEADERS.add(Constants.BUNDLE_LICENSE);
43 MAJOR_HEADERS.add(Constants.BUNDLE_NATIVECODE);
44 MAJOR_HEADERS.add(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT);
45 MAJOR_HEADERS.add(Constants.DYNAMICIMPORT_PACKAGE);
46
47 IGNORE_HEADERS.add(Constants.TOOL);
48 IGNORE_HEADERS.add(Constants.BND_LASTMODIFIED);
49 IGNORE_HEADERS.add(Constants.CREATED_BY);
50 }
51
52 /**
Stuart McCullochf3173222012-06-07 21:57:32 +000053 * @see aQute.bnd.service.diff.Differ#diff(aQute.lib.resource.Jar,
54 * aQute.lib.resource.Jar)
55 */
56 public Tree tree(File newer) throws Exception {
57 Jar jnewer = new Jar(newer);
58 try {
59 return tree(jnewer);
Stuart McCulloch4482c702012-06-15 13:27:53 +000060 }
61 finally {
Stuart McCullochf3173222012-06-07 21:57:32 +000062 jnewer.close();
63 }
64 }
65
66 /**
Stuart McCullochf3173222012-06-07 21:57:32 +000067 * @see aQute.bnd.service.diff.Differ#diff(aQute.lib.resource.Jar,
68 * aQute.lib.resource.Jar)
69 */
70 public Tree tree(Jar newer) throws Exception {
71 Analyzer anewer = new Analyzer();
72 try {
Stuart McCulloch4482c702012-06-15 13:27:53 +000073 anewer.setJar(newer);
74 return tree(anewer);
75 }
76 finally {
Stuart McCullochf3173222012-06-07 21:57:32 +000077 anewer.setJar((Jar) null);
78 anewer.close();
79 }
80 }
81
82 public Tree tree(Analyzer newer) throws Exception {
83 return bundleElement(newer);
84 }
85
86 /**
87 * Create an element representing a bundle from the Jar.
88 *
Stuart McCulloch4482c702012-06-15 13:27:53 +000089 * @param infos
Stuart McCullochf3173222012-06-07 21:57:32 +000090 * @param jar
91 * The Jar to be analyzed
92 * @return the elements that should be compared
93 * @throws Exception
94 */
95 private Element bundleElement(Analyzer analyzer) throws Exception {
96 List<Element> result = new ArrayList<Element>();
97
98 Manifest manifest = analyzer.getJar().getManifest();
99
100 if (manifest != null) {
101 result.add(JavaElement.getAPI(analyzer));
102 result.add(manifestElement(manifest));
103 }
104 result.add(resourcesElement(analyzer.getJar()));
Stuart McCulloch4482c702012-06-15 13:27:53 +0000105 return new Element(Type.BUNDLE, analyzer.getJar().getName(), result, CHANGED, CHANGED, null);
Stuart McCullochf3173222012-06-07 21:57:32 +0000106 }
107
108 /**
109 * Create an element representing all resources in the JAR
110 *
111 * @param jar
112 * @return
113 * @throws Exception
114 */
115 private Element resourcesElement(Jar jar) throws Exception {
116 List<Element> resources = new ArrayList<Element>();
Stuart McCulloch4482c702012-06-15 13:27:53 +0000117 for (Map.Entry<String,Resource> entry : jar.getResources().entrySet()) {
Stuart McCullochf3173222012-06-07 21:57:32 +0000118
119 InputStream in = entry.getValue().openInputStream();
120 try {
121 Digester<SHA1> digester = SHA1.getDigester();
122 IO.copy(in, digester);
123 String value = Hex.toHexString(digester.digest().digest());
Stuart McCulloch42151ee2012-07-16 13:43:38 +0000124 resources.add(new Element(Type.RESOURCE, entry.getKey(), Arrays.asList(new Element(Type.SHA,value)), CHANGED, CHANGED, null));
Stuart McCulloch4482c702012-06-15 13:27:53 +0000125 }
126 finally {
Stuart McCullochf3173222012-06-07 21:57:32 +0000127 in.close();
128 }
129 }
130 return new Element(Type.RESOURCES, "<resources>", resources, CHANGED, CHANGED, null);
131 }
132
Stuart McCullochf3173222012-06-07 21:57:32 +0000133 /**
134 * Create an element for each manifest header. There are
135 * {@link #IGNORE_HEADERS} and {@link #MAJOR_HEADERS} that will be treated
136 * differently.
137 *
138 * @param manifest
139 * @return
140 */
141
142 private Element manifestElement(Manifest manifest) {
143 List<Element> result = new ArrayList<Element>();
144
145 for (Object key : manifest.getMainAttributes().keySet()) {
146 String header = key.toString();
147 String value = manifest.getMainAttributes().getValue(header);
148 if (IGNORE_HEADERS.contains(header))
149 continue;
150
151 if (MAJOR_HEADERS.contains(header)) {
152 Parameters clauses = OSGiHeader.parseHeader(value);
153 Collection<Element> clausesDef = new ArrayList<Element>();
Stuart McCulloch4482c702012-06-15 13:27:53 +0000154 for (Map.Entry<String,Attrs> clause : clauses.entrySet()) {
Stuart McCullochf3173222012-06-07 21:57:32 +0000155 Collection<Element> parameterDef = new ArrayList<Element>();
Stuart McCulloch4482c702012-06-15 13:27:53 +0000156 for (Map.Entry<String,String> parameter : clause.getValue().entrySet()) {
Stuart McCulloch2a0afd62012-09-06 18:28:06 +0000157 String paramValue = parameter.getValue();
158 if (Constants.EXPORT_PACKAGE.equals(header) && Constants.USES_DIRECTIVE.equals(parameter.getKey())) {
159 ExtList<String> uses = ExtList.from(parameter.getValue());
160 Collections.sort(uses);
161 paramValue = uses.join();
162 }
163 parameterDef.add(new Element(Type.PARAMETER, parameter.getKey() + ":" + paramValue,
Stuart McCulloch4482c702012-06-15 13:27:53 +0000164 null, CHANGED, CHANGED, null));
Stuart McCullochf3173222012-06-07 21:57:32 +0000165 }
Stuart McCulloch4482c702012-06-15 13:27:53 +0000166 clausesDef.add(new Element(Type.CLAUSE, clause.getKey(), parameterDef, CHANGED, CHANGED, null));
Stuart McCullochf3173222012-06-07 21:57:32 +0000167 }
168 result.add(new Element(Type.HEADER, header, clausesDef, CHANGED, CHANGED, null));
169 } else {
Stuart McCulloch4482c702012-06-15 13:27:53 +0000170 result.add(new Element(Type.HEADER, header + ":" + value, null, CHANGED, CHANGED, null));
Stuart McCullochf3173222012-06-07 21:57:32 +0000171 }
172 }
173 return new Element(Type.MANIFEST, "<manifest>", result, CHANGED, CHANGED, null);
174 }
175
176 public Tree deserialize(Data data) throws Exception {
177 return new Element(data);
178 }
179
180}