summaryrefslogtreecommitdiffstats
path: root/vespajlib
diff options
context:
space:
mode:
authorBjørn Christian Seime <bjorncs@yahoo-inc.com>2017-07-19 13:40:35 +0200
committerBjørn Christian Seime <bjorncs@yahoo-inc.com>2017-07-20 13:15:21 +0200
commit43298ad59815c9731e0d2f0cd8604af08a119021 (patch)
treed8b3a52ec2d618e66f107181a5c791ba63ec5b07 /vespajlib
parent94e3449f4b57647b199a03e2e59f91ee902b35eb (diff)
Rewrite ProjectBundleClassPaths to Java
Diffstat (limited to 'vespajlib')
-rw-r--r--vespajlib/src/main/java/com/yahoo/osgi/maven/ProjectBundleClassPaths.java130
-rw-r--r--vespajlib/src/test/java/com/yahoo/osgi/maven/ProjectBundleClassPathsTest.java35
2 files changed, 165 insertions, 0 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/osgi/maven/ProjectBundleClassPaths.java b/vespajlib/src/main/java/com/yahoo/osgi/maven/ProjectBundleClassPaths.java
new file mode 100644
index 00000000000..92ba8b79572
--- /dev/null
+++ b/vespajlib/src/main/java/com/yahoo/osgi/maven/ProjectBundleClassPaths.java
@@ -0,0 +1,130 @@
+// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.osgi.maven;
+
+import com.yahoo.slime.Cursor;
+import com.yahoo.slime.Inspector;
+import com.yahoo.slime.JsonDecoder;
+import com.yahoo.slime.JsonFormat;
+import com.yahoo.slime.Slime;
+
+import java.io.BufferedOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Represents the bundles in a maven project and the classpath elements
+ * corresponding to code that would end up in the bundle.
+ *
+ * @author tonytv
+ * @author bjorncs
+ */
+
+public class ProjectBundleClassPaths {
+ public static final String CLASSPATH_MAPPINGS_FILENAME = "bundle-plugin.bundle-classpath-mappings.json";
+
+ public final BundleClasspathMapping mainBundle;
+ public final List<BundleClasspathMapping> providedDependencies;
+
+ public ProjectBundleClassPaths(BundleClasspathMapping mainBundle,
+ List<BundleClasspathMapping> providedDependencies) {
+ this.mainBundle = mainBundle;
+ this.providedDependencies = providedDependencies;
+ }
+
+ public static void save(Path path, ProjectBundleClassPaths mappings) throws IOException {
+ Files.createDirectories(path.getParent());
+ try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(path))) {
+ save(out, mappings);
+ }
+ }
+
+ static void save(OutputStream out, ProjectBundleClassPaths mappings) throws IOException {
+ Slime slime = new Slime();
+ Cursor rootCursor = slime.setObject();
+ Cursor mainBundleCursor = rootCursor.setObject("mainBundle");
+ BundleClasspathMapping.save(mainBundleCursor, mappings.mainBundle);
+ Cursor dependenciesCursor = rootCursor.setArray("providedDependencies");
+ mappings.providedDependencies
+ .forEach(d -> BundleClasspathMapping.save(dependenciesCursor.addObject(), d));
+ new JsonFormat(false).encode(out, slime);
+ }
+
+ public static ProjectBundleClassPaths load(Path path) throws IOException {
+ byte[] bytes = Files.readAllBytes(path);
+ return load(bytes);
+ }
+
+ static ProjectBundleClassPaths load(byte[] bytes) {
+ Slime slime = new Slime();
+ new JsonDecoder().decode(slime, bytes);
+ Inspector inspector = slime.get();
+ BundleClasspathMapping mainBundle = BundleClasspathMapping.load(inspector.field("mainBundle"));
+ Inspector dependenciesInspector = inspector.field("providedDependencies");
+ List<BundleClasspathMapping> providedDependencies = new ArrayList<>();
+ for (int i = 0; i < dependenciesInspector.entries(); i++) {
+ providedDependencies.add(BundleClasspathMapping.load(dependenciesInspector.entry(i)));
+ }
+ return new ProjectBundleClassPaths(mainBundle, providedDependencies);
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ ProjectBundleClassPaths that = (ProjectBundleClassPaths) o;
+ return Objects.equals(mainBundle, that.mainBundle) &&
+ Objects.equals(providedDependencies, that.providedDependencies);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(mainBundle, providedDependencies);
+ }
+
+ public static class BundleClasspathMapping {
+ public final String bundleSymbolicName;
+ public final List<String> classPathElements;
+
+ public BundleClasspathMapping(String bundleSymbolicName,
+ List<String> classPathElements) {
+ this.bundleSymbolicName = bundleSymbolicName;
+ this.classPathElements = classPathElements;
+ }
+
+ static void save(Cursor rootCursor, BundleClasspathMapping mapping) {
+ rootCursor.setString("bundleSymbolicName", mapping.bundleSymbolicName);
+ Cursor arrayCursor = rootCursor.setArray("classPathElements");
+ mapping.classPathElements.forEach(arrayCursor::addString);
+ }
+
+ static BundleClasspathMapping load(Inspector inspector) {
+ String bundleSymoblicName = inspector.field("bundleSymbolicName").asString();
+ Inspector elementsInspector = inspector.field("classPathElements");
+ List<String> classPathElements = new ArrayList<>();
+ for (int i = 0; i < elementsInspector.entries(); i++) {
+ classPathElements.add(elementsInspector.entry(i).asString());
+ }
+ return new BundleClasspathMapping(bundleSymoblicName, classPathElements);
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ BundleClasspathMapping that = (BundleClasspathMapping) o;
+ return Objects.equals(bundleSymbolicName, that.bundleSymbolicName) &&
+ Objects.equals(classPathElements, that.classPathElements);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(bundleSymbolicName, classPathElements);
+ }
+ }
+
+}
diff --git a/vespajlib/src/test/java/com/yahoo/osgi/maven/ProjectBundleClassPathsTest.java b/vespajlib/src/test/java/com/yahoo/osgi/maven/ProjectBundleClassPathsTest.java
new file mode 100644
index 00000000000..9bb66afd876
--- /dev/null
+++ b/vespajlib/src/test/java/com/yahoo/osgi/maven/ProjectBundleClassPathsTest.java
@@ -0,0 +1,35 @@
+package com.yahoo.osgi.maven;
+
+import com.yahoo.osgi.maven.ProjectBundleClassPaths.BundleClasspathMapping;
+import org.junit.Test;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+import static java.util.Arrays.asList;
+import static org.junit.Assert.assertEquals;
+
+/**
+ * @author bjorncs
+ */
+public class ProjectBundleClassPathsTest {
+
+ @Test
+ public void bundle_classpaths_serializes_correctly_to_json() throws IOException {
+ ProjectBundleClassPaths projectBundleClassPaths =
+ new ProjectBundleClassPaths(
+ new BundleClasspathMapping("main-bundle-name", asList("classpath-elem-0-1", "classpath-elem-0-2")),
+ asList(
+ new BundleClasspathMapping(
+ "main-bundle-dep1",
+ asList("classpath-elem-1-1", "classpath-elem-1-2")),
+ new BundleClasspathMapping(
+ "main-bundle-dep2",
+ asList("classpath-elem-2-1", "classpath-elem-2-2"))));
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ ProjectBundleClassPaths.save(out, projectBundleClassPaths);
+ ProjectBundleClassPaths deserialized = ProjectBundleClassPaths.load(out.toByteArray());
+ assertEquals(projectBundleClassPaths, deserialized);
+ }
+
+}