Example usage for javax.sql DataSource getClass

List of usage examples for javax.sql DataSource getClass

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:org.piraso.server.sql.SQLContextLogger.java

/**
 * Wraps the given {@link DataSource} to add support for context logging.
 *
 * @param dataSource {@link DataSource} to add context logging support.
 * @param id the unique identification for this dataSource.
 * @return the proxy {@link DataSource} with context logging support.
 *///from   w  ww.  j  ava 2 s .  c o m
public static DataSource create(DataSource dataSource, String id) {
    DataSourceProxyFactory factory = new DataSourceProxyFactory(dataSource.getClass(), new GroupChainId(id));

    return factory.getProxy(dataSource);
}

From source file:info.bunji.jdbc.logger.JdbcLoggerFactory.java

/**
 ********************************************
 * get loggerName from jdbc url.//  w w  w.ja  v  a  2  s  .c o  m
 *
 * @param url connection url
 * @return loggerName(jdbc url or DataSourceName)
 ********************************************
 */
private static String getLoggerName(String url) {
    if (!dsNameMap.containsKey(url)) {
        dsNameMap.put(url, url);

        // datasource????
        InitialContext ctx = null;
        try {
            // JNDI??????
            ctx = new InitialContext();
            NamingEnumeration<NameClassPair> ne = ctx.list("java:comp/env/jdbc");
            while (ne.hasMoreElements()) {
                NameClassPair nc = ne.nextElement();
                DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/" + nc.getName());
                // ?DataSource????getUrl()
                Method method = ds.getClass().getMethod("getUrl");
                String dsUrl = (String) method.invoke(ds);
                if (dsUrl.startsWith(DriverEx.DRIVER_URL_PREFIX)) {
                    dsUrl = dsUrl.replace(DriverEx.DRIVER_URL_PREFIX, "jdbc:");
                    if (dsUrl.equals(url)) {
                        dsNameMap.put(url, nc.getName());
                        break;
                    }
                }
            }
        } catch (Throwable t) {
            printThrowable(t);
        } finally {
            try {
                if (ctx != null)
                    ctx.close();
            } catch (NamingException e) {
            }
        }
    }
    return dsNameMap.get(url);
}

From source file:com.amazon.carbonado.repo.jdbc.JDBCRepository.java

/**
 * Attempts to close a DataSource by searching for a "close" method. For
 * some reason, there's no standard way to close a DataSource.
 *
 * @return false if DataSource doesn't have a close method.
 *///  w  ww.j  a va2 s  .co  m
public static boolean closeDataSource(DataSource ds) throws SQLException {
    try {
        Method closeMethod = ds.getClass().getMethod("close");
        try {
            closeMethod.invoke(ds);
        } catch (Throwable e) {
            ThrowUnchecked.fireFirstDeclaredCause(e, SQLException.class);
        }
        return true;
    } catch (NoSuchMethodException e) {
        return false;
    }
}

From source file:com.nebhale.demo.web.DataSourceController.java

private boolean isClass(DataSource dataSource, String className) {
    return dataSource.getClass().getName().equals(className);
}

From source file:com.gopivotal.cloudfoundry.test.core.DataSourceUtils.java

private DataSource getDataSource(DataSource dataSource) {
    try {/*from  w w w.ja  va 2  s . c om*/
        Field field = dataSource.getClass().getDeclaredField("dataSource");
        ReflectionUtils.makeAccessible(field);

        return (DataSource) field.get(dataSource);
    } catch (NoSuchFieldException | IllegalAccessException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}

From source file:com.gopivotal.cloudfoundry.test.core.DataSourceUtils.java

private DataSource getTargetDataSource(DataSource dataSource) {
    Method method = ReflectionUtils.findMethod(dataSource.getClass(), "getTargetDataSource");
    return (DataSource) ReflectionUtils.invokeMethod(method, dataSource);
}

From source file:com.gopivotal.cloudfoundry.test.core.DataSourceUtils.java

private String invokeMethod(DataSource dataSource, String methodName) {
    Method method = ReflectionUtils.findMethod(dataSource.getClass(), methodName);
    return (String) ReflectionUtils.invokeMethod(method, dataSource);
}

From source file:org.ualerts.chat.database.EmbeddedHsqlDatabaseServer.java

private boolean wantEmbeddedDatabase() {
    boolean useEmbedded = false;
    try {//www. j  av  a2 s.  c  o m
        Context initCtx = new InitialContext();
        DataSource ds = (DataSource) initCtx.lookup(dataSourceName);
        Class<?> dataSourceClass = ds.getClass();
        Method urlMethod = dataSourceClass.getMethod("getUrl");
        String url = (String) urlMethod.invoke(ds);
        useEmbedded = url.endsWith("/" + DEFAULT_DATABASE_NAME);
    } catch (NoSuchMethodException ex) {
        logger.warn("data source has no accessor for the URL");
    } catch (IllegalAccessException ex) {
        logger.warn("cannot access the data source URL");
    } catch (InvocationTargetException ex) {
        logger.warn("error accessing the data source URL");
    } catch (NamingException ex) {
        logger.warn("JNDI lookup failed: " + ex);
    }
    return useEmbedded;
}

From source file:com.gopivotal.cloudfoundry.test.core.DataSourceUtilsTest.java

@Test
public void indeterminateUrl() {
    DataSource dataSource = mock(DataSource.class);
    assertEquals(/*from w ww .  java2s.c o  m*/
            String.format("Unable to determine URL for DataSource of type %s", dataSource.getClass().getName()),
            this.dataSourceUtils.getUrl(dataSource));
}

From source file:com.netspective.sparx.console.util.ResinDataSourcePoolStatistics.java

public void setConnectionProviderEntry(ConnectionProviderEntry entry) {
    this.entry = entry;

    if (entry != null) {
        DataSource ds = entry.getDataSource();
        if (ds == null)
            throw new RuntimeException("DataSource is NULL");

        try {//from w  w  w  .ja  v a 2s .  c  o  m
            activeConnsMethod = ds.getClass().getMethod("getActiveConnections", null);
            maxConnsMethod = ds.getClass().getMethod("getMaxConnections", null);
            totalConnsMethod = ds.getClass().getMethod("getTotalConnections", null);
        } catch (Exception e) {
            throw new NestableRuntimeException(e);
        }
    }
}