Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onlab.osgiwrap; |
| 18 | |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 19 | import aQute.bnd.header.Attrs; |
| 20 | import aQute.bnd.header.Parameters; |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 21 | import aQute.bnd.osgi.Analyzer; |
Brian O'Connor | 8aec1a1 | 2016-04-06 14:10:43 -0700 | [diff] [blame] | 22 | import aQute.bnd.osgi.Builder; |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 23 | import aQute.bnd.osgi.FileResource; |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 24 | import aQute.bnd.osgi.Jar; |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 25 | import aQute.bnd.osgi.Resource; |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 26 | import com.google.common.base.Joiner; |
| 27 | import com.google.common.base.MoreObjects; |
| 28 | import com.google.common.collect.Lists; |
| 29 | import com.google.common.collect.Maps; |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 30 | import com.google.common.collect.Sets; |
Thomas Vachuska | e443694 | 2018-08-07 19:27:10 -0700 | [diff] [blame] | 31 | import com.google.common.io.ByteStreams; |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 32 | import org.apache.felix.scrplugin.bnd.SCRDescriptorBndPlugin; |
| 33 | |
| 34 | import java.io.File; |
Thomas Vachuska | e443694 | 2018-08-07 19:27:10 -0700 | [diff] [blame] | 35 | import java.io.FileInputStream; |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 36 | import java.io.IOException; |
| 37 | import java.nio.file.FileVisitResult; |
| 38 | import java.nio.file.FileVisitor; |
| 39 | import java.nio.file.Path; |
| 40 | import java.nio.file.Paths; |
| 41 | import java.nio.file.SimpleFileVisitor; |
| 42 | import java.nio.file.attribute.BasicFileAttributes; |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 43 | import java.util.Arrays; |
| 44 | import java.util.HashSet; |
| 45 | import java.util.List; |
| 46 | import java.util.Map; |
| 47 | import java.util.Objects; |
| 48 | import java.util.Set; |
Thomas Vachuska | e443694 | 2018-08-07 19:27:10 -0700 | [diff] [blame] | 49 | import java.util.jar.JarEntry; |
| 50 | import java.util.jar.JarInputStream; |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 51 | import java.util.jar.Manifest; |
| 52 | |
Thomas Vachuska | e443694 | 2018-08-07 19:27:10 -0700 | [diff] [blame] | 53 | import static com.google.common.io.Files.createParentDirs; |
| 54 | import static com.google.common.io.Files.write; |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 55 | import static java.nio.file.Files.walkFileTree; |
| 56 | |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 57 | /** |
| 58 | * BND-based wrapper to convert Buck JARs to OSGi-compatible JARs. |
| 59 | */ |
| 60 | public class OSGiWrapper { |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 61 | private static final String NONE = "NONE"; |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 62 | |
| 63 | private String inputJar; |
| 64 | private String outputJar; |
| 65 | private List<String> classpath; |
| 66 | |
| 67 | private String bundleName; |
| 68 | private String groupId; |
| 69 | private String bundleSymbolicName; |
| 70 | private String bundleVersion; |
| 71 | |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 72 | private String importPackages; |
Yuta HIGUCHI | f05db40 | 2016-08-12 18:36:33 -0700 | [diff] [blame] | 73 | private String dynamicimportPackages; |
| 74 | |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 75 | private String exportPackages; |
| 76 | private String includeResources; |
| 77 | private Set<String> includedResources = Sets.newHashSet(); |
| 78 | |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 79 | private String bundleDescription; |
| 80 | private String bundleLicense; |
| 81 | |
| 82 | private String webContext; |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 83 | private String webXmlRoot; |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 84 | private String destdir; |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 85 | |
Jian Li | 4ad8687 | 2018-08-05 03:34:35 +0900 | [diff] [blame] | 86 | private String bundleClasspath; |
| 87 | |
Yuta HIGUCHI | f05db40 | 2016-08-12 18:36:33 -0700 | [diff] [blame] | 88 | // FIXME should consider using Commons CLI, etc. |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 89 | public static void main(String[] args) { |
Jian Li | 4ad8687 | 2018-08-05 03:34:35 +0900 | [diff] [blame] | 90 | if (args.length < 14) { |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 91 | System.err.println("Not enough args"); |
| 92 | System.exit(1); |
| 93 | } |
| 94 | |
| 95 | String jar = args[0]; |
| 96 | String output = args[1]; |
| 97 | String cp = args[2]; |
| 98 | String name = args[3]; |
| 99 | String group = args[4]; |
| 100 | String version = args[5]; |
| 101 | String license = args[6]; |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 102 | String importPackages = args[7]; |
| 103 | String exportPackages = args[8]; |
| 104 | String includeResources = args[9]; |
| 105 | String webContext = args[10]; |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 106 | String webXmlRoot = args[11]; |
| 107 | String dynamicimportPackages = args[12]; |
| 108 | String destdir = args[13]; |
Jian Li | 4ad8687 | 2018-08-05 03:34:35 +0900 | [diff] [blame] | 109 | String bundleClasspath = args[14]; |
Yuta HIGUCHI | f05db40 | 2016-08-12 18:36:33 -0700 | [diff] [blame] | 110 | String desc = Joiner.on(' ').join(Arrays.copyOfRange(args, 12, args.length)); |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 111 | |
| 112 | OSGiWrapper wrapper = new OSGiWrapper(jar, output, cp, |
Jian Li | 4ad8687 | 2018-08-05 03:34:35 +0900 | [diff] [blame] | 113 | name, group, |
| 114 | version, license, |
| 115 | importPackages, exportPackages, |
| 116 | includeResources, |
| 117 | webContext, |
| 118 | webXmlRoot, |
| 119 | dynamicimportPackages, |
| 120 | desc, |
| 121 | destdir, |
| 122 | bundleClasspath); |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 123 | wrapper.log(wrapper + "\n"); |
| 124 | if (!wrapper.execute()) { |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 125 | System.err.printf("Error generating %s\n", name); |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 126 | System.exit(2); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | |
| 131 | public OSGiWrapper(String inputJar, |
| 132 | String outputJar, |
| 133 | String classpath, |
| 134 | String bundleName, |
| 135 | String groupId, |
| 136 | String bundleVersion, |
| 137 | String bundleLicense, |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 138 | String importPackages, |
| 139 | String exportPackages, |
| 140 | String includeResources, |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 141 | String webContext, |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 142 | String webXmlRoot, |
Yuta HIGUCHI | f05db40 | 2016-08-12 18:36:33 -0700 | [diff] [blame] | 143 | String dynamicimportPackages, |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 144 | String bundleDescription, |
Jian Li | 4ad8687 | 2018-08-05 03:34:35 +0900 | [diff] [blame] | 145 | String destdir, |
| 146 | String bundleClasspath) { |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 147 | this.inputJar = inputJar; |
| 148 | this.classpath = Lists.newArrayList(classpath.split(":")); |
| 149 | if (!this.classpath.contains(inputJar)) { |
| 150 | this.classpath.add(0, inputJar); |
| 151 | } |
| 152 | this.outputJar = outputJar; |
| 153 | |
| 154 | this.bundleName = bundleName; |
| 155 | this.groupId = groupId; |
| 156 | this.bundleSymbolicName = String.format("%s.%s", groupId, bundleName); |
| 157 | |
| 158 | this.bundleVersion = bundleVersion; |
| 159 | this.bundleLicense = bundleLicense; |
| 160 | this.bundleDescription = bundleDescription; |
| 161 | |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 162 | this.importPackages = importPackages; |
Yuta HIGUCHI | f05db40 | 2016-08-12 18:36:33 -0700 | [diff] [blame] | 163 | this.dynamicimportPackages = dynamicimportPackages; |
| 164 | if (Objects.equals(dynamicimportPackages, "''")) { |
| 165 | this.dynamicimportPackages = null; |
| 166 | } |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 167 | this.exportPackages = exportPackages; |
| 168 | if (!Objects.equals(includeResources, NONE)) { |
| 169 | this.includeResources = includeResources; |
| 170 | } |
| 171 | |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 172 | this.webContext = webContext; |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 173 | this.webXmlRoot = webXmlRoot; |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 174 | this.destdir = destdir; |
Jian Li | 4ad8687 | 2018-08-05 03:34:35 +0900 | [diff] [blame] | 175 | |
| 176 | this.bundleClasspath = bundleClasspath; |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | private void setProperties(Analyzer analyzer) { |
| 180 | analyzer.setProperty(Analyzer.BUNDLE_NAME, bundleName); |
| 181 | analyzer.setProperty(Analyzer.BUNDLE_SYMBOLICNAME, bundleSymbolicName); |
| 182 | analyzer.setProperty(Analyzer.BUNDLE_VERSION, bundleVersion.replace('-', '.')); |
| 183 | |
| 184 | analyzer.setProperty(Analyzer.BUNDLE_DESCRIPTION, bundleDescription); |
| 185 | analyzer.setProperty(Analyzer.BUNDLE_LICENSE, bundleLicense); |
| 186 | |
| 187 | //TODO consider using stricter version policy |
| 188 | //analyzer.setProperty("-provider-policy", "${range;[===,==+)}"); |
| 189 | //analyzer.setProperty("-consumer-policy", "${range;[===,==+)}"); |
| 190 | |
Yuta HIGUCHI | f05db40 | 2016-08-12 18:36:33 -0700 | [diff] [blame] | 191 | analyzer.setProperty(Analyzer.DYNAMICIMPORT_PACKAGE, dynamicimportPackages); |
| 192 | |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 193 | // TODO include version in export, but not in import |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 194 | analyzer.setProperty(Analyzer.EXPORT_PACKAGE, exportPackages); |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 195 | |
| 196 | // TODO we may need INCLUDE_RESOURCE, or that might be done by Buck |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 197 | if (includeResources != null) { |
| 198 | analyzer.setProperty(Analyzer.INCLUDE_RESOURCE, includeResources); |
| 199 | } |
| 200 | |
Jian Li | 4ad8687 | 2018-08-05 03:34:35 +0900 | [diff] [blame] | 201 | // There are no good defaults so make sure you set the Import-Package |
| 202 | analyzer.setProperty(Analyzer.IMPORT_PACKAGE, importPackages); |
| 203 | |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 204 | if (isWab()) { |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 205 | analyzer.setProperty(Analyzer.WAB, webXmlRoot); |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 206 | analyzer.setProperty("Web-ContextPath", webContext); |
Jian Li | 4ad8687 | 2018-08-05 03:34:35 +0900 | [diff] [blame] | 207 | analyzer.setProperty(Analyzer.IMPORT_PACKAGE, importPackages + |
| 208 | ",org.glassfish.jersey.servlet,org.jvnet.mimepull\n"); |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
| 212 | public boolean execute() { |
Brian O'Connor | 8aec1a1 | 2016-04-06 14:10:43 -0700 | [diff] [blame] | 213 | Analyzer analyzer = new Builder(); |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 214 | try { |
Thomas Vachuska | e443694 | 2018-08-07 19:27:10 -0700 | [diff] [blame] | 215 | // Extract the input jar contents into the specified output directory |
| 216 | expandJar(inputJar, new File(destdir)); |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 217 | |
| 218 | Jar jar = new Jar(new File(inputJar)); // where our data is |
| 219 | analyzer.setJar(jar); // give bnd the contents |
| 220 | |
| 221 | // You can provide additional class path entries to allow |
| 222 | // bnd to pickup export version from the packageinfo file, |
| 223 | // Version annotation, or their manifests. |
| 224 | analyzer.addClasspath(classpath); |
| 225 | |
| 226 | setProperties(analyzer); |
| 227 | |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 228 | // ------------- let's begin... ------------------------- |
| 229 | |
| 230 | // Analyze the target JAR first |
| 231 | analyzer.analyze(); |
| 232 | |
| 233 | // Scan the JAR for Felix SCR annotations and generate XML files |
| 234 | Map<String, String> properties = Maps.newHashMap(); |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 235 | // destdir hack |
| 236 | properties.put("destdir", destdir); |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 237 | SCRDescriptorBndPlugin scrDescriptorBndPlugin = new SCRDescriptorBndPlugin(); |
| 238 | scrDescriptorBndPlugin.setProperties(properties); |
| 239 | scrDescriptorBndPlugin.setReporter(analyzer); |
| 240 | scrDescriptorBndPlugin.analyzeJar(analyzer); |
| 241 | |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 242 | if (includeResources != null) { |
| 243 | doIncludeResources(analyzer); |
| 244 | } |
| 245 | |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 246 | // Repack the JAR as a WAR |
| 247 | doWabStaging(analyzer); |
| 248 | |
| 249 | // Calculate the manifest |
| 250 | Manifest manifest = analyzer.calcManifest(); |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 251 | |
| 252 | if (analyzer.isOk()) { |
| 253 | analyzer.getJar().setManifest(manifest); |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 254 | if (analyzer.save(new File(outputJar), true)) { |
| 255 | log("Saved!\n"); |
| 256 | } else { |
| 257 | warn("Failed to create jar \n"); |
| 258 | return false; |
| 259 | } |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 260 | } else { |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 261 | warn("Analyzer Errors:\n%s\n", analyzer.getErrors()); |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 262 | return false; |
| 263 | } |
| 264 | |
| 265 | analyzer.close(); |
| 266 | |
| 267 | return true; |
| 268 | } catch (Exception e) { |
| 269 | e.printStackTrace(); |
| 270 | return false; |
| 271 | } |
| 272 | } |
| 273 | |
Thomas Vachuska | e443694 | 2018-08-07 19:27:10 -0700 | [diff] [blame] | 274 | // Expands the specified jar file into the given directory |
| 275 | private void expandJar(String inputJar, File intoDir) throws IOException { |
| 276 | try (JarInputStream jis = new JarInputStream(new FileInputStream(inputJar))) { |
| 277 | JarEntry entry; |
| 278 | while ((entry = jis.getNextJarEntry()) != null) { |
| 279 | if (!entry.isDirectory()) { |
| 280 | byte[] data = ByteStreams.toByteArray(jis); |
| 281 | jis.closeEntry(); |
| 282 | if (!entry.getName().contains("..")) { |
| 283 | File file = new File(intoDir, entry.getName()); |
| 284 | createParentDirs(file); |
| 285 | write(data, file); |
| 286 | } else { |
| 287 | throw new IOException("Corrupt jar file"); |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 294 | private boolean isWab() { |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 295 | return !Objects.equals(webContext, NONE); |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | private void doWabStaging(Analyzer analyzer) throws Exception { |
| 299 | if (!isWab()) { |
| 300 | return; |
| 301 | } |
| 302 | String wab = analyzer.getProperty(analyzer.WAB); |
| 303 | Jar dot = analyzer.getJar(); |
| 304 | |
| 305 | log("wab %s", wab); |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 306 | |
Jian Li | 4ad8687 | 2018-08-05 03:34:35 +0900 | [diff] [blame] | 307 | String specifiedClasspath = this.bundleClasspath; |
Ray Milkey | 6b3775a | 2018-06-28 11:18:44 -0700 | [diff] [blame] | 308 | String bundleClasspath = "WEB-INF/classes"; |
| 309 | if (specifiedClasspath != null) { |
| 310 | bundleClasspath += "," + specifiedClasspath; |
| 311 | } |
| 312 | analyzer.setBundleClasspath(bundleClasspath); |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 313 | |
Yuta HIGUCHI | f05db40 | 2016-08-12 18:36:33 -0700 | [diff] [blame] | 314 | Set<String> paths = new HashSet<>(dot.getResources().keySet()); |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 315 | |
| 316 | for (String path : paths) { |
| 317 | if (path.indexOf('/') > 0 && !Character.isUpperCase(path.charAt(0))) { |
| 318 | log("wab: moving: %s", path); |
| 319 | dot.rename(path, "WEB-INF/classes/" + path); |
| 320 | } |
| 321 | } |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 322 | |
| 323 | Path wabRoot = Paths.get(wab); |
Ray Milkey | 25747d8 | 2018-06-13 14:12:51 -0700 | [diff] [blame] | 324 | log("wab root " + wabRoot.toString()); |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 325 | includeFiles(dot, null, wabRoot.toString()); |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Parse the Bundle-Includes header. Files in the bundles Include header are |
| 330 | * included in the jar. The source can be a directory or a file. |
| 331 | * |
| 332 | * @throws Exception |
| 333 | */ |
| 334 | private void doIncludeResources(Analyzer analyzer) throws Exception { |
| 335 | String includes = analyzer.getProperty(Analyzer.INCLUDE_RESOURCE); |
| 336 | if (includes == null) { |
| 337 | return; |
| 338 | } |
| 339 | Parameters clauses = analyzer.parseHeader(includes); |
| 340 | Jar jar = analyzer.getJar(); |
| 341 | |
| 342 | for (Map.Entry<String, Attrs> entry : clauses.entrySet()) { |
| 343 | String name = entry.getKey(); |
| 344 | Map<String, String> extra = entry.getValue(); |
| 345 | // TODO consider doing something with extras |
| 346 | |
| 347 | String[] parts = name.split("\\s*=\\s*"); |
| 348 | String source = parts[0]; |
| 349 | String destination = parts[0]; |
| 350 | if (parts.length == 2) { |
| 351 | source = parts[1]; |
| 352 | } |
| 353 | |
| 354 | includeFiles(jar, destination, source); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | private void includeFiles(Jar jar, String destinationRoot, String sourceRoot) |
| 359 | throws IOException { |
| 360 | Path sourceRootPath = Paths.get(sourceRoot); |
| 361 | // iterate through sources |
| 362 | // put each source on the jar |
| 363 | FileVisitor<Path> visitor = new SimpleFileVisitor<Path>() { |
| 364 | @Override |
| 365 | public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { |
| 366 | Path relativePath = sourceRootPath.relativize(file); |
| 367 | String destination = destinationRoot != null ? |
| 368 | destinationRoot + "/" + relativePath.toString() : //TODO |
| 369 | relativePath.toString(); |
| 370 | |
| 371 | addFileToJar(jar, destination, file.toAbsolutePath().toString()); |
| 372 | return FileVisitResult.CONTINUE; |
| 373 | } |
| 374 | }; |
| 375 | File dir = new File(sourceRoot); |
| 376 | if (dir.isFile()) { |
| 377 | addFileToJar(jar, destinationRoot, dir.getAbsolutePath()); |
| 378 | } else if (dir.isDirectory()) { |
| 379 | walkFileTree(sourceRootPath, visitor); |
| 380 | } else { |
| 381 | warn("Skipping resource in bundle %s: %s (File Not Found)\n", |
Jian Li | 4ad8687 | 2018-08-05 03:34:35 +0900 | [diff] [blame] | 382 | bundleSymbolicName, sourceRoot); |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
| 386 | private boolean addFileToJar(Jar jar, String destination, String sourceAbsPath) { |
| 387 | if (includedResources.contains(sourceAbsPath)) { |
| 388 | log("Skipping already included resource: %s\n", sourceAbsPath); |
| 389 | return false; |
| 390 | } |
| 391 | File file = new File(sourceAbsPath); |
| 392 | if (!file.isFile()) { |
| 393 | throw new RuntimeException( |
| 394 | String.format("Skipping non-existent file: %s\n", sourceAbsPath)); |
| 395 | } |
| 396 | Resource resource = new FileResource(file); |
| 397 | if (jar.getResource(destination) != null) { |
| 398 | warn("Skipping duplicate resource: %s\n", destination); |
| 399 | return false; |
| 400 | } |
| 401 | jar.putResource(destination, resource); |
| 402 | includedResources.add(sourceAbsPath); |
| 403 | log("Adding resource: %s\n", destination); |
| 404 | return true; |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | private void log(String format, Object... objects) { |
| 408 | //System.err.printf(format, objects); |
| 409 | } |
| 410 | |
| 411 | private void warn(String format, Object... objects) { |
| 412 | System.err.printf(format, objects); |
| 413 | } |
| 414 | |
| 415 | @Override |
| 416 | public String toString() { |
| 417 | return MoreObjects.toStringHelper(this) |
| 418 | .add("inputJar", inputJar) |
| 419 | .add("outputJar", outputJar) |
| 420 | .add("classpath", classpath) |
| 421 | .add("bundleName", bundleName) |
| 422 | .add("groupId", groupId) |
| 423 | .add("bundleSymbolicName", bundleSymbolicName) |
| 424 | .add("bundleVersion", bundleVersion) |
| 425 | .add("bundleDescription", bundleDescription) |
| 426 | .add("bundleLicense", bundleLicense) |
Jian Li | 4ad8687 | 2018-08-05 03:34:35 +0900 | [diff] [blame] | 427 | .add("bundleClassPath", bundleClasspath) |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 428 | .toString(); |
| 429 | |
| 430 | } |
| 431 | } |