JndiDataSourceFactory.java

  1. /*
  2.  *    Copyright 2009-2024 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 org.apache.ibatis.datasource.jndi;

  17. import java.util.Map.Entry;
  18. import java.util.Properties;

  19. import javax.naming.Context;
  20. import javax.naming.InitialContext;
  21. import javax.naming.NamingException;
  22. import javax.sql.DataSource;

  23. import org.apache.ibatis.datasource.DataSourceException;
  24. import org.apache.ibatis.datasource.DataSourceFactory;

  25. /**
  26.  * @author Clinton Begin
  27.  */
  28. public class JndiDataSourceFactory implements DataSourceFactory {

  29.   public static final String INITIAL_CONTEXT = "initial_context";
  30.   public static final String DATA_SOURCE = "data_source";
  31.   public static final String ENV_PREFIX = "env.";

  32.   private DataSource dataSource;

  33.   @Override
  34.   public void setProperties(Properties properties) {
  35.     try {
  36.       InitialContext initCtx;
  37.       Properties env = getEnvProperties(properties);
  38.       if (env == null) {
  39.         initCtx = new InitialContext();
  40.       } else {
  41.         initCtx = new InitialContext(env);
  42.       }

  43.       if (properties.containsKey(INITIAL_CONTEXT) && properties.containsKey(DATA_SOURCE)) {
  44.         Context ctx = (Context) initCtx.lookup(properties.getProperty(INITIAL_CONTEXT));
  45.         dataSource = (DataSource) ctx.lookup(properties.getProperty(DATA_SOURCE));
  46.       } else if (properties.containsKey(DATA_SOURCE)) {
  47.         dataSource = (DataSource) initCtx.lookup(properties.getProperty(DATA_SOURCE));
  48.       }

  49.     } catch (NamingException e) {
  50.       throw new DataSourceException("There was an error configuring JndiDataSourceTransactionPool. Cause: " + e, e);
  51.     }
  52.   }

  53.   @Override
  54.   public DataSource getDataSource() {
  55.     return dataSource;
  56.   }

  57.   private static Properties getEnvProperties(Properties allProps) {
  58.     Properties contextProperties = null;
  59.     for (Entry<Object, Object> entry : allProps.entrySet()) {
  60.       String key = (String) entry.getKey();
  61.       String value = (String) entry.getValue();
  62.       if (key.startsWith(ENV_PREFIX)) {
  63.         if (contextProperties == null) {
  64.           contextProperties = new Properties();
  65.         }
  66.         contextProperties.put(key.substring(ENV_PREFIX.length()), value);
  67.       }
  68.     }
  69.     return contextProperties;
  70.   }

  71. }