source code formatting.
[utils] / test / eclipselink / src / main / java / org / wamblee / support / persistence / eclipselink / JndiSessionCustomizer.java
1 /*
2  * SCJD assignment, URLyBird, Erik Brakkee.
3  * Candidate ID: sr1399267.
4  */
5 package org.wamblee.support.persistence.eclipselink;
6
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;
12
13 import javax.naming.Context;
14 import javax.naming.InitialContext;
15
16
17 /**
18  * See
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
23  * JNDI stub.
24  */
25 public class JndiSessionCustomizer implements SessionCustomizer {
26 /**
27      * Creates a new JndiSessionCustomizer object.
28      */
29     public JndiSessionCustomizer() {
30         // Empty.
31     }
32
33     /**
34      * Get a dataSource connection and set it on the session with
35      * lookupType=STRING_LOOKUP
36      *
37      * @param session DOCUMENT ME!
38      *
39      * @throws Exception DOCUMENT ME!
40      */
41     public void customize(Session session) throws Exception {
42         JNDIConnector connector = null;
43         Context       context   = null;
44
45         try {
46             context = new InitialContext();
47
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
54
55                 connector.setLookupType(JNDIConnector.STRING_LOOKUP);
56
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()
59                     .getConnector();
60                 writeConnector.setLookupType(JNDIConnector.STRING_LOOKUP);
61
62                 JNDIConnector readConnector = (JNDIConnector) ((DatabaseLogin) ((ServerSession) session).getReadConnectionPool()
63                     .getLogin()).getConnector();
64                 readConnector.setLookupType(JNDIConnector.STRING_LOOKUP);
65
66                 System.out.println("JndiSessionCustomizer: configured "
67                     + connector.getName());
68             } else {
69                 throw new Exception("JndiSessionCustomizer: Context is null");
70             }
71         } catch (Exception e) {
72             e.printStackTrace();
73         }
74     }
75 }