X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Ftest%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fsupport%2Fpersistence%2Ftoplink%2FJndiSessionCustomizer.java;fp=support%2Ftest%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fsupport%2Fpersistence%2Ftoplink%2FJndiSessionCustomizer.java;h=814fe5933145a61fb31ec659c4116f0997de4f8d;hb=e8b7f484cbd8168c4341eee5cb020cdd080ce944;hp=0000000000000000000000000000000000000000;hpb=c6dd7d29643a8e4a6ec11088ecb5ab9f58845e0b;p=utils diff --git a/support/test/src/main/java/org/wamblee/support/persistence/toplink/JndiSessionCustomizer.java b/support/test/src/main/java/org/wamblee/support/persistence/toplink/JndiSessionCustomizer.java new file mode 100644 index 00000000..814fe593 --- /dev/null +++ b/support/test/src/main/java/org/wamblee/support/persistence/toplink/JndiSessionCustomizer.java @@ -0,0 +1,59 @@ +package org.wamblee.support.persistence.toplink; + +import javax.naming.Context; +import javax.naming.InitialContext; + +import oracle.toplink.essentials.jndi.JNDIConnector; +import oracle.toplink.essentials.sessions.DatabaseLogin; +import oracle.toplink.essentials.sessions.Session; +import oracle.toplink.essentials.threetier.ServerSession; +import oracle.toplink.essentials.tools.sessionconfiguration.SessionCustomizer; + +/** + * See http://wiki.eclipse.org/Customizing_the_EclipseLink_Application_(ELUG) Use for clients that would like to use a + * JTA SE pu instead of a RESOURCE_LOCAL SE pu. + * + * This utility also makes sure that using a persistence.xml with a JTA datasource works in a standalone Java SE + * environment together with our JNDI stub. + */ +public class JndiSessionCustomizer + implements SessionCustomizer { + + public JndiSessionCustomizer() { + // Empty. + } + + /** + * Get a dataSource connection and set it on the session with lookupType=STRING_LOOKUP + */ + public void customize(Session session) throws Exception { + JNDIConnector connector = null; + Context context = null; + try { + context = new InitialContext(); + if(null != context) { + connector = (JNDIConnector)session.getLogin().getConnector(); // possible CCE + // Change from COMPOSITE_NAME_LOOKUP to STRING_LOOKUP + // Note: if both jta and non-jta elements exist this will only change the first one - and may still result in + // the COMPOSITE_NAME_LOOKUP being set + // Make sure only jta-data-source is in persistence.xml with no non-jta-data-source property set + connector.setLookupType(JNDIConnector.STRING_LOOKUP); + + // Or, if you are specifying both JTA and non-JTA in your persistence.xml then set both connectors to be safe + JNDIConnector writeConnector = (JNDIConnector)session.getLogin().getConnector(); + writeConnector.setLookupType(JNDIConnector.STRING_LOOKUP); + JNDIConnector readConnector = + (JNDIConnector)((DatabaseLogin)((ServerSession)session).getReadConnectionPool().getLogin()).getConnector(); + readConnector.setLookupType(JNDIConnector.STRING_LOOKUP); + + System.out.println("JndiSessionCustomizer: configured " + connector.getName()); + } + else { + throw new Exception("JndiSessionCustomizer: Context is null"); + } + } + catch(Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file