2 * Copyright 2005-2010 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.support.persistence.toplink;
18 import oracle.toplink.essentials.jndi.JNDIConnector;
19 import oracle.toplink.essentials.sessions.DatabaseLogin;
20 import oracle.toplink.essentials.sessions.Session;
21 import oracle.toplink.essentials.threetier.ServerSession;
22 import oracle.toplink.essentials.tools.sessionconfiguration.SessionCustomizer;
24 import javax.naming.Context;
25 import javax.naming.InitialContext;
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.
34 public class JndiSessionCustomizer implements SessionCustomizer {
36 * Creates a new JndiSessionCustomizer object.
38 public JndiSessionCustomizer() {
43 * Get a dataSource connection and set it on the session with
44 * lookupType=STRING_LOOKUP
48 public void customize(Session session) throws Exception {
49 JNDIConnector connector = null;
50 Context context = null;
53 context = new InitialContext();
55 if (null != context) {
56 connector = (JNDIConnector) session.getLogin().getConnector(); // possible
58 // Change from COMPOSITE_NAME_LOOKUP to STRING_LOOKUP
59 // Note: if both jta and non-jta elements exist this will only
60 // change the first one - and may still result in
61 // the COMPOSITE_NAME_LOOKUP being set
62 // Make sure only jta-data-source is in persistence.xml with no
63 // non-jta-data-source property set
65 connector.setLookupType(JNDIConnector.STRING_LOOKUP);
67 // Or, if you are specifying both JTA and non-JTA in your
68 // persistence.xml then set both connectors to be safe
69 JNDIConnector writeConnector = (JNDIConnector) session
70 .getLogin().getConnector();
71 writeConnector.setLookupType(JNDIConnector.STRING_LOOKUP);
73 JNDIConnector readConnector = (JNDIConnector) ((DatabaseLogin) ((ServerSession) session)
74 .getReadConnectionPool().getLogin()).getConnector();
75 readConnector.setLookupType(JNDIConnector.STRING_LOOKUP);
77 System.out.println("JndiSessionCustomizer: configured " +
80 throw new Exception("JndiSessionCustomizer: Context is null");
82 } catch (Exception e) {