Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Different proxy objects with same URI
Different proxy objects with same URI [message #558163] Fri, 10 September 2010 11:39 Go to next message
Markus Herrmannsdoerfer is currently offline Markus HerrmannsdoerferFriend
Messages: 50
Registered: July 2009
Member
This is a multi-part message in MIME format.
--------------010202070803090703010109
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Hi,

I have a model that refers to an element, that is only available as a
proxy, from two places. Even though they have the same URI, two proxy
objects are created. I would expect that only one proxy object is
created so that proxies have the same behavior as their elements. I have
attached a test case which reveals this. Is that a bug or is this
behavior intended?
Thanks for you answer.
Cheers,

Markus

P.S.: BTW. I need that to migrate models that consist of several
resources and only some of the resources are available.

--------------010202070803090703010109
Content-Type: text/plain;
name="ProxyTest.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="ProxyTest.java"

package proxytest;

import java.io.IOException;

import junit.framework.Assert;
import junit.framework.TestCase;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EcoreFactory;
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.util.EcoreUtil;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;

public class ProxyTest extends TestCase {

static {
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put( "*",
new XMIResourceFactoryImpl());
}

public void testProxy() throws IOException {
EClass s = createProxyResource();
saveResource(s);
loadResource();
}

/**
* Create a resource that is not persisted, i.e. for whose elements a proxy
* should be created. This resource contains an Ecore model with a class
* that serves as a superclass of classes defined in another resource.
*/
private EClass createProxyResource() {
ResourceSet resourceSet = new ResourceSetImpl();
Resource proxyResource = resourceSet.createResource(URI
.createFileURI("proxy.ecore"));

EPackage p = EcoreFactory.eINSTANCE.createEPackage();
p.setName("proxyPackage");
proxyResource.getContents().add(p);

EClass s = EcoreFactory.eINSTANCE.createEClass();
s.setName("SuperClass");
p.getEClassifiers().add(s);

return s;
}

/**
* Create a resource that is persisted and that refers to the superclass
* contained in the proxy resource. This resource contains an Ecore model
* with two classes both of which have the same superclass.
*/
private void saveResource(EClass s)
throws IOException {
ResourceSet resourceSet = s.eResource().getResourceSet();
Resource resource = resourceSet.createResource(URI
.createFileURI("test.ecore"));

EPackage p = EcoreFactory.eINSTANCE.createEPackage();
p.setName("package");
resource.getContents().add(p);

EClass c1 = EcoreFactory.eINSTANCE.createEClass();
c1.setName("Class1");
p.getEClassifiers().add(c1);

EClass c2 = EcoreFactory.eINSTANCE.createEClass();
c2.setName("Class2");
p.getEClassifiers().add(c2);

c1.getESuperTypes().add(s);
c2.getESuperTypes().add(s);

resource.save(null);
}

/**
* Load the persisted resource and check whether the proxies of the
* superclasses of both classes are the same.
*/
private void loadResource() throws IOException {
ResourceSet resourceSet = new ResourceSetImpl();
Resource resource = resourceSet.getResource(URI
.createFileURI("test.ecore"), true);
resource.load(null);

EPackage p = (EPackage) resource.getContents().get(0);
EClass c1 = (EClass) p.getEClassifier("Class1");
EClass c2 = (EClass) p.getEClassifier("Class2");

EClass s1 = c1.getESuperTypes().get(0);
EClass s2 = c2.getESuperTypes().get(0);

Assert.assertTrue(s1.eIsProxy());
Assert.assertTrue(s2.eIsProxy());
Assert.assertEquals(EcoreUtil.getURI(s1), EcoreUtil.getURI(s2));
Assert.assertTrue(s1 == s2);
}

}

--------------010202070803090703010109--
Re: Different proxy objects with same URI [message #558224 is a reply to message #558163] Fri, 10 September 2010 14:52 Go to previous message
Eclipse UserFriend
Originally posted by: richkulp.us.NOSPAM.ibm.com

Hi,

No. A proxy is really just a placeholder in your Resource until it can
be resolved to a live object. When it is resolved the actual instance
will be loaded into your resource set and then the proxy object will be
physically replaced in the EObject/EList with the resolved loaded EObject.

In fact it is typically HARD to get access to the proxy object. All of
the standard methods to access data will automatically try to resolve
the proxy into a live object. You would only get a proxy back from such
an call if the live object could not be found and loaded into your
resource set. There are some special methods (like basicGet...) that can
be used to not do the proxy resolution but they aren't in the standard
interface for an EObject.

It isn't like a proxy that would be used in Remote Procedure Calls
(RPC), where all access goes through the proxy to the live object
somewhere else.


--
Thanks,
Rich Kulp
Previous Topic:[CDO] queryXRefs
Next Topic:[CDO] run CDO Server in OSGI
Goto Forum:
  


Current Time: Fri Apr 26 14:18:57 GMT 2024

Powered by FUDForum. Page generated in 0.03086 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top