Skip to content

Commit

Permalink
Prototype an LMImporter eclipse-cdo#52
Browse files Browse the repository at this point in the history
  • Loading branch information
estepper committed Jun 15, 2024
1 parent b9424c3 commit f522faf
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -597,19 +599,21 @@ public UnaryOperator<CONTENTS> getContentsModifier()
return contentsModifier;
}

public void setContentsModifier(UnaryOperator<CONTENTS> contentsModifier)
public ImportLeaf<CONTENTS> setContentsModifier(UnaryOperator<CONTENTS> contentsModifier)
{
this.contentsModifier = contentsModifier;
return this;
}

public String getCopyPath()
{
return copyPath;
}

public void setCopyPath(String copyPath)
public ImportLeaf<CONTENTS> setCopyPath(String copyPath)
{
this.copyPath = copyPath;
return this;
}
}

Expand All @@ -635,6 +639,18 @@ public Resource getResource()
{
return resource;
}

@Override
public ImportResource setContentsModifier(UnaryOperator<EList<EObject>> contentsModifier)
{
return (ImportResource)super.setContentsModifier(contentsModifier);
}

@Override
public ImportResource setCopyPath(String copyPath)
{
return (ImportResource)super.setCopyPath(copyPath);
}
}

/**
Expand All @@ -652,6 +668,18 @@ public Type getType()
{
return Type.BINARY;
}

@Override
public ImportBinary setContentsModifier(UnaryOperator<InputStream> contentsModifier)
{
return (ImportBinary)super.setContentsModifier(contentsModifier);
}

@Override
public ImportBinary setCopyPath(String copyPath)
{
return (ImportBinary)super.setCopyPath(copyPath);
}
}

/**
Expand All @@ -677,6 +705,44 @@ public String getEncoding()
{
return encoding;
}

@Override
public ImportText setContentsModifier(UnaryOperator<Reader> contentsModifier)
{
return (ImportText)super.setContentsModifier(contentsModifier);
}

public ImportText setStringContentsModifier(UnaryOperator<String> 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<Reader>
{
private final UnaryOperator<String> stringModifier;

public StringContentsModifier(UnaryOperator<String> stringModifier)
{
this.stringModifier = stringModifier;
}

@Override
public Reader apply(Reader in)
{
String text = IOUtil.readText(in);
text = stringModifier.apply(text);
return new StringReader(text);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,19 +27,24 @@
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;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
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;

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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)
{
Expand All @@ -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
{
Expand All @@ -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
{
Expand Down

0 comments on commit f522faf

Please sign in to comment.