Example usage for javax.sql DataSource unwrap

List of usage examples for javax.sql DataSource unwrap

Introduction

In this page you can find the example usage for javax.sql DataSource unwrap.

Prototype

<T> T unwrap(java.lang.Class<T> iface) throws java.sql.SQLException;

Source Link

Document

Returns an object that implements the given interface to allow access to non-standard methods, or standard methods not exposed by the proxy.

Usage

From source file:com.micromux.cassandra.jdbc.DataSourceTest.java

@Test(expected = SQLFeatureNotSupportedException.class)
public void testUnwrap() throws Exception {
    DataSource ds = new CassandraDataSource(HOST, PORT, KEYSPACE, USER, PASSWORD, VERSION, CONSISTENCY,
            TRUST_STORE, TRUST_PASS);//  ww w. j av a 2  s  . c  o  m

    // it is a wrapper for DataSource
    DataSource newds = ds.unwrap(DataSource.class);
    assertNotNull(newds);

    // it is not a wrapper for this test class
    newds = (DataSource) ds.unwrap(this.getClass());
    assertNotNull(newds);
}

From source file:org.geotools.data.h2.H2DataStoreFactoryTest.java

public void testCreateDataStoreMVCC() throws Exception {
    Map clonedParams = new HashMap(params);
    clonedParams.put(H2DataStoreFactory.MVCC.key, true);
    JDBCDataStore ds = factory.createDataStore(clonedParams);
    assertNotNull(ds);/*w w w .  j av  a 2 s.  co  m*/
    final DataSource source = ds.getDataSource();
    assertNotNull(source);
    final DataSource wrapped = source.unwrap(DataSource.class);
    assertNotNull(wrapped);
    if (wrapped instanceof BasicDataSource) {
        final BasicDataSource basicSource = (BasicDataSource) wrapped;
        final String url = basicSource.getUrl();
        assertTrue(url.contains("MVCC=true"));
    }
}

From source file:org.kuali.rice.web.health.DatabaseConnectionPoolMetricSet.java

private <T> T tryUnwrap(DataSource dataSource, Class<T> targetType) {
    if (targetType.isInstance(dataSource)) {
        return targetType.cast(dataSource);
    }//  w  w  w .  jav  a2  s .  c o  m
    try {
        if (dataSource.isWrapperFor(targetType)) {
            return dataSource.unwrap(targetType);
        }
    } catch (SQLException e) {
        LOG.warn("Exception when trying to unwrap datasource as " + targetType, e);
    }
    return null;
}

From source file:org.kuali.rice.web.health.DatabaseConnectionPoolMetricSetTest.java

@Test
public void testGetMetrics_Wrapped_XAPool() throws Exception {
    StandardXAPoolDataSource dataSource = mock(StandardXAPoolDataSource.class);
    when(dataSource.getLockedObjectCount()).thenReturn(10);
    when(dataSource.getMinSize()).thenReturn(5);
    when(dataSource.getMaxSize()).thenReturn(20);

    // create the wrapper
    DataSource wrapperDataSource = mock(DataSource.class);
    when(wrapperDataSource.isWrapperFor(StandardXAPoolDataSource.class)).thenReturn(true);
    when(wrapperDataSource.unwrap(StandardXAPoolDataSource.class)).thenReturn(dataSource);

    DatabaseConnectionPoolMetricSet metricSet = new DatabaseConnectionPoolMetricSet("xapool:",
            wrapperDataSource);/*from  ww w .ja v  a 2 s .c  om*/
    Map<String, Metric> metrics = metricSet.getMetrics();
    assertEquals(10, ((Gauge) metrics.get("xapool:pool.active")).getValue());
    assertEquals(5, ((Gauge) metrics.get("xapool:pool.min")).getValue());
    assertEquals(20, ((Gauge) metrics.get("xapool:pool.max")).getValue());
    assertEquals(0.5, ((Gauge) metrics.get("xapool:pool.usage")).getValue());
}

From source file:org.orbisgis.geoserver.h2gis.datastore.H2DataStoreFactoryTest.java

@Test
public void testCreateDataStoreMVCC() throws Exception {
    Map clonedParams = new HashMap(params);
    clonedParams.put(H2GISDataStoreFactory.MVCC.key, true);
    JDBCDataStore ds = factory.createDataStore(clonedParams);
    assertNotNull(ds);//  w  w w  .  j  av a 2  s. c o m
    final DataSource source = ds.getDataSource();
    assertNotNull(source);
    final DataSource wrapped = source.unwrap(DataSource.class);
    assertNotNull(wrapped);
    if (wrapped instanceof BasicDataSource) {
        final BasicDataSource basicSource = (BasicDataSource) wrapped;
        final String url = basicSource.getUrl();
        assertTrue(url.contains("MVCC=true"));
    }
}