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