blob: c1d28a4c98028fa012ee972d929dcd939e59e072 [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
Stuart McCullochc73903f2012-10-18 20:01:21 +000036 /**
37 * Headers that have values that should be sorted
38 */
39 final static Set<String> ORDERED_HEADERS = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
40
Stuart McCullochf3173222012-06-07 21:57:32 +000041 static {
42 MAJOR_HEADERS.add(Constants.EXPORT_PACKAGE);
43 MAJOR_HEADERS.add(Constants.IMPORT_PACKAGE);
44 MAJOR_HEADERS.add(Constants.REQUIRE_BUNDLE);
45 MAJOR_HEADERS.add(Constants.FRAGMENT_HOST);
46 MAJOR_HEADERS.add(Constants.BUNDLE_SYMBOLICNAME);
47 MAJOR_HEADERS.add(Constants.BUNDLE_LICENSE);
48 MAJOR_HEADERS.add(Constants.BUNDLE_NATIVECODE);
49 MAJOR_HEADERS.add(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT);
50 MAJOR_HEADERS.add(Constants.DYNAMICIMPORT_PACKAGE);
51
52 IGNORE_HEADERS.add(Constants.TOOL);
53 IGNORE_HEADERS.add(Constants.BND_LASTMODIFIED);
54 IGNORE_HEADERS.add(Constants.CREATED_BY);
Stuart McCullochc73903f2012-10-18 20:01:21 +000055
56 ORDERED_HEADERS.add(Constants.SERVICE_COMPONENT);
Stuart McCullochf3173222012-06-07 21:57:32 +000057 }
58
59 /**
Stuart McCullochf3173222012-06-07 21:57:32 +000060 * @see aQute.bnd.service.diff.Differ#diff(aQute.lib.resource.Jar,
61 * aQute.lib.resource.Jar)
62 */
63 public Tree tree(File newer) throws Exception {
64 Jar jnewer = new Jar(newer);
65 try {
66 return tree(jnewer);
Stuart McCulloch4482c702012-06-15 13:27:53 +000067 }
68 finally {
Stuart McCullochf3173222012-06-07 21:57:32 +000069 jnewer.close();
70 }
71 }
72
73 /**
Stuart McCullochf3173222012-06-07 21:57:32 +000074 * @see aQute.bnd.service.diff.Differ#diff(aQute.lib.resource.Jar,
75 * aQute.lib.resource.Jar)
76 */
77 public Tree tree(Jar newer) throws Exception {
78 Analyzer anewer = new Analyzer();
79 try {
Stuart McCulloch4482c702012-06-15 13:27:53 +000080 anewer.setJar(newer);
81 return tree(anewer);
82 }
83 finally {
Stuart McCullochf3173222012-06-07 21:57:32 +000084 anewer.setJar((Jar) null);
85 anewer.close();
86 }
87 }
88
89 public Tree tree(Analyzer newer) throws Exception {
90 return bundleElement(newer);
91 }
92
93 /**
94 * Create an element representing a bundle from the Jar.
95 *
Stuart McCulloch4482c702012-06-15 13:27:53 +000096 * @param infos
Stuart McCullochf3173222012-06-07 21:57:32 +000097 * @param jar
98 * The Jar to be analyzed
99 * @return the elements that should be compared
100 * @throws Exception
101 */
102 private Element bundleElement(Analyzer analyzer) throws Exception {
103 List<Element> result = new ArrayList<Element>();
104
105 Manifest manifest = analyzer.getJar().getManifest();
106
107 if (manifest != null) {
108 result.add(JavaElement.getAPI(analyzer));
109 result.add(manifestElement(manifest));
110 }
111 result.add(resourcesElement(analyzer.getJar()));
Stuart McCulloch4482c702012-06-15 13:27:53 +0000112 return new Element(Type.BUNDLE, analyzer.getJar().getName(), result, CHANGED, CHANGED, null);
Stuart McCullochf3173222012-06-07 21:57:32 +0000113 }
114
115 /**
116 * Create an element representing all resources in the JAR
117 *
118 * @param jar
119 * @return
120 * @throws Exception
121 */
122 private Element resourcesElement(Jar jar) throws Exception {
123 List<Element> resources = new ArrayList<Element>();
Stuart McCulloch4482c702012-06-15 13:27:53 +0000124 for (Map.Entry<String,Resource> entry : jar.getResources().entrySet()) {
Stuart McCullochf3173222012-06-07 21:57:32 +0000125
126 InputStream in = entry.getValue().openInputStream();
127 try {
128 Digester<SHA1> digester = SHA1.getDigester();
129 IO.copy(in, digester);
130 String value = Hex.toHexString(digester.digest().digest());
Stuart McCulloch42151ee2012-07-16 13:43:38 +0000131 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 +0000132 }
133 finally {
Stuart McCullochf3173222012-06-07 21:57:32 +0000134 in.close();
135 }
136 }
137 return new Element(Type.RESOURCES, "<resources>", resources, CHANGED, CHANGED, null);
138 }
139
Stuart McCullochf3173222012-06-07 21:57:32 +0000140 /**
141 * Create an element for each manifest header. There are
142 * {@link #IGNORE_HEADERS} and {@link #MAJOR_HEADERS} that will be treated
143 * differently.
144 *
145 * @param manifest
146 * @return
147 */
148
149 private Element manifestElement(Manifest manifest) {
150 List<Element> result = new ArrayList<Element>();
151
152 for (Object key : manifest.getMainAttributes().keySet()) {
153 String header = key.toString();
154 String value = manifest.getMainAttributes().getValue(header);
155 if (IGNORE_HEADERS.contains(header))
156 continue;
157
158 if (MAJOR_HEADERS.contains(header)) {
159 Parameters clauses = OSGiHeader.parseHeader(value);
160 Collection<Element> clausesDef = new ArrayList<Element>();
Stuart McCulloch4482c702012-06-15 13:27:53 +0000161 for (Map.Entry<String,Attrs> clause : clauses.entrySet()) {
Stuart McCullochf3173222012-06-07 21:57:32 +0000162 Collection<Element> parameterDef = new ArrayList<Element>();
Stuart McCulloch4482c702012-06-15 13:27:53 +0000163 for (Map.Entry<String,String> parameter : clause.getValue().entrySet()) {
Stuart McCulloch2a0afd62012-09-06 18:28:06 +0000164 String paramValue = parameter.getValue();
165 if (Constants.EXPORT_PACKAGE.equals(header) && Constants.USES_DIRECTIVE.equals(parameter.getKey())) {
166 ExtList<String> uses = ExtList.from(parameter.getValue());
167 Collections.sort(uses);
168 paramValue = uses.join();
169 }
170 parameterDef.add(new Element(Type.PARAMETER, parameter.getKey() + ":" + paramValue,
Stuart McCulloch4482c702012-06-15 13:27:53 +0000171 null, CHANGED, CHANGED, null));
Stuart McCullochf3173222012-06-07 21:57:32 +0000172 }
Stuart McCulloch4482c702012-06-15 13:27:53 +0000173 clausesDef.add(new Element(Type.CLAUSE, clause.getKey(), parameterDef, CHANGED, CHANGED, null));
Stuart McCullochf3173222012-06-07 21:57:32 +0000174 }
175 result.add(new Element(Type.HEADER, header, clausesDef, CHANGED, CHANGED, null));
Stuart McCullochc73903f2012-10-18 20:01:21 +0000176 } else if (ORDERED_HEADERS.contains(header)) {
177 ExtList<String> values = ExtList.from(value);
178 Collections.sort(values);
179 result.add(new Element(Type.HEADER, header + ":" + values.join(), null, CHANGED, CHANGED, null));
Stuart McCullochf3173222012-06-07 21:57:32 +0000180 } else {
Stuart McCulloch4482c702012-06-15 13:27:53 +0000181 result.add(new Element(Type.HEADER, header + ":" + value, null, CHANGED, CHANGED, null));
Stuart McCullochf3173222012-06-07 21:57:32 +0000182 }
183 }
184 return new Element(Type.MANIFEST, "<manifest>", result, CHANGED, CHANGED, null);
185 }
186
187 public Tree deserialize(Data data) throws Exception {
188 return new Element(data);
189 }
190
191}