Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Run a CDO container inside eclipse runtime

Revision as of 06:20, 4 July 2008 by Stefan.winkler-et.fernuni-hagen.de (Talk | contribs) (New page: If you want to use the CDO container inside an eclipse runtime, create a plug-in project managing the resouces (e.g. the connection). The plugin's activator class could look like this: <s...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

If you want to use the CDO container inside an eclipse runtime, create a plug-in project managing the resouces (e.g. the connection). The plugin's activator class could look like this:

package your.packagename.here;
 
import java.util.HashMap;
import java.util.Map;
 
import org.eclipse.emf.cdo.CDOSession;
import org.eclipse.emf.cdo.CDOSessionConfiguration;
import org.eclipse.emf.cdo.CDOTransaction;
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.server.CDOServerUtil;
import org.eclipse.emf.cdo.server.IRepository;
import org.eclipse.emf.cdo.server.IStore;
import org.eclipse.emf.cdo.server.IRepository.Props;
import org.eclipse.emf.cdo.server.hibernate.teneo.TeneoHibernateMappingProvider;
import org.eclipse.emf.cdo.server.internal.hibernate.HibernateStore;
import org.eclipse.emf.cdo.util.CDOUtil;
import org.eclipse.emf.teneo.PersistenceOptions;
import org.eclipse.net4j.connector.IConnector;
import org.eclipse.net4j.jvm.IJVMAcceptor;
import org.eclipse.net4j.jvm.IJVMConnector;
import org.eclipse.net4j.jvm.JVMUtil;
import org.eclipse.net4j.util.container.IManagedContainer;
import org.eclipse.net4j.util.container.IPluginContainer;
import org.eclipse.net4j.util.lifecycle.LifecycleUtil;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
 
public class Activator extends AbstractUIPlugin {
 
/**
 * The Plugin ID  
 */
	public static final String PLUGIN_ID = "your.plugin.id.here";
 
	/** The repository name 
	 */
	private static final String REPOSITORY_NAME = "your_repositoryname";
 
	/** The shared instance 
	 */
	private static Activator plugin;
 
	/**
	 * The constructor
	 */
	public Activator() { }
 
	/**
	 * The usual start implementation ...
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
	}
 
	/**
	 * The usual stop implementation ... BUT including some CDO cleanup.
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
 
		if(resource != null) 
			LifecycleUtil.deactivate(resource);
 
		if(transaction != null) 
			LifecycleUtil.deactivate(transaction);
 
		if(session != null)
			LifecycleUtil.deactivate(session);
 
		if(acceptor != null)
			LifecycleUtil.deactivate(acceptor);
 
		if(connector != null) 
			LifecycleUtil.deactivate(connector);
 
		if(repository != null) 
			LifecycleUtil.deactivate(repository);
 
		super.stop(context);
	}
 
	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static Activator getDefault() {
		return plugin;
	}
 
	/**
	 * Holding the session (lazy initialization via getSession())
	 */
	private CDOSession session = null;
 
	private IJVMAcceptor acceptor;
 
	private IRepository repository;
 
	private IConnector connector;
 
	/**
	 * Getter for session including lazy initialization 
	 * @return the CDO session
	 */
	public CDOSession getSession() {
		if(session == null) {
			IManagedContainer container = IPluginContainer.INSTANCE;
 
			// initialize acceptor
			if(acceptor == null)
				acceptor = JVMUtil.getAcceptor(container, "default");
 
			if(repository == null) {
				repository = createRepository();
				CDOServerUtil.addRepository(container, repository);
			}
 
			if(connector == null)
				connector = JVMUtil.getConnector(container, "default");
 
			CDOSessionConfiguration config = CDOUtil.createSessionConfiguration();
			config.setConnector(connector);
			config.setRepositoryName(REPOSITORY_NAME);
			config.setLegacySupportEnabled(false);
 
			// (1)
			config.setDemandPopulatingPackageRegistry();
 
			session = config.openSession();			
		}
		return session;
	}
 
 
	/**
	 * Holding the transaction (lazy initialization via getTransaction())
	 */
	private CDOTransaction transaction = null;
 
	/**
	 * Getter for transaction including lazy initialization
	 * @return the transaction
	 */
	public CDOTransaction getTransaction() {
		if(transaction == null) {
			transaction = getSession().openTransaction();
		}
 
		return transaction;
	}
 
	CDOResource resource = null; 
 
	/**
	 * Getter for resource (e.g. if you use only one central resource) 
	 * @return the resource
	 */
	public CDOResource getResource() {
		if(resource == null) {
			/* getOrCreateResource will handle both loading existing resources
			 * (equivalent to transaction.getResource()) and creating/initializing
			 * a new one (equivalent to transaction.createResource())
			 */
			resource = getTransaction().getOrCreateResource("/test");
		}
 
		return resource;
	}
 
	/** 
	 * Create and initialize/configure a repository 
	 * 
	 * // (2)
	 * 
	 * @return the CDO repository created
	 */
	private IRepository createRepository() {
 
		final String DATABASE_NAME = "mydatabase";
		final String DATABASE_USER = "scott";
		final String DATABASE_PASS = "tiger";
 
		Map<String, String> props = new HashMap<String, String>();
		props.put(Props.PROP_OVERRIDE_UUID,
				"f8188187-65de-4c8a-8e75-e0ce5949837a");
		props.put(Props.PROP_SUPPORTING_AUDITS, "false");
		props.put(Props.PROP_SUPPORTING_REVISION_DELTAS, "false");
		props.put(Props.PROP_VERIFYING_REVISIONS, "false");
		props.put(Props.PROP_CURRENT_LRU_CAPACITY, "10000");
		props.put(Props.PROP_REVISED_LRU_CAPACITY, "10000");
		props.put(PersistenceOptions.ID_FEATURE_AS_PRIMARY_KEY, "false");
		props.put("hibernate.connection.autocommit", "true");
		props.put("hibernate.cache.provider_class",
				"org.hibernate.cache.HashtableCacheProvider");
		props.put("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
		props.put("hibernate.connection.url", "jdbc:mysql://localhost:3306/"
				+ DATABASE_NAME);
		props.put("hibernate.connection.username", DATABASE_USER);
		props.put("hibernate.connection.password", DATABASE_PASS);
		props.put("hibernate.dialect",
				"org.hibernate.dialect.MySQLInnoDBDialect");
 
		props.put("hibernate.hbm2ddl.auto", "update");
 
		return CDOServerUtil.createRepository(REPOSITORY_NAME, createStore(),
				props);
	}
 
	/** 
	 * create the CDO store 
	 * @return the CDO store to use with the repository 
	 */
	private IStore createStore() {
		return new HibernateStore(new TeneoHibernateMappingProvider());
	}	
}

Back to the top