|
| 1 | +// Protocol Buffers - Google's data interchange format |
| 2 | +// Copyright 2008 Google Inc. All rights reserved. |
| 3 | +// https://developers.google.com/protocol-buffers/ |
| 4 | +// |
| 5 | +// Redistribution and use in source and binary forms, with or without |
| 6 | +// modification, are permitted provided that the following conditions are |
| 7 | +// met: |
| 8 | +// |
| 9 | +// * Redistributions of source code must retain the above copyright |
| 10 | +// notice, this list of conditions and the following disclaimer. |
| 11 | +// * Redistributions in binary form must reproduce the above |
| 12 | +// copyright notice, this list of conditions and the following disclaimer |
| 13 | +// in the documentation and/or other materials provided with the |
| 14 | +// distribution. |
| 15 | +// * Neither the name of Google Inc. nor the names of its |
| 16 | +// contributors may be used to endorse or promote products derived from |
| 17 | +// this software without specific prior written permission. |
| 18 | +// |
| 19 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | + |
| 31 | +package com.google.protobuf.osgi; |
| 32 | + |
| 33 | +import aQute.bnd.osgi.Analyzer; |
| 34 | +import aQute.bnd.osgi.Jar; |
| 35 | +import java.io.File; |
| 36 | +import java.util.Arrays; |
| 37 | +import java.util.concurrent.Callable; |
| 38 | +import java.util.jar.Manifest; |
| 39 | +import java.util.stream.Collectors; |
| 40 | +import picocli.CommandLine; |
| 41 | +import picocli.CommandLine.Command; |
| 42 | +import picocli.CommandLine.Option; |
| 43 | + |
| 44 | +/** Java binary that runs bndlib to analyze a jar file to generate OSGi bundle manifest. */ |
| 45 | +@Command(name = "osgi_wrapper") |
| 46 | +public final class OsgiWrapper implements Callable<Integer> { |
| 47 | + private static final String REMOVEHEADERS = |
| 48 | + Arrays.stream( |
| 49 | + new String[] { |
| 50 | + "Embed-Dependency", |
| 51 | + "Embed-Transitive", |
| 52 | + "Built-By", |
| 53 | + // "Tool", |
| 54 | + "Created-By", |
| 55 | + // "Build-Jdk", |
| 56 | + "Originally-Created-By", |
| 57 | + "Archiver-Version", |
| 58 | + "Include-Resource", |
| 59 | + "Private-Package", |
| 60 | + "Ignore-Package", |
| 61 | + // "Bnd-LastModified", |
| 62 | + "Target-Label" |
| 63 | + }) |
| 64 | + .collect(Collectors.joining(",")); |
| 65 | + |
| 66 | + @Option( |
| 67 | + names = {"--input_jar"}, |
| 68 | + description = "The jar file to wrap with OSGi metadata") |
| 69 | + private File inputJar; |
| 70 | + |
| 71 | + @Option( |
| 72 | + names = {"--output_jar"}, |
| 73 | + description = "Output path to the wrapped jar") |
| 74 | + private File outputJar; |
| 75 | + |
| 76 | + @Option( |
| 77 | + names = {"--classpath"}, |
| 78 | + description = "The classpath that contains dependencies of the input jar, separated with :") |
| 79 | + private String classpath; |
| 80 | + |
| 81 | + @Option( |
| 82 | + names = {"--bundle_copyright"}, |
| 83 | + description = "Copyright string for the bundle") |
| 84 | + private String bundleCopyright; |
| 85 | + |
| 86 | + @Option( |
| 87 | + names = {"--bundle_description"}, |
| 88 | + description = "Description of the bundle") |
| 89 | + private String bundleDescription; |
| 90 | + |
| 91 | + @Option( |
| 92 | + names = {"--bundle_doc_url"}, |
| 93 | + description = "Documentation URL for the bundle") |
| 94 | + private String bundleDocUrl; |
| 95 | + |
| 96 | + @Option( |
| 97 | + names = {"--bundle_license"}, |
| 98 | + description = "URL for the license of the bundle") |
| 99 | + private String bundleLicense; |
| 100 | + |
| 101 | + @Option( |
| 102 | + names = {"--bundle_name"}, |
| 103 | + description = "The name of the bundle") |
| 104 | + private String bundleName; |
| 105 | + |
| 106 | + @Option( |
| 107 | + names = {"--bundle_symbolic_name"}, |
| 108 | + description = "The symbolic name of the bundle") |
| 109 | + private String bundleSymbolicName; |
| 110 | + |
| 111 | + @Option( |
| 112 | + names = {"--bundle_version"}, |
| 113 | + description = "The version of the bundle") |
| 114 | + private String bundleVersion; |
| 115 | + |
| 116 | + @Option( |
| 117 | + names = {"--export_package"}, |
| 118 | + description = "The exported packages from this bundle") |
| 119 | + private String exportPackage; |
| 120 | + |
| 121 | + @Option( |
| 122 | + names = {"--import_package"}, |
| 123 | + description = "The imported packages from this bundle") |
| 124 | + private String importPackage; |
| 125 | + |
| 126 | + @Override |
| 127 | + public Integer call() throws Exception { |
| 128 | + Jar bin = new Jar(inputJar); |
| 129 | + |
| 130 | + Analyzer analyzer = new Analyzer(); |
| 131 | + analyzer.setJar(bin); |
| 132 | + analyzer.setProperty(Analyzer.BUNDLE_NAME, bundleName); |
| 133 | + analyzer.setProperty(Analyzer.BUNDLE_SYMBOLICNAME, bundleSymbolicName); |
| 134 | + analyzer.setProperty(Analyzer.BUNDLE_VERSION, bundleVersion); |
| 135 | + analyzer.setProperty(Analyzer.IMPORT_PACKAGE, importPackage); |
| 136 | + analyzer.setProperty(Analyzer.EXPORT_PACKAGE, exportPackage); |
| 137 | + analyzer.setProperty(Analyzer.BUNDLE_DESCRIPTION, bundleDescription); |
| 138 | + analyzer.setProperty(Analyzer.BUNDLE_COPYRIGHT, bundleCopyright); |
| 139 | + analyzer.setProperty(Analyzer.BUNDLE_DOCURL, bundleDocUrl); |
| 140 | + analyzer.setProperty(Analyzer.BUNDLE_LICENSE, bundleLicense); |
| 141 | + analyzer.setProperty(Analyzer.REMOVEHEADERS, REMOVEHEADERS); |
| 142 | + |
| 143 | + if (classpath != null) { |
| 144 | + for (String dep : Arrays.asList(classpath.split(":"))) { |
| 145 | + analyzer.addClasspath(new File(dep)); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + analyzer.analyze(); |
| 150 | + |
| 151 | + Manifest manifest = analyzer.calcManifest(); |
| 152 | + |
| 153 | + if (analyzer.isOk()) { |
| 154 | + analyzer.getJar().setManifest(manifest); |
| 155 | + if (analyzer.save(outputJar, true)) { |
| 156 | + return 0; |
| 157 | + } |
| 158 | + } |
| 159 | + return 1; |
| 160 | + } |
| 161 | + |
| 162 | + public static void main(String[] args) { |
| 163 | + int exitCode = new CommandLine(new OsgiWrapper()).execute(args); |
| 164 | + System.exit(exitCode); |
| 165 | + } |
| 166 | +} |
0 commit comments