Search in sources :

Example 56 with Reference

use of javax.naming.Reference in project derby by apache.

the class DataSourceReferenceTest method assertDataSourceReferenceEmpty.

/**
 * Make sure it is possible to create a new data source using
 * <code>Referencable</code>, that the new instance has the correct
 * default values set for the bean properties and finally that the
 * data source can be serialized/deserialized.
 *
 * @param dsDesc data source descriptor
 * @param className data source class name
 * @throws Exception on a wide variety of error conditions...
 */
private void assertDataSourceReferenceEmpty(DataSourceDescriptor dsDesc, String className) throws Exception {
    println("Testing recreated empty data source.");
    // Create an empty data source.
    Class<?> clazz = Class.forName(className);
    Object ds = clazz.getConstructor().newInstance();
    Referenceable refDs = (Referenceable) ds;
    Reference dsAsReference = refDs.getReference();
    String factoryClassName = dsAsReference.getFactoryClassName();
    clazz = Class.forName(factoryClassName);
    ObjectFactory factory = (ObjectFactory) clazz.getConstructor().newInstance();
    Object recreatedDs = factory.getObjectInstance(dsAsReference, null, null, null);
    // Empty, recreated data source should not be the same as the one we
    // created earlier on.
    assertNotNull("Recreated datasource is <null>", recreatedDs);
    assertNotSame(recreatedDs, ds);
    compareDataSources(dsDesc, ds, recreatedDs, true);
    // Serialize and recreate data source with default values.
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(ds);
    oos.flush();
    oos.close();
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ObjectInputStream ois = new ObjectInputStream(bais);
    recreatedDs = ois.readObject();
    compareDataSources(dsDesc, ds, recreatedDs, true);
}
Also used : Referenceable(javax.naming.Referenceable) ObjectFactory(javax.naming.spi.ObjectFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) Reference(javax.naming.Reference) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 57 with Reference

use of javax.naming.Reference in project derby by apache.

the class ClientDataSource method getReference.

// ------------------------ Referenceable interface methods -----------------------------
@Override
public Reference getReference() throws NamingException {
    // This method creates a new Reference object to represent this data
    // source.  The class name of the data source object is saved in the
    // Reference, so that an object factory will know that it should
    // create an instance of that class when a lookup operation is
    // performed. The class name of the object factory,
    // org.apache.derby.client.ClientBaseDataSourceFactory, is also stored
    // in the reference.  This is not required by JNDI, but is recommend
    // in practice.  JNDI will always use the object factory class
    // specified in the reference when reconstructing an object, if a
    // class name has been specified.
    // 
    // See the JNDI SPI documentation for further details on this topic,
    // and for a complete description of the Reference and StringRefAddr
    // classes.
    // 
    // This BasicClientDataSource40 class provides several standard JDBC
    // properties.  The names and values of the data source properties are
    // also stored in the reference using the StringRefAddr class.  This
    // is all the information needed to reconstruct a ClientDataSource
    // object.
    Reference ref = new Reference(this.getClass().getName(), ClientDataSourceFactory.class.getName(), null);
    addBeanProperties(ref);
    return ref;
}
Also used : Reference(javax.naming.Reference) ClientDataSourceFactory(org.apache.derby.client.ClientDataSourceFactory)

Example 58 with Reference

use of javax.naming.Reference in project derby by apache.

the class EmbeddedDataSource method getReference.

/**
 * {@code javax.naming.Referenceable} interface
 */
/**
 * This method creates a new {@code Reference} object to represent this
 * data source.  The class name of the data source object is saved
 * in the {@code Reference}, so that an object factory will know that it
 * should create an instance of that class when a lookup operation
 * is performed. The class is also stored in the reference.  This
 * is not required by JNDI, but is recommend in practice.  JNDI
 * will always use the object factory class specified in the
 * reference when reconstructing an object, if a class name has
 * been specified.  See the JNDI SPI documentation for further
 * details on this topic, and for a complete description of the
 * {@code Reference} and {@code StringRefAddr} classes.
 * <p/>
 * Derby data source classes class provides several standard JDBC
 * properties.  The names and values of the data source properties
 * are also stored in the reference using the {@code StringRefAddr} class.
 * This is all the information needed to reconstruct an embedded
 * data source object.
 *
 * @return the created reference object for this data source
 * @exception NamingException cannot find named object
 */
@Override
public final Reference getReference() throws NamingException {
    // These fields will be set by the JNDI server when it decides to
    // materialize a data source.
    Reference ref = new Reference(this.getClass().getName(), "org.apache.derby.jdbc.ReferenceableDataSource", null);
    addBeanProperties(this, ref);
    return ref;
}
Also used : Reference(javax.naming.Reference)

Example 59 with Reference

use of javax.naming.Reference in project jaybird by FirebirdSQL.

the class FBConnectionPoolDataSource method getReference.

public Reference getReference() throws NamingException {
    Reference ref = new Reference(getClass().getName(), DataSourceFactory.class.getName(), null);
    FBAbstractCommonDataSource.updateReference(ref, this);
    return ref;
}
Also used : Reference(javax.naming.Reference)

Example 60 with Reference

use of javax.naming.Reference in project jaybird by FirebirdSQL.

the class TestDataSourceFactory method testBuildFBConnectionPoolDataSource_nonStandardProperties.

/**
 * Tests reconstruction of a {@link FBConnectionPoolDataSource} using a reference.
 * <p>
 * This test is done with a selection of properties set through the {@link FBConnectionPoolDataSource#setNonStandardProperty(String)} methods. It tests
 * <ol>
 * <li>If the reference returned has the right factory name</li>
 * <li>If the reference returned has the right classname</li>
 * <li>If the object returned by the factory is a distinct new instance</li>
 * <li>If all the properties set on the original are also set on the new instance</li>
 * <li>If an unset property is handled correctly</li>
 * </ol>
 * </p>
 * @throws Exception
 */
@Test
public void testBuildFBConnectionPoolDataSource_nonStandardProperties() throws Exception {
    final FBConnectionPoolDataSource originalDS = new FBConnectionPoolDataSource();
    // note number of buffers is apparently byte, so using higher values can give weird results
    originalDS.setNonStandardProperty("buffersNumber=127");
    originalDS.setNonStandardProperty("defaultTransactionIsolation", Integer.toString(Connection.TRANSACTION_SERIALIZABLE));
    originalDS.setNonStandardProperty("madeUpProperty", "madeUpValue");
    Reference ref = originalDS.getReference();
    FBConnectionPoolDataSource newDS = (FBConnectionPoolDataSource) new DataSourceFactory().getObjectInstance(ref, null, null, null);
    assertEquals("127", newDS.getNonStandardProperty("buffersNumber"));
    assertEquals(Integer.toString(Connection.TRANSACTION_SERIALIZABLE), newDS.getNonStandardProperty("defaultTransactionIsolation"));
    assertEquals("madeUpValue", newDS.getNonStandardProperty("madeUpProperty"));
    assertNull(newDS.getDescription());
}
Also used : Reference(javax.naming.Reference) Test(org.junit.Test)

Aggregations

Reference (javax.naming.Reference)226 NamingException (javax.naming.NamingException)79 RefAddr (javax.naming.RefAddr)59 StringRefAddr (javax.naming.StringRefAddr)54 Context (javax.naming.Context)41 NameNotFoundException (javax.naming.NameNotFoundException)40 InitialContext (javax.naming.InitialContext)37 Test (org.junit.Test)33 Name (javax.naming.Name)30 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)28 Properties (java.util.Properties)26 OperationNotSupportedException (javax.naming.OperationNotSupportedException)25 NotContextException (javax.naming.NotContextException)24 ObjectFactory (javax.naming.spi.ObjectFactory)23 CompositeName (javax.naming.CompositeName)21 Referenceable (javax.naming.Referenceable)18 Binding (javax.naming.Binding)17 CompoundName (javax.naming.CompoundName)16 LinkRef (javax.naming.LinkRef)15 Hashtable (java.util.Hashtable)12