Example usage for javax.naming.spi ObjectFactory getObjectInstance

List of usage examples for javax.naming.spi ObjectFactory getObjectInstance

Introduction

In this page you can find the example usage for javax.naming.spi ObjectFactory getObjectInstance.

Prototype

public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment)
        throws Exception;

Source Link

Document

Creates an object using the location or reference information specified.

Usage

From source file:org.jahia.services.content.JCRStoreProvider.java

protected Repository getRepositoryByRMI() {
    Repository instance = null;//from w  w  w.j  a  v  a2  s  . co m
    try {
        Class<? extends ObjectFactory> factoryClass = Class.forName(factory).asSubclass(ObjectFactory.class);
        ObjectFactory factory = (ObjectFactory) factoryClass.newInstance();
        instance = (Repository) factory.getObjectInstance(
                new Reference(Repository.class.getName(), new StringRefAddr("url", url)), null, null, null);
        logger.info("Repository {} acquired via RMI", getKey());
    } catch (Exception e) {
        logger.error("Cannot get by RMI", e);
    }
    return instance;
}

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 .  co m

    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;
}