Home » Modeling » EMF » Problems try to use Offline example code(Problems try to use Offline example code)
Problems try to use Offline example code [message #1021840] |
Wed, 20 March 2013 19:21 |
Andrew Whelan Messages: 71 Registered: October 2012 Location: Syracuse NY |
Member |
|
|
Hello,
I am in the process of trying to figure out the correct way to set up CDO Server/Client connectivity in a way that allows my project to use the CDO Offline functionality. I have started looking at the org.eclipse.emf.cdo.examples.client.offline example because it is what Eike seems to recommend.
I am working on a project that already has an RCP GUI so I am trying to figure out how to separate the offline example GUI code from the server/client code that deals with the store/session/transaction stuff. I am trying to figure out how to do this by creating a main() method that creates a NodeManager, a Node that is of type NodeType.NormalRepository, and a node that is of type NodeType.Client. This is just an exercise to help me separate out the GUI stuff in the example. At this point, if I can do all that and then have the client node save something to the H2 database that the example uses then I will consider that success.
Where I'm crashing is in trying to save the first record. As I said, I am not using the GUI. In the example you are pulling data out of properties for the various fields of the CompanyPackage. How you add these records to a CDOTransaction is kind of lost to me in the example. I see that in org.eclipse.emf.cdo.examples.client.offline.ClientView we are commiting data in the CommitAction class, but I'm not sure how the data gets associated with the transaction.
I have substituted the LibraryPackage of the Library model that is used in the EMF tutorials. Other than that most of the code from the Offline example hasn't changed. The things I changed involved changing the access privileges for a couple of methods (to public) so I can call them directly in the main(). Below is the method doing the saving.
I added saveData to org.eclipse.emf.cdo.examples.client.offline.nodes.Node to add a record to the database. The exception shown at the bottom of this post is caused by the line transaction.getOrCreateResource(type.REPOSITORY_NAME). I'm really not sure how else to add data to the transaction.
I used both the name of the repository ("repository") and "/repo1" and got the same result (Exception is "RootResourceID is null; is the repository not yet initialized?").
public void saveData(Library library)
{
CDOTransaction transaction = getObject(CDOTransaction.class);
if (transaction == null)
{
throw new IllegalArgumentException("No CDOTransaction is set.");
}
try
{
if ((CDOObject)library.cdoView() == null)
{
//This breaks with "/repo1" and type.REPOSITORY_NAME
//which equals "repository".
CDOResource resource = transaction.getOrCreateResource(type.REPOSITORY_NAME);
resource.getContents().add(library);
transaction.commit();
}
else
{
((CDOTransaction)library.cdoView()).commit();
}
}
catch (Exception e)
{
throw new IllegalStateException("Could not save data.", e);
}
}
Below is the main.
public class TestCDOOffline {
private static final String ROOT_PROPERTY = "node.manager.root";
public static void main(String[] args) {
OMPlatform.INSTANCE.setDebugging(true);
OMPlatform.INSTANCE.addTraceHandler(PrintTraceHandler.CONSOLE);
OMPlatform.INSTANCE.addLogHandler(PrintLogHandler.CONSOLE);
NodeManager nodeManager = createNodeManager();
Net4jUtil.prepareContainer(IPluginContainer.INSTANCE);
TCPUtil.prepareContainer(IPluginContainer.INSTANCE);
JVMUtil.prepareContainer(IPluginContainer.INSTANCE); //ditto JVM support
CDONet4jUtil.prepareContainer(IPluginContainer.INSTANCE);
CDONet4jServerUtil.prepareContainer(IPluginContainer.INSTANCE);
IPluginContainer.INSTANCE.registerFactory(new CDOServerBrowser.ContainerBased.Factory());
IPluginContainer.INSTANCE.addPostProcessor(new Net4jConnectorInjector());
String normalRepositoryName = "normal-repository-1";
String normalClient1 = "normal-client-1";
//createNormalRepository is a separate method below this main()
NodeType.NormalRepository repository = createNormalRepository(
normalRepositoryName, nodeManager);
nodeManager.createNode(repository);
Node normal1 = nodeManager.getNode(normalRepositoryName);
//Start the normal repository.
normal1.start();
System.out.println("Starting the normal repository");
// createClient is a separate method below this main()
NodeType.Client client1 = createClient(normalClient1,
nodeManager, "7778", normalRepositoryName);
nodeManager.createNode(client1);
Node c1 = nodeManager.getNode(normalClient1);
c1.start();
System.out.println("Started the client");
//editData is a separate method below this main()
editData(c1);
System.out.println("Edited data in the client");
c1.stop();
System.out.println("Stopped the client");
try {
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
}
normal1.stop();
System.out.println("Stopped the normal repository");
}
//This creates Library data and calls the saveData method
//shown above.
private static void editData(Node client)
{
Library library = LibraryFactory.eINSTANCE.createLibrary();
library.setName("Public Library");
Book book = LibraryFactory.eINSTANCE.createBook();
book.setTitle("book1");
library.getBooks().add(book);
Writer writer = LibraryFactory.eINSTANCE.createWriter();
writer.setName("author 1");
library.getWriters().add(writer);
book.setAuthor(writer);
Book book2 = LibraryFactory.eINSTANCE.createBook();
book2.setTitle("book2");
Writer writer2 = LibraryFactory.eINSTANCE.createWriter();
writer2.setName("author 2");
library.getWriters().add(writer2);
book2.setAuthor(writer2);
library.getBooks().add(book2);
//This is the saveData method,which was the first snippet of code shown above.
client.saveData(library);
}
public static NodeManager createNodeManager() {
File f = new File(ROOT_PROPERTY);
System.out.println("root path: " + f.getAbsolutePath());
return new NodeManager(f);
}
public static NodeType.NormalRepository createNormalRepository(String name,
NodeManager nodeManager) {
NodeType.NormalRepository repository = new NodeType.NormalRepository(
nodeManager);
repository.getSettings().setProperty(NodeType.NAME_PROPERTY, name);
repository.getSettings().setProperty(NodeType.BROWSER_PROPERTY, "7777");
repository.getSettings().setProperty(NodeType.PORT_PROPERTY, "2036");
return repository;
}
public static NodeType.Client createClient(String name,
NodeManager nodeManager, String browserPort, String server) {
NodeType.Client client = new NodeType.Client(nodeManager);
client.getSettings().setProperty(NodeType.NAME_PROPERTY, name);
client.getSettings()
.setProperty(NodeType.BROWSER_PROPERTY, browserPort);
client.getSettings().setProperty(NodeType.SERVER_PROPERTY, server);
return client;
}
}
Can someone give me an Idea what I might be missing? Following is the Exeption stack trace
Caused by: java.lang.IllegalStateException: RootResourceID is null; is the repository not yet initialized?
at org.eclipse.emf.internal.cdo.view.AbstractCDOView.getRootResource(AbstractCDOView.java:242)
at org.eclipse.emf.internal.cdo.transaction.CDOTransactionImpl.attachNewResourceNode(CDOTransactionImpl.java:1010)
at org.eclipse.emf.internal.cdo.transaction.CDOTransactionImpl.attachNewResource(CDOTransactionImpl.java:940)
at org.eclipse.emf.internal.cdo.transaction.CDOTransactionImpl.attachResource(CDOTransactionImpl.java:929)
at org.eclipse.emf.cdo.eresource.impl.CDOResourceImpl.basicSetResourceSet(CDOResourceImpl.java:1511)
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$ResourcesEList.inverseAdd(ResourceSetImpl.java:586)
at org.eclipse.emf.common.notify.impl.NotifyingListImpl.addUnique(NotifyingListImpl.java:286)
at org.eclipse.emf.common.util.AbstractEList.add(AbstractEList.java:301)
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.createResource(ResourceSetImpl.java:435)
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.createResource(ResourceSetImpl.java:423)
at org.eclipse.emf.internal.cdo.transaction.CDOTransactionImpl.createResource(CDOTransactionImpl.java:840)
at org.eclipse.emf.internal.cdo.transaction.CDOTransactionImpl.getOrCreateResource(CDOTransactionImpl.java:860)
at org.eclipse.emf.cdo.examples.client.offline.nodes.Node.saveData(Node.java:161)
... 2 more
|
|
|
Re: Problems try to use Offline example code [message #1021855 is a reply to message #1021840] |
Wed, 20 March 2013 20:00 |
|
Am 20.03.2013 20:21, schrieb Andrew Whelan:
> Hello,
>
> I am in the process of trying to figure out the correct way to set up CDO Server/Client connectivity in a way that
> allows my project to use the CDO Offline functionality. I have started looking at the
> org.eclipse.emf.cdo.examples.client.offline example because it is what Eike seems to recommend.
> I am working on a project that already has an RCP GUI so I am trying to figure out how to separate the offline example
> GUI code from the server/client code that deals with the store/session/transaction stuff. I am trying to figure out
> how to do this by creating a main() method that creates a NodeManager, a Node that is of type
> NodeType.NormalRepository, and a node that is of type NodeType.Client. This is just an exercise to help me separate
> out the GUI stuff in the example. At this point, if I can do all that and then have the client node save something to
> the H2 database that the example uses then I will consider that success.
> Where I'm crashing is in trying to save the first record. As I said, I am not using the GUI. In the example you are
> pulling data out of properties for the various fields of the CompanyPackage.
Sorry, what's this "pulling data out of ..."? Can you point me to the code?
> How you add these records to a CDOTransaction is kind of lost to me in the example.
By "records" you mean EObjects?
> I see that in org.eclipse.emf.cdo.examples.client.offline.ClientView we are commiting data in the CommitAction class,
> but I'm not sure how the data gets associated with the transaction.
In org.eclipse.emf.cdo.examples.client.offline.nodes.NodeType.Client.createTransaction(Node) is remember the transaction:
node.setObject(CDOTransaction.class, transaction);
In org.eclipse.emf.cdo.examples.client.offline.ClientView.CommitAction.run() is retrieve it from there:
CDOTransaction transaction = Application.NODE.getObject(CDOTransaction.class);
In org.eclipse.emf.cdo.examples.client.offline.ClientView.hookDoubleClick().new IDoubleClickListener()
{...}.doubleClick(DoubleClickEvent) i open the editor:
CDOEditorUtil.openEditor(getSite().getPage(), resource);
That resource is the selection of org.eclipse.emf.cdo.examples.client.offline.ClientView.treeViewer, which uses
CDOItemProvider with the transaction (as a child of the session). So the selected resource object and all the contained
EObjects are already managed by the correct transaction.
> I have substituted the LibraryPackage of the Library model that is used in the EMF tutorials.
I don't have that in my target platform, so I can't run your main() ;-(
Note that your client will have to enable CDO's legacy mode if you don't regenerate the LibraryPackagefor CDO native use
(that's not needed anymore as of 4.2 M6).
> Other than that most of the code from the Offline example hasn't changed. The things I changed involved changing the
> access privileges for a couple of methods (to public) so I can call them directly in the main().
If you submit a patch I can review and eventually apply it ;-)
> Below is the method doing the saving.
> I added saveData to org.eclipse.emf.cdo.examples.client.offline.nodes.Node to add a record to the database. The
> exception shown at the bottom of this post is caused by the line
> transaction.getOrCreateResource(type.REPOSITORY_NAME). I'm really not sure how else to add data to the transaction. I
> used both the name of the repository ("repository") and "/repo1"
You're expected to pass the name (actually the repository-relative path) of a Resource, some String that all clients
agree upon to enter the persistent graph.
> and got the same result (Exception is "RootResourceID is null; is the repository not yet initialized?").
I remember that formerly that was a result of connecting a client to a clone repository that has never been synchronized
with its master. I thought these days the exception should be more concise.
> public void saveData(Library library)
> {
> CDOTransaction transaction = getObject(CDOTransaction.class);
>
> if (transaction == null)
> {
> throw new IllegalArgumentException("No CDOTransaction is set.");
> }
>
> try
> {
> if ((CDOObject)library.cdoView() == null)
> {
> //This breaks with "/repo1" and type.REPOSITORY_NAME //which equals "repository". CDOResource resource =
> transaction.getOrCreateResource(type.REPOSITORY_NAME);
> resource.getContents().add(library);
> transaction.commit();
> }
> else
> {
> ((CDOTransaction)library.cdoView()).commit();
> }
> }
> catch (Exception e)
> {
> throw new IllegalStateException("Could not save data.", e);
> }
> }
>
> Below is the main.
>
> public class TestCDOOffline {
>
> private static final String ROOT_PROPERTY = "node.manager.root";
>
> public static void main(String[] args) {
>
> OMPlatform.INSTANCE.setDebugging(true);
> OMPlatform.INSTANCE.addTraceHandler(PrintTraceHandler.CONSOLE);
> OMPlatform.INSTANCE.addLogHandler(PrintLogHandler.CONSOLE);
>
> NodeManager nodeManager = createNodeManager();
>
> Net4jUtil.prepareContainer(IPluginContainer.INSTANCE);
> TCPUtil.prepareContainer(IPluginContainer.INSTANCE);
> JVMUtil.prepareContainer(IPluginContainer.INSTANCE); //ditto JVM support
>
> CDONet4jUtil.prepareContainer(IPluginContainer.INSTANCE);
> CDONet4jServerUtil.prepareContainer(IPluginContainer.INSTANCE); IPluginContainer.INSTANCE.registerFactory(new
> CDOServerBrowser.ContainerBased.Factory());
>
> IPluginContainer.INSTANCE.addPostProcessor(new Net4jConnectorInjector());
>
> String normalRepositoryName = "normal-repository-1";
> String normalClient1 = "normal-client-1";
>
> //createNormalRepository is a separate method below this main()
> NodeType.NormalRepository repository = createNormalRepository(
> normalRepositoryName, nodeManager);
>
> nodeManager.createNode(repository);
> Node normal1 = nodeManager.getNode(normalRepositoryName);
>
> //Start the normal repository.
> normal1.start();
>
> System.out.println("Starting the normal repository");
>
> // createClient is a separate method below this main()
> NodeType.Client client1 = createClient(normalClient1,
> nodeManager, "7778", normalRepositoryName);
> nodeManager.createNode(client1);
> Node c1 = nodeManager.getNode(normalClient1);
>
> c1.start();
> System.out.println("Started the client");
>
> //editData is a separate method below this main()
> editData(c1);
>
> System.out.println("Edited data in the client");
>
> c1.stop();
> System.out.println("Stopped the client");
> try {
> Thread.sleep(5000);
> } catch (Exception e) {
> e.printStackTrace();
> }
>
> normal1.stop();
> System.out.println("Stopped the normal repository");
> }
>
>
> //This creates Library data and calls the saveData method
> //shown above.
> private static void editData(Node client)
> {
> Library library = LibraryFactory.eINSTANCE.createLibrary();
> library.setName("Public Library");
> Book book = LibraryFactory.eINSTANCE.createBook();
> book.setTitle("book1");
> library.getBooks().add(book);
>
> Writer writer = LibraryFactory.eINSTANCE.createWriter();
>
> writer.setName("author 1");
> library.getWriters().add(writer);
> book.setAuthor(writer);
>
> Book book2 = LibraryFactory.eINSTANCE.createBook();
> book2.setTitle("book2");
> Writer writer2 = LibraryFactory.eINSTANCE.createWriter();
> writer2.setName("author 2");
>
> library.getWriters().add(writer2);
> book2.setAuthor(writer2);
> library.getBooks().add(book2);
>
> //This is the saveData method,which was the first snippet of code shown above.
> client.saveData(library);
> }
>
>
> public static NodeManager createNodeManager() {
> File f = new File(ROOT_PROPERTY);
> System.out.println("root path: " + f.getAbsolutePath());
> return new NodeManager(f);
> }
>
> public static NodeType.NormalRepository createNormalRepository(String name,
> NodeManager nodeManager) {
> NodeType.NormalRepository repository = new NodeType.NormalRepository(
> nodeManager);
>
> repository.getSettings().setProperty(NodeType.NAME_PROPERTY, name);
> repository.getSettings().setProperty(NodeType.BROWSER_PROPERTY, "7777");
> repository.getSettings().setProperty(NodeType.PORT_PROPERTY, "2036");
> return repository;
> }
>
> public static NodeType.Client createClient(String name,
> NodeManager nodeManager, String browserPort, String server) {
>
> NodeType.Client client = new NodeType.Client(nodeManager);
>
> client.getSettings().setProperty(NodeType.NAME_PROPERTY, name);
> client.getSettings()
> .setProperty(NodeType.BROWSER_PROPERTY, browserPort);
> client.getSettings().setProperty(NodeType.SERVER_PROPERTY, server);
> return client;
> }
>
> }
>
> Can someone give me an Idea what I might be missing?
Sorry, I can't run your main() because it uses a package that I don't have.
Cheers
/Eike
----
http://www.esc-net.de
http://thegordian.blogspot.com
http://twitter.com/eikestepper
> Following is the Exeption stack trace
>
> Caused by: java.lang.IllegalStateException: RootResourceID is null; is the repository not yet initialized?
> at org.eclipse.emf.internal.cdo.view.AbstractCDOView.getRootResource(AbstractCDOView.java:242)
> at org.eclipse.emf.internal.cdo.transaction.CDOTransactionImpl.attachNewResourceNode(CDOTransactionImpl.java:1010)
> at org.eclipse.emf.internal.cdo.transaction.CDOTransactionImpl.attachNewResource(CDOTransactionImpl.java:940)
> at org.eclipse.emf.internal.cdo.transaction.CDOTransactionImpl.attachResource(CDOTransactionImpl.java:929)
> at org.eclipse.emf.cdo.eresource.impl.CDOResourceImpl.basicSetResourceSet(CDOResourceImpl.java:1511)
> at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$ResourcesEList.inverseAdd(ResourceSetImpl.java:586)
> at org.eclipse.emf.common.notify.impl.NotifyingListImpl.addUnique(NotifyingListImpl.java:286)
> at org.eclipse.emf.common.util.AbstractEList.add(AbstractEList.java:301)
> at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.createResource(ResourceSetImpl.java:435)
> at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.createResource(ResourceSetImpl.java:423)
> at org.eclipse.emf.internal.cdo.transaction.CDOTransactionImpl.createResource(CDOTransactionImpl.java:840)
> at org.eclipse.emf.internal.cdo.transaction.CDOTransactionImpl.getOrCreateResource(CDOTransactionImpl.java:860)
> at org.eclipse.emf.cdo.examples.client.offline.nodes.Node.saveData(Node.java:161)
> ... 2 more
>
Cheers
/Eike
----
http://www.esc-net.de
http://thegordian.blogspot.com
http://twitter.com/eikestepper
|
|
| | | |
Goto Forum:
Current Time: Wed Apr 17 22:06:16 GMT 2024
Powered by FUDForum. Page generated in 0.02590 seconds
|