Removed DOCUMENT ME comments that were generated and applied source code
[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  * 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.
22  */
23 public class JndiSessionCustomizer implements SessionCustomizer {
24     /**
25      * Creates a new JndiSessionCustomizer object.
26      */
27     public JndiSessionCustomizer() {
28         // Empty.
29     }
30
31     /**
32      * Get a dataSource connection and set it on the session with
33      * lookupType=STRING_LOOKUP
34      * 
35      * 
36      */
37     public void customize(Session session) throws Exception {
38         JNDIConnector connector = null;
39         Context context = null;
40
41         try {
42             context = new InitialContext();
43
44             if (null != context) {
45                 connector = (JNDIConnector) session.getLogin().getConnector(); // possible
46                                                                                // CCE
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
53
54                 connector.setLookupType(JNDIConnector.STRING_LOOKUP);
55
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);
61
62                 JNDIConnector readConnector = (JNDIConnector) ((DatabaseLogin) ((ServerSession) session)
63                     .getReadConnectionPool().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 }