Example usage for javax.naming StringRefAddr StringRefAddr

List of usage examples for javax.naming StringRefAddr StringRefAddr

Introduction

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

Prototype

public StringRefAddr(String addrType, String addr) 

Source Link

Document

Constructs a new instance of StringRefAddr using its address type and contents.

Usage

From source file:NonSerializableFactory.java

/** A convenience method that simplifies the process of rebinding a
 non-serializable object into a JNDI context.
        /* w ww.ja  va 2s  .  c om*/
@param ctx the JNDI context to rebind to.
@param key the key to use in both the NonSerializableFactory map and JNDI. It
must be a valid name for use in ctx.bind().
@param target the non-Serializable object to bind.
@throws NamingException thrown on failure to rebind key into ctx.
*/
public static synchronized void rebind(Context ctx, String key, Object target) throws NamingException {
    NonSerializableFactory.rebind(key, target);
    // Bind a reference to target using NonSerializableFactory as the ObjectFactory
    String className = target.getClass().getName();
    String factory = NonSerializableFactory.class.getName();
    StringRefAddr addr = new StringRefAddr("nns", key);
    Reference memoryRef = new Reference(className, addr, factory, null);
    ctx.rebind(key, memoryRef);
}

From source file:com.funambol.server.db.DataSourceFactoryTest.java

/**
 * Test of getObjectInstance method, of class DataSourceFactory.
 *///from  www . j ava 2  s  .  co  m
public void testGetObjectInstance_fnbluser() throws Exception {
    DataSourceFactory dataSourceFactory = new DataSourceFactory();
    InitialContext context = new InitialContext();
    Hashtable environment = new Hashtable();

    Name name = new CompositeName("fnbluser");

    Reference ref = new Reference("javax.sql.DataSource");
    RefAddr userName = new StringRefAddr("minIdle", "7");
    ref.add(userName);

    //
    // At the end this should be overwritten by the value in the fnblds.xml file
    //
    RefAddr url = new StringRefAddr("url", "this-is-the-url");
    ref.add(url);

    Object result = dataSourceFactory.getObjectInstance(ref, name, context, environment);
    assertTrue(result instanceof org.apache.tomcat.dbcp.dbcp.BasicDataSource);

    BasicDataSource bds = (BasicDataSource) result;
    assertEquals(7, bds.getMinIdle());

    //
    // These are read from the fnbluser.xml
    //
    assertEquals("fnbluserpwd", bds.getUsername());
    assertEquals("fnblpwd", bds.getPassword());
    assertEquals("org.fnbluser", bds.getDriverClassName());
    assertEquals("jdbc:hsqldb:hsql://localhost/user", bds.getUrl());

}

From source file:com.funambol.server.db.DataSourceFactoryTest.java

/**
 * Test of getObjectInstance method, of class DataSourceFactory.
 *//*from   w w  w.ja  v a 2s  . c  om*/
public void testGetObjectInstance_fnblcore_with_wrappedFactory() throws Exception {

    DataSourceFactory dataSourceFactory = new DataSourceFactory();
    InitialContext context = new InitialContext();
    Hashtable environment = new Hashtable();

    Name name = new CompositeName("fnblcore");

    Reference ref = new Reference("javax.sql.DataSource");

    RefAddr wrFactory = new StringRefAddr("wrappedFactory", "org.apache.commons.dbcp.BasicDataSourceFactory");
    ref.add(wrFactory);

    RefAddr minIdle = new StringRefAddr("minIdle", "3");
    ref.add(minIdle);

    Object result = dataSourceFactory.getObjectInstance(ref, name, context, environment);
    assertTrue(result instanceof org.apache.commons.dbcp.BasicDataSource);

    org.apache.commons.dbcp.BasicDataSource bds = (org.apache.commons.dbcp.BasicDataSource) result;
    assertEquals(3, bds.getMinIdle());
    //
    // These are read from the db.xml
    //
    assertEquals("db", bds.getUsername());
    assertEquals("org.hsqldb.jdbcDriver", bds.getDriverClassName());

    //
    // These are read from the fnblds.xml
    //
    assertEquals("ss", bds.getPassword());
    assertEquals("jdbc:hsqldb:hsql://localhost/funambol", bds.getUrl());
}

From source file:com.alibaba.wasp.jdbcx.JdbcDataSource.java

/**
 * Get a new reference for this object, using the current settings.
 * /*from   w w  w. j a  v  a 2  s.c o m*/
 * @return the new reference
 */
public Reference getReference() {
    String factoryClassName = JdbcDataSourceFactory.class.getName();
    Reference ref = new Reference(getClass().getName(), factoryClassName, null);
    ref.add(new StringRefAddr("url", url));
    ref.add(new StringRefAddr("user", userName));
    ref.add(new StringRefAddr("password", convertToString(passwordChars)));
    ref.add(new StringRefAddr("loginTimeout", String.valueOf(loginTimeout)));
    ref.add(new StringRefAddr("description", description));
    return ref;
}

From source file:com.funambol.server.db.DataSourceFactoryTest.java

/**
 * Test of getObjectInstance method, of class DataSourceFactory.
 *//*  www.jav a  2s.  c  o  m*/
public void testGetObjectInstance_fnbluser_routing_no_partition() throws Exception {

    DataSourceFactory dataSourceFactory = new DataSourceFactory();
    InitialContext context = new InitialContext();
    Hashtable environment = new Hashtable();

    Name name = new CompositeName("fnbluser-routing-nopartition");

    Reference ref = new Reference("javax.sql.DataSource");
    RefAddr userName = new StringRefAddr("minIdle", "7");
    ref.add(userName);

    //
    // At the end this should be overwritten by the value in the fnblds.xml file
    //
    RefAddr url = new StringRefAddr("url", "this-is-the-url");
    ref.add(url);

    Object result = dataSourceFactory.getObjectInstance(ref, name, context, environment);
    assertTrue(result instanceof RoutingDataSource);

    BasicDataSource bds = (BasicDataSource) PrivateAccessor.getField(result, "defaultDataSource");

    assertEquals(7, bds.getMinIdle());

    //
    // These are read from the fnbluser.xml
    //
    assertEquals("fnbluserpwd", bds.getUsername());
    assertEquals("fnblpwd", bds.getPassword());
    assertEquals("org.fnbluser", bds.getDriverClassName());
    assertEquals("jdbc:hsqldb:hsql://localhost/user", bds.getUrl());

    assertEquals(15, bds.getNumTestsPerEvictionRun()); // coming from db.xml
}

From source file:com.funambol.server.db.DataSourceFactoryTest.java

/**
 * Test of getObjectInstance method, of class DataSourceFactory.
 *///w ww.  j a  va 2 s .c o  m
public void testGetObjectInstance_fnbluser_routing_with_partition() throws Exception {

    DataSourceFactory dataSourceFactory = new DataSourceFactory();
    InitialContext context = new InitialContext();
    Hashtable environment = new Hashtable();

    Name name = new CompositeName("fnbluser-routing-with-partition");

    Reference ref = new Reference("javax.sql.DataSource");
    RefAddr userName = new StringRefAddr("minIdle", "7");
    ref.add(userName);

    //
    // At the end this should be overwritten by the value in the fnblds.xml file
    //
    RefAddr url = new StringRefAddr("url", "this-is-the-url");
    ref.add(url);

    Object result = dataSourceFactory.getObjectInstance(ref, name, context, environment);
    assertTrue(result instanceof RoutingDataSource);

    RoutingDataSource rds = (RoutingDataSource) result;
    rds.init();
    rds.configure();
    Map<String, DataSource> datasources = rds.getDataSources();
    assertEquals("Wrong number of datasources", 3, datasources.size());

    BasicDataSource ds1 = (BasicDataSource) datasources.get("user_part1");
    assertNotNull("user_part1 must be not null", ds1);
    assertEquals("sa", ds1.getUsername());
    assertEquals("", ds1.getPassword());
    assertEquals("jdbc:hsqldb:mem:user_part1", ds1.getUrl());
    assertEquals(15, ds1.getNumTestsPerEvictionRun()); // from db.xml
    assertEquals(7, ds1.getMinIdle()); // set with StringRefAddr

    BasicDataSource ds2 = (BasicDataSource) datasources.get("user_part2");
    assertNotNull("user_part2 must be not null", ds2);
    assertEquals("sa", ds2.getUsername());
    assertEquals("", ds2.getPassword());
    assertEquals("jdbc:hsqldb:mem:user_part2", ds2.getUrl());
    assertEquals(15, ds2.getNumTestsPerEvictionRun()); // from db.xml
    assertEquals(7, ds2.getMinIdle()); // set with StringRefAddr

    BasicDataSource ds3 = (BasicDataSource) datasources.get("user_part3");
    assertNotNull("user_part3 must be not null", ds3);
    assertEquals("sa", ds3.getUsername());
    assertEquals("", ds3.getPassword());
    assertEquals("jdbc:hsqldb:mem:user_part3", ds3.getUrl());
    assertEquals(15, ds3.getNumTestsPerEvictionRun()); // from db.xml
    assertEquals(7, ds3.getMinIdle()); // set with StringRefAddr

    BasicDataSource bds = (BasicDataSource) PrivateAccessor.getField(result, "defaultDataSource");

    assertEquals(7, bds.getMinIdle());

    //
    // These are read from the fnbluser.xml
    //
    assertEquals("fnbluserpartition", bds.getUsername());
    assertEquals("fnblpwd", bds.getPassword());
    assertEquals("org.fnbluser", bds.getDriverClassName());
    assertEquals("jdbc:hsqldb:hsql://localhost/user", bds.getUrl());

    assertTrue(rds.getPartitioningCriteria() instanceof MockPartitioningCriteria);
}

From source file:com.frameworkset.commons.dbcp2.datasources.PerUserPoolDataSource.java

/**
 * Returns a <code>PerUserPoolDataSource</code> {@link Reference}.
 *//*from   w  w  w. jav a2 s .co m*/
@Override
public Reference getReference() throws NamingException {
    Reference ref = new Reference(getClass().getName(), PerUserPoolDataSourceFactory.class.getName(), null);
    ref.add(new StringRefAddr("instanceKey", getInstanceKey()));
    return ref;
}

From source file:org.hibernate.internal.SessionFactoryImpl.java

public Reference getReference() {
    // from javax.naming.Referenceable
    LOG.debug("Returning a Reference to the SessionFactory");
    return new Reference(SessionFactoryImpl.class.getName(), new StringRefAddr("uuid", uuid),
            SessionFactoryRegistry.ObjectFactoryImpl.class.getName(), null);
}

From source file:org.apache.synapse.commons.datasource.JNDIBasedDataSourceRepository.java

/**
 * Register a DataSource in the JNDI tree
 *
 * @see DataSourceRepository#register(DataSourceInformation)
 *///from   w  ww  . j  ava  2  s.  c o  m
public void register(DataSourceInformation information) {

    validateInitialized();
    String dataSourceName = information.getDatasourceName();
    validateDSName(dataSourceName);
    Properties properties = information.getProperties();

    InitialContext context = null;
    Properties jndiEvn = null;

    if (properties == null || properties.isEmpty()) {
        if (initialContext != null) {
            context = initialContext;
            if (log.isDebugEnabled()) {
                log.debug("Empty JNDI properties for datasource " + dataSourceName);
                log.debug("Using system-wide jndi properties : " + jndiProperties);
            }

            jndiEvn = jndiProperties;
        }
    }

    if (context == null) {

        jndiEvn = createJNDIEnvironment(properties, information.getAlias());
        context = createInitialContext(jndiEvn);

        if (context == null) {

            validateInitialContext(initialContext);
            context = initialContext;

            if (log.isDebugEnabled()) {
                log.debug("Cannot create a name context with provided jndi properties : " + jndiEvn);
                log.debug("Using system-wide JNDI properties : " + jndiProperties);
            }

            jndiEvn = jndiProperties;
        } else {
            perDataSourceICMap.put(dataSourceName, context);
        }
    }

    String dsType = information.getType();
    String driver = information.getDriver();
    String url = information.getUrl();

    String user = information.getSecretInformation().getUser();
    String password = information.getSecretInformation().getResolvedSecret();

    String maxActive = String.valueOf(information.getMaxActive());
    String maxIdle = String.valueOf(information.getMaxIdle());
    String maxWait = String.valueOf(information.getMaxWait());

    //populates context tree
    populateContextTree(context, dataSourceName);

    if (DataSourceInformation.BASIC_DATA_SOURCE.equals(dsType)) {

        Reference ref = new Reference("javax.sql.DataSource", "org.apache.commons.dbcp.BasicDataSourceFactory",
                null);

        ref.add(new StringRefAddr(DataSourceConstants.PROP_DRIVER_CLS_NAME, driver));
        ref.add(new StringRefAddr(DataSourceConstants.PROP_URL, url));
        ref.add(new StringRefAddr(SecurityConstants.PROP_USER_NAME, user));
        ref.add(new StringRefAddr(SecurityConstants.PROP_PASSWORD, password));
        ref.add(new StringRefAddr(DataSourceConstants.PROP_MAX_ACTIVE, maxActive));
        ref.add(new StringRefAddr(DataSourceConstants.PROP_MAX_IDLE, maxIdle));
        ref.add(new StringRefAddr(DataSourceConstants.PROP_MAX_WAIT, maxWait));

        // set BasicDataSource specific parameters
        setBasicDataSourceParameters(ref, information);
        //set default jndiProperties for reference
        setCommonParameters(ref, information);

        try {

            if (log.isDebugEnabled()) {
                log.debug("Registering a DataSource with name : " + dataSourceName
                        + " in the JNDI tree with jndiProperties : " + jndiEvn);
            }

            context.rebind(dataSourceName, ref);
        } catch (NamingException e) {
            String msg = " Error binding name ' " + dataSourceName + " ' to "
                    + "the DataSource(BasicDataSource) reference";
            throw new SynapseCommonsException(msg, e, log);
        }

    } else if (DataSourceInformation.PER_USER_POOL_DATA_SOURCE.equals(dsType)) {

        // Construct DriverAdapterCPDS reference
        String className = (String) information.getParameter(DataSourceConstants.PROP_CPDS_ADAPTER
                + DataSourceConstants.DOT_STRING + DataSourceConstants.PROP_CPDS_CLASS_NAME);
        String factory = (String) information.getParameter(DataSourceConstants.PROP_CPDS_ADAPTER
                + DataSourceConstants.DOT_STRING + DataSourceConstants.PROP_CPDS_FACTORY);
        String name = (String) information.getParameter(DataSourceConstants.PROP_CPDS_ADAPTER
                + DataSourceConstants.DOT_STRING + DataSourceConstants.PROP_CPDS_NAME);

        Reference cpdsRef = new Reference(className, factory, null);

        cpdsRef.add(new StringRefAddr(DataSourceConstants.PROP_DRIVER, driver));
        cpdsRef.add(new StringRefAddr(DataSourceConstants.PROP_URL, url));
        cpdsRef.add(new StringRefAddr(DataSourceConstants.PROP_USER, user));
        cpdsRef.add(new StringRefAddr(SecurityConstants.PROP_PASSWORD, password));

        try {
            context.rebind(name, cpdsRef);
        } catch (NamingException e) {
            String msg = "Error binding name '" + name + "' to " + "the DriverAdapterCPDS reference";
            throw new SynapseCommonsException(msg, e, log);
        }

        // Construct PerUserPoolDataSource reference
        Reference ref = new Reference("org.apache.commons.dbcp.datasources.PerUserPoolDataSource",
                "org.apache.commons.dbcp.datasources.PerUserPoolDataSourceFactory", null);

        ref.add(new BinaryRefAddr(DataSourceConstants.PROP_JNDI_ENV, MiscellaneousUtil.serialize(jndiEvn)));
        ref.add(new StringRefAddr(DataSourceConstants.PROP_DATA_SOURCE_NAME, name));
        ref.add(new StringRefAddr(DataSourceConstants.PROP_DEFAULT_MAX_ACTIVE, maxActive));
        ref.add(new StringRefAddr(DataSourceConstants.PROP_DEFAULT_MAX_IDLE, maxIdle));
        ref.add(new StringRefAddr(DataSourceConstants.PROP_DEFAULT_MAX_WAIT, maxWait));

        //set default jndiProperties for reference
        setCommonParameters(ref, information);

        try {

            if (log.isDebugEnabled()) {
                log.debug("Registering a DataSource with name : " + dataSourceName
                        + " in the JNDI tree with jndiProperties : " + jndiEvn);
            }

            context.rebind(dataSourceName, ref);
        } catch (NamingException e) {
            String msg = "Error binding name ' " + dataSourceName + " ' to "
                    + "the PerUserPoolDataSource reference";
            throw new SynapseCommonsException(msg, e, log);
        }

    } else {
        throw new SynapseCommonsException("Unsupported data source type : " + dsType, log);
    }
    cachedNameList.add(dataSourceName);
}