UnpooledDataSourceFactory.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.unpooled;

  17. import java.util.Properties;

  18. import javax.sql.DataSource;

  19. import org.apache.ibatis.datasource.DataSourceException;
  20. import org.apache.ibatis.datasource.DataSourceFactory;
  21. import org.apache.ibatis.reflection.MetaObject;
  22. import org.apache.ibatis.reflection.SystemMetaObject;

  23. /**
  24.  * @author Clinton Begin
  25.  */
  26. public class UnpooledDataSourceFactory implements DataSourceFactory {

  27.   private static final String DRIVER_PROPERTY_PREFIX = "driver.";
  28.   private static final int DRIVER_PROPERTY_PREFIX_LENGTH = DRIVER_PROPERTY_PREFIX.length();

  29.   protected DataSource dataSource;

  30.   public UnpooledDataSourceFactory() {
  31.     this.dataSource = new UnpooledDataSource();
  32.   }

  33.   @Override
  34.   public void setProperties(Properties properties) {
  35.     Properties driverProperties = new Properties();
  36.     MetaObject metaDataSource = SystemMetaObject.forObject(dataSource);
  37.     for (Object key : properties.keySet()) {
  38.       String propertyName = (String) key;
  39.       if (propertyName.startsWith(DRIVER_PROPERTY_PREFIX)) {
  40.         String value = properties.getProperty(propertyName);
  41.         driverProperties.setProperty(propertyName.substring(DRIVER_PROPERTY_PREFIX_LENGTH), value);
  42.       } else if (metaDataSource.hasSetter(propertyName)) {
  43.         String value = (String) properties.get(propertyName);
  44.         Object convertedValue = convertValue(metaDataSource, propertyName, value);
  45.         metaDataSource.setValue(propertyName, convertedValue);
  46.       } else {
  47.         throw new DataSourceException("Unknown DataSource property: " + propertyName);
  48.       }
  49.     }
  50.     if (driverProperties.size() > 0) {
  51.       metaDataSource.setValue("driverProperties", driverProperties);
  52.     }
  53.   }

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

  58.   private Object convertValue(MetaObject metaDataSource, String propertyName, String value) {
  59.     Object convertedValue = value;
  60.     Class<?> targetType = metaDataSource.getSetterType(propertyName);
  61.     if (targetType == Integer.class || targetType == int.class) {
  62.       convertedValue = Integer.valueOf(value);
  63.     } else if (targetType == Long.class || targetType == long.class) {
  64.       convertedValue = Long.valueOf(value);
  65.     } else if (targetType == Boolean.class || targetType == boolean.class) {
  66.       convertedValue = Boolean.valueOf(value);
  67.     }
  68.     return convertedValue;
  69.   }

  70. }