Example usage for javax.naming RefAddr getContent

List of usage examples for javax.naming RefAddr getContent

Introduction

In this page you can find the example usage for javax.naming RefAddr getContent.

Prototype

public abstract Object getContent();

Source Link

Document

Retrieves the contents of this address.

Usage

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

/**
 * Create and return a new <code>BasicManagedDataSource</code> instance. If
 * no instance can be created, return <code>null</code> instead.
 *
 * @param obj The possibly null object containing location or reference
 *            information that can be used in creating an object
 * @param name The name of this object relative to <code>nameCtx</code>
 * @param nameCtx The context relative to which the <code>name</code>
 *            parameter is specified, or <code>null</code> if
 *            <code>name</code> is relative to the default initial context
 * @param environment The possibly null environment that is used in creating
 *            this object/*from  w  ww .  j av  a  2s . c om*/
 *
 * @exception Exception if an exception occurs creating the instance
 */
@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment)
        throws Exception {

    // We only know how to deal with <code>javax.naming.Reference</code>s
    // that specify a class name of "javax.sql.DataSource"
    if ((obj == null) || !(obj instanceof Reference)) {
        return null;
    }
    Reference ref = (Reference) obj;
    if (!"javax.sql.DataSource".equals(ref.getClassName())) {
        return null;
    }

    Properties properties = new Properties();
    for (String propertyName : ALL_PROPERTIES) {
        RefAddr ra = ref.get(propertyName);
        if (ra != null) {
            String propertyValue = ra.getContent().toString();
            properties.setProperty(propertyName, propertyValue);
        }
    }

    return createDataSource(properties);
}

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;
    }/*  www.ja va2s .  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;
}

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

protected String refAttribute(Reference ref, String key, String defvalue) {
    RefAddr addr = ref.get(key);
    if (addr == null) {
        if (defvalue == null) {
            throw new IllegalArgumentException(key + " address is mandatory");
        }/*ww  w . ja va2s. co m*/
        return defvalue;
    }
    return (String) addr.getContent();
}

From source file:org.nuxeo.runtime.jtajca.NuxeoConnectionManagerFactory.java

@Override
public Object getObjectInstance(Object obj, Name objName, Context nameCtx, Hashtable<?, ?> env) {
    Reference ref = (Reference) obj;
    if (!ConnectionManager.class.getName().equals(ref.getClassName())) {
        return null;
    }//from   www . j a  v  a  2s  .  c o m
    String name;
    int size = objName.size();
    if (size == 1) {
        name = "default";
    } else {
        name = objName.get(size - 1);
    }

    final ConnectionManager cm = NuxeoContainer.connectionManagers.get(name);
    if (cm != null) {
        return cm;
    }

    NuxeoConnectionManagerConfiguration config = new NuxeoConnectionManagerConfiguration();
    for (RefAddr addr : Collections.list(ref.getAll())) {
        String type = addr.getType();
        String content = (String) addr.getContent();
        try {
            BeanUtils.setProperty(config, type, content);
        } catch (ReflectiveOperationException e) {
            log.error(String.format("NuxeoConnectionManagerFactory cannot set %s = %s", type, content));
        }
    }
    return NuxeoContainer.initConnectionManager(config);
}

From source file:org.nuxeo.runtime.jtajca.NuxeoConnectionManagerFactory.java

public static NuxeoConnectionManagerConfiguration getConfig(Reference ref) {
    NuxeoConnectionManagerConfiguration config = new NuxeoConnectionManagerConfiguration();
    IllegalArgumentException errors = new IllegalArgumentException("wrong naming config");
    for (RefAddr addr : Collections.list(ref.getAll())) {
        String name = addr.getType();
        String value = (String) addr.getContent();
        try {// w w  w.j a  v a  2 s  . co m
            BeanUtils.setProperty(config, name, value);
        } catch (ReflectiveOperationException cause) {
            errors.addSuppressed(cause);
        }
    }
    if (errors.getSuppressed().length > 0) {
        throw errors;
    }
    return config;
}

From source file:org.nuxeo.runtime.jtajca.NuxeoTransactionManagerFactory.java

@Override
public Object getObjectInstance(Object obj, Name objName, Context nameCtx, Hashtable<?, ?> env) {
    Reference ref = (Reference) obj;
    if (!TransactionManager.class.getName().equals(ref.getClassName())) {
        return null;
    }/* w  ww. j a  v a 2s. c o  m*/
    if (NuxeoContainer.tm != null) {
        return NuxeoContainer.tm;
    }

    // initialize
    TransactionManagerConfiguration config = new TransactionManagerConfiguration();
    for (RefAddr addr : Collections.list(ref.getAll())) {
        String name = addr.getType();
        String value = (String) addr.getContent();
        try {
            BeanUtils.setProperty(config, name, value);
        } catch (ReflectiveOperationException e) {
            log.error(String.format("NuxeoTransactionManagerFactory cannot set %s = %s", name, value));
        }
    }
    return NuxeoContainer.initTransactionManager(config);
}

From source file:org.pepstock.jem.node.resources.impl.jdbc.JdbcFactory.java

@Override
public Object getObjectInstance(Object object, Name name, Context ctx,
        @SuppressWarnings("rawtypes") Hashtable env) throws Exception {
    // reads the reference
    Reference ref = (Reference) object;

    // get the resource custom properties
    // if exits, adds to CONNECTION properties,
    // which is usd by Apache DB pool to set connection properties
    RefAddr ra = ref.get(CommonKeys.RESOURCE_CUSTOM_PROPERTIES);
    if (ra != null) {
        String propertyValue = ra.getContent().toString();
        ref.add(new StringRefAddr(JdbcResourceKeys.PROP_CONNECTIONPROPERTIES, propertyValue));
    }/*from   w w  w  . j a  va 2  s  .  co  m*/
    // DBPool want USERNAME instead of USERID
    RefAddr raUser = ref.get(CommonKeys.USERID);
    if (raUser != null) {
        String propertyValue = raUser.getContent().toString();
        ref.add(new StringRefAddr(JdbcResourceKeys.PROP_USERNAME, propertyValue));
    }
    // call super
    return super.getObjectInstance(ref, name, ctx, env);
}

From source file:org.pepstock.jem.node.resources.impl.jndi.JndiFactory.java

@Override
public Object getObjectInstance(Object object, Name name, Context ctx,
        @SuppressWarnings("rawtypes") Hashtable env) throws Exception {
    // reads the reference
    Reference ref = (Reference) object;
    // creates the environment for JNDI object
    Hashtable<String, String> newEnv = new Hashtable<String, String>();

    boolean readOnly = false;
    // loads all properties defined for this resource
    Properties props = loadProperties(object, JndiResourceKeys.PROPERTIES_ALL);
    for (Entry<Object, Object> entry : props.entrySet()) {
        // Doesn't add the readOnly property to the environment
        // but sets readonly
        if (entry.getKey().toString().equalsIgnoreCase(JndiResourceKeys.READONLY)) {
            // gets if is read only registry. Default is false
            readOnly = Parser.parseBoolean(props.getProperty(JndiResourceKeys.READONLY, "false"), false);
        } else {//from   w w w. j  av  a 2 s  . c om
            newEnv.put(entry.getKey().toString(), entry.getValue().toString());
        }
    }
    // get the resource custom properties
    RefAddr ra = ref.get(CommonKeys.RESOURCE_CUSTOM_PROPERTIES);
    if (ra != null) {
        // loads environments
        String propertyValue = ra.getContent().toString();
        // parses the custom properties
        // format: key equals value semi-colon
        String[] keys = propertyValue.split(";");
        for (int i = 0; i < keys.length; i++) {
            String[] values = keys[i].split("=");
            // loads the new environment
            newEnv.put(values[0], values[1]);
        }
    }
    // return initial context
    return new ContextWrapper(new InitialContext(newEnv), readOnly);
}

From source file:org.soulwing.cas.client.ProtocolConfigurationFactory.java

public Object getObjectInstance(Object o, Name name, Context ctx, Hashtable env) throws Exception {

    if (o == null || !(o instanceof Reference)) {
        log.error("expected a Reference object");
        return null;
    }//  w  w w. ja  va  2 s .c  om

    Reference ref = (Reference) o;
    if (!ProtocolConfiguration.class.isAssignableFrom(Class.forName(ref.getClassName()))) {
        log.error("expected a reference type of " + ProtocolConfiguration.class.getCanonicalName());
        return null;
    }

    RefAddr ra = null;
    ProtocolConfigurationImpl config = new ProtocolConfigurationImpl();

    ra = ref.get("serverUrl");
    if (ra != null) {
        config.setServerUrl(ra.getContent().toString());
    } else {
        log.error("serverUrl property is required");
        return null;
    }

    ra = ref.get("serviceUrl");
    if (ra != null) {
        config.setServiceUrl(ra.getContent().toString());
    }

    ra = ref.get("proxyCallbackUrl");
    if (ra != null) {
        config.setProxyCallbackUrl(ra.getContent().toString());
    }

    ra = ref.get("renew");
    if (ra != null) {
        config.setRenewFlag(Boolean.parseBoolean(ra.getContent().toString().toLowerCase()));
    }

    ra = ref.get("gateway");
    if (ra != null) {
        config.setGatewayFlag(Boolean.parseBoolean(ra.getContent().toString().toLowerCase()));
    }

    return config;
}

From source file:ru.zinin.redis.factory.JedisPoolFactory.java

/** {@inheritDoc} */
@Override/*from   w  w  w  .ja  v a2s  .c  o m*/
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment)
        throws Exception {
    if ((obj == null) || !(obj instanceof Reference)) {
        return (null);
    }
    Reference ref = (Reference) obj;
    if (!"redis.clients.jedis.JedisPool".equals(ref.getClassName())) {
        return (null);
    }

    String host = "localhost";
    int port = Protocol.DEFAULT_PORT;
    int timeout = Protocol.DEFAULT_TIMEOUT;
    String password = null;

    RefAddr hostRefAddr = ref.get("host");
    if (hostRefAddr != null) {
        host = hostRefAddr.getContent().toString();
    }
    RefAddr portRefAddr = ref.get("port");
    if (portRefAddr != null) {
        port = Integer.parseInt(portRefAddr.getContent().toString());
    }
    RefAddr timeoutRefAddr = ref.get("timeout");
    if (timeoutRefAddr != null) {
        timeout = Integer.parseInt(timeoutRefAddr.getContent().toString());
    }
    RefAddr passwordRefAddr = ref.get("password");
    if (passwordRefAddr != null) {
        password = passwordRefAddr.getContent().toString();
    }

    log.debug("Creating pool...");
    log.debug("Host: " + host);
    log.debug("Port: " + port);
    log.debug("Timeout: " + timeout);
    log.trace("Password: " + password);

    JedisPoolConfig config = new JedisPoolConfig();
    RefAddr maxActiveRefAddr = ref.get("max-active");
    if (maxActiveRefAddr != null) {
        int maxActive = Integer.parseInt(maxActiveRefAddr.getContent().toString());
        log.debug("Setting maxActive to " + maxActive);
        config.maxActive = maxActive;
    }

    return new JedisPool(config, host, port, timeout, password);
}