Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » e4 DI, Gemini & Persistence
e4 DI, Gemini & Persistence [message #798896] Wed, 15 February 2012 02:53 Go to next message
Filipp A. is currently offline Filipp A.
Messages: 49
Registered: February 2010
Member
Hi all, i try to implement @PersistenceContext with different Persistence Units and Gemini in e4 with DI, but somthing is wrong. For no double postings, please see hier http://www.eclipse.org/forums/index.php/t/290891/
Thank you in advance!
Re: e4 DI, Gemini & Persistence [message #798925 is a reply to message #798896] Wed, 15 February 2012 03:27 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5278
Registered: July 2009
Senior Member
First of all on stuff all you should not use @Inject with handlers but
let the framework pass all informations in the @Execute-method.

2nd e4-DI has no idea about @PersistenceContext and so it can't cope
with it and even if it has an idea about this annotation you are missing
@Inject so the code has to look like this:

------8<------
@Inject
private EntityManagerProducer emp;

@Inject
@PersistenceContext
private EntityManager entityManager;

@Inject
@PersistenceContext(unitName="puTest")
private EntityManager entityManager2;
------8<------

3rd as said e4 does not know about @PersistenceContext and you need to
teach it what todo with it. In my EclipseCon Europe talk "Eclipse 4
Application Platform: Not commonly known features of the new platform"
[1] I show how to teach e4-di new annotations.

You can study a full example in the following e4-bundles:
*
http://git.eclipse.org/c/e4/org.eclipse.e4.tools.git/tree/bundles/org.eclipse.e4.tools.services
=> the @Translation and TranslationObjectSupplier

*
http://git.eclipse.org/c/platform/eclipse.platform.runtime.git/tree/bundles/org.eclipse.e4.core.di.extensions
=> @EventTopic/EventObjectSupplier and
@Preference/PreferencesObjectSupplier

*
http://git.eclipse.org/c/platform/eclipse.platform.ui.git/tree/bundles/org.eclipse.e4.ui.di
=> @UIEventTopic/UIEventObjectSupplier

4th is EntityManagerProducer an OSGi-Service, else it won't be injected
as well but you don't mention it to be null.

My guess is that the EntityManagerProducer is used to create an
EntityManager and so it should be quite easy to implement an
ObjectSupplier and register it via OSGi-DS.

I hope this helps you and maybe you can convince the Gemini guys to
provide such an e4 di extension out of the box which I think would a
really cool thing.

Tom

[1]http://tomsondev.bestsolution.at/2011/11/07/slides-from-latest-talks/

Am 15.02.12 08:53, schrieb Filipp A.:
> Hi all, i try to implement @PersistenceContext with different
> Persistence Units and Gemini in e4 with DI, but somthing is wrong. For
> no double postings, please see hier
> http://www.eclipse.org/forums/index.php/t/290891/
> Thank you in advance!
Re: e4 DI, Gemini &amp; Persistence [message #798931 is a reply to message #798925] Wed, 15 February 2012 03:34 Go to previous messageGo to next message
Filipp A. is currently offline Filipp A.
Messages: 49
Registered: February 2010
Member
Thank you Tom, i try it now!
Re: e4 DI, Gemini &amp; Persistence [message #799175 is a reply to message #798931] Wed, 15 February 2012 10:47 Go to previous messageGo to next message
Filipp A. is currently offline Filipp A.
Messages: 49
Registered: February 2010
Member
I playing with DS but not so successfully...
firsts i want to get EntityManagerFactory with annotation
@Inject
@PersistenceUnit(unitName="puTest")
EntityManagerFactory emf;

i have /OSGI-INF/geminiEMF.xml
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="geminiEMFsupplier">
   <implementation class="org.e4.gemini.di.GeminiEMFObjectSupplier"/>
   <service>
      <provide interface="javax.persistence.EntityManagerFactory"/>
   </service>
   <property name="dependency.injection.annotation" type="String" value="javax.persistence.PersistenceUnit"/>
</scr:component>

in /META-INF/MANIFEST.MF i added
Service-Component: OSGI-INF/geminiEMF.xml

GeminiEMFObjectSupplier.java
package org.e4.gemini.di;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.EntityManagerFactory;
import javax.persistence.spi.PersistenceUnitTransactionType;

import org.eclipse.e4.core.di.suppliers.ExtendedObjectSupplier;
import org.eclipse.e4.core.di.suppliers.IObjectDescriptor;
import org.eclipse.e4.core.di.suppliers.IRequestor;
import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.service.jpa.EntityManagerFactoryBuilder;

public class GeminiEMFObjectSupplier extends ExtendedObjectSupplier {

	/**
	 * for future use
	 * Read config from preferences
	 * @Inject
	 * @Preference(value = "jdbc_driver")
	 */
	private String jdbc_driver = "org.gjt.mm.mysql.Driver";
	private String jdbc_url = "jdbc:mysql://127.0.0.1/test";
	private String jdbc_user = "test";
	private String jdbc_password = "test";
	private boolean ddl_drop_and_create_tables = true;
	private boolean ddl_create_tables = false;
	private int min_conn = 20;
	private String logging = "FINEST";
	
	@Override
	public Object get(IObjectDescriptor descriptor, IRequestor requestor, boolean track, boolean group) {
		Class<?> descriptorsClass = getDesiredClass(descriptor.getDesiredType());
		String unitName = getUnitName(descriptor, requestor.getRequestingObjectClass());
		
		EntityManagerFactoryBuilder emfb = lookupEntityManagerFactoryBuilder(unitName);
		if(emfb == null){
			System.err.println("EntityManagerFactoryBuilder is null...");
			return null;
		}
		EntityManagerFactory emf = emfb.createEntityManagerFactory(getEntityManagerProperties());
		
//////////////////////////////////////////////
// ???????????????????????????????
//////////////////////////////////////////////

		return null;
	}
	
	private Class<?> getDesiredClass(Type desiredType) {
		if (desiredType instanceof Class<?>)
			return (Class<?>) desiredType;
		if (desiredType instanceof ParameterizedType) {
			Type rawType = ((ParameterizedType) desiredType).getRawType();
			if (rawType instanceof Class<?>)
				return (Class<?>) rawType;
		}
		return null;
	}
	
	private String getUnitName(IObjectDescriptor descriptor, Class<?> requestingObject) {
        if (descriptor == null)
                return null;
        GeminiPersistenceContext qualifier = descriptor.getQualifier(GeminiPersistenceContext.class);
        return qualifier.unitName();
	}
	
	public EntityManagerFactoryBuilder lookupEntityManagerFactoryBuilder(String puName) {
		//Get Context
		Bundle bundle = FrameworkUtil.getBundle(getClass());
		BundleContext context = bundle.getBundleContext();
		
		//lookup EMFB
		String filter = "(osgi.unit.name=" + puName + ")";
		ServiceReference<?>[] refs = null;
		try {
			refs = context.getServiceReferences(EntityManagerFactoryBuilder.class.getName(), filter);
		} catch (InvalidSyntaxException isEx) {
			new RuntimeException("Bad filter", isEx);
		}
		System.out.println("EMF Builder Service refs looked up from registry: " + refs);
		return (refs == null) ? null : (EntityManagerFactoryBuilder) context.getService(refs[0]);
	}
	
	protected Map<String, Object> getEntityManagerProperties(){
		Map<String, Object> properties = new HashMap<String, Object>();
		properties.put(PersistenceUnitProperties.TRANSACTION_TYPE, PersistenceUnitTransactionType.RESOURCE_LOCAL.name());
		properties.put(PersistenceUnitProperties.JDBC_DRIVER, jdbc_driver );
		properties.put(PersistenceUnitProperties.JDBC_URL, jdbc_url);
		properties.put(PersistenceUnitProperties.JDBC_USER, jdbc_user);
		properties.put(PersistenceUnitProperties.JDBC_PASSWORD, jdbc_password);
		if(ddl_drop_and_create_tables) {
			properties.put(PersistenceUnitProperties.DDL_GENERATION, "drop-and-create-tables");
		}
		if(ddl_create_tables) {
			properties.put(PersistenceUnitProperties.DDL_GENERATION, "create-tables");
		}
		properties.put(PersistenceUnitProperties.DDL_GENERATION_MODE, "database");
		properties.put(PersistenceUnitProperties.CONNECTION_POOL_MIN, min_conn);
			
		properties.put(PersistenceUnitProperties.TEMPORAL_MUTABLE, "true");
		
		// Logging
		properties.put(PersistenceUnitProperties.LOGGING_LEVEL, logging);
		properties.put(PersistenceUnitProperties.LOGGING_TIMESTAMP, "true");
		properties.put(PersistenceUnitProperties.LOGGING_SESSION, "true");
		properties.put(PersistenceUnitProperties.LOGGING_THREAD, "true");
		properties.put(PersistenceUnitProperties.LOGGING_EXCEPTIONS, "true");
		
		properties.put(PersistenceUnitProperties.WEAVING, "false");
		properties.put(PersistenceUnitProperties.WEAVING_INTERNAL, "false");
//		properties.put("eclipselink.weaving", "dynamic");

		System.out.println(properties.toString());
		return properties;
	}
}


i am not really understand what must be in public Object get(IObjectDescriptor descriptor, IRequestor requestor, boolean track, boolean group)
can you please explain.
Did i forget anything else?
Re: e4 DI, Gemini &amp;amp; Persistence [message #799312 is a reply to message #799175] Wed, 15 February 2012 13:22 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5278
Registered: July 2009
Senior Member
I'd say you simply return emf instead of null in the get() - that's it.

Tom

Am 15.02.12 16:47, schrieb Filipp A.:
> I playing with DS but not so successfully...
> firsts i want to get EntityManagerFactory with annotation
>
> @Inject
> @PersistenceUnit(unitName="puTest")
> EntityManagerFactory emf;
>
> i have /OSGI-INF/geminiEMF.xml
>
> <?xml version="1.0" encoding="UTF-8"?>
> <scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0"
> name="geminiEMFsupplier">
> <implementation class="org.e4.gemini.di.GeminiEMFObjectSupplier"/>
> <service>
> <provide interface="javax.persistence.EntityManagerFactory"/>
> </service>
> <property name="dependency.injection.annotation" type="String"
> value="javax.persistence.PersistenceUnit"/>
> </scr:component>
>
> in /META-INF/MANIFEST.MF i added
>
> Service-Component: OSGI-INF/geminiEMF.xml
>
> GeminiEMFObjectSupplier.java
>
> package org.e4.gemini.di;
>
> import java.lang.reflect.ParameterizedType;
> import java.lang.reflect.Type;
> import java.util.HashMap;
> import java.util.Map;
>
> import javax.persistence.EntityManagerFactory;
> import javax.persistence.spi.PersistenceUnitTransactionType;
>
> import org.eclipse.e4.core.di.suppliers.ExtendedObjectSupplier;
> import org.eclipse.e4.core.di.suppliers.IObjectDescriptor;
> import org.eclipse.e4.core.di.suppliers.IRequestor;
> import org.eclipse.persistence.config.PersistenceUnitProperties;
> import org.osgi.framework.Bundle;
> import org.osgi.framework.BundleContext;
> import org.osgi.framework.FrameworkUtil;
> import org.osgi.framework.InvalidSyntaxException;
> import org.osgi.framework.ServiceReference;
> import org.osgi.service.jpa.EntityManagerFactoryBuilder;
>
> public class GeminiEMFObjectSupplier extends ExtendedObjectSupplier {
>
> /**
> * for future use
> * Read config from preferences
> * @Inject
> * @Preference(value = "jdbc_driver")
> */
> private String jdbc_driver = "org.gjt.mm.mysql.Driver";
> private String jdbc_url = "jdbc:mysql://127.0.0.1/test";
> private String jdbc_user = "test";
> private String jdbc_password = "test";
> private boolean ddl_drop_and_create_tables = true;
> private boolean ddl_create_tables = false;
> private int min_conn = 20;
> private String logging = "FINEST";
>
> @Override
> public Object get(IObjectDescriptor descriptor, IRequestor
> requestor, boolean track, boolean group) {
> Class<?> descriptorsClass =
> getDesiredClass(descriptor.getDesiredType());
> String unitName = getUnitName(descriptor,
> requestor.getRequestingObjectClass());
>
> EntityManagerFactoryBuilder emfb =
> lookupEntityManagerFactoryBuilder(unitName);
> if(emfb == null){
> System.err.println("EntityManagerFactoryBuilder is null...");
> return null;
> }
> EntityManagerFactory emf =
> emfb.createEntityManagerFactory(getEntityManagerProperties());
>
> //////////////////////////////////////////////
> // ???????????????????????????????
> //////////////////////////////////////////////
>
> return null;
> }
>
> private Class<?> getDesiredClass(Type desiredType) {
> if (desiredType instanceof Class<?>)
> return (Class<?>) desiredType;
> if (desiredType instanceof ParameterizedType) {
> Type rawType = ((ParameterizedType) desiredType).getRawType();
> if (rawType instanceof Class<?>)
> return (Class<?>) rawType;
> }
> return null;
> }
>
> private String getUnitName(IObjectDescriptor descriptor, Class<?>
> requestingObject) {
> if (descriptor == null)
> return null;
> GeminiPersistenceContext qualifier =
> descriptor.getQualifier(GeminiPersistenceContext.class);
> return qualifier.unitName();
> }
>
> public EntityManagerFactoryBuilder
> lookupEntityManagerFactoryBuilder(String puName) {
> //Get Context
> Bundle bundle = FrameworkUtil.getBundle(getClass());
> BundleContext context = bundle.getBundleContext();
>
> //lookup EMFB
> String filter = "(osgi.unit.name=" + puName + ")";
> ServiceReference<?>[] refs = null;
> try {
> refs =
> context.getServiceReferences(EntityManagerFactoryBuilder.class.getName(), filter);
>
> } catch (InvalidSyntaxException isEx) {
> new RuntimeException("Bad filter", isEx);
> }
> System.out.println("EMF Builder Service refs looked up from
> registry: " + refs);
> return (refs == null) ? null : (EntityManagerFactoryBuilder)
> context.getService(refs[0]);
> }
>
> protected Map<String, Object> getEntityManagerProperties(){
> Map<String, Object> properties = new HashMap<String, Object>();
> properties.put(PersistenceUnitProperties.TRANSACTION_TYPE,
> PersistenceUnitTransactionType.RESOURCE_LOCAL.name());
> properties.put(PersistenceUnitProperties.JDBC_DRIVER,
> jdbc_driver );
> properties.put(PersistenceUnitProperties.JDBC_URL, jdbc_url);
> properties.put(PersistenceUnitProperties.JDBC_USER, jdbc_user);
> properties.put(PersistenceUnitProperties.JDBC_PASSWORD,
> jdbc_password);
> if(ddl_drop_and_create_tables) {
> properties.put(PersistenceUnitProperties.DDL_GENERATION,
> "drop-and-create-tables");
> }
> if(ddl_create_tables) {
> properties.put(PersistenceUnitProperties.DDL_GENERATION,
> "create-tables");
> }
> properties.put(PersistenceUnitProperties.DDL_GENERATION_MODE,
> "database");
> properties.put(PersistenceUnitProperties.CONNECTION_POOL_MIN,
> min_conn);
>
> properties.put(PersistenceUnitProperties.TEMPORAL_MUTABLE, "true");
>
> // Logging
> properties.put(PersistenceUnitProperties.LOGGING_LEVEL, logging);
> properties.put(PersistenceUnitProperties.LOGGING_TIMESTAMP,
> "true");
> properties.put(PersistenceUnitProperties.LOGGING_SESSION, "true");
> properties.put(PersistenceUnitProperties.LOGGING_THREAD, "true");
> properties.put(PersistenceUnitProperties.LOGGING_EXCEPTIONS,
> "true");
>
> properties.put(PersistenceUnitProperties.WEAVING, "false");
> properties.put(PersistenceUnitProperties.WEAVING_INTERNAL,
> "false");
> // properties.put("eclipselink.weaving", "dynamic");
>
> System.out.println(properties.toString());
> return properties;
> }
> }
>
>
> i am not really understand what must be in public Object
> get(IObjectDescriptor descriptor, IRequestor requestor, boolean track,
> boolean group)
> can you please explain.
> Did i forget anything else?
Re: e4 DI, Gemini &amp;amp; Persistence [message #799320 is a reply to message #799312] Wed, 15 February 2012 13:41 Go to previous messageGo to next message
Filipp A. is currently offline Filipp A.
Messages: 49
Registered: February 2010
Member
i get 2 exceptions, and i saw, that javax.persistence.PersistenceUnit annotation don't have @Qualifier. is it required ? do i need to reimplement PersistenceUnit 1:1 with @Qualifier ?
!ENTRY org.e4.gemini.util 4 0 2012-02-15 19:36:12.861
!MESSAGE FrameworkEvent ERROR
!STACK 0
org.osgi.framework.ServiceException: org.eclipse.equinox.internal.ds.ServiceReg.getService() returned a service object that is not an instance of the service class javax.persistence.EntityManagerFactory
	at org.eclipse.osgi.internal.serviceregistry.ServiceUse.getService(ServiceUse.java:174)
	at org.eclipse.osgi.internal.serviceregistry.ServiceRegistrationImpl.getService(ServiceRegistrationImpl.java:468)
	at org.eclipse.osgi.internal.serviceregistry.ServiceRegistry.getService(ServiceRegistry.java:467)
	at org.eclipse.osgi.framework.internal.core.BundleContextImpl.getService(BundleContextImpl.java:594)
	at org.e4.gemini.test.data.EMFServiceTracker.addingService(EMFServiceTracker.java:20)
	at org.osgi.util.tracker.ServiceTracker$Tracked.customizerAdding(ServiceTracker.java:990)
	at org.osgi.util.tracker.ServiceTracker$Tracked.customizerAdding(ServiceTracker.java:1)
	at org.osgi.util.tracker.AbstractTracked.trackAdding(AbstractTracked.java:262)
	at org.osgi.util.tracker.AbstractTracked.trackInitial(AbstractTracked.java:185)
	at org.osgi.util.tracker.ServiceTracker.open(ServiceTracker.java:348)
	at org.osgi.util.tracker.ServiceTracker.open(ServiceTracker.java:283)
	at org.e4.gemini.test.data.Activator.initService(Activator.java:40)
	at org.e4.gemini.test.data.Activator.start(Activator.java:24)
	at org.eclipse.osgi.framework.internal.core.BundleContextImpl$1.run(BundleContextImpl.java:711)
	at java.security.AccessController.doPrivileged(Native Method)
	at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:702)
	at org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:683)
	at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:381)
	at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:300)
	at org.eclipse.osgi.framework.internal.core.PackageAdminImpl.resumeBundles(PackageAdminImpl.java:314)
	at org.eclipse.osgi.framework.internal.core.PackageAdminImpl.processDelta(PackageAdminImpl.java:558)
	at org.eclipse.osgi.framework.internal.core.PackageAdminImpl.doResolveBundles(PackageAdminImpl.java:254)
	at org.eclipse.osgi.framework.internal.core.PackageAdminImpl$1.run(PackageAdminImpl.java:174)
	at java.lang.Thread.run(Thread.java:680)

!ENTRY org.eclipse.e4.ui.workbench 4 0 2012-02-15 19:36:14.596
!MESSAGE Unable to create class 'org.e4.gemini.rcp.handlers.OpenHandler' from bundle '50'
!STACK 0
org.eclipse.e4.core.di.InjectionException: Unable to process "OpenHandler.emf": no actual value was found for the argument "EntityManagerFactory".
	at org.eclipse.e4.core.internal.di.InjectorImpl.reportUnresolvedArgument(InjectorImpl.java:387)
	at org.eclipse.e4.core.internal.di.InjectorImpl.resolveRequestorArgs(InjectorImpl.java:382)
	at org.eclipse.e4.core.internal.di.InjectorImpl.inject(InjectorImpl.java:99)
	at org.eclipse.e4.core.internal.di.InjectorImpl.internalMake(InjectorImpl.java:317)
	at org.eclipse.e4.core.internal.di.InjectorImpl.make(InjectorImpl.java:239)
	at org.eclipse.e4.core.contexts.ContextInjectionFactory.make(ContextInjectionFactory.java:153)
	at org.eclipse.e4.ui.internal.workbench.ReflectionContributionFactory.createFromBundle(ReflectionContributionFactory.java:102)
	at org.eclipse.e4.ui.internal.workbench.ReflectionContributionFactory.doCreate(ReflectionContributionFactory.java:71)
	at org.eclipse.e4.ui.internal.workbench.ReflectionContributionFactory.create(ReflectionContributionFactory.java:53)
	at org.eclipse.e4.ui.internal.workbench.E4Workbench.processHierarchy(E4Workbench.java:166)
	at org.eclipse.e4.ui.internal.workbench.E4Workbench.init(E4Workbench.java:117)
	at org.eclipse.e4.ui.internal.workbench.E4Workbench.<init>(E4Workbench.java:68)
	at org.eclipse.e4.ui.internal.workbench.swt.E4Application.createE4Workbench(E4Application.java:298)
	at org.eclipse.e4.ui.internal.workbench.swt.E4Application.start(E4Application.java:129)
	at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
	at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
	at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
	at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:352)
	at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:624)
	at org.eclipse.equinox.launcher.Main.basicRun(Main.java:579)
	at org.eclipse.equinox.launcher.Main.run(Main.java:1433)
	at org.eclipse.equinox.launcher.Main.main(Main.java:1409)
Re: e4 DI, Gemini &amp;amp; Persistence [message #799421 is a reply to message #799320] Wed, 15 February 2012 16:41 Go to previous messageGo to next message
Filipp A. is currently offline Filipp A.
Messages: 49
Registered: February 2010
Member
i have made own implementation of PersistenceUnit -> GeminiPersistenceUnit
package org.e4.gemini.di;

import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import javax.inject.Qualifier;


@Qualifier
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
public @interface GeminiPersistenceUnit {

	String name() default "";

	String unitName() default "";

}

and yet i get only one exception
org.eclipse.e4.core.di.InjectionException: Unable to process "OpenHandler.emf": no actual value was found for the argument "EntityManagerFactory[@org.e4.gemini.di.GeminiPersistenceUnit(unitName=puTest, name=test)]".
	at org.eclipse.e4.core.internal.di.InjectorImpl.reportUnresolvedArgument(InjectorImpl.java:387)
...

i checked already everything...
my actual code
/OSGI-INF/geminiEMF.xml
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.e4.gemini.util">
   <implementation class="org.e4.gemini.di.GeminiEMFObjectSupplier"/>
   <service>
      <provide interface="org.e4.gemini.di.GeminiPersistenceUnit"/>
   </service>
   <property name="dependency.injection.annotation" type="String" value="org.e4.gemini.di.GeminiPersistenceUnit"/>
</scr:component>

META-INF/MANIFEST.MF
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: 
 Gemini DI
Bundle-SymbolicName: org.e4.gemini.util
Bundle-Version: 1.0.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: org.osgi.framework;version="1.3.0",
 javax.annotation;version="1.0.0",
 javax.inject;version="1.0.0"
Require-Bundle: javax.persistence;bundle-version="2.0.3";visibility:=reexport,
 org.eclipse.persistence.antlr;bundle-version="2.3.2";visibility:=reexport,
 org.eclipse.persistence.asm;bundle-version="2.3.2";visibility:=reexport,
 org.eclipse.persistence.core;bundle-version="2.3.2";visibility:=reexport,
 org.eclipse.persistence.jpa;bundle-version="2.3.2";visibility:=reexport,
 javax.inject;bundle-version="1.0.0";visibility:=reexport,
 org.eclipse.equinox.ds;bundle-version="1.4.0",
 org.eclipse.e4.core.di;bundle-version="1.1.0",
 org.eclipse.swt;bundle-version="3.7.0",
 org.eclipse.equinox.common;bundle-version="3.6.0",
 org.eclipse.e4.core.contexts;bundle-version="0.9.0",
 org.eclipse.osgi;bundle-version="3.7.0",
 org.eclipse.e4.core.services;bundle-version="0.9.1"
Export-Package: org.e4.gemini.di,
 org.e4.gemini.util
Service-Component: OSGI-INF/geminiEMF.xml
Bundle-ActivationPolicy: lazy

GeminiEMFObjectSupplier.java
package org.e4.gemini.di;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.EntityManagerFactory;
import javax.persistence.spi.PersistenceUnitTransactionType;

import org.eclipse.e4.core.di.suppliers.ExtendedObjectSupplier;
import org.eclipse.e4.core.di.suppliers.IObjectDescriptor;
import org.eclipse.e4.core.di.suppliers.IRequestor;
import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.service.jpa.EntityManagerFactoryBuilder;

@SuppressWarnings("restriction")
public class GeminiEMFObjectSupplier extends ExtendedObjectSupplier {

	/**
	 * for future use
	 * Read config from preferences
	 * @Inject
	 * @Preference(value = "jdbc_driver")
	 */
	private String jdbc_driver = "org.gjt.mm.mysql.Driver";
	private String jdbc_url = "jdbc:mysql://127.0.0.1/test";
	private String jdbc_user = "test";
	private String jdbc_password = "test";
	private boolean ddl_drop_and_create_tables = true;
	private boolean ddl_create_tables = false;
	private int min_conn = 20;
	private String logging = "FINEST";
	
	@Override
	public Object get(IObjectDescriptor descriptor, IRequestor requestor, boolean track, boolean group) {
//		Class<?> descriptorsClass = getDesiredClass(descriptor.getDesiredType());
		String unitName = getUnitName(descriptor, requestor.getRequestingObjectClass());
//		String unitName = "puTest";
		EntityManagerFactoryBuilder emfb = lookupEntityManagerFactoryBuilder(unitName);
		if(emfb == null){
			System.err.println("EntityManagerFactoryBuilder is null...");
			return null;
		}
		EntityManagerFactory emf = emfb.createEntityManagerFactory(getEntityManagerProperties());
		return emf;
	}
	
//	private Class<?> getDesiredClass(Type desiredType) {
//		if (desiredType instanceof Class<?>)
//			return (Class<?>) desiredType;
//		if (desiredType instanceof ParameterizedType) {
//			Type rawType = ((ParameterizedType) desiredType).getRawType();
//			if (rawType instanceof Class<?>)
//				return (Class<?>) rawType;
//		}
//		return null;
//	}
	
	private String getUnitName(IObjectDescriptor descriptor, Class<?> requestingObject) {
        if (descriptor == null)
                return null;
        GeminiPersistenceUnit qualifier = descriptor.getQualifier(GeminiPersistenceUnit.class);
        return qualifier.unitName();
	}
	
	public EntityManagerFactoryBuilder lookupEntityManagerFactoryBuilder(String puName) {
		//Get Context
		Bundle bundle = FrameworkUtil.getBundle(getClass());
		BundleContext context = bundle.getBundleContext();
		
		//lookup EMFB
		String filter = "(osgi.unit.name=" + puName + ")";
		ServiceReference<?>[] refs = null;
		try {
			refs = context.getServiceReferences(EntityManagerFactoryBuilder.class.getName(), filter);
		} catch (InvalidSyntaxException isEx) {
			new RuntimeException("Bad filter", isEx);
		}
//		System.out.println("EMF Builder Service refs looked up from registry: " + refs);
		return (refs == null) ? null : (EntityManagerFactoryBuilder) context.getService(refs[0]);
	}
	
	protected Map<String, Object> getEntityManagerProperties(){
		Map<String, Object> properties = new HashMap<String, Object>();
		properties.put(PersistenceUnitProperties.TRANSACTION_TYPE, PersistenceUnitTransactionType.RESOURCE_LOCAL.name());
		properties.put(PersistenceUnitProperties.JDBC_DRIVER, jdbc_driver );
		properties.put(PersistenceUnitProperties.JDBC_URL, jdbc_url);
		properties.put(PersistenceUnitProperties.JDBC_USER, jdbc_user);
		properties.put(PersistenceUnitProperties.JDBC_PASSWORD, jdbc_password);
		if(ddl_drop_and_create_tables) {
			properties.put(PersistenceUnitProperties.DDL_GENERATION, "drop-and-create-tables");
		}
		if(ddl_create_tables) {
			properties.put(PersistenceUnitProperties.DDL_GENERATION, "create-tables");
		}
		properties.put(PersistenceUnitProperties.DDL_GENERATION_MODE, "database");
		properties.put(PersistenceUnitProperties.CONNECTION_POOL_MIN, min_conn);
			
		properties.put(PersistenceUnitProperties.TEMPORAL_MUTABLE, "true");
		
		// Logging
		properties.put(PersistenceUnitProperties.LOGGING_LEVEL, logging);
		properties.put(PersistenceUnitProperties.LOGGING_TIMESTAMP, "true");
		properties.put(PersistenceUnitProperties.LOGGING_SESSION, "true");
		properties.put(PersistenceUnitProperties.LOGGING_THREAD, "true");
		properties.put(PersistenceUnitProperties.LOGGING_EXCEPTIONS, "true");
		
		properties.put(PersistenceUnitProperties.WEAVING, "false");
		properties.put(PersistenceUnitProperties.WEAVING_INTERNAL, "false");
//		properties.put("eclipselink.weaving", "dynamic");

//		System.out.println(properties.toString());
		return properties;
	}
}
Re: e4 DI, Gemini &amp;amp; Persistence [message #799659 is a reply to message #799421] Thu, 16 February 2012 01:29 Go to previous message
Filipp A. is currently offline Filipp A.
Messages: 49
Registered: February 2010
Member
I have it!
<provide interface="org.eclipse.e4.core.di.suppliers.ExtendedObjectSupplier"/>
Previous Topic:Create a modal window
Next Topic:@Preference(value="data") as value for other Annotation
Goto Forum:
  


Current Time: Thu Aug 21 02:17:59 EDT 2014

Powered by FUDForum. Page generated in 0.08161 seconds