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);
}
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;
}
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;
}
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;
}
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());
}
Aggregations