CDO/Spring

From Eclipsepedia

< CDO
Jump to: navigation, search

Contents

Summary

This page provides an example of how to configure and run a standalone Spring Framework wired CDO server and client. The CDO server and client lifecycle is managed by the Spring's container lifecycle. The examples use the CDO 3.0 code from the CVS HEAD.

Java classes and Spring beans

  • ContextFactoryRegistrar - is a bean post processor that is responsible to register factory beans of a specified product group with the managed container. The factory beans are defined in the spring context.
  • ContextManagedContainer - extends the ManagedContainer class, and implements ApplicationListener interface. This bean is acts upon ContextRefreshEvent and activates the ManagedContainer, after the spring context has been initialized or refreshed - after all of the factories are registered with the ManagedContainer by the ContextFactoryRegistrar
  • ContextRepositoryProvider - provides repositories defined as beans in the Spring context.
  • CommonsLogger - delegates the OMPlatform logging to Apache's commons logging engine

Source files archive - CDO_Spring.zip

CDO server context

  • file name cdo-server.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
 
	<util:map id="props" key-type="java.lang.String" value-type="java.lang.String">
		<entry key="overrideUUID" value="1ff5d226-b1f0-40fb-aba2-0c31b38c764f" />
		<entry key="supportingAudits" value="true" />
		<entry key="verifyingRevisions" value="false" />
		<entry key="currentLRUCapacity" value="10000" />
		<entry key="revisedLRUCapacity" value="100" />
	</util:map>
 
    <bean id="dataSource" class="com.mysql.jdbc.jdbc2.optional.MysqlDataSource">
        <property name="user" value="cdo"/>
        <property name="password" value="cdo"/>
        <property name="serverName" value="d510"/>
        <property name="port" value="3306"/>
        <property name="databaseName" value="cdo"/>
    </bean>
 
    <bean id="dbStore" class="org.eclipse.emf.cdo.server.internal.db.DBStore">
    	<property name="dataSource" ref="dataSource"/>
    	<property name="DBAdapter">
    		<bean id="dbAdapter" class="org.eclipse.net4j.db.mysql.MYSQLAdapter"/>
    	</property>    	
    	<property name="mappingStrategy">
    		<bean id="mappingStrategy" class="org.eclipse.emf.cdo.server.internal.db.mapping.horizontal.HorizontalAuditMappingStrategy"/>
    	</property>    	
    </bean>
 
    <bean id="repo1" class="org.eclipse.emf.cdo.internal.server.Repository" init-method="activate" destroy-method="deactivate">
    	<property name="name" value="repo1"/>
    	<property name="store" ref="dbStore"/>
    	<property name="properties" ref="props"/>    	
    	<property name="packageRegistry"><bean class="org.eclipse.emf.cdo.internal.common.model.CDOPackageRegistryImpl"/></property>
    	<property name="sessionManager"><bean class="org.eclipse.emf.cdo.internal.server.SessionManager"/></property>
    	<property name="revisionManager"><bean class="org.eclipse.emf.cdo.internal.common.revision.CDORevisionManagerImpl"/></property>
    	<property name="queryManager"><bean class="org.eclipse.emf.cdo.internal.server.QueryManager"/></property>
    	<property name="notificationManager"><bean class="org.eclipse.emf.cdo.internal.server.NotificationManager"/></property>
    	<property name="commitManager"><bean class="org.eclipse.emf.cdo.internal.server.CommitManager"/></property>
    	<property name="lockManager"><bean class="org.eclipse.emf.cdo.internal.server.LockManager"/></property>
    </bean>
 
    <!-- container factories & post processors-->
 
    <bean id="serverProtocolFactory" class="org.eclipse.emf.cdo.server.internal.net4j.protocol.CDOServerProtocolFactory">
    	<constructor-arg><bean id="contextRepositoryProvider" class="org.eclipse.emf.cdo.server.spring.ContextRepositoryProvider"/></constructor-arg>
    </bean>
 
    <!-- ContainerUtil -->
    <bean id="executorServiceFactory" class="org.eclipse.net4j.util.concurrent.ExecutorServiceFactory"/>
    <bean id="timerLifecycle.DaemonFactory" class="org.eclipse.net4j.util.concurrent.TimerLifecycle$DaemonFactory"/>
    <bean id="randomizerFactory" class="org.eclipse.net4j.util.security.RandomizerFactory"/>
    <bean id="fileUserManagerFactory" class="org.eclipse.net4j.util.security.FileUserManagerFactory"/>
 
    <!-- Net4JUtil -->    
    <bean id="bufferProviderFactory" class="org.eclipse.internal.net4j.buffer.BufferProviderFactory"/>
    <bean id="transportInjector" class="org.eclipse.net4j.TransportInjector"/>
    <bean id="heartBeatProtocol.Server.Factory" class="org.eclipse.net4j.signal.heartbeat.HeartBeatProtocol$Server$Factory"/>
 
    <!-- TCPUtil -->    
    <bean id="tcpSelectorFactory" class="org.eclipse.net4j.internal.tcp.TCPSelectorFactory"/>
    <bean id="tcpAcceptorFactory" class="org.eclipse.net4j.internal.tcp.TCPAcceptorFactory"/>
    <bean id="tcpConnectorFactory" class="org.eclipse.net4j.internal.tcp.TCPConnectorFactory"/>
    <bean id="tcpSelectorInjector" class="org.eclipse.net4j.internal.tcp.TCPSelectorInjector"/>
 
    <bean id="container" class="org.eclipse.emf.cdo.server.spring.ContextManagedContainer"
    	destroy-method="deactivate"/>
 
    <bean id="serverProtocolFactoryRegistrar" class="org.eclipse.emf.cdo.server.spring.ContextFactoryRegistrar">        
        <property name="container" ref="container"/>
        <property name="productGroup" value="org.eclipse.net4j.serverProtocols"/>
    </bean>
 
     <!-- Ne4j -->    
    <bean id="serverProtocolProvider" class="org.eclipse.net4j.FactoriesProtocolProvider">
    	<constructor-arg ref="serverProtocolFactory"/>
    </bean>
 
    <bean id="serverConfig" class="org.eclipse.internal.net4j.TransportConfig">
		<property name="bufferProvider">
			<bean factory-bean="bufferProviderFactory" factory-method="create">
				<constructor-arg value=""/>
			</bean>
		</property>		
		<property name="protocolProvider"  ref="serverProtocolProvider"/>		
		<property name="receiveExecutor">
			<bean factory-bean="executorServiceFactory" factory-method="create">
				<constructor-arg value="net4j"/>
			</bean>
		</property>			
	</bean>
 
	<bean id="accpetor" class="org.eclipse.net4j.internal.tcp.TCPAcceptor" init-method="activate" destroy-method="deactivate">
		<property name="address" value="localhost"/>
		<property name="port" value="2036"/>
		<property name="selector">
			<bean id="selector" class="org.eclipse.net4j.internal.tcp.TCPSelector" init-method="activate" destroy-method="deactivate"/>
		</property>
		<property name="config" ref="serverConfig"/>
	</bean>
 
</beans>

CDO client context

  • file name cdo-client.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"
	default-init-method="activate"
	default-destroy-method="deactivate"
	>
 
	<bean id="threadFactory" class="org.springframework.scheduling.concurrent.CustomizableThreadFactory" p:daemon="true" p:threadGroupName="net4j"/>
	<bean id="threadPool" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
		<property name="staticMethod" value="java.util.concurrent.Executors.newCachedThreadPool"/>
		<property name="arguments">
			<list><ref bean="threadFactory"/></list>
		</property>
	</bean>
 
	<bean id="bufferFactory" class="org.eclipse.internal.net4j.buffer.BufferFactory">
		<constructor-arg value="4096"/>
	</bean>
	<bean id="bufferPool" class="org.eclipse.internal.net4j.buffer.BufferPool">
		<constructor-arg ref="bufferFactory"/>
	</bean>
 
	<bean id="clientProtocolFactory" class="org.eclipse.emf.cdo.internal.net4j.protocol.CDOClientProtocolFactory"/>
	<bean id="clientProtocolProvider" class="org.eclipse.net4j.FactoriesProtocolProvider">
		<constructor-arg ref="clientProtocolFactory"/>
	</bean>
 
	<bean id="selector" class="org.eclipse.net4j.internal.tcp.TCPSelector"/>
 
	<bean id="clientConfig" class="org.eclipse.internal.net4j.TransportConfig">
		<property name="bufferProvider" ref="bufferPool"/>
		<property name="protocolProvider" ref="clientProtocolProvider"/>
		<property name="receiveExecutor" ref="threadPool"/>		
	</bean>
 
	<bean id="connector" class="org.eclipse.net4j.internal.tcp.TCPClientConnector">
		<property name="host" value="localhost"/>
		<property name="port" value="2036"/>
		<property name="selector" ref="selector"/>
		<property name="config" ref="clientConfig"/>			
	</bean> 
 
	<bean id="sessionConfiguration" class="org.eclipse.emf.cdo.internal.net4j.CDONet4jSessionConfigurationImpl">
		<property name="activateOnOpen" value="true"/>
		<property name="repositoryName" value="repo1"/>
		<property name="connector" ref="connector"/>		
	</bean>
 
</beans>

JUnit Tests

  • CDO server - just initialize the spring context and register a shutdown hook.
@ContextConfiguration(locations={"classpath:/cdo-server.xml"})
public class ServerTest extends AbstractJUnit4SpringContextTests {
	public ServerTest() {	
	}
 
	@BeforeClass
	public static void setup() {		
		OMPlatform.INSTANCE.setDebugging(true);
	    OMPlatform.INSTANCE.addLogHandler(CommonsLogger.LOGGER);
	    OMPlatform.INSTANCE.addTraceHandler(CommonsLogger.LOGGER);
	}
 
	@Test
	public void testServer() throws IOException, InterruptedException {
		((AbstractApplicationContext) applicationContext).registerShutdownHook(); 
   		System.out.println("Server Started. Press ENTER to exit.");
   		while (System.in.read()==-1) {
   			Thread.sleep(500);
   		}
	}
}
  • CDO client
@ContextConfiguration(locations={"classpath:/cdo-client.xml"})
public class ClientTest extends AbstractJUnit4SpringContextTests {
	public ClientTest() {}
 
	@Test
	public void testSession() {
		CDOSessionConfiguration sessionConfiguration = (CDOSessionConfiguration) applicationContext.getBean("sessionConfiguration");
		CDOSession session = sessionConfiguration.openSession();		
		session.getPackageRegistry().putEPackage(IptvPackage.eINSTANCE);
		logger.info("Session Package Registry:");
		for (CDOPackageInfo packageInfo : session.getPackageRegistry().getPackageInfos()) {
			logger.info(packageInfo.toString());
		}		
		session.close();
		assertTrue(true);
	}
}