Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Stepper2012-09-07 08:12:05 +0000
committerEike Stepper2012-09-07 08:12:05 +0000
commitc26b4d70ad02851641b1fc94e8b5f23a56ab376e (patch)
tree021dc3f77c48485dcd0026dbc25e522046124962 /plugins
parent4dcf5a50faa210568a06dea53de9ec3b12cb2886 (diff)
downloadcdo-c26b4d70ad02851641b1fc94e8b5f23a56ab376e.tar.gz
cdo-c26b4d70ad02851641b1fc94e8b5f23a56ab376e.tar.xz
cdo-c26b4d70ad02851641b1fc94e8b5f23a56ab376e.zip
[388928] Develop a resource transfer framework
https://bugs.eclipse.org/bugs/show_bug.cgi?id=388928
Diffstat (limited to 'plugins')
-rw-r--r--plugins/org.eclipse.emf.cdo.transfer/src/org/eclipse/emf/cdo/transfer/CDOTransfer.java203
-rw-r--r--plugins/org.eclipse.emf.cdo.transfer/src/org/eclipse/emf/cdo/transfer/CDOTransferMappingImpl.java8
-rw-r--r--plugins/org.eclipse.emf.cdo.transfer/src/org/eclipse/emf/cdo/transfer/TransferTest.java13
3 files changed, 142 insertions, 82 deletions
diff --git a/plugins/org.eclipse.emf.cdo.transfer/src/org/eclipse/emf/cdo/transfer/CDOTransfer.java b/plugins/org.eclipse.emf.cdo.transfer/src/org/eclipse/emf/cdo/transfer/CDOTransfer.java
index b3fb4273e6..da024157ec 100644
--- a/plugins/org.eclipse.emf.cdo.transfer/src/org/eclipse/emf/cdo/transfer/CDOTransfer.java
+++ b/plugins/org.eclipse.emf.cdo.transfer/src/org/eclipse/emf/cdo/transfer/CDOTransfer.java
@@ -56,11 +56,7 @@ public class CDOTransfer implements INotifier
private CDOTransferType defaultTransferType = CDOTransferType.UNKNOWN;
- private ResourceSet sourceResourceSet;
-
- private ResourceSet targetResourceSet;
-
- private Set<Resource> resourcesToSave = new HashSet<Resource>();
+ private ModelTransferContext modelTransferContext = new ModelTransferContext();
public CDOTransfer(CDOTransferSystem sourceSystem, CDOTransferSystem targetSystem)
{
@@ -98,32 +94,9 @@ public class CDOTransfer implements INotifier
return targetSystem;
}
- public final ResourceSet getSourceResourceSet()
+ public ModelTransferContext getModelTransferContext()
{
- if (sourceResourceSet == null)
- {
- sourceResourceSet = sourceSystem.provideResourceSet();
- if (sourceResourceSet == null)
- {
- sourceResourceSet = createResourceSet(sourceSystem);
- }
- }
-
- return sourceResourceSet;
- }
-
- public final ResourceSet getTargetResourceSet()
- {
- if (targetResourceSet == null)
- {
- targetResourceSet = targetSystem.provideResourceSet();
- if (targetResourceSet == null)
- {
- targetResourceSet = createResourceSet(targetSystem);
- }
- }
-
- return targetResourceSet;
+ return modelTransferContext;
}
protected ResourceSet createResourceSet(CDOTransferSystem system)
@@ -232,20 +205,12 @@ public class CDOTransfer implements INotifier
mapping.getChildren();
}
- protected boolean hasResourceFactory(CDOTransferElement source)
- {
- URI uri = source.getURI();
- // TODO Derive resourceSet from element.getSystem()?
- Registry registry = getSourceResourceSet().getResourceFactoryRegistry();
- return registry.getFactory(uri) != null;
- }
-
protected CDOTransferType getTransferType(CDOTransferElement source)
{
CDOTransferType type = source.getSystem().getDefaultTransferType(source);
if (type == CDOTransferType.UNKNOWN)
{
- if (hasResourceFactory(source))
+ if (modelTransferContext.hasResourceFactory(source))
{
return CDOTransferType.MODEL;
}
@@ -259,20 +224,6 @@ public class CDOTransfer implements INotifier
return type;
}
- protected Resource getSourceResource(CDOTransferMapping mapping)
- {
- URI uri = mapping.getSource().getURI();
- ResourceSet sourceResourceSet = getSourceResourceSet();
- return sourceResourceSet.getResource(uri, true);
- }
-
- protected Resource getTargetResource(CDOTransferMapping mapping) throws IOException
- {
- IPath path = mapping.getFullPath();
- ResourceSet targetResourceSet = getTargetResourceSet();
- return targetSystem.createModel(targetResourceSet, path);
- }
-
protected void validate(CDOTransferMapping mapping)
{
if (mapping.getStatus() == CDOTransferMapping.Status.CONFLICT)
@@ -290,13 +241,7 @@ public class CDOTransfer implements INotifier
{
validate(rootMapping);
perform(rootMapping);
-
- for (Resource resource : resourcesToSave)
- {
- resource.save(null);
- }
-
- resourcesToSave.clear();
+ modelTransferContext.save();
}
protected void perform(CDOTransferMapping mapping) throws IOException
@@ -336,15 +281,7 @@ public class CDOTransfer implements INotifier
protected void performModel(CDOTransferMapping mapping) throws IOException
{
- Resource sourceResource = getSourceResource(mapping);
- Resource targetResource = getTargetResource(mapping);
-
- EList<EObject> sourceContents = sourceResource.getContents();
- Collection<EObject> targetContents = EcoreUtil.copyAll(sourceContents);
-
- EList<EObject> contents = targetResource.getContents();
- contents.addAll(targetContents);
- resourcesToSave.add(targetResource);
+ modelTransferContext.perform(mapping);
}
protected void performBinary(CDOTransferMapping mapping) throws IOException
@@ -380,6 +317,134 @@ public class CDOTransfer implements INotifier
/**
* @author Eike Stepper
*/
+ public class ModelTransferContext
+ {
+ private ResourceSet sourceResourceSet;
+
+ private ResourceSet targetResourceSet;
+
+ private Map<CDOTransferElement, Resource> resources = new HashMap<CDOTransferElement, Resource>();
+
+ private Set<Resource> resourcesToSave = new HashSet<Resource>();
+
+ protected ModelTransferContext()
+ {
+ }
+
+ public final ResourceSet getSourceResourceSet()
+ {
+ if (sourceResourceSet == null)
+ {
+ sourceResourceSet = sourceSystem.provideResourceSet();
+ if (sourceResourceSet == null)
+ {
+ sourceResourceSet = createResourceSet(sourceSystem);
+ }
+ }
+
+ return sourceResourceSet;
+ }
+
+ public final ResourceSet getTargetResourceSet()
+ {
+ if (targetResourceSet == null)
+ {
+ targetResourceSet = targetSystem.provideResourceSet();
+ if (targetResourceSet == null)
+ {
+ targetResourceSet = createResourceSet(targetSystem);
+ }
+ }
+
+ return targetResourceSet;
+ }
+
+ public void registerSourceExtension(String extension, Resource.Factory factory)
+ {
+ Map<String, Object> map = getSourceResourceSet().getResourceFactoryRegistry().getExtensionToFactoryMap();
+ map.put(extension, factory);
+ }
+
+ public void registerSourceProtocol(String protocol, Resource.Factory factory)
+ {
+ Map<String, Object> map = getSourceResourceSet().getResourceFactoryRegistry().getProtocolToFactoryMap();
+ map.put(protocol, factory);
+ }
+
+ public void registerSourceContentType(String contentType, Resource.Factory factory)
+ {
+ Map<String, Object> map = getSourceResourceSet().getResourceFactoryRegistry().getContentTypeToFactoryMap();
+ map.put(contentType, factory);
+ }
+
+ public void registerTargetExtension(String extension, Resource.Factory factory)
+ {
+ Map<String, Object> map = getTargetResourceSet().getResourceFactoryRegistry().getExtensionToFactoryMap();
+ map.put(extension, factory);
+ }
+
+ public void registerTargetProtocol(String protocol, Resource.Factory factory)
+ {
+ Map<String, Object> map = getTargetResourceSet().getResourceFactoryRegistry().getProtocolToFactoryMap();
+ map.put(protocol, factory);
+ }
+
+ public void registerTargetContentType(String contentType, Resource.Factory factory)
+ {
+ Map<String, Object> map = getTargetResourceSet().getResourceFactoryRegistry().getContentTypeToFactoryMap();
+ map.put(contentType, factory);
+ }
+
+ protected Resource getSourceResource(CDOTransferMapping mapping)
+ {
+ URI uri = mapping.getSource().getURI();
+ ResourceSet sourceResourceSet = getSourceResourceSet();
+ return sourceResourceSet.getResource(uri, true);
+ }
+
+ protected Resource getTargetResource(CDOTransferMapping mapping) throws IOException
+ {
+ IPath path = mapping.getFullPath();
+ ResourceSet targetResourceSet = getTargetResourceSet();
+ return targetSystem.createModel(targetResourceSet, path);
+ }
+
+ protected boolean hasResourceFactory(CDOTransferElement source)
+ {
+ URI uri = source.getURI();
+ // TODO Derive resourceSet from element.getSystem()?
+ Registry registry = getSourceResourceSet().getResourceFactoryRegistry();
+ return registry.getFactory(uri) != null;
+ }
+
+ protected void perform(CDOTransferMapping mapping) throws IOException
+ {
+ Resource sourceResource = getSourceResource(mapping);
+ Resource targetResource = getTargetResource(mapping);
+
+ EList<EObject> sourceContents = sourceResource.getContents();
+ Collection<EObject> targetContents = EcoreUtil.copyAll(sourceContents);
+
+ EList<EObject> contents = targetResource.getContents();
+ contents.addAll(targetContents);
+ resourcesToSave.add(targetResource);
+ }
+
+ protected void save() throws IOException
+ {
+ for (Resource resource : resourcesToSave)
+ {
+ resource.save(null);
+ }
+
+ resourcesToSave.clear();
+ }
+
+ }
+
+ /**
+ * @author Eike Stepper
+ */
public static abstract class MappingEvent extends Event
{
private static final long serialVersionUID = 1L;
diff --git a/plugins/org.eclipse.emf.cdo.transfer/src/org/eclipse/emf/cdo/transfer/CDOTransferMappingImpl.java b/plugins/org.eclipse.emf.cdo.transfer/src/org/eclipse/emf/cdo/transfer/CDOTransferMappingImpl.java
index 8510054ca3..438764e016 100644
--- a/plugins/org.eclipse.emf.cdo.transfer/src/org/eclipse/emf/cdo/transfer/CDOTransferMappingImpl.java
+++ b/plugins/org.eclipse.emf.cdo.transfer/src/org/eclipse/emf/cdo/transfer/CDOTransferMappingImpl.java
@@ -240,10 +240,10 @@ class CDOTransferMappingImpl implements CDOTransferMapping
{
if (!ObjectUtil.equals(this.transferType, transferType))
{
- if (transferType == CDOTransferType.MODEL && !transfer.hasResourceFactory(source))
- {
- throw new IllegalStateException("No resource factory registered for " + this);
- }
+ // if (transferType == CDOTransferType.MODEL && !transfer.getModelTransferContext().hasResourceFactory(source))
+ // {
+ // throw new IllegalStateException("No resource factory registered for " + this);
+ // }
CDOTransferType oldType = this.transferType;
this.transferType = transferType;
diff --git a/plugins/org.eclipse.emf.cdo.transfer/src/org/eclipse/emf/cdo/transfer/TransferTest.java b/plugins/org.eclipse.emf.cdo.transfer/src/org/eclipse/emf/cdo/transfer/TransferTest.java
index c7a7fbe74a..d3a41c90d5 100644
--- a/plugins/org.eclipse.emf.cdo.transfer/src/org/eclipse/emf/cdo/transfer/TransferTest.java
+++ b/plugins/org.eclipse.emf.cdo.transfer/src/org/eclipse/emf/cdo/transfer/TransferTest.java
@@ -15,7 +15,6 @@ import org.eclipse.emf.cdo.spi.transfer.FileSystemTransferSystem;
import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl;
import java.io.IOException;
-import java.util.Map;
/**
* @author Eike Stepper
@@ -30,10 +29,8 @@ public class TransferTest
CDOTransfer transfer = createTransfer();
dump(transfer.getRootMapping());
- Map<String, Object> map = transfer.getTargetResourceSet().getResourceFactoryRegistry().getExtensionToFactoryMap();
- map.put("ecore", new EcoreResourceFactoryImpl());
- map.put("genmodel", new EcoreResourceFactoryImpl());
-
+ transfer.getModelTransferContext().registerTargetExtension("ecore", new EcoreResourceFactoryImpl());
+ transfer.getModelTransferContext().registerTargetExtension("genmodel", new EcoreResourceFactoryImpl());
transfer.perform();
}
@@ -42,10 +39,8 @@ public class TransferTest
CDOTransfer transfer = new CDOTransfer(FILE_SYSTEM, FILE_SYSTEM);
transfer.getRootMapping().setRelativePath("C:/develop/transfer");
transfer.setDefaultTransferType(CDOTransferType.UTF8);
-
- Map<String, Object> map = transfer.getSourceResourceSet().getResourceFactoryRegistry().getExtensionToFactoryMap();
- map.put("ecore", new EcoreResourceFactoryImpl());
- map.put("genmodel", new EcoreResourceFactoryImpl());
+ transfer.getModelTransferContext().registerSourceExtension("ecore", new EcoreResourceFactoryImpl());
+ transfer.getModelTransferContext().registerSourceExtension("genmodel", new EcoreResourceFactoryImpl());
CDOTransferMapping mapping = transfer.map("C:/develop/git/cdo/plugins/org.eclipse.emf.cdo.tests.model2");
mapping.setRelativePath("");

Back to the top