summaryrefslogtreecommitdiffstats
path: root/jdisc_core/src/main/java/com/yahoo/jdisc/core/ExportPackages.java
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
committerJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
commit72231250ed81e10d66bfe70701e64fa5fe50f712 (patch)
tree2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /jdisc_core/src/main/java/com/yahoo/jdisc/core/ExportPackages.java
Publish
Diffstat (limited to 'jdisc_core/src/main/java/com/yahoo/jdisc/core/ExportPackages.java')
-rw-r--r--jdisc_core/src/main/java/com/yahoo/jdisc/core/ExportPackages.java98
1 files changed, 98 insertions, 0 deletions
diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/core/ExportPackages.java b/jdisc_core/src/main/java/com/yahoo/jdisc/core/ExportPackages.java
new file mode 100644
index 00000000000..afe43718bc5
--- /dev/null
+++ b/jdisc_core/src/main/java/com/yahoo/jdisc/core/ExportPackages.java
@@ -0,0 +1,98 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.jdisc.core;
+
+import com.yahoo.container.plugin.bundle.AnalyzeBundle;
+import com.yahoo.container.plugin.bundle.TransformExportPackages;
+import com.yahoo.container.plugin.osgi.ExportPackages.Export;
+import org.apache.felix.framework.util.Util;
+import org.osgi.framework.Constants;
+import scala.collection.immutable.List;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.Properties;
+import java.util.jar.JarInputStream;
+
+/**
+ * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen Hult</a>
+ */
+public class ExportPackages {
+
+ public static final String PROPERTIES_FILE = "/exportPackages.properties";
+ public static final String EXPORT_PACKAGES = "exportPackages";
+ private static final String REPLACE_VERSION_PREFIX = "__REPLACE_VERSION__";
+
+ public static void main(String[] args) throws IOException {
+ String fileName = args[0];
+ if (!fileName.endsWith(PROPERTIES_FILE)) {
+ throw new IllegalArgumentException("Expected '" + PROPERTIES_FILE + "', got '" + fileName + "'.");
+ }
+ StringBuilder out = new StringBuilder();
+ out.append(getSystemPackages()).append(",")
+ .append("com.sun.security.auth,")
+ .append("com.sun.security.auth.module,")
+ .append("com.sun.management,")
+ .append("com.yahoo.jdisc,")
+ .append("com.yahoo.jdisc.application,")
+ .append("com.yahoo.jdisc.handler,")
+ .append("com.yahoo.jdisc.service,")
+ .append("javax.inject;version=1.0.0,") // Included in guice, but not exported. Needed by container-jersey.
+ .append("org.aopalliance.intercept,")
+ .append("org.aopalliance.aop,")
+ .append("org.w3c.dom.css,")
+ .append("org.w3c.dom.html,")
+ .append("org.w3c.dom.ranges,")
+ .append("org.w3c.dom.stylesheets,")
+ .append("org.w3c.dom.traversal,")
+ .append("org.w3c.dom.views,")
+ .append("sun.misc,")
+ .append("sun.net.util,")
+ .append("sun.security.krb5");
+ for (int i = 1; i < args.length; ++i) {
+ out.append(",").append(getExportedPackages(args[i]));
+ }
+ Properties props = new Properties();
+ props.setProperty(EXPORT_PACKAGES, out.toString());
+
+ try (FileWriter writer = new FileWriter(new File(fileName))) {
+ props.store(writer, "generated by " + ExportPackages.class.getName());
+ }
+ }
+
+ public static String readExportProperty() {
+ Properties props = new Properties();
+ try {
+ props.load(ExportPackages.class.getResourceAsStream(PROPERTIES_FILE));
+ } catch (IOException e) {
+ throw new IllegalStateException("Failed to read resource '" + PROPERTIES_FILE + "'.");
+ }
+ return props.getProperty(EXPORT_PACKAGES);
+ }
+
+ public static String getSystemPackages() {
+ return Util.getDefaultProperty(null, "org.osgi.framework.system.packages");
+ }
+
+ private static String getExportedPackages(String argument) throws IOException {
+ if (argument.startsWith(REPLACE_VERSION_PREFIX)) {
+ String jarFile = argument.substring(REPLACE_VERSION_PREFIX.length());
+ return readExportHeader(jarFile);
+ } else {
+ return readExportHeader(argument);
+ }
+ }
+
+ private static String readExportHeader(String jarFile) throws IOException {
+ try (JarInputStream jar = new JarInputStream(new FileInputStream(jarFile))) {
+ return jar.getManifest().getMainAttributes().getValue(Constants.EXPORT_PACKAGE);
+ }
+ }
+
+ private static String transformExports(List<Export> exports, String newVersion) {
+ return TransformExportPackages.toExportPackageProperty(
+ TransformExportPackages.removeUses(
+ TransformExportPackages.replaceVersions(exports, newVersion)));
+ }
+}