use of javax.naming.spi.ObjectFactory in project aries by apache.
the class ObjectFactoryHelper method getObjectInstanceUsingRefAddress.
private Object getObjectInstanceUsingRefAddress(Enumeration<RefAddr> addresses, Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
Object result = null;
while (addresses.hasMoreElements()) {
RefAddr address = addresses.nextElement();
if (address instanceof StringRefAddr && "URL".equals(address.getType())) {
String urlScheme = getUrlScheme((String) address.getContent());
ServicePair<ObjectFactory> factoryService = ContextHelper.getURLObjectFactory(callerContext, urlScheme, environment);
if (factoryService != null) {
ObjectFactory factory = factoryService.get();
String value = (String) address.getContent();
try {
result = factory.getObjectInstance(value, name, nameCtx, environment);
} finally {
factoryService.unget();
}
// loop we are in.
if (result != null && result != obj) {
break;
}
}
}
}
return (result == null) ? obj : result;
}
use of javax.naming.spi.ObjectFactory in project aries by apache.
the class DirObjectFactoryHelper method getObjectInstanceUsingObjectFactoryBuilders.
private Object getObjectInstanceUsingObjectFactoryBuilders(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment, Attributes attrs) throws Exception {
ObjectFactory factory = null;
ServiceReference[] refs = Utils.getReferencesPrivileged(callerContext, ObjectFactoryBuilder.class);
if (refs != null) {
Arrays.sort(refs, Utils.SERVICE_REFERENCE_COMPARATOR);
for (ServiceReference ref : refs) {
ObjectFactoryBuilder builder = (ObjectFactoryBuilder) Utils.getServicePrivileged(callerContext, ref);
try {
factory = builder.createObjectFactory(obj, environment);
} catch (NamingException e) {
// TODO: log it
} finally {
callerContext.ungetService(ref);
}
if (factory != null) {
break;
}
}
}
Object result = null;
if (factory != null) {
if (factory instanceof DirObjectFactory) {
result = ((DirObjectFactory) factory).getObjectInstance(obj, name, nameCtx, environment, attrs);
} else {
result = factory.getObjectInstance(obj, name, nameCtx, environment);
}
}
return (result == null) ? obj : result;
}
use of javax.naming.spi.ObjectFactory in project jdk8u_jdk by JetBrains.
the class LdapCtxFactory method getObjectInstance.
// ----------------- ObjectFactory interface --------------------
public Object getObjectInstance(Object ref, Name name, Context nameCtx, Hashtable<?, ?> env) throws Exception {
if (!isLdapRef(ref)) {
return null;
}
ObjectFactory factory = new ldapURLContextFactory();
String[] urls = getURLs((Reference) ref);
return factory.getObjectInstance(urls, name, nameCtx, env);
}
use of javax.naming.spi.ObjectFactory in project derby by apache.
the class DataSourceReferenceTest method assertDataSourceReferencePopulated.
/**
* Make sure it is possible to recreate and serialize/deserialize a
* populated data source.
* <p>
* Populated means the various bean properties have non-default
* values set.
*
* @param dsDesc data source descriptor
* @param className data source class name
* @throws Exception on a wide variety of error conditions...
*/
private void assertDataSourceReferencePopulated(DataSourceDescriptor dsDesc, String className) throws Exception {
println("Testing recreated populated data source.");
Class<?> clazz = Class.forName(className);
Object ds = clazz.getConstructor().newInstance();
// Populate the data source.
Iterator propIter = dsDesc.getPropertyIterator();
while (propIter.hasNext()) {
String property = (String) propIter.next();
String value = dsDesc.getPropertyValue(property);
Method getMethod = getGet(property, ds);
Method setMethod = getSet(getMethod, ds);
Class paramType = getMethod.getReturnType();
if (paramType.equals(Integer.TYPE)) {
setMethod.invoke(ds, new Object[] { Integer.valueOf(value) });
} else if (paramType.equals(String.class)) {
setMethod.invoke(ds, new Object[] { value });
} else if (paramType.equals(Boolean.TYPE)) {
setMethod.invoke(ds, new Object[] { Boolean.valueOf(value) });
} else if (paramType.equals(Short.TYPE)) {
setMethod.invoke(ds, new Object[] { Short.valueOf(value) });
} else if (paramType.equals(Long.TYPE)) {
setMethod.invoke(ds, new Object[] { Long.valueOf(value) });
} else {
fail("'" + property + "' not settable - update test!!");
}
}
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);
// Recreated should not be same instance as original.
assertNotSame(recreatedDs, ds);
compareDataSources(dsDesc, ds, recreatedDs, false);
// Serialize and recreate.
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, false);
}
use of javax.naming.spi.ObjectFactory 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);
}
Aggregations