View Javadoc
1   /*
2    * Copyright 2004-2025 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.datasource;
17  
18  import com.ibatis.sqlmap.client.SqlMapException;
19  
20  import java.util.Iterator;
21  import java.util.Map;
22  import java.util.Properties;
23  
24  import javax.naming.Context;
25  import javax.naming.InitialContext;
26  import javax.naming.NamingException;
27  import javax.sql.DataSource;
28  
29  /**
30   * DataSourceFactory implementation for JNDI.
31   */
32  public class JndiDataSourceFactory implements DataSourceFactory {
33  
34    /** The data source. */
35    private DataSource dataSource;
36  
37    @Override
38    public void initialize(Map properties) {
39      try {
40        InitialContext initCtx = null;
41        Properties context = getContextProperties(properties);
42  
43        if (context == null) {
44          initCtx = new InitialContext();
45        } else {
46          initCtx = new InitialContext(context);
47        }
48  
49        if (properties.containsKey("DataSource")) {
50          dataSource = (DataSource) initCtx.lookup((String) properties.get("DataSource"));
51        } else if (properties.containsKey("DBJndiContext")) {
52          // LEGACY --Backward compatibility
53          dataSource = (DataSource) initCtx.lookup((String) properties.get("DBJndiContext"));
54        } else if (properties.containsKey("DBFullJndiContext")) {
55          // LEGACY --Backward compatibility
56          dataSource = (DataSource) initCtx.lookup((String) properties.get("DBFullJndiContext"));
57        } else if (properties.containsKey("DBInitialContext") && properties.containsKey("DBLookup")) {
58          // LEGACY - Backward compatibility
59          Context ctx = (Context) initCtx.lookup((String) properties.get("DBInitialContext"));
60          dataSource = (DataSource) ctx.lookup((String) properties.get("DBLookup"));
61        }
62  
63      } catch (NamingException e) {
64        throw new SqlMapException("There was an error configuring JndiDataSourceTransactionPool. Cause: " + e, e);
65      }
66    }
67  
68    @Override
69    public DataSource getDataSource() {
70      return dataSource;
71    }
72  
73    /**
74     * Gets the context properties.
75     *
76     * @param allProps
77     *          the all props
78     *
79     * @return the context properties
80     */
81    private static Properties getContextProperties(Map allProps) {
82      final String PREFIX = "context.";
83      Properties contextProperties = null;
84      Iterator keys = allProps.keySet().iterator();
85      while (keys.hasNext()) {
86        String key = (String) keys.next();
87        String value = (String) allProps.get(key);
88        if (key.startsWith(PREFIX)) {
89          if (contextProperties == null) {
90            contextProperties = new Properties();
91          }
92          contextProperties.put(key.substring(PREFIX.length()), value);
93        }
94      }
95      return contextProperties;
96    }
97  
98  }