Skip to content

Commit af5961e

Browse files
authored
Merge pull request #31232 from tjwatson/31231-transactionJndi
Fix for #31231 to correctly list transaction objects from jndi
2 parents f497761 + 4883933 commit af5961e

File tree

5 files changed

+158
-36
lines changed

5 files changed

+158
-36
lines changed

dev/com.ibm.ws.transaction.core_fat.base/fat/src/com/ibm/ws/transaction/test/FATSuite.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2024 IBM Corporation and others.
2+
* Copyright (c) 2017, 2025 IBM Corporation and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License 2.0
55
* which accompanies this distribution, and is available at
@@ -19,6 +19,7 @@
1919

2020
import com.ibm.ws.transaction.test.tests.BadLogTest;
2121
import com.ibm.ws.transaction.test.tests.CorruptLogTest;
22+
import com.ibm.ws.transaction.test.tests.JndiTest;
2223
import com.ibm.ws.transaction.test.tests.SimpleTest;
2324

2425
import componenttest.rules.repeater.FeatureReplacementAction;
@@ -29,6 +30,7 @@
2930
CorruptLogTest.class,
3031
BadLogTest.class,
3132
SimpleTest.class,
33+
JndiTest.class
3234
})
3335
public class FATSuite {
3436

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 IBM Corporation and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License 2.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* IBM Corporation - initial API and implementation
12+
*******************************************************************************/
13+
package com.ibm.ws.transaction.test.tests;
14+
15+
import org.junit.AfterClass;
16+
import org.junit.BeforeClass;
17+
import org.junit.runner.RunWith;
18+
19+
import com.ibm.websphere.simplicity.ShrinkHelper;
20+
21+
import componenttest.annotation.Server;
22+
import componenttest.annotation.TestServlet;
23+
import componenttest.custom.junit.runner.FATRunner;
24+
import componenttest.topology.impl.LibertyServer;
25+
import componenttest.topology.utils.FATServletClient;
26+
import servlets.JavaCompServlet;
27+
28+
@RunWith(FATRunner.class)
29+
public class JndiTest extends FATServletClient {
30+
31+
public static final String APP_NAME = "transaction";
32+
33+
@Server("com.ibm.ws.transaction")
34+
@TestServlet(servlet = JavaCompServlet.class, contextRoot = APP_NAME)
35+
public static LibertyServer server;
36+
37+
@BeforeClass
38+
public static void setUp() throws Exception {
39+
40+
ShrinkHelper.defaultApp(server, APP_NAME, "servlets.*");
41+
server.startServer();
42+
}
43+
44+
@AfterClass
45+
public static void tearDown() throws Exception {
46+
server.stopServer();
47+
}
48+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 IBM Corporation and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License 2.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* IBM Corporation - initial API and implementation
12+
*******************************************************************************/
13+
package servlets;
14+
15+
import static org.junit.Assert.assertEquals;
16+
17+
import java.util.Collections;
18+
import java.util.Map;
19+
import java.util.stream.Collectors;
20+
21+
import javax.naming.Context;
22+
import javax.naming.InitialContext;
23+
import javax.naming.NameClassPair;
24+
import javax.naming.NamingEnumeration;
25+
import javax.servlet.annotation.WebServlet;
26+
import javax.transaction.TransactionManager;
27+
import javax.transaction.TransactionSynchronizationRegistry;
28+
import javax.transaction.UserTransaction;
29+
30+
import org.junit.Test;
31+
32+
import componenttest.app.FATServlet;
33+
34+
@SuppressWarnings("serial")
35+
@WebServlet("/JavaCompServlet")
36+
public class JavaCompServlet extends FATServlet {
37+
38+
@Test
39+
public void testListJavaComp() throws Exception {
40+
NamingEnumeration<NameClassPair> ne = ((Context) new InitialContext().lookup("java:comp")).list("");
41+
Map<String, String> found = Collections.list(ne)
42+
.stream()
43+
.filter((n) -> n.getName().contains("Transaction"))
44+
.collect(Collectors.toMap((n) -> n.getName(), (n) -> n.getClassName()));
45+
assertEquals("Wrong number found: " + found, 3, found.size());
46+
assertEquals("No TransactionManager", TransactionManager.class.getName(), found.get("TransactionManager"));
47+
assertEquals("No TransactionSynchronizationRegistry", TransactionSynchronizationRegistry.class.getName(), found.get("TransactionSynchronizationRegistry"));
48+
assertEquals("No UserTransaction", UserTransaction.class.getName(), found.get("UserTransaction"));
49+
}
50+
51+
@Test
52+
public void testListJavaCompWebSphere() throws Exception {
53+
NamingEnumeration<NameClassPair> ne = ((Context) new InitialContext().lookup("java:comp/websphere")).list("");
54+
Map<String, String> found = Collections.list(ne)
55+
.stream()
56+
.collect(Collectors.toMap((n) -> n.getName(), (n) -> n.getClassName()));
57+
assertEquals("Wrong number found: " + found, 2, found.size());
58+
assertEquals("No ExtendedJTATransaction", "com.ibm.websphere.jtaextensions.ExtendedJTATransaction", found.get("ExtendedJTATransaction"));
59+
assertEquals("No UOWManager", com.ibm.wsspi.uow.UOWManager.class.getName(), found.get("UOWManager"));
60+
}
61+
}

dev/com.ibm.ws.transaction/src/com/ibm/ws/transaction/services/TransactionJavaColonHelper.java

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2024 IBM Corporation and others.
2+
* Copyright (c) 2010, 2025 IBM Corporation and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License 2.0
55
* which accompanies this distribution, and is available at
@@ -25,18 +25,17 @@
2525
import javax.naming.InvalidNameException;
2626
import javax.naming.NameClassPair;
2727
import javax.naming.NamingException;
28+
import javax.transaction.TransactionManager;
2829
import javax.transaction.TransactionSynchronizationRegistry;
2930
import javax.transaction.UserTransaction;
3031

3132
import org.osgi.framework.ServiceReference;
3233
import org.osgi.service.component.ComponentContext;
3334

3435
import com.ibm.tx.jta.embeddable.UserTransactionDecorator;
35-
import com.ibm.tx.jta.embeddable.impl.EmbeddableUserTransactionImpl;
3636
import com.ibm.ws.container.service.naming.JavaColonNamingHelper;
3737
import com.ibm.ws.container.service.naming.NamingConstants.JavaColonNamespace;
3838
import com.ibm.ws.tx.embeddable.EmbeddableWebSphereUserTransaction;
39-
import com.ibm.ws.uow.embeddable.UOWManager;
4039
import com.ibm.wsspi.kernel.service.utils.AtomicServiceReference;
4140

4241
/**
@@ -97,20 +96,27 @@ protected void unsetUserTransactionDecorator(ServiceReference<UserTransactionDec
9796
@Override
9897
public Object getObjectInstance(JavaColonNamespace namespace, String name) throws NamingException {
9998
switch (namespace) {
100-
case COMP:
101-
switch (name) {
102-
case "TransactionManager": return getTransactionManager();
103-
case "TransactionSynchronizationRegistry": return getTransactionSynchronizationRegistry();
104-
// If we have a service reference we know it's safe to return a reference
105-
case "UserTransaction": return userTranSvcRef == null ? null : getUserTransaction(false, null);
106-
default: return null;
107-
}
108-
case COMP_WS:
109-
switch (name) {
110-
case "UOWManager": return getUOWManager();
111-
default: return null;
112-
}
113-
default: return null;
99+
case COMP:
100+
switch (name) {
101+
case "TransactionManager":
102+
return getTransactionManager();
103+
case "TransactionSynchronizationRegistry":
104+
return getTransactionSynchronizationRegistry();
105+
// If we have a service reference we know it's safe to return a reference
106+
case "UserTransaction":
107+
return userTranSvcRef == null ? null : getUserTransaction(false, null);
108+
default:
109+
return null;
110+
}
111+
case COMP_WS:
112+
switch (name) {
113+
case "UOWManager":
114+
return getUOWManager();
115+
default:
116+
return null;
117+
}
118+
default:
119+
return null;
114120
}
115121
}
116122

@@ -130,27 +136,33 @@ public boolean hasObjectWithPrefix(JavaColonNamespace namespace, String name) th
130136
/** {@inheritDoc} */
131137
@Override
132138
public Collection<? extends NameClassPair> listInstances(JavaColonNamespace namespace, String nameInContext) {
133-
134-
if (JavaColonNamespace.COMP.equals(namespace) && "".equals(nameInContext)) {
135-
ArrayList<NameClassPair> retVal = new ArrayList<>();
136-
if (userTranSvcRef != null) {
137-
NameClassPair pair = new NameClassPair(nameInContext, EmbeddableUserTransactionImpl.class.getName());
138-
retVal.add(pair);
139-
}
140-
141-
retVal.add(new NameClassPair(nameInContext, TransactionSynchronizationRegistry.class.getName()));
142-
retVal.add(new NameClassPair(nameInContext, UOWManager.class.getName()));
143-
144-
return retVal;
145-
} else {
139+
if (!"".equals(nameInContext)) {
146140
return Collections.emptyList();
147141
}
142+
switch (namespace) {
143+
case COMP:
144+
ArrayList<NameClassPair> retVal = new ArrayList<>();
145+
if (userTranSvcRef != null) {
146+
NameClassPair pair = new NameClassPair("UserTransaction", UserTransaction.class.getName());
147+
retVal.add(pair);
148+
}
149+
150+
retVal.add(new NameClassPair("TransactionSynchronizationRegistry", TransactionSynchronizationRegistry.class.getName()));
151+
retVal.add(new NameClassPair("TransactionManager", TransactionManager.class.getName()));
152+
153+
return retVal;
154+
case COMP_WS:
155+
// Using the API type for the class name here instead of non API type com.ibm.ws.uow.embeddable.UOWManager
156+
return Collections.singleton(new NameClassPair("UOWManager", com.ibm.wsspi.uow.UOWManager.class.getName()));
157+
default:
158+
return Collections.emptyList();
159+
}
148160
}
149161

150162
/**
151163
* Helper method for use with injection TransactionObjectFactoruy.
152164
*
153-
* @param injection if the UserTransaction is being obtained for injection
165+
* @param injection if the UserTransaction is being obtained for injection
154166
* @param injectionContext the injection target context if injection is true, or null if unspecified
155167
* @return UserTransaction object with decorator applied if present
156168
* @throws NamingException if the decorator determines the UserTransaction is not available
@@ -165,4 +177,3 @@ protected UserTransaction getUserTransaction(boolean injection, Object injection
165177
}
166178
}
167179
}
168-

dev/com.ibm.ws.tx.jta.extensions/src/com/ibm/ws/jtaextensions/ExtendedTransactionJavaColonNamingHelper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/*******************************************************************************
2-
* Copyright (c) 2011 IBM Corporation and others.
2+
* Copyright (c) 2011, 2025 IBM Corporation and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License 2.0
55
* which accompanies this distribution, and is available at
66
* http://www.eclipse.org/legal/epl-2.0/
7-
*
7+
*
88
* SPDX-License-Identifier: EPL-2.0
99
*
1010
* Contributors:
@@ -76,7 +76,7 @@ public Collection<? extends NameClassPair> listInstances(JavaColonNamespace name
7676
}
7777

7878
if (JavaColonNamespace.COMP_WS.equals(namespace) && "".equals(nameInContext)) {
79-
NameClassPair pair = new NameClassPair(nameInContext, ExtendedJTATransaction.class.getName());
79+
NameClassPair pair = new NameClassPair(EXTENDED_JTA_TRANSACTION, ExtendedJTATransaction.class.getName());
8080
return Collections.singletonList(pair);
8181
}
8282

0 commit comments

Comments
 (0)