blob: 4da23cc2e8819ffc27a7aeee6aa15bb1bb77fbfd [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;
13import aQute.lib.hex.*;
14import aQute.lib.io.*;
Stuart McCullochf3173222012-06-07 21:57:32 +000015import aQute.libg.cryptography.*;
Stuart McCullochf3173222012-06-07 21:57:32 +000016
Stuart McCullochf3173222012-06-07 21:57:32 +000017/**
18 * This Diff Plugin Implementation will compare JARs for their API (based on the
19 * Bundle Class Path and exported packages), the Manifest, and the resources.
20 * The Differences are represented in a {@link Diff} tree.
21 */
22public class DiffPluginImpl implements Differ {
Stuart McCulloch4482c702012-06-15 13:27:53 +000023
Stuart McCullochf3173222012-06-07 21:57:32 +000024 /**
25 * Headers that are considered major enough to parse according to spec and
26 * compare their constituents
27 */
Stuart McCulloch4482c702012-06-15 13:27:53 +000028 final static Set<String> MAJOR_HEADERS = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
Stuart McCullochf3173222012-06-07 21:57:32 +000029
30 /**
31 * Headers that are considered not major enough to be considered
32 */
Stuart McCulloch4482c702012-06-15 13:27:53 +000033 final static Set<String> IGNORE_HEADERS = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
Stuart McCullochf3173222012-06-07 21:57:32 +000034
35 static {
36 MAJOR_HEADERS.add(Constants.EXPORT_PACKAGE);
37 MAJOR_HEADERS.add(Constants.IMPORT_PACKAGE);
38 MAJOR_HEADERS.add(Constants.REQUIRE_BUNDLE);
39 MAJOR_HEADERS.add(Constants.FRAGMENT_HOST);
40 MAJOR_HEADERS.add(Constants.BUNDLE_SYMBOLICNAME);
41 MAJOR_HEADERS.add(Constants.BUNDLE_LICENSE);
42 MAJOR_HEADERS.add(Constants.BUNDLE_NATIVECODE);
43 MAJOR_HEADERS.add(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT);
44 MAJOR_HEADERS.add(Constants.DYNAMICIMPORT_PACKAGE);
45
46 IGNORE_HEADERS.add(Constants.TOOL);
47 IGNORE_HEADERS.add(Constants.BND_LASTMODIFIED);
48 IGNORE_HEADERS.add(Constants.CREATED_BY);
49 }
50
51 /**
Stuart McCullochf3173222012-06-07 21:57:32 +000052 * @see aQute.bnd.service.diff.Differ#diff(aQute.lib.resource.Jar,
53 * aQute.lib.resource.Jar)
54 */
55 public Tree tree(File newer) throws Exception {
56 Jar jnewer = new Jar(newer);
57 try {
58 return tree(jnewer);
Stuart McCulloch4482c702012-06-15 13:27:53 +000059 }
60 finally {
Stuart McCullochf3173222012-06-07 21:57:32 +000061 jnewer.close();
62 }
63 }
64
65 /**
Stuart McCullochf3173222012-06-07 21:57:32 +000066 * @see aQute.bnd.service.diff.Differ#diff(aQute.lib.resource.Jar,
67 * aQute.lib.resource.Jar)
68 */
69 public Tree tree(Jar newer) throws Exception {
70 Analyzer anewer = new Analyzer();
71 try {
Stuart McCulloch4482c702012-06-15 13:27:53 +000072 anewer.setJar(newer);
73 return tree(anewer);
74 }
75 finally {
Stuart McCullochf3173222012-06-07 21:57:32 +000076 anewer.setJar((Jar) null);
77 anewer.close();
78 }
79 }
80
81 public Tree tree(Analyzer newer) throws Exception {
82 return bundleElement(newer);
83 }
84
85 /**
86 * Create an element representing a bundle from the Jar.
87 *
Stuart McCulloch4482c702012-06-15 13:27:53 +000088 * @param infos
Stuart McCullochf3173222012-06-07 21:57:32 +000089 * @param jar
90 * The Jar to be analyzed
91 * @return the elements that should be compared
92 * @throws Exception
93 */
94 private Element bundleElement(Analyzer analyzer) throws Exception {
95 List<Element> result = new ArrayList<Element>();
96
97 Manifest manifest = analyzer.getJar().getManifest();
98
99 if (manifest != null) {
100 result.add(JavaElement.getAPI(analyzer));
101 result.add(manifestElement(manifest));
102 }
103 result.add(resourcesElement(analyzer.getJar()));
Stuart McCulloch4482c702012-06-15 13:27:53 +0000104 return new Element(Type.BUNDLE, analyzer.getJar().getName(), result, CHANGED, CHANGED, null);
Stuart McCullochf3173222012-06-07 21:57:32 +0000105 }
106
107 /**
108 * Create an element representing all resources in the JAR
109 *
110 * @param jar
111 * @return
112 * @throws Exception
113 */
114 private Element resourcesElement(Jar jar) throws Exception {
115 List<Element> resources = new ArrayList<Element>();
Stuart McCulloch4482c702012-06-15 13:27:53 +0000116 for (Map.Entry<String,Resource> entry : jar.getResources().entrySet()) {
Stuart McCullochf3173222012-06-07 21:57:32 +0000117
118 InputStream in = entry.getValue().openInputStream();
119 try {
120 Digester<SHA1> digester = SHA1.getDigester();
121 IO.copy(in, digester);
122 String value = Hex.toHexString(digester.digest().digest());
Stuart McCulloch42151ee2012-07-16 13:43:38 +0000123 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 +0000124 }
125 finally {
Stuart McCullochf3173222012-06-07 21:57:32 +0000126 in.close();
127 }
128 }
129 return new Element(Type.RESOURCES, "<resources>", resources, CHANGED, CHANGED, null);
130 }
131
Stuart McCullochf3173222012-06-07 21:57:32 +0000132 /**
133 * Create an element for each manifest header. There are
134 * {@link #IGNORE_HEADERS} and {@link #MAJOR_HEADERS} that will be treated
135 * differently.
136 *
137 * @param manifest
138 * @return
139 */
140
141 private Element manifestElement(Manifest manifest) {
142 List<Element> result = new ArrayList<Element>();
143
144 for (Object key : manifest.getMainAttributes().keySet()) {
145 String header = key.toString();
146 String value = manifest.getMainAttributes().getValue(header);
147 if (IGNORE_HEADERS.contains(header))
148 continue;
149
150 if (MAJOR_HEADERS.contains(header)) {
151 Parameters clauses = OSGiHeader.parseHeader(value);
152 Collection<Element> clausesDef = new ArrayList<Element>();
Stuart McCulloch4482c702012-06-15 13:27:53 +0000153 for (Map.Entry<String,Attrs> clause : clauses.entrySet()) {
Stuart McCullochf3173222012-06-07 21:57:32 +0000154 Collection<Element> parameterDef = new ArrayList<Element>();
Stuart McCulloch4482c702012-06-15 13:27:53 +0000155 for (Map.Entry<String,String> parameter : clause.getValue().entrySet()) {
156 parameterDef.add(new Element(Type.PARAMETER, parameter.getKey() + ":" + parameter.getValue(),
157 null, CHANGED, CHANGED, null));
Stuart McCullochf3173222012-06-07 21:57:32 +0000158 }
Stuart McCulloch4482c702012-06-15 13:27:53 +0000159 clausesDef.add(new Element(Type.CLAUSE, clause.getKey(), parameterDef, CHANGED, CHANGED, null));
Stuart McCullochf3173222012-06-07 21:57:32 +0000160 }
161 result.add(new Element(Type.HEADER, header, clausesDef, CHANGED, CHANGED, null));
162 } else {
Stuart McCulloch4482c702012-06-15 13:27:53 +0000163 result.add(new Element(Type.HEADER, header + ":" + value, null, CHANGED, CHANGED, null));
Stuart McCullochf3173222012-06-07 21:57:32 +0000164 }
165 }
166 return new Element(Type.MANIFEST, "<manifest>", result, CHANGED, CHANGED, null);
167 }
168
169 public Tree deserialize(Data data) throws Exception {
170 return new Element(data);
171 }
172
173}