Java Code Examples for javax.naming.spi.ObjectFactory#getObjectInstance()

The following examples show how to use javax.naming.spi.ObjectFactory#getObjectInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: right_LmiInitialContext_1.6.java    From gumtree-spoon-ast-diff with Apache License 2.0 6 votes vote down vote up
/**
 * Resolve a Remote Object: If this object is a reference return the
 * reference
 * @param o the object to resolve
 * @param n the name of this object
 * @return a <code>Referenceable</code> if o is a Reference and the
 *         inititial object o if else
 */
private Object resolveObject(Object o, Name name) {
    try {
        if (o instanceof Reference) {
            // build of the Referenceable object with is Reference
            Reference objRef = (Reference) o;
            ObjectFactory objFact = (ObjectFactory) (Thread.currentThread().getContextClassLoader()
                    .loadClass(objRef.getFactoryClassName())).newInstance();
            return objFact.getObjectInstance(objRef, name, this, this.getEnvironment());
        } else {
            return o;
        }
    } catch (Exception e) {
        TraceCarol.error("LmiInitialContext.resolveObject()", e);
        return o;
    }
}
 
Example 2
Source File: left_LmiInitialContext_1.5.java    From gumtree-spoon-ast-diff with Apache License 2.0 6 votes vote down vote up
/**
 * Resolve a Remote Object: If this object is a reference return the
 * reference
 * @param o the object to resolve
 * @param n the name of this object
 * @return a <code>Referenceable</code> if o is a Reference and the
 *         inititial object o if else
 */
private Object resolveObject(Object o, Name name) {
    try {
        if (o instanceof Reference) {
            // build of the Referenceable object with is Reference
            Reference objRef = (Reference) o;
            ObjectFactory objFact = (ObjectFactory) (Thread.currentThread().getContextClassLoader()
                    .loadClass(objRef.getFactoryClassName())).newInstance();
            return objFact.getObjectInstance(objRef, name, this, this.getEnvironment());
        } else {
            return o;
        }
    } catch (Exception e) {
        TraceCarol.error("LmiInitialContext.resolveObject()", e);
        return o;
    }
}
 
Example 3
Source File: LdapCtxFactory.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 4
Source File: StringRefAddrReferenceTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
private <T> T getObject(Reference reference, Class<T> tClass) throws Exception {
   String factoryName = reference.getFactoryClassName();
   Class<?> factoryClass = Class.forName(factoryName);
   ObjectFactory factory = (ObjectFactory) factoryClass.newInstance();
   Object o = factory.getObjectInstance(reference, null, null, null);
   if (tClass.isAssignableFrom(tClass)) {
      return tClass.cast(o);
   } else {
      throw new IllegalStateException("Expected class, " + tClass.getName());
   }
}
 
Example 5
Source File: DestinationObjectFactoryTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testReference() throws Exception {
   ActiveMQDestination queue = (ActiveMQDestination) ActiveMQJMSClient.createQueue(RandomUtil.randomString());
   Reference reference = queue.getReference();
   String factoryName = reference.getFactoryClassName();
   Class<?> factoryClass = Class.forName(factoryName);
   ObjectFactory factory = (ObjectFactory) factoryClass.newInstance();
   Object object = factory.getObjectInstance(reference, null, null, null);
   Assert.assertNotNull(object);
   Assert.assertTrue(object instanceof ActiveMQDestination);
   Assert.assertEquals(queue, object);
}
 
Example 6
Source File: LdapCtxFactory.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 7
Source File: DataSourceReferenceTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * 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.
    Object ds = Class.forName(className).newInstance();
    Referenceable refDs = (Referenceable)ds;
    Reference dsAsReference = refDs.getReference();
    String factoryClassName = dsAsReference.getFactoryClassName();
    ObjectFactory factory =
        (ObjectFactory)Class.forName(factoryClassName).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);
}
 
Example 8
Source File: LdapCtxFactory.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 9
Source File: LdapCtxFactory.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 10
Source File: DataSourceReferenceTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * 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.
    Object ds = Class.forName(className).newInstance();
    Referenceable refDs = (Referenceable)ds;
    Reference dsAsReference = refDs.getReference();
    String factoryClassName = dsAsReference.getFactoryClassName();
    ObjectFactory factory =
        (ObjectFactory)Class.forName(factoryClassName).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);
}
 
Example 11
Source File: LdapCtxFactory.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 12
Source File: LdapCtxFactory.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 13
Source File: LdapCtxFactory.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 14
Source File: LdapCtxFactory.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 15
Source File: LdapCtxFactory.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 16
Source File: LdapCtxFactory.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 17
Source File: DataSourceReferenceTest.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 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.
    Object ds = Class.forName(className).newInstance();
    Referenceable refDs = (Referenceable)ds;
    Reference dsAsReference = refDs.getReference();
    String factoryClassName = dsAsReference.getFactoryClassName();
    ObjectFactory factory =
        (ObjectFactory)Class.forName(factoryClassName).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);
}
 
Example 18
Source File: DataSourceReferenceTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * 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.");
    Object ds = Class.forName(className).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();
    ObjectFactory factory =
        (ObjectFactory)Class.forName(factoryClassName).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);
}
 
Example 19
Source File: DataSourceReferenceTest.java    From spliceengine with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * 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.");
    Object ds = Class.forName(className).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();
    ObjectFactory factory =
        (ObjectFactory)Class.forName(factoryClassName).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);
}
 
Example 20
Source File: ReferenceableTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testReferenceCF() throws Exception {
   Reference cfRef = ((Referenceable) cf).getReference();

   String factoryName = cfRef.getFactoryClassName();

   Class<?> factoryClass = Class.forName(factoryName);

   ObjectFactory factory = (ObjectFactory) factoryClass.newInstance();

   Object instance = factory.getObjectInstance(cfRef, null, null, null);

   ProxyAssertSupport.assertTrue(instance instanceof ActiveMQConnectionFactory);

   ActiveMQJMSConnectionFactory cf2 = (ActiveMQJMSConnectionFactory) instance;

   simpleSendReceive(cf2, queue1);
}