DI Injection for Handlers [message #1174717] |
Thu, 07 November 2013 09:19 |
Ludwig Moser Messages: 476 Registered: July 2009 |
Senior Member |
|
|
Hello,
i am developing an E4 application with compatibility layer for E3 code because my actual code is E3 but i need some features from E4.
Overview of the Problem:
Createable (DAO)
E3 Handler (which needs the createable
E4 Handler, wrapping the E3 Handler (to use DI)
E4 Handler does not get the DAO (the Createable)
for example i got an DAO (Data Access Object) defined like this:
@Creatable
public class DAO {
@Inject
@Preference(nodePath="/instance")
IEclipsePreferences preferences;
@Inject
@GeminiPersistenceContext(unitName = "configured", properties = {
@GeminiPersistenceProperty(name = PersistenceUnitProperties.JDBC_DRIVER, valuePref = @Preference(nodePath="/instance", value="jdbc_driver")),
@GeminiPersistenceProperty(name = PersistenceUnitProperties.JDBC_URL, valuePref = @Preference(nodePath="/instance", value="jdbc_url")),
@GeminiPersistenceProperty(name = PersistenceUnitProperties.JDBC_USER, valuePref = @Preference(nodePath="/instance", value="jdbc_user")),
@GeminiPersistenceProperty(name = PersistenceUnitProperties.JDBC_PASSWORD, valuePref = @Preference(nodePath="/instance", value="jdbc_pwd")),
@GeminiPersistenceProperty(name = PersistenceUnitProperties.LOGGING_LEVEL, value = "FINE"),
@GeminiPersistenceProperty(name = PersistenceUnitProperties.WEAVING, value = "false"),
@GeminiPersistenceProperty(name = PersistenceUnitProperties.WEAVING_INTERNAL, value = "false"),
@GeminiPersistenceProperty(name = GeminiPersistenceProperties.GEMINI_REINIT) })
private EntityManager em;
@Inject
public DAO() {
preferences.put(GeminiPersistenceProperties.GEMINI_REINIT, "false");
try {
preferences.flush();
} catch (BackingStoreException e) {
e.printStackTrace();
}
}
public Object void save(Object dataObj) throws SQLException {
checkConnection();
EntityTransaction trx = em.getTransaction();
trx.begin();
em.persist(dataObj);
trx.commit();
}
@PreDestroy
public void destroy() {
if (em != null && em.isOpen()) {
em.close();
}
}
private void checkConnection() throws SQLException {
if (em == null) {
throw new SQLException(
"EntityManager is null. Not connected to database!");
}
}
}
further i got an handler (POJO)
public class TestSaveDBHandler {
@Execute
public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell,
DAO dao) throws InvocationTargetException, InterruptedException {
try {
// Via injected DAO
Contact c = BioofficeFactory.eINSTANCE.createContact();
c.setFirstName("Ludwig");
c.setLastName("Spitzenstätter via DAO");
System.out.println("dao=" + dao);
System.out.println("contact=" + c);
dao.save(c);
} catch (Exception e) {
MessageDialog.openError(shell, "Error persisting Contact",
"Something went wrong in the DAO \n " + e.getMessage());
}
}
}
i wrap this pojo handler to an E4 handler:
import org.eclipse.e4.tools.compat.parts.DIHandler;
public class E4TestSaveDBHandler extends DIHandler<TestSaveDBHandler> {
public E4TestSaveDBHandler() {
super(TestSaveDBHandler.class);
}
}
last but not least i define the handler in my plugin.xml
<command
defaultHandler="handlers.E4TestSaveDBHandler"
id="TestSaveDBHandler"
name="TestSaveDBHandler">
</command>
when executing the command in the rcp app i get the following exception
Caused by: org.eclipse.e4.core.di.InjectionException: Unable to process "TestSaveDBHandler#execute()": no actual value was found for the argument "DAO".
at org.eclipse.e4.core.internal.di.InjectorImpl.reportUnresolvedArgument(InjectorImpl.java:412)
at org.eclipse.e4.core.internal.di.InjectorImpl.invokeUsingClass(InjectorImpl.java:239)
at org.eclipse.e4.core.internal.di.InjectorImpl.invoke(InjectorImpl.java:213)
at org.eclipse.e4.core.contexts.ContextInjectionFactory.invoke(ContextInjectionFactory.java:90)
at org.eclipse.e4.tools.compat.parts.DIHandler.execute(DIHandler.java:48)
at org.eclipse.ui.internal.handlers.HandlerProxy.execute(HandlerProxy.java:290)
at org.eclipse.ui.internal.handlers.E4HandlerProxy.execute(E4HandlerProxy.java:90)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:56)
... 37 more
as DAO is defined as @Createable it shoule be available. so what am i missing?
i am not sure if this makes any difference: i do start the app via my .product
[Updated on: Thu, 07 November 2013 09:29] Report message to a moderator
|
|
|
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04683 seconds