2 * SCJD assignment, URLyBird, Erik Brakkee.
3 * Candidate ID: sr1399267.
5 package org.wamblee.support.persistence.toplink;
7 import oracle.toplink.essentials.jndi.JNDIConnector;
8 import oracle.toplink.essentials.sessions.DatabaseLogin;
9 import oracle.toplink.essentials.sessions.Session;
10 import oracle.toplink.essentials.threetier.ServerSession;
11 import oracle.toplink.essentials.tools.sessionconfiguration.SessionCustomizer;
13 import javax.naming.Context;
14 import javax.naming.InitialContext;
19 * http://wiki.eclipse.org/Customizing_the_EclipseLink_Application_(ELUG) Use
20 * for clients that would like to use a JTA SE pu instead of a RESOURCE_LOCAL
21 * SE pu. This utility also makes sure that using a persistence.xml with a
22 * JTA datasource works in a standalone Java SE environment together with our
25 public class JndiSessionCustomizer implements SessionCustomizer {
27 * Creates a new JndiSessionCustomizer object.
29 public JndiSessionCustomizer() {
34 * Get a dataSource connection and set it on the session with
35 * lookupType=STRING_LOOKUP
37 * @param session DOCUMENT ME!
39 * @throws Exception DOCUMENT ME!
41 public void customize(Session session) throws Exception {
42 JNDIConnector connector = null;
43 Context context = null;
46 context = new InitialContext();
48 if (null != context) {
49 connector = (JNDIConnector) session.getLogin().getConnector(); // possible CCE
50 // Change from COMPOSITE_NAME_LOOKUP to STRING_LOOKUP
51 // Note: if both jta and non-jta elements exist this will only change the first one - and may still result in
52 // the COMPOSITE_NAME_LOOKUP being set
53 // Make sure only jta-data-source is in persistence.xml with no non-jta-data-source property set
55 connector.setLookupType(JNDIConnector.STRING_LOOKUP);
57 // Or, if you are specifying both JTA and non-JTA in your persistence.xml then set both connectors to be safe
58 JNDIConnector writeConnector = (JNDIConnector) session.getLogin()
60 writeConnector.setLookupType(JNDIConnector.STRING_LOOKUP);
62 JNDIConnector readConnector = (JNDIConnector) ((DatabaseLogin) ((ServerSession) session).getReadConnectionPool()
63 .getLogin()).getConnector();
64 readConnector.setLookupType(JNDIConnector.STRING_LOOKUP);
66 System.out.println("JndiSessionCustomizer: configured "
67 + connector.getName());
69 throw new Exception("JndiSessionCustomizer: Context is null");
71 } catch (Exception e) {