Now using connections managed by JPA instead of using JNDI mocking.
[utils] / test / eclipselink / src / main / java / org / wamblee / support / persistence / eclipselink / JndiSessionCustomizer.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.support.persistence.eclipselink;
17
18 import org.eclipse.persistence.config.SessionCustomizer;
19 import org.eclipse.persistence.sessions.DatabaseLogin;
20 import org.eclipse.persistence.sessions.JNDIConnector;
21 import org.eclipse.persistence.sessions.Session;
22 import org.eclipse.persistence.sessions.server.ServerSession;
23
24 import javax.naming.Context;
25 import javax.naming.InitialContext;
26
27 /**
28  * See http://wiki.eclipse.org/Customizing_the_EclipseLink_Application_(ELUG)
29  * Use for clients that would like to use a JTA SE pu instead of a
30  * RESOURCE_LOCAL SE pu. This utility also makes sure that using a
31  * persistence.xml with a JTA datasource works in a standalone Java SE
32  * environment together with our JNDI stub.
33  */
34 public class JndiSessionCustomizer implements SessionCustomizer {
35     /**
36      * Creates a new JndiSessionCustomizer object.
37      */
38     public JndiSessionCustomizer() {
39         // Empty.
40     }
41
42     /**
43      * Get a dataSource connection and set it on the session with
44      * lookupType=STRING_LOOKUP
45      * 
46      * 
47      */
48     public void customize(Session aSession) throws Exception {
49         JNDIConnector connector = null;
50         Context context = null;
51
52         try {
53             context = new InitialContext();
54
55             connector = (JNDIConnector) aSession.getLogin().getConnector(); // possible
56             // CCE
57             // Change from COMPOSITE_NAME_LOOKUP to STRING_LOOKUP
58             // Note: if both jta and non-jta elements exist this will only
59             // change the first one - and may still result in
60             // the COMPOSITE_NAME_LOOKUP being set
61             // Make sure only jta-data-source is in persistence.xml with no
62             // non-jta-data-source property set
63
64             connector.setLookupType(JNDIConnector.STRING_LOOKUP);
65
66             // Or, if you are specifying both JTA and non-JTA in your
67             // persistence.xml then set both connectors to be safe
68             JNDIConnector writeConnector = (JNDIConnector) aSession.getLogin()
69                 .getConnector();
70             writeConnector.setLookupType(JNDIConnector.STRING_LOOKUP);
71
72             JNDIConnector readConnector = (JNDIConnector) ((DatabaseLogin) ((ServerSession) aSession)
73                 .getReadConnectionPool().getLogin()).getConnector();
74             readConnector.setLookupType(JNDIConnector.STRING_LOOKUP);
75
76             System.out.println("JndiSessionCustomizer: configured " +
77                 connector.getName());
78         } catch (Exception e) {
79             e.printStackTrace();
80         }
81     }
82 }