Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
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; |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 31 | import org.apache.felix.scrplugin.bnd.SCRDescriptorBndPlugin; |
| 32 | |
| 33 | import java.io.File; |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 34 | import java.io.IOException; |
| 35 | import java.nio.file.FileVisitResult; |
| 36 | import java.nio.file.FileVisitor; |
| 37 | import java.nio.file.Path; |
| 38 | import java.nio.file.Paths; |
| 39 | import java.nio.file.SimpleFileVisitor; |
| 40 | import java.nio.file.attribute.BasicFileAttributes; |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 41 | import java.util.Arrays; |
| 42 | import java.util.HashSet; |
| 43 | import java.util.List; |
| 44 | import java.util.Map; |
| 45 | import java.util.Objects; |
| 46 | import java.util.Set; |
| 47 | import java.util.jar.Manifest; |
| 48 | |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 49 | import static java.nio.file.Files.walkFileTree; |
| 50 | |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 51 | /** |
| 52 | * BND-based wrapper to convert Buck JARs to OSGi-compatible JARs. |
| 53 | */ |
| 54 | public class OSGiWrapper { |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 55 | private static final String NONE = "NONE"; |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 56 | |
| 57 | private String inputJar; |
| 58 | private String outputJar; |
| 59 | private List<String> classpath; |
| 60 | |
| 61 | private String bundleName; |
| 62 | private String groupId; |
| 63 | private String bundleSymbolicName; |
| 64 | private String bundleVersion; |
| 65 | |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 66 | private String importPackages; |
| 67 | private String exportPackages; |
| 68 | private String includeResources; |
| 69 | private Set<String> includedResources = Sets.newHashSet(); |
| 70 | |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 71 | private String bundleDescription; |
| 72 | private String bundleLicense; |
| 73 | |
| 74 | private String webContext; |
| 75 | |
| 76 | public static void main(String[] args) { |
Brian O'Connor | b5c1112 | 2016-04-29 15:22:57 -0700 | [diff] [blame] | 77 | if (args.length < 11) { |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 78 | System.err.println("Not enough args"); |
| 79 | System.exit(1); |
| 80 | } |
| 81 | |
| 82 | String jar = args[0]; |
| 83 | String output = args[1]; |
| 84 | String cp = args[2]; |
| 85 | String name = args[3]; |
| 86 | String group = args[4]; |
| 87 | String version = args[5]; |
| 88 | String license = args[6]; |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 89 | String importPackages = args[7]; |
| 90 | String exportPackages = args[8]; |
| 91 | String includeResources = args[9]; |
| 92 | String webContext = args[10]; |
| 93 | String desc = Joiner.on(' ').join(Arrays.copyOfRange(args, 11, args.length)); |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 94 | |
| 95 | OSGiWrapper wrapper = new OSGiWrapper(jar, output, cp, |
| 96 | name, group, |
| 97 | version, license, |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 98 | importPackages, exportPackages, |
| 99 | includeResources, |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 100 | webContext, desc); |
| 101 | wrapper.log(wrapper + "\n"); |
| 102 | if (!wrapper.execute()) { |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 103 | System.err.printf("Error generating %s\n", name); |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 104 | System.exit(2); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | |
| 109 | public OSGiWrapper(String inputJar, |
| 110 | String outputJar, |
| 111 | String classpath, |
| 112 | String bundleName, |
| 113 | String groupId, |
| 114 | String bundleVersion, |
| 115 | String bundleLicense, |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 116 | String importPackages, |
| 117 | String exportPackages, |
| 118 | String includeResources, |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 119 | String webContext, |
| 120 | String bundleDescription) { |
| 121 | this.inputJar = inputJar; |
| 122 | this.classpath = Lists.newArrayList(classpath.split(":")); |
| 123 | if (!this.classpath.contains(inputJar)) { |
| 124 | this.classpath.add(0, inputJar); |
| 125 | } |
| 126 | this.outputJar = outputJar; |
| 127 | |
| 128 | this.bundleName = bundleName; |
| 129 | this.groupId = groupId; |
| 130 | this.bundleSymbolicName = String.format("%s.%s", groupId, bundleName); |
| 131 | |
| 132 | this.bundleVersion = bundleVersion; |
| 133 | this.bundleLicense = bundleLicense; |
| 134 | this.bundleDescription = bundleDescription; |
| 135 | |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 136 | this.importPackages = importPackages; |
| 137 | this.exportPackages = exportPackages; |
| 138 | if (!Objects.equals(includeResources, NONE)) { |
| 139 | this.includeResources = includeResources; |
| 140 | } |
| 141 | |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 142 | this.webContext = webContext; |
| 143 | } |
| 144 | |
| 145 | private void setProperties(Analyzer analyzer) { |
| 146 | analyzer.setProperty(Analyzer.BUNDLE_NAME, bundleName); |
| 147 | analyzer.setProperty(Analyzer.BUNDLE_SYMBOLICNAME, bundleSymbolicName); |
| 148 | analyzer.setProperty(Analyzer.BUNDLE_VERSION, bundleVersion.replace('-', '.')); |
| 149 | |
| 150 | analyzer.setProperty(Analyzer.BUNDLE_DESCRIPTION, bundleDescription); |
| 151 | analyzer.setProperty(Analyzer.BUNDLE_LICENSE, bundleLicense); |
| 152 | |
| 153 | //TODO consider using stricter version policy |
| 154 | //analyzer.setProperty("-provider-policy", "${range;[===,==+)}"); |
| 155 | //analyzer.setProperty("-consumer-policy", "${range;[===,==+)}"); |
| 156 | |
| 157 | // There are no good defaults so make sure you set the Import-Package |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 158 | analyzer.setProperty(Analyzer.IMPORT_PACKAGE, importPackages); |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 159 | |
| 160 | // TODO include version in export, but not in import |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 161 | analyzer.setProperty(Analyzer.EXPORT_PACKAGE, exportPackages); |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 162 | |
| 163 | // 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] | 164 | if (includeResources != null) { |
| 165 | analyzer.setProperty(Analyzer.INCLUDE_RESOURCE, includeResources); |
| 166 | } |
| 167 | |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 168 | if (isWab()) { |
| 169 | analyzer.setProperty(Analyzer.WAB, "src/main/webapp/"); |
| 170 | analyzer.setProperty("Web-ContextPath", webContext); |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 171 | analyzer.setProperty(Analyzer.IMPORT_PACKAGE, "*,org.glassfish.jersey.servlet,org.jvnet.mimepull\n"); |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | public boolean execute() { |
Brian O'Connor | 8aec1a1 | 2016-04-06 14:10:43 -0700 | [diff] [blame] | 176 | Analyzer analyzer = new Builder(); |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 177 | try { |
| 178 | |
| 179 | Jar jar = new Jar(new File(inputJar)); // where our data is |
| 180 | analyzer.setJar(jar); // give bnd the contents |
| 181 | |
| 182 | // You can provide additional class path entries to allow |
| 183 | // bnd to pickup export version from the packageinfo file, |
| 184 | // Version annotation, or their manifests. |
| 185 | analyzer.addClasspath(classpath); |
| 186 | |
| 187 | setProperties(analyzer); |
| 188 | |
| 189 | // analyzer.setProperty("DESTDIR"); |
| 190 | // analyzer.setBase(); |
| 191 | |
| 192 | // ------------- let's begin... ------------------------- |
| 193 | |
| 194 | // Analyze the target JAR first |
| 195 | analyzer.analyze(); |
| 196 | |
| 197 | // Scan the JAR for Felix SCR annotations and generate XML files |
| 198 | Map<String, String> properties = Maps.newHashMap(); |
| 199 | SCRDescriptorBndPlugin scrDescriptorBndPlugin = new SCRDescriptorBndPlugin(); |
| 200 | scrDescriptorBndPlugin.setProperties(properties); |
| 201 | scrDescriptorBndPlugin.setReporter(analyzer); |
| 202 | scrDescriptorBndPlugin.analyzeJar(analyzer); |
| 203 | |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 204 | if (includeResources != null) { |
| 205 | doIncludeResources(analyzer); |
| 206 | } |
| 207 | |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 208 | // Repack the JAR as a WAR |
| 209 | doWabStaging(analyzer); |
| 210 | |
| 211 | // Calculate the manifest |
| 212 | Manifest manifest = analyzer.calcManifest(); |
| 213 | //OutputStream s = new FileOutputStream("/tmp/foo2.txt"); |
| 214 | //manifest.write(s); |
| 215 | //s.close(); |
| 216 | |
| 217 | if (analyzer.isOk()) { |
| 218 | analyzer.getJar().setManifest(manifest); |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 219 | if (analyzer.save(new File(outputJar), true)) { |
| 220 | log("Saved!\n"); |
| 221 | } else { |
| 222 | warn("Failed to create jar \n"); |
| 223 | return false; |
| 224 | } |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 225 | } else { |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 226 | warn("Analyzer Errors:\n%s\n", analyzer.getErrors()); |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 227 | return false; |
| 228 | } |
| 229 | |
| 230 | analyzer.close(); |
| 231 | |
| 232 | return true; |
| 233 | } catch (Exception e) { |
| 234 | e.printStackTrace(); |
| 235 | return false; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | private boolean isWab() { |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 240 | return !Objects.equals(webContext, NONE); |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | private void doWabStaging(Analyzer analyzer) throws Exception { |
| 244 | if (!isWab()) { |
| 245 | return; |
| 246 | } |
| 247 | String wab = analyzer.getProperty(analyzer.WAB); |
| 248 | Jar dot = analyzer.getJar(); |
| 249 | |
| 250 | log("wab %s", wab); |
| 251 | analyzer.setBundleClasspath("WEB-INF/classes," + |
| 252 | analyzer.getProperty(analyzer.BUNDLE_CLASSPATH)); |
| 253 | |
| 254 | Set<String> paths = new HashSet<String>(dot.getResources().keySet()); |
| 255 | |
| 256 | for (String path : paths) { |
| 257 | if (path.indexOf('/') > 0 && !Character.isUpperCase(path.charAt(0))) { |
| 258 | log("wab: moving: %s", path); |
| 259 | dot.rename(path, "WEB-INF/classes/" + path); |
| 260 | } |
| 261 | } |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 262 | |
| 263 | Path wabRoot = Paths.get(wab); |
| 264 | includeFiles(dot, null, wabRoot.toString()); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Parse the Bundle-Includes header. Files in the bundles Include header are |
| 269 | * included in the jar. The source can be a directory or a file. |
| 270 | * |
| 271 | * @throws Exception |
| 272 | */ |
| 273 | private void doIncludeResources(Analyzer analyzer) throws Exception { |
| 274 | String includes = analyzer.getProperty(Analyzer.INCLUDE_RESOURCE); |
| 275 | if (includes == null) { |
| 276 | return; |
| 277 | } |
| 278 | Parameters clauses = analyzer.parseHeader(includes); |
| 279 | Jar jar = analyzer.getJar(); |
| 280 | |
| 281 | for (Map.Entry<String, Attrs> entry : clauses.entrySet()) { |
| 282 | String name = entry.getKey(); |
| 283 | Map<String, String> extra = entry.getValue(); |
| 284 | // TODO consider doing something with extras |
| 285 | |
| 286 | String[] parts = name.split("\\s*=\\s*"); |
| 287 | String source = parts[0]; |
| 288 | String destination = parts[0]; |
| 289 | if (parts.length == 2) { |
| 290 | source = parts[1]; |
| 291 | } |
| 292 | |
| 293 | includeFiles(jar, destination, source); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | private void includeFiles(Jar jar, String destinationRoot, String sourceRoot) |
| 298 | throws IOException { |
| 299 | Path sourceRootPath = Paths.get(sourceRoot); |
| 300 | // iterate through sources |
| 301 | // put each source on the jar |
| 302 | FileVisitor<Path> visitor = new SimpleFileVisitor<Path>() { |
| 303 | @Override |
| 304 | public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { |
| 305 | Path relativePath = sourceRootPath.relativize(file); |
| 306 | String destination = destinationRoot != null ? |
| 307 | destinationRoot + "/" + relativePath.toString() : //TODO |
| 308 | relativePath.toString(); |
| 309 | |
| 310 | addFileToJar(jar, destination, file.toAbsolutePath().toString()); |
| 311 | return FileVisitResult.CONTINUE; |
| 312 | } |
| 313 | }; |
| 314 | File dir = new File(sourceRoot); |
| 315 | if (dir.isFile()) { |
| 316 | addFileToJar(jar, destinationRoot, dir.getAbsolutePath()); |
| 317 | } else if (dir.isDirectory()) { |
| 318 | walkFileTree(sourceRootPath, visitor); |
| 319 | } else { |
| 320 | warn("Skipping resource in bundle %s: %s (File Not Found)\n", |
| 321 | bundleSymbolicName, sourceRoot); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | private boolean addFileToJar(Jar jar, String destination, String sourceAbsPath) { |
| 326 | if (includedResources.contains(sourceAbsPath)) { |
| 327 | log("Skipping already included resource: %s\n", sourceAbsPath); |
| 328 | return false; |
| 329 | } |
| 330 | File file = new File(sourceAbsPath); |
| 331 | if (!file.isFile()) { |
| 332 | throw new RuntimeException( |
| 333 | String.format("Skipping non-existent file: %s\n", sourceAbsPath)); |
| 334 | } |
| 335 | Resource resource = new FileResource(file); |
| 336 | if (jar.getResource(destination) != null) { |
| 337 | warn("Skipping duplicate resource: %s\n", destination); |
| 338 | return false; |
| 339 | } |
| 340 | jar.putResource(destination, resource); |
| 341 | includedResources.add(sourceAbsPath); |
| 342 | log("Adding resource: %s\n", destination); |
| 343 | return true; |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | private void log(String format, Object... objects) { |
| 347 | //System.err.printf(format, objects); |
| 348 | } |
| 349 | |
| 350 | private void warn(String format, Object... objects) { |
| 351 | System.err.printf(format, objects); |
| 352 | } |
| 353 | |
| 354 | @Override |
| 355 | public String toString() { |
| 356 | return MoreObjects.toStringHelper(this) |
| 357 | .add("inputJar", inputJar) |
| 358 | .add("outputJar", outputJar) |
| 359 | .add("classpath", classpath) |
| 360 | .add("bundleName", bundleName) |
| 361 | .add("groupId", groupId) |
| 362 | .add("bundleSymbolicName", bundleSymbolicName) |
| 363 | .add("bundleVersion", bundleVersion) |
| 364 | .add("bundleDescription", bundleDescription) |
| 365 | .add("bundleLicense", bundleLicense) |
| 366 | .toString(); |
| 367 | |
| 368 | } |
| 369 | } |