Example usage for javax.sql XADataSource getClass

List of usage examples for javax.sql XADataSource getClass

Introduction

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

Prototype

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

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:org.nuxeo.runtime.datasource.DataSourceFactory.java

@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> env) throws Exception {
    Reference ref = (Reference) obj;
    if (!DataSource.class.getName().equals(ref.getClassName())) {
        return null;
    }/*from w w w.j a v  a  2s  .  c om*/

    TransactionManager transactionManager;
    try {
        transactionManager = TransactionHelper.lookupTransactionManager();
    } catch (NamingException e) {
        transactionManager = null;
    }

    boolean xa = ref.get(BasicManagedDataSourceFactory.PROP_XADATASOURCE) != null;
    log.info(String.format("Creating pooled %s datasource: %s/%s", xa ? "XA" : "non-XA",
            nameCtx.getNameInNamespace(), name));

    if (xa && transactionManager == null) {
        throw new RuntimeException(
                "Cannot configure XA datasource " + name + " without an available transaction manager");
    }

    // extract properties from Reference
    Map<String, String> properties = new HashMap<String, String>();
    Enumeration<RefAddr> refAddrs = ref.getAll();
    while (refAddrs.hasMoreElements()) {
        RefAddr ra = refAddrs.nextElement();
        String key = ra.getType();
        String value = ra.getContent().toString();
        if (key.startsWith(DataSourceDescriptor.PROP_PREFIX)) {
            key = key.substring(DataSourceDescriptor.PROP_PREFIX.length());
            properties.put(key, value);
        }
    }

    DataSource ds;
    if (!xa) {
        // fetch url from properties
        for (Entry<String, String> en : properties.entrySet()) {
            // often misspelled, thus the ignore case
            if (URL_LOWER.equalsIgnoreCase(en.getKey())) {
                ref.add(new StringRefAddr(URL_LOWER, en.getValue()));
            }
        }
        ObjectFactory factory = new BasicDataSourceFactory();
        ds = (DataSource) factory.getObjectInstance(ref, name, nameCtx, env);
        BasicDataSource bds = (BasicDataSource) ds;

        // set properties
        for (Entry<String, String> en : properties.entrySet()) {
            String key = en.getKey();
            if (URL_LOWER.equalsIgnoreCase(key)) {
                continue;
            }
            bds.addConnectionProperty(key, en.getValue());
        }
    } else {
        ObjectFactory factory = new BasicManagedDataSourceFactory();
        ds = (DataSource) factory.getObjectInstance(obj, name, nameCtx, env);
        if (ds == null) {
            return null;
        }
        BasicManagedDataSource bmds = (BasicManagedDataSource) ds;

        // set transaction manager
        bmds.setTransactionManager(transactionManager);

        // set properties
        XADataSource xaDataSource = bmds.getXaDataSourceInstance();
        if (xaDataSource == null) {
            return null;
        }
        for (Entry<String, String> en : properties.entrySet()) {
            String key = en.getKey();
            // proper JavaBean convention for initial cap
            if (Character.isLowerCase(key.charAt(1))) {
                key = Character.toLowerCase(key.charAt(0)) + key.substring(1);
            }
            String value = en.getValue();
            boolean ok = false;
            try {
                BeanUtils.setProperty(xaDataSource, key, value);
                ok = true;
            } catch (Exception e) {
                if (URL_LOWER.equals(key)) {
                    // commonly misspelled
                    try {
                        BeanUtils.setProperty(xaDataSource, URL_UPPER, value);
                        ok = true;
                    } catch (Exception ee) {
                        // log error below
                    }
                }
            }
            if (!ok) {
                log.error(String.format("Cannot set %s = %s on %s", key, value,
                        xaDataSource.getClass().getName()));
            }
        }
    }
    return ds;
}

From source file:org.teiid.spring.autoconfigure.TeiidServer.java

String getDriverName(Object source) {
    String driverName = null;/*from  w  w  w.  j  av  a  2  s  .c o  m*/
    if (source instanceof org.apache.tomcat.jdbc.pool.DataSource) {
        driverName = ((org.apache.tomcat.jdbc.pool.DataSource) source).getDriverClassName();
    } else {
        if (source instanceof DataSource) {
            try {
                XADataSource xads = ((DataSource) source).unwrap(XADataSource.class);
                if (xads != null) {
                    if (xads instanceof XADataSourceBuilder) {
                        driverName = ((XADataSourceBuilder) xads).getDataSourceClassName();
                    } else {
                        driverName = xads.getClass().getName();
                    }
                }
            } catch (SQLException e1) {
                // ignore.
            }
        }
    }
    return driverName;
}