From f522faff3b803b800b532ccdb0a6f1433ad4f3e7 Mon Sep 17 00:00:00 2001 From: Eike Stepper Date: Sat, 15 Jun 2024 08:25:57 +0200 Subject: [PATCH] Prototype an LMImporter #52 --- .../cdo/lm/internal/client/LMImporter.java | 70 ++++++++++++++++++- .../emf/cdo/tests/lm/LMImporterTest.java | 29 +++++++- .../src/org/eclipse/net4j/util/io/IOUtil.java | 35 ++++++---- 3 files changed, 116 insertions(+), 18 deletions(-) diff --git a/plugins/org.eclipse.emf.cdo.lm.client/src/org/eclipse/emf/cdo/lm/internal/client/LMImporter.java b/plugins/org.eclipse.emf.cdo.lm.client/src/org/eclipse/emf/cdo/lm/internal/client/LMImporter.java index 181c78339b..6da6bd11be 100644 --- a/plugins/org.eclipse.emf.cdo.lm.client/src/org/eclipse/emf/cdo/lm/internal/client/LMImporter.java +++ b/plugins/org.eclipse.emf.cdo.lm.client/src/org/eclipse/emf/cdo/lm/internal/client/LMImporter.java @@ -49,6 +49,7 @@ import org.eclipse.net4j.util.collection.CollectionUtil; import org.eclipse.net4j.util.collection.Pair; import org.eclipse.net4j.util.io.IORuntimeException; +import org.eclipse.net4j.util.io.IOUtil; import org.eclipse.net4j.util.lifecycle.LifecycleUtil; import org.eclipse.emf.common.util.EList; @@ -74,6 +75,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; +import java.io.StringReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -597,9 +599,10 @@ public UnaryOperator getContentsModifier() return contentsModifier; } - public void setContentsModifier(UnaryOperator contentsModifier) + public ImportLeaf setContentsModifier(UnaryOperator contentsModifier) { this.contentsModifier = contentsModifier; + return this; } public String getCopyPath() @@ -607,9 +610,10 @@ public String getCopyPath() return copyPath; } - public void setCopyPath(String copyPath) + public ImportLeaf setCopyPath(String copyPath) { this.copyPath = copyPath; + return this; } } @@ -635,6 +639,18 @@ public Resource getResource() { return resource; } + + @Override + public ImportResource setContentsModifier(UnaryOperator> contentsModifier) + { + return (ImportResource)super.setContentsModifier(contentsModifier); + } + + @Override + public ImportResource setCopyPath(String copyPath) + { + return (ImportResource)super.setCopyPath(copyPath); + } } /** @@ -652,6 +668,18 @@ public Type getType() { return Type.BINARY; } + + @Override + public ImportBinary setContentsModifier(UnaryOperator contentsModifier) + { + return (ImportBinary)super.setContentsModifier(contentsModifier); + } + + @Override + public ImportBinary setCopyPath(String copyPath) + { + return (ImportBinary)super.setCopyPath(copyPath); + } } /** @@ -677,6 +705,44 @@ public String getEncoding() { return encoding; } + + @Override + public ImportText setContentsModifier(UnaryOperator contentsModifier) + { + return (ImportText)super.setContentsModifier(contentsModifier); + } + + public ImportText setStringContentsModifier(UnaryOperator stringModifier) + { + return (ImportText)super.setContentsModifier(new StringContentsModifier(stringModifier)); + } + + @Override + public ImportText setCopyPath(String copyPath) + { + return (ImportText)super.setCopyPath(copyPath); + } + } + + /** + * @author Eike Stepper + */ + public static final class StringContentsModifier implements UnaryOperator + { + private final UnaryOperator stringModifier; + + public StringContentsModifier(UnaryOperator stringModifier) + { + this.stringModifier = stringModifier; + } + + @Override + public Reader apply(Reader in) + { + String text = IOUtil.readText(in); + text = stringModifier.apply(text); + return new StringReader(text); + } } } diff --git a/plugins/org.eclipse.emf.cdo.tests.lm/src/org/eclipse/emf/cdo/tests/lm/LMImporterTest.java b/plugins/org.eclipse.emf.cdo.tests.lm/src/org/eclipse/emf/cdo/tests/lm/LMImporterTest.java index f684a9fd6a..36aa0d8151 100644 --- a/plugins/org.eclipse.emf.cdo.tests.lm/src/org/eclipse/emf/cdo/tests/lm/LMImporterTest.java +++ b/plugins/org.eclipse.emf.cdo.tests.lm/src/org/eclipse/emf/cdo/tests/lm/LMImporterTest.java @@ -10,10 +10,14 @@ */ package org.eclipse.emf.cdo.tests.lm; +import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.startsWith; import static org.hamcrest.MatcherAssert.assertThat; +import org.eclipse.emf.cdo.common.lob.CDOClob; import org.eclipse.emf.cdo.eresource.CDOResource; +import org.eclipse.emf.cdo.eresource.CDOTextResource; import org.eclipse.emf.cdo.explorer.checkouts.CDOCheckout; import org.eclipse.emf.cdo.lm.Module; import org.eclipse.emf.cdo.lm.Stream; @@ -23,11 +27,14 @@ import org.eclipse.emf.cdo.lm.internal.client.LMImporter; import org.eclipse.emf.cdo.lm.internal.client.LMImporter.ImportModule; import org.eclipse.emf.cdo.lm.internal.client.LMImporter.ImportResolution; +import org.eclipse.emf.cdo.tests.lm.bundle.OM; import org.eclipse.emf.cdo.tests.model1.Category; import org.eclipse.emf.cdo.tests.model1.Model1Factory; import org.eclipse.emf.cdo.tests.model1.Product1; import org.eclipse.emf.cdo.view.CDOView; +import org.eclipse.net4j.util.io.IOUtil; + import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.ResourceSet; @@ -35,7 +42,9 @@ import org.eclipse.emf.ecore.xmi.impl.XMLResourceFactoryImpl; import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; import java.util.List; /** @@ -67,14 +76,23 @@ public void testModuleImport() throws Exception assertThat(modules.get(1).getName(), is(ext1.getName())); assertThat(modules.get(2).getName(), is(ext2.getName())); - // Use a "base" module checkout to verify that the custom copy paths are respected. + // Create a "base" module checkout. Stream baseStream = modules.get(0).getStreams().get(0); IAssemblyDescriptor baseDescriptor = IAssemblyManager.INSTANCE.createDescriptor("base assembly descriptor", baseStream, monitor()); CDOCheckout baseCheckout = baseDescriptor.getCheckout(); CDOView baseView = baseCheckout.openView(true); + + // Verify that the custom copy paths are respected. CDOResource baseResource = baseView.getResource("moved/renamed.xml"); // Use the custom path. Category baseCategory = (Category)baseResource.getContents().get(0); assertThat(baseCategory.getName(), is("base products")); + + // Verify that text resources are imported and their contents are modified. + CDOTextResource baseText = baseView.getTextResource("manifest.txt"); + CDOClob baseClob = baseText.getContents(); + String baseString = baseClob.getString(); + assertThat(baseString, startsWith("Manifest-Version: 1.0")); + assertThat(baseString, containsString("CDO.server.net4j")); } private ImportModule createTestModule(LMImporter importer, ResourceSet resourceSet, String name, String... dependencies) throws IOException @@ -105,8 +123,17 @@ private ImportModule createTestModule(LMImporter importer, ResourceSet resourceS resource.save(null); + try (InputStream in = OM.BUNDLE.getInputStream("META-INF/MANIFEST.MF"); + FileOutputStream out = IOUtil.openOutputStream(rootURI.appendSegment("MANIFEST.MF").toFileString())) + { + IOUtil.copy(in, out); + } + ImportModule module = importer.addModule(name, rootURI); module.addResource("model.xml").setCopyPath("moved/renamed.xml"); + module.addText("MANIFEST.MF", "UTF-8").setCopyPath("manifest.txt") // + .setStringContentsModifier(str -> str.replaceAll("org\\.eclipse\\.emf\\.cdo", "CDO")); + return module; } diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/IOUtil.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/IOUtil.java index b1e8c9e16a..6a0c2ac2a0 100644 --- a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/IOUtil.java +++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/IOUtil.java @@ -741,6 +741,23 @@ public static void copyFile(File source, File target) throws IORuntimeException } } + /** + * @since 3.25 + */ + public static String readText(Reader input) throws IORuntimeException + { + try + { + CharArrayWriter output = new CharArrayWriter(); + copyCharacter(input, output); + return output.toString(); + } + catch (IOException ex) + { + throw new IORuntimeException(ex); + } + } + /** * @since 3.4 */ @@ -766,7 +783,7 @@ public static String readText(URL url) throws IORuntimeException charset = Charset.forName(encoding); } - input = new InputStreamReader(connection.getInputStream(), charset); + input = new BufferedReader(new InputStreamReader(connection.getInputStream(), charset)); } catch (IOException ex) { @@ -775,13 +792,7 @@ public static String readText(URL url) throws IORuntimeException try { - CharArrayWriter output = new CharArrayWriter(); - copyCharacter(input, output); - return output.toString(); - } - catch (IOException ex) - { - throw new IORuntimeException(ex); + return readText(input); } finally { @@ -798,13 +809,7 @@ public static String readTextFile(File file) throws IORuntimeException try { - CharArrayWriter output = new CharArrayWriter(); - copyCharacter(input, output); - return output.toString(); - } - catch (IOException ex) - { - throw new IORuntimeException(ex); + return readText(input); } finally {