Example #1
0
  public static DataSource pooledDataSource(
      DataSource unpooledDataSource, String configName, Map overrideProps) throws SQLException {
    try {
      WrapperConnectionPoolDataSource wcpds = new WrapperConnectionPoolDataSource(configName);
      wcpds.setNestedDataSource(unpooledDataSource);
      if (overrideProps != null)
        BeansUtils.overwriteAccessiblePropertiesFromMap(
            overrideProps, wcpds, false, null, true, MLevel.WARNING, MLevel.WARNING, false);

      PoolBackedDataSource nascent_pbds = new PoolBackedDataSource(configName);
      nascent_pbds.setConnectionPoolDataSource(wcpds);
      if (overrideProps != null)
        BeansUtils.overwriteAccessiblePropertiesFromMap(
            overrideProps, nascent_pbds, false, null, true, MLevel.WARNING, MLevel.WARNING, false);

      return nascent_pbds;
    }
    // 	catch ( PropertyVetoException e )
    // 	    {
    // 		e.printStackTrace();
    // 		PropertyChangeEvent evt = e.getPropertyChangeEvent();
    // 		throw new SQLException("Illegal value attempted for property " + evt.getPropertyName() + ":
    // " + evt.getNewValue());
    // 	    }
    catch (Exception e) {
      // e.printStackTrace();
      SQLException sqle =
          SqlUtils.toSQLException("Exception configuring pool-backed DataSource: " + e, e);
      if (Debug.DEBUG
          && Debug.TRACE >= Debug.TRACE_MED
          && logger.isLoggable(MLevel.FINE)
          && e != sqle) logger.log(MLevel.FINE, "Converted exception to throwable SQLException", e);
      throw sqle;
    }
  }
Example #2
0
  /**
   * Creates a pooled version of an unpooled DataSource using configuration information supplied
   * explicitly by a {@link com.mchange.v2.c3p0.PoolConfig}.
   *
   * @return a DataSource that can be cast to a {@link PooledDataSource} if you are interested in
   *     pool statistics
   * @deprecated if you want to set properties programmatically, please construct a
   *     ComboPooledDataSource and set its properties rather than using PoolConfig
   */
  public static DataSource pooledDataSource(DataSource unpooledDataSource, PoolConfig pcfg)
      throws SQLException {
    try {
      WrapperConnectionPoolDataSource wcpds = new WrapperConnectionPoolDataSource();
      wcpds.setNestedDataSource(unpooledDataSource);

      // set PoolConfig info -- WrapperConnectionPoolDataSource properties
      BeansUtils.overwriteSpecificAccessibleProperties(
          pcfg, wcpds, WRAPPER_CXN_POOL_DATA_SOURCE_OVERWRITE_PROPS);

      PoolBackedDataSource nascent_pbds = new PoolBackedDataSource();
      nascent_pbds.setConnectionPoolDataSource(wcpds);
      BeansUtils.overwriteSpecificAccessibleProperties(
          pcfg, nascent_pbds, POOL_BACKED_DATA_SOURCE_OVERWRITE_PROPS);

      return nascent_pbds;
    }
    // 	catch ( PropertyVetoException e )
    // 	    {
    // 		e.printStackTrace();
    // 		PropertyChangeEvent evt = e.getPropertyChangeEvent();
    // 		throw new SQLException("Illegal value attempted for property " + evt.getPropertyName() + ":
    // " + evt.getNewValue());
    // 	    }
    catch (Exception e) {
      // e.printStackTrace();
      SQLException sqle =
          SqlUtils.toSQLException("Exception configuring pool-backed DataSource: " + e, e);
      if (Debug.DEBUG
          && Debug.TRACE >= Debug.TRACE_MED
          && logger.isLoggable(MLevel.FINE)
          && e != sqle) logger.log(MLevel.FINE, "Converted exception to throwable SQLException", e);
      throw sqle;
    }
  }
Example #3
0
  public Reference createReference(Object bean) throws NamingException {
    try {
      BeanInfo bi = Introspector.getBeanInfo(bean.getClass());
      PropertyDescriptor[] pds = bi.getPropertyDescriptors();
      List refAddrs = new ArrayList();
      String factoryClassLocation = defaultFactoryClassLocation;

      boolean using_ref_props = referenceProperties.size() > 0;

      // we only include this so that on dereference we are not surprised to find some properties
      // missing
      if (using_ref_props)
        refAddrs.add(
            new BinaryRefAddr(REF_PROPS_KEY, SerializableUtils.toByteArray(referenceProperties)));

      for (int i = 0, len = pds.length; i < len; ++i) {
        PropertyDescriptor pd = pds[i];
        String propertyName = pd.getName();
        // System.err.println("Making Reference: " + propertyName);

        if (using_ref_props && !referenceProperties.contains(propertyName)) {
          // System.err.println("Not a ref_prop -- continuing.");
          continue;
        }

        Class propertyType = pd.getPropertyType();
        Method getter = pd.getReadMethod();
        Method setter = pd.getWriteMethod();
        if (getter != null
            && setter != null) // only use properties that are both readable and writable
        {
          Object val = getter.invoke(bean, EMPTY_ARGS);
          // System.err.println( "val: " + val );
          if (propertyName.equals("factoryClassLocation")) {
            if (String.class != propertyType)
              throw new NamingException(
                  this.getClass().getName()
                      + " requires a factoryClassLocation property to be a string, "
                      + propertyType.getName()
                      + " is not valid.");
            factoryClassLocation = (String) val;
          }

          if (val == null) {
            RefAddr addMe = new BinaryRefAddr(propertyName, NULL_TOKEN_BYTES);
            refAddrs.add(addMe);
          } else if (Coerce.canCoerce(propertyType)) {
            RefAddr addMe = new StringRefAddr(propertyName, String.valueOf(val));
            refAddrs.add(addMe);
          } else // other Object properties
          {
            RefAddr addMe = null;
            PropertyEditor pe = BeansUtils.findPropertyEditor(pd);
            if (pe != null) {
              pe.setValue(val);
              String textValue = pe.getAsText();
              if (textValue != null) addMe = new StringRefAddr(propertyName, textValue);
            }
            if (addMe == null) // property editor approach failed
            addMe =
                  new BinaryRefAddr(
                      propertyName,
                      SerializableUtils.toByteArray(
                          val, indirector, IndirectPolicy.INDIRECT_ON_EXCEPTION));
            refAddrs.add(addMe);
          }
        } else {
          // 				System.err.println(this.getClass().getName() +
          // 						   ": Skipping " + propertyName + " because it is " + (setter == null ?
          // "read-only." : "write-only."));

          if (logger.isLoggable(MLevel.WARNING))
            logger.warning(
                this.getClass().getName()
                    + ": Skipping "
                    + propertyName
                    + " because it is "
                    + (setter == null ? "read-only." : "write-only."));
        }
      }
      Reference out =
          new Reference(bean.getClass().getName(), factoryClassName, factoryClassLocation);
      for (Iterator ii = refAddrs.iterator(); ii.hasNext(); ) out.add((RefAddr) ii.next());
      return out;
    } catch (Exception e) {
      // e.printStackTrace();
      if (Debug.DEBUG && logger.isLoggable(MLevel.FINE))
        logger.log(MLevel.FINE, "Exception trying to create Reference.", e);

      throw new NamingException("Could not create reference from bean: " + e.toString());
    }
  }