public void performJNDILookup(Context context, PoolConfiguration poolProperties) {
   Object jndiDS = null;
   try {
     if (context != null) {
       jndiDS = context.lookup(poolProperties.getDataSourceJNDI());
     } else {
       log.warn("dataSourceJNDI property is configued, but local JNDI context is null.");
     }
   } catch (NamingException e) {
     log.debug(
         "The name \""
             + poolProperties.getDataSourceJNDI()
             + "\" can not be found in the local context.");
   }
   if (jndiDS == null) {
     try {
       context = new InitialContext();
       jndiDS = context.lookup(poolProperties.getDataSourceJNDI());
     } catch (NamingException e) {
       log.warn(
           "The name \""
               + poolProperties.getDataSourceJNDI()
               + "\" can not be found in the InitialContext.");
     }
   }
   if (jndiDS != null) {
     poolProperties.setDataSource(jndiDS);
   }
 }
 public DataSource createDataSource(Properties properties, Context context, boolean XA)
     throws Exception {
   PoolConfiguration poolProperties = DataSourceFactory.parsePoolProperties(properties);
   if (poolProperties.getDataSourceJNDI() != null && poolProperties.getDataSource() == null) {
     performJNDILookup(context, poolProperties);
   }
   org.apache.tomcat.jdbc.pool.DataSource dataSource =
       XA
           ? new org.apache.tomcat.jdbc.pool.XADataSource(poolProperties)
           : new org.apache.tomcat.jdbc.pool.DataSource(poolProperties);
   // initialise the pool itself
   dataSource.createPool();
   // Return the configured DataSource instance
   return dataSource;
 }
  public static PoolConfiguration parsePoolProperties(Properties properties) {
    PoolConfiguration poolProperties = new PoolProperties();
    String value = null;

    value = properties.getProperty(PROP_DEFAULTAUTOCOMMIT);
    if (value != null) {
      poolProperties.setDefaultAutoCommit(Boolean.valueOf(value));
    }

    value = properties.getProperty(PROP_DEFAULTREADONLY);
    if (value != null) {
      poolProperties.setDefaultReadOnly(Boolean.valueOf(value));
    }

    value = properties.getProperty(PROP_DEFAULTTRANSACTIONISOLATION);
    if (value != null) {
      int level = UNKNOWN_TRANSACTIONISOLATION;
      if ("NONE".equalsIgnoreCase(value)) {
        level = Connection.TRANSACTION_NONE;
      } else if ("READ_COMMITTED".equalsIgnoreCase(value)) {
        level = Connection.TRANSACTION_READ_COMMITTED;
      } else if ("READ_UNCOMMITTED".equalsIgnoreCase(value)) {
        level = Connection.TRANSACTION_READ_UNCOMMITTED;
      } else if ("REPEATABLE_READ".equalsIgnoreCase(value)) {
        level = Connection.TRANSACTION_REPEATABLE_READ;
      } else if ("SERIALIZABLE".equalsIgnoreCase(value)) {
        level = Connection.TRANSACTION_SERIALIZABLE;
      } else {
        try {
          level = Integer.parseInt(value);
        } catch (NumberFormatException e) {
          System.err.println("Could not parse defaultTransactionIsolation: " + value);
          System.err.println("WARNING: defaultTransactionIsolation not set");
          System.err.println("using default value of database driver");
          level = UNKNOWN_TRANSACTIONISOLATION;
        }
      }
      poolProperties.setDefaultTransactionIsolation(level);
    }

    value = properties.getProperty(PROP_DEFAULTCATALOG);
    if (value != null) {
      poolProperties.setDefaultCatalog(value);
    }

    value = properties.getProperty(PROP_DRIVERCLASSNAME);
    if (value != null) {
      poolProperties.setDriverClassName(value);
    }

    value = properties.getProperty(PROP_MAXACTIVE);
    if (value != null) {
      poolProperties.setMaxActive(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_MAXIDLE);
    if (value != null) {
      poolProperties.setMaxIdle(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_MINIDLE);
    if (value != null) {
      poolProperties.setMinIdle(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_INITIALSIZE);
    if (value != null) {
      poolProperties.setInitialSize(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_MAXWAIT);
    if (value != null) {
      poolProperties.setMaxWait(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_TESTONBORROW);
    if (value != null) {
      poolProperties.setTestOnBorrow(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(PROP_TESTONRETURN);
    if (value != null) {
      poolProperties.setTestOnReturn(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(PROP_TESTONCONNECT);
    if (value != null) {
      poolProperties.setTestOnConnect(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(PROP_TIMEBETWEENEVICTIONRUNSMILLIS);
    if (value != null) {
      poolProperties.setTimeBetweenEvictionRunsMillis(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_NUMTESTSPEREVICTIONRUN);
    if (value != null) {
      poolProperties.setNumTestsPerEvictionRun(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_MINEVICTABLEIDLETIMEMILLIS);
    if (value != null) {
      poolProperties.setMinEvictableIdleTimeMillis(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_TESTWHILEIDLE);
    if (value != null) {
      poolProperties.setTestWhileIdle(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(PROP_PASSWORD);
    if (value != null) {
      poolProperties.setPassword(value);
    }

    value = properties.getProperty(PROP_URL);
    if (value != null) {
      poolProperties.setUrl(value);
    }

    value = properties.getProperty(PROP_USERNAME);
    if (value != null) {
      poolProperties.setUsername(value);
    }

    value = properties.getProperty(PROP_VALIDATIONQUERY);
    if (value != null) {
      poolProperties.setValidationQuery(value);
    }

    value = properties.getProperty(PROP_VALIDATOR_CLASS_NAME);
    if (value != null) {
      poolProperties.setValidatorClassName(value);
    }

    value = properties.getProperty(PROP_VALIDATIONINTERVAL);
    if (value != null) {
      poolProperties.setValidationInterval(Long.parseLong(value));
    }

    value = properties.getProperty(PROP_ACCESSTOUNDERLYINGCONNECTIONALLOWED);
    if (value != null) {
      poolProperties.setAccessToUnderlyingConnectionAllowed(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(PROP_REMOVEABANDONED);
    if (value != null) {
      poolProperties.setRemoveAbandoned(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(PROP_REMOVEABANDONEDTIMEOUT);
    if (value != null) {
      poolProperties.setRemoveAbandonedTimeout(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_LOGABANDONED);
    if (value != null) {
      poolProperties.setLogAbandoned(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(PROP_POOLPREPAREDSTATEMENTS);
    if (value != null) {
      log.warn(PROP_POOLPREPAREDSTATEMENTS + " is not a valid setting, it will have no effect.");
    }

    value = properties.getProperty(PROP_MAXOPENPREPAREDSTATEMENTS);
    if (value != null) {
      log.warn(PROP_MAXOPENPREPAREDSTATEMENTS + " is not a valid setting, it will have no effect.");
    }

    value = properties.getProperty(PROP_CONNECTIONPROPERTIES);
    if (value != null) {
      Properties p = getProperties(value);
      poolProperties.setDbProperties(p);
    } else {
      poolProperties.setDbProperties(new Properties());
    }

    if (poolProperties.getUsername() != null) {
      poolProperties.getDbProperties().setProperty("user", poolProperties.getUsername());
    }
    if (poolProperties.getPassword() != null) {
      poolProperties.getDbProperties().setProperty("password", poolProperties.getPassword());
    }

    value = properties.getProperty(PROP_INITSQL);
    if (value != null) {
      poolProperties.setInitSQL(value);
    }

    value = properties.getProperty(PROP_INTERCEPTORS);
    if (value != null) {
      poolProperties.setJdbcInterceptors(value);
    }

    value = properties.getProperty(PROP_JMX_ENABLED);
    if (value != null) {
      poolProperties.setJmxEnabled(Boolean.parseBoolean(value));
    }

    value = properties.getProperty(PROP_FAIR_QUEUE);
    if (value != null) {
      poolProperties.setFairQueue(Boolean.parseBoolean(value));
    }

    value = properties.getProperty(PROP_USE_EQUALS);
    if (value != null) {
      poolProperties.setUseEquals(Boolean.parseBoolean(value));
    }

    value = properties.getProperty(OBJECT_NAME);
    if (value != null) {
      poolProperties.setName(ObjectName.quote(value));
    }

    value = properties.getProperty(PROP_ABANDONWHENPERCENTAGEFULL);
    if (value != null) {
      poolProperties.setAbandonWhenPercentageFull(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_MAXAGE);
    if (value != null) {
      poolProperties.setMaxAge(Long.parseLong(value));
    }

    value = properties.getProperty(PROP_USE_CON_LOCK);
    if (value != null) {
      poolProperties.setUseLock(Boolean.parseBoolean(value));
    }

    value = properties.getProperty(PROP_DATASOURCE);
    if (value != null) {
      // this should never happen
      throw new IllegalArgumentException(
          "Can't set dataSource property as a string, this must be a javax.sql.DataSource object.");
    }

    value = properties.getProperty(PROP_DATASOURCE_JNDI);
    if (value != null) {
      poolProperties.setDataSourceJNDI(value);
    }

    value = properties.getProperty(PROP_SUSPECT_TIMEOUT);
    if (value != null) {
      poolProperties.setSuspectTimeout(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_ALTERNATE_USERNAME_ALLOWED);
    if (value != null) {
      poolProperties.setAlternateUsernameAllowed(Boolean.parseBoolean(value));
    }

    value = properties.getProperty(PROP_COMMITONRETURN);
    if (value != null) {
      poolProperties.setCommitOnReturn(Boolean.parseBoolean(value));
    }

    value = properties.getProperty(PROP_ROLLBACKONRETURN);
    if (value != null) {
      poolProperties.setRollbackOnReturn(Boolean.parseBoolean(value));
    }

    value = properties.getProperty(PROP_USEDISPOSABLECONNECTIONFACADE);
    if (value != null) {
      poolProperties.setUseDisposableConnectionFacade(Boolean.parseBoolean(value));
    }

    value = properties.getProperty(PROP_LOGVALIDATIONERRORS);
    if (value != null) {
      poolProperties.setLogValidationErrors(Boolean.parseBoolean(value));
    }

    value = properties.getProperty(PROP_PROPAGATEINTERRUPTSTATE);
    if (value != null) {
      poolProperties.setPropagateInterruptState(Boolean.parseBoolean(value));
    }

    return poolProperties;
  }