View Javadoc
1   /*
2    * Copyright 2004-2026 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    *    https://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 com.ibatis.sqlmap.engine.transaction.jta;
17  
18  /**
19   * The Class JTANamespace.
20   * <p>
21   * Utility class for detecting presence of Jakarta and Javax Transaction API classes.
22   */
23  public final class JTANamespace {
24  
25    /** The Constant classloader. */
26    private static final ClassLoader classloader = JTANamespace.class.getClassLoader();
27  
28    /** The Constant JAKARTA_TRANSACTION_CLASS. */
29    public static final Class<?> JAKARTA_TRANSACTION_CLASS = JTANamespace
30        .searchTypeInClasspath("jakarta.transaction.Transaction");
31  
32    /** The Constant JAVAX_TRANSACTION_CLASS. */
33    public static final Class<?> JAVAX_TRANSACTION_CLASS = JTANamespace
34        .searchTypeInClasspath("javax.transaction.Transaction");
35  
36    /**
37     * Private constructor to prevent instantiation.
38     */
39    private JTANamespace() {
40      // Utility class
41    }
42  
43    /**
44     * Attempts to load the class with the given name using this class's classloader. Returns null if the class is not
45     * found.
46     *
47     * @param <T>
48     *          the generic type
49     * @param typeName
50     *          the type name
51     *
52     * @return the class if found, or null if not found
53     */
54    @SuppressWarnings("unchecked")
55    private static <T> Class<? extends T> searchTypeInClasspath(final String typeName) {
56      try {
57        return (Class<? extends T>) Class.forName(typeName, false, JTANamespace.classloader);
58      } catch (final ClassNotFoundException e) {
59        return null;
60      }
61    }
62  
63  }