|
|
Re: [CDO] how to create a IConnector? [message #420006 is a reply to message #420005] |
Tue, 17 June 2008 10:47 |
Andre Dietisheim Messages: 131 Registered: July 2009 |
Senior Member |
|
|
Hi Eike
great!!! I was on my way to manual wiring (found the
TCPConnectionFactory, discovered the protocol-registry and was figuring
how to wire all those components). Your example just worked out of the
box, thanks a lot! I'll probably stick to the spring-wiring.
Cheers + thanks!
André
Eike Stepper wrote:
> Hi André,
>
> Both Net4j and CDO have the same basic design separation between
> component implementations and wiring/configuration code. That makes it
> possible to use very different wiring technologies:
>
> 1) Manual wiring by explicitely calling ctors and setters. See
> http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.emf/org .eclipse.emf.cdo/examples/org.eclipse.emf.cdo.examples/src/o rg/eclipse/emf/cdo/examples/StandaloneManualExample.java?roo t=Modeling_Project&view=markup
>
> 2) Factory based wiring with IManagedContainers.
> IPluginContainer.INSTANCE is only one variant for Eclipse/OSGi. See
> standalone example at
> http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.emf/org .eclipse.emf.cdo/examples/org.eclipse.emf.cdo.examples/src/o rg/eclipse/emf/cdo/examples/StandaloneContainerExample.java? root=Modeling_Project&view=markup
>
> 3) Spring Framework application contexts, see
> http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.emf/org .eclipse.emf.cdo/examples/org.eclipse.emf.cdo.examples.sprin g/?root=Modeling_Project
>
> 4) OSGi service registry (no example provided)
> and possibly others...
>
> Since in standalone mode there are no visibility restrictions on
> internal packages you could use 1) "Manual wiring"
>
> | // Enable logging and tracing
> OMPlatform.INSTANCE.setDebugging(*true*);
> OMPlatform.INSTANCE.addLogHandler(PrintLogHandler.CONSOLE);
> OMPlatform.INSTANCE.addTraceHandler(PrintTraceHandler.CONSOL E);
>
> // Prepare receiveExecutor
> *final *ThreadGroup threadGroup = *new *ThreadGroup("net4j");
> ExecutorService receiveExecutor = Executors.newCachedThreadPool(*new *ThreadFactory()
> {
> *public *Thread newThread(Runnable r)
> {
> Thread thread = *new *Thread(threadGroup, r);
> thread.setDaemon(*true*);
> *return *thread;
> }
> });
>
> // Prepare bufferProvider
> IBufferProvider bufferProvider = Net4jUtil.createBufferPool();
> LifecycleUtil.activate(bufferProvider);
>
> // Prepare protocolFactoryRegistry
> IFactory protocolFactory = *new *org.eclipse.emf.internal.cdo.protocol.CDOClientProtocolFact ory();
> IRegistry<IFactoryKey, IFactory> protocolFactoryRegistry = *new *HashMapRegistry<IFactoryKey, IFactory>();
> protocolFactoryRegistry.put(protocolFactory.getKey(), protocolFactory);
> LifecycleUtil.activate(protocolFactoryRegistry);
>
> // Prepare selector
> org.eclipse.net4j.internal.tcp.TCPSelector selector = *new *org.eclipse.net4j.internal.tcp.TCPSelector();
> selector.activate();
>
> // Prepare connector
> org.eclipse.net4j.internal.tcp.TCPClientConnector connector = *new *org.eclipse.net4j.internal.tcp.TCPClientConnector();
> connector.setReceiveExecutor(receiveExecutor);
> connector.setBufferProvider(bufferProvider);
> connector.setProtocolFactoryRegistry(protocolFactoryRegistry );
> connector.setSelector(selector);
> connector.setNegotiator(*null*);
> connector.setHost("localhost");
> connector.setPort(2036);
> connector.activate();
>
> // Create configuration
> CDOSessionConfiguration configuration = CDOUtil.createSessionConfiguration();
> configuration.setConnector(connector);
> configuration.setRepositoryName("repo1");
>
> // Open session
> CDOSession session = configuration.openSession();
> session.getPackageRegistry().putEPackage(Model1Package.eINST ANCE);
>
> // Open transaction
> CDOTransaction transaction = session.openTransaction();
>
> // Get or create resource
> CDOResource resource = transaction.getOrCreateResource("/path/to/my/resource");
>
> // Work with the resource and commit the transaction
> EObject object = Model1Factory.eINSTANCE.createCompany();
> resource.getContents().add(object);
> transaction.commit();
>
> // Cleanup
> session.close();
> connector.deactivate();|
>
>
> You can also used an IManagedContainer in standalone mode to avoid
> implementation dependencies that you have to maintain:
>
> | // Enable logging and tracing
> OMPlatform.INSTANCE.setDebugging(*true*);
> OMPlatform.INSTANCE.addLogHandler(PrintLogHandler.CONSOLE);
> OMPlatform.INSTANCE.addTraceHandler(PrintTraceHandler.CONSOL E);
>
> | | // Prepare container
> IManagedContainer container = ContainerUtil.createContainer();
> Net4jUtil.prepareContainer(container); // Register Net4j factories
> TCPUtil.prepareContainer(container); // Register TCP factories
> CDOUtil.prepareContainer(container, *false*); // Register CDO factories
>
> // Create connector
> IConnector connector = TCPUtil.getConnector(container, "localhost:2036");
>
> // Create configuration
> CDOSessionConfiguration configuration = CDOUtil.createSessionConfiguration();
> configuration.setConnector(connector);
> configuration.setRepositoryName("my-repo");
>
> // Open session
> CDOSession session = configuration.openSession();
> session.getPackageRegistry().putEPackage(Model1Package.eINST ANCE);
>
> // Open transaction
> CDOTransaction transaction = session.openTransaction();
>
> // Get or create resource
> CDOResource resource = transaction.getOrCreateResource("/path/to/my/resource");
>
> // Work with the resource and commit the transaction
> EObject object = Model1Factory.eINSTANCE.createCompany();
> resource.getContents().add(object);
> transaction.commit();
>
> // Cleanup
> session.close();
> LifecycleUtil.deactivate(connector);|
>
>
> Please note that CDOUtil.openSession() has been replaced by
> |CDOUtil.createSessionConfiguration()!|
>
> Cheers
> /Eike
>
>
> André Dietisheim schrieb:
>> Hi Eike, hi Simon
>>
>> I currently try to implement a standalone (non-OSGI) CDO client. I
>> found the snippets on the eclipse twiki and the examples project):
>>
>>
>> IConnector connector = ...;
>> CDOSession session = CDOUtil.openSession(connector, "repo1");
>> session.setDisableLegacyObjects(...);
>> session.setReferenceChunkSize(...);
>> .....
>> session.close();
>>
>>
>> and
>>
>>
>> CDOSession session =
>> CDOSessionFactory.get(IPluginContainer.INSTANCE,"tcp://repos.company.com:2036/repo1");
>>
>> .....
>> session.close();
>>
>>
>> the 2nd snippet is to be used in an OSGI container (I tried it and
>> tracked the source down to where it looks through the plugin-registry).
>> The first one does not show how to create connection. The same applies
>> to the example project that throws a NotImplementedException when it's
>> up to create a connection.
>> Could anyone point me how to create a connection in a non-OSGI usage
>> scenario? Your help's greatly appreciated!
>>
>> Thanks + Cheers
>> André
|
|
|
Powered by
FUDForum. Page generated in 0.01387 seconds