Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.bnd.differ; |
| 2 | |
| 3 | import static aQute.bnd.service.diff.Delta.*; |
| 4 | |
| 5 | import java.io.*; |
| 6 | import java.util.*; |
| 7 | import java.util.jar.*; |
| 8 | |
Stuart McCulloch | 42151ee | 2012-07-16 13:43:38 +0000 | [diff] [blame] | 9 | import aQute.bnd.header.*; |
| 10 | import aQute.bnd.osgi.*; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 11 | import aQute.bnd.service.diff.*; |
| 12 | import aQute.bnd.service.diff.Tree.Data; |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame] | 13 | import aQute.lib.collections.*; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 14 | import aQute.lib.hex.*; |
| 15 | import aQute.lib.io.*; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 16 | import aQute.libg.cryptography.*; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 17 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 18 | /** |
| 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 | */ |
| 23 | public class DiffPluginImpl implements Differ { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 24 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 25 | /** |
| 26 | * Headers that are considered major enough to parse according to spec and |
| 27 | * compare their constituents |
| 28 | */ |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 29 | final static Set<String> MAJOR_HEADERS = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Headers that are considered not major enough to be considered |
| 33 | */ |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 34 | final static Set<String> IGNORE_HEADERS = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 35 | |
Stuart McCulloch | c73903f | 2012-10-18 20:01:21 +0000 | [diff] [blame] | 36 | /** |
| 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 McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 41 | 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 McCulloch | c73903f | 2012-10-18 20:01:21 +0000 | [diff] [blame] | 55 | |
| 56 | ORDERED_HEADERS.add(Constants.SERVICE_COMPONENT); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /** |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 60 | * @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 McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 67 | } |
| 68 | finally { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 69 | jnewer.close(); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /** |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 74 | * @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 McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 80 | anewer.setJar(newer); |
| 81 | return tree(anewer); |
| 82 | } |
| 83 | finally { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 84 | 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 McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 96 | * @param infos |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 97 | * @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 McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 112 | return new Element(Type.BUNDLE, analyzer.getJar().getName(), result, CHANGED, CHANGED, null); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 113 | } |
| 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 McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 124 | for (Map.Entry<String,Resource> entry : jar.getResources().entrySet()) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 125 | |
| 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 McCulloch | 42151ee | 2012-07-16 13:43:38 +0000 | [diff] [blame] | 131 | resources.add(new Element(Type.RESOURCE, entry.getKey(), Arrays.asList(new Element(Type.SHA,value)), CHANGED, CHANGED, null)); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 132 | } |
| 133 | finally { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 134 | in.close(); |
| 135 | } |
| 136 | } |
| 137 | return new Element(Type.RESOURCES, "<resources>", resources, CHANGED, CHANGED, null); |
| 138 | } |
| 139 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 140 | /** |
| 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 McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 161 | for (Map.Entry<String,Attrs> clause : clauses.entrySet()) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 162 | Collection<Element> parameterDef = new ArrayList<Element>(); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 163 | for (Map.Entry<String,String> parameter : clause.getValue().entrySet()) { |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame] | 164 | 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 McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 171 | null, CHANGED, CHANGED, null)); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 172 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 173 | clausesDef.add(new Element(Type.CLAUSE, clause.getKey(), parameterDef, CHANGED, CHANGED, null)); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 174 | } |
| 175 | result.add(new Element(Type.HEADER, header, clausesDef, CHANGED, CHANGED, null)); |
Stuart McCulloch | c73903f | 2012-10-18 20:01:21 +0000 | [diff] [blame] | 176 | } 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 McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 180 | } else { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 181 | result.add(new Element(Type.HEADER, header + ":" + value, null, CHANGED, CHANGED, null)); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 182 | } |
| 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 | } |