Stuart McCulloch | bb01437 | 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 | 39cc9ac | 2012-07-16 13:43:38 +0000 | [diff] [blame^] | 9 | import aQute.bnd.header.*; |
| 10 | import aQute.bnd.osgi.*; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 11 | import aQute.bnd.service.diff.*; |
| 12 | import aQute.bnd.service.diff.Tree.Data; |
| 13 | import aQute.lib.hex.*; |
| 14 | import aQute.lib.io.*; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 15 | import aQute.libg.cryptography.*; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 16 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 17 | /** |
| 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 | */ |
| 22 | public class DiffPluginImpl implements Differ { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 23 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 24 | /** |
| 25 | * Headers that are considered major enough to parse according to spec and |
| 26 | * compare their constituents |
| 27 | */ |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 28 | final static Set<String> MAJOR_HEADERS = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Headers that are considered not major enough to be considered |
| 32 | */ |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 33 | final static Set<String> IGNORE_HEADERS = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 34 | |
| 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 McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 52 | * @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 McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 59 | } |
| 60 | finally { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 61 | jnewer.close(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 66 | * @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 McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 72 | anewer.setJar(newer); |
| 73 | return tree(anewer); |
| 74 | } |
| 75 | finally { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 76 | 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 McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 88 | * @param infos |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 89 | * @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 McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 104 | return new Element(Type.BUNDLE, analyzer.getJar().getName(), result, CHANGED, CHANGED, null); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 105 | } |
| 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 McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 116 | for (Map.Entry<String,Resource> entry : jar.getResources().entrySet()) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 117 | |
| 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 McCulloch | 39cc9ac | 2012-07-16 13:43:38 +0000 | [diff] [blame^] | 123 | resources.add(new Element(Type.RESOURCE, entry.getKey(), Arrays.asList(new Element(Type.SHA,value)), CHANGED, CHANGED, null)); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 124 | } |
| 125 | finally { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 126 | in.close(); |
| 127 | } |
| 128 | } |
| 129 | return new Element(Type.RESOURCES, "<resources>", resources, CHANGED, CHANGED, null); |
| 130 | } |
| 131 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 132 | /** |
| 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 McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 153 | for (Map.Entry<String,Attrs> clause : clauses.entrySet()) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 154 | Collection<Element> parameterDef = new ArrayList<Element>(); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 155 | 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 McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 158 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 159 | clausesDef.add(new Element(Type.CLAUSE, clause.getKey(), parameterDef, CHANGED, CHANGED, null)); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 160 | } |
| 161 | result.add(new Element(Type.HEADER, header, clausesDef, CHANGED, CHANGED, null)); |
| 162 | } else { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 163 | result.add(new Element(Type.HEADER, header + ":" + value, null, CHANGED, CHANGED, null)); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 164 | } |
| 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 | } |