1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file or at 6 // https://developers.google.com/open-source/licenses/bsd 7 8 package com.google.protobuf.osgi; 9 10 import aQute.bnd.osgi.Analyzer; 11 import aQute.bnd.osgi.Jar; 12 import java.io.File; 13 import java.util.Arrays; 14 import java.util.concurrent.Callable; 15 import java.util.jar.Manifest; 16 import java.util.stream.Collectors; 17 import picocli.CommandLine; 18 import picocli.CommandLine.Command; 19 import picocli.CommandLine.Option; 20 21 /** Java binary that runs bndlib to analyze a jar file to generate OSGi bundle manifest. */ 22 @Command(name = "osgi_wrapper") 23 public final class OsgiWrapper implements Callable<Integer> { 24 private static final String REMOVEHEADERS = 25 Arrays.stream( 26 new String[] { 27 "Embed-Dependency", 28 "Embed-Transitive", 29 "Built-By", 30 // "Tool", 31 "Created-By", 32 // "Build-Jdk", 33 "Originally-Created-By", 34 "Archiver-Version", 35 "Include-Resource", 36 "Private-Package", 37 "Ignore-Package", 38 // "Bnd-LastModified", 39 "Target-Label" 40 }) 41 .collect(Collectors.joining(",")); 42 43 @Option( 44 names = {"--input_jar"}, 45 description = "The jar file to wrap with OSGi metadata") 46 private File inputJar; 47 48 @Option( 49 names = {"--output_jar"}, 50 description = "Output path to the wrapped jar") 51 private File outputJar; 52 53 @Option( 54 names = {"--classpath"}, 55 description = "The classpath that contains dependencies of the input jar, separated with :") 56 private String classpath; 57 58 @Option( 59 names = {"--automatic_module_name"}, 60 description = "The automatic module name of the bundle") 61 private String automaticModuleName; 62 63 @Option( 64 names = {"--bundle_copyright"}, 65 description = "Copyright string for the bundle") 66 private String bundleCopyright; 67 68 @Option( 69 names = {"--bundle_description"}, 70 description = "Description of the bundle") 71 private String bundleDescription; 72 73 @Option( 74 names = {"--bundle_doc_url"}, 75 description = "Documentation URL for the bundle") 76 private String bundleDocUrl; 77 78 @Option( 79 names = {"--bundle_license"}, 80 description = "URL for the license of the bundle") 81 private String bundleLicense; 82 83 @Option( 84 names = {"--bundle_name"}, 85 description = "The name of the bundle") 86 private String bundleName; 87 88 @Option( 89 names = {"--bundle_symbolic_name"}, 90 description = "The symbolic name of the bundle") 91 private String bundleSymbolicName; 92 93 @Option( 94 names = {"--bundle_version"}, 95 description = "The version of the bundle") 96 private String bundleVersion; 97 98 @Option( 99 names = {"--export_package"}, 100 description = "The exported packages from this bundle") 101 private String exportPackage; 102 103 @Option( 104 names = {"--import_package"}, 105 description = "The imported packages from this bundle") 106 private String importPackage; 107 108 @Override call()109 public Integer call() throws Exception { 110 Jar bin = new Jar(inputJar); 111 112 Analyzer analyzer = new Analyzer(); 113 analyzer.setJar(bin); 114 analyzer.setProperty(Analyzer.AUTOMATIC_MODULE_NAME, automaticModuleName); 115 analyzer.setProperty(Analyzer.BUNDLE_NAME, bundleName); 116 analyzer.setProperty(Analyzer.BUNDLE_SYMBOLICNAME, bundleSymbolicName); 117 analyzer.setProperty(Analyzer.BUNDLE_VERSION, bundleVersion); 118 analyzer.setProperty(Analyzer.IMPORT_PACKAGE, importPackage); 119 analyzer.setProperty(Analyzer.EXPORT_PACKAGE, exportPackage); 120 analyzer.setProperty(Analyzer.BUNDLE_DESCRIPTION, bundleDescription); 121 analyzer.setProperty(Analyzer.BUNDLE_COPYRIGHT, bundleCopyright); 122 analyzer.setProperty(Analyzer.BUNDLE_DOCURL, bundleDocUrl); 123 analyzer.setProperty(Analyzer.BUNDLE_LICENSE, bundleLicense); 124 analyzer.setProperty(Analyzer.REMOVEHEADERS, REMOVEHEADERS); 125 126 if (classpath != null) { 127 for (String dep : Arrays.asList(classpath.split(":"))) { 128 analyzer.addClasspath(new File(dep)); 129 } 130 } 131 132 analyzer.analyze(); 133 134 Manifest manifest = analyzer.calcManifest(); 135 136 if (analyzer.isOk()) { 137 analyzer.getJar().setManifest(manifest); 138 if (analyzer.save(outputJar, true)) { 139 return 0; 140 } 141 } 142 return 1; 143 } 144 main(String[] args)145 public static void main(String[] args) { 146 int exitCode = new CommandLine(new OsgiWrapper()).execute(args); 147 System.exit(exitCode); 148 } 149 } 150