Search in sources :

Example 1 with ObjectFactory

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;
}
Also used : RefAddr(javax.naming.RefAddr) StringRefAddr(javax.naming.StringRefAddr) DirObjectFactory(javax.naming.spi.DirObjectFactory) ObjectFactory(javax.naming.spi.ObjectFactory) StringRefAddr(javax.naming.StringRefAddr)

Example 2 with ObjectFactory

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;
}
Also used : DirObjectFactory(javax.naming.spi.DirObjectFactory) ObjectFactory(javax.naming.spi.ObjectFactory) ObjectFactoryBuilder(javax.naming.spi.ObjectFactoryBuilder) NamingException(javax.naming.NamingException) DirObjectFactory(javax.naming.spi.DirObjectFactory) ServiceReference(org.osgi.framework.ServiceReference)

Example 3 with ObjectFactory

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);
}
Also used : com.sun.jndi.url.ldap.ldapURLContextFactory(com.sun.jndi.url.ldap.ldapURLContextFactory) ObjectFactory(javax.naming.spi.ObjectFactory)

Example 4 with ObjectFactory

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);
}
Also used : Reference(javax.naming.Reference) Method(java.lang.reflect.Method) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) Referenceable(javax.naming.Referenceable) ObjectFactory(javax.naming.spi.ObjectFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) Iterator(java.util.Iterator) ObjectInputStream(java.io.ObjectInputStream)

Example 5 with ObjectFactory

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

Aggregations

ObjectFactory (javax.naming.spi.ObjectFactory)59 Reference (javax.naming.Reference)22 NamingException (javax.naming.NamingException)21 Test (org.junit.Test)16 DirObjectFactory (javax.naming.spi.DirObjectFactory)14 RefAddr (javax.naming.RefAddr)12 Hashtable (java.util.Hashtable)10 BundleContext (org.osgi.framework.BundleContext)9 Context (javax.naming.Context)7 MethodCall (org.apache.aries.unittest.mocks.MethodCall)6 ServiceAwareObjectFactory (org.jboss.as.naming.ServiceAwareObjectFactory)6 Name (javax.naming.Name)5 Referenceable (javax.naming.Referenceable)5 ServiceReference (org.osgi.framework.ServiceReference)5 InitialContext (javax.naming.InitialContext)4 StringRefAddr (javax.naming.StringRefAddr)4 InitialLdapContext (javax.naming.ldap.InitialLdapContext)4 LdapContext (javax.naming.ldap.LdapContext)4 Properties (java.util.Properties)3 ObjectFactoryBuilder (javax.naming.spi.ObjectFactoryBuilder)3