Search in sources :

Example 61 with Reference

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

the class TestDataSourceFactory method testBuildFBXADataSource_nonStandardProperties.

/**
 * Tests reconstruction of a {@link FBXADataSource} using a reference.
 * <p>
 * This test is done with a selection of properties set through the {@link FBXADataSource#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 testBuildFBXADataSource_nonStandardProperties() throws Exception {
    final FBXADataSource originalDS = new FBXADataSource();
    // 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();
    FBXADataSource newDS = (FBXADataSource) 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)

Example 62 with Reference

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

the class TestDataSourceFactory method testBuildFBConnectionPoolDataSource_basicProperties.

/**
 * Tests reconstruction of a {@link FBConnectionPoolDataSource} using a reference.
 * <p>
 * This test is done with the basic properties exposed through setters. 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>
 * </ol>
 * </p>
 * @throws Exception
 */
@Test
public void testBuildFBConnectionPoolDataSource_basicProperties() throws Exception {
    final FBConnectionPoolDataSource originalDS = new FBConnectionPoolDataSource();
    fillFBAbstractCommonDataSourceProperties(originalDS);
    Reference ref = originalDS.getReference();
    assertEquals("Unexpected factory name", DataSourceFactory.class.getName(), ref.getFactoryClassName());
    assertEquals("Unexpected class name", FBConnectionPoolDataSource.class.getName(), ref.getClassName());
    FBConnectionPoolDataSource newDS = (FBConnectionPoolDataSource) new DataSourceFactory().getObjectInstance(ref, null, null, null);
    assertNotSame("Expected distinct new object", originalDS, newDS);
    assertFBAbstractCommonDataSourceProperties(newDS);
}
Also used : Reference(javax.naming.Reference) Test(org.junit.Test)

Example 63 with Reference

use of javax.naming.Reference in project activemq-artemis by apache.

the class ActiveMQRAConnectionFactoryObjectFactory method getObjectInstance.

@Override
public Object getObjectInstance(final Object ref, final Name name, final Context ctx, final Hashtable<?, ?> props) throws Exception {
    Reference r = (Reference) ref;
    byte[] bytes = (byte[]) r.get("ActiveMQ-CF").getContent();
    // Deserialize
    return SerializableObjectRefAddr.deserialize(bytes);
}
Also used : Reference(javax.naming.Reference)

Example 64 with Reference

use of javax.naming.Reference in project activemq-artemis by apache.

the class ReadOnlyContext method lookup.

@Override
public Object lookup(String name) throws NamingException {
    if (name.length() == 0) {
        return this;
    }
    Object result = treeBindings.get(name);
    if (result == null) {
        result = bindings.get(name);
    }
    if (result == null) {
        int pos = name.indexOf(':');
        if (pos > 0) {
            String scheme = name.substring(0, pos);
            Context ctx = NamingManager.getURLContext(scheme, environment);
            if (ctx == null) {
                throw new NamingException("scheme " + scheme + " not recognized");
            }
            return ctx.lookup(name);
        } else {
            // Split out the first name of the path
            // and look for it in the bindings map.
            CompositeName path = new CompositeName(name);
            if (path.size() == 0) {
                return this;
            } else {
                String first = path.get(0);
                Object obj = bindings.get(first);
                if (obj == null) {
                    throw new NameNotFoundException(name);
                } else if (obj instanceof Context && path.size() > 1) {
                    Context subContext = (Context) obj;
                    obj = subContext.lookup(path.getSuffix(1));
                }
                return obj;
            }
        }
    }
    if (result instanceof LinkRef) {
        LinkRef ref = (LinkRef) result;
        result = lookup(ref.getLinkName());
    }
    if (result instanceof Reference) {
        try {
            result = NamingManager.getObjectInstance(result, null, null, this.environment);
        } catch (NamingException e) {
            throw e;
        } catch (Exception e) {
            throw (NamingException) new NamingException("could not look up : " + name).initCause(e);
        }
    }
    if (result instanceof ReadOnlyContext) {
        String prefix = getNameInNamespace();
        if (prefix.length() > 0) {
            prefix = prefix + SEPARATOR;
        }
        result = new ReadOnlyContext((ReadOnlyContext) result, environment, prefix + name);
    }
    return result;
}
Also used : Context(javax.naming.Context) NameNotFoundException(javax.naming.NameNotFoundException) Reference(javax.naming.Reference) CompositeName(javax.naming.CompositeName) NamingException(javax.naming.NamingException) NotContextException(javax.naming.NotContextException) NamingException(javax.naming.NamingException) OperationNotSupportedException(javax.naming.OperationNotSupportedException) NameNotFoundException(javax.naming.NameNotFoundException) LinkRef(javax.naming.LinkRef)

Example 65 with Reference

use of javax.naming.Reference in project activemq-artemis by apache.

the class JNDIReferenceFactory method createReference.

/**
 * Create a Reference instance from a JNDIStorable object
 *
 * @param instanceClassName
 *     The name of the class that is being created.
 * @param po
 *     The properties object to use when configuring the new instance.
 *
 * @return Reference
 *
 * @throws NamingException if an error occurs while creating the new instance.
 */
public static Reference createReference(String instanceClassName, JNDIStorable po) throws NamingException {
    Reference result = new Reference(instanceClassName, JNDIReferenceFactory.class.getName(), null);
    try {
        Properties props = po.getProperties();
        for (Enumeration iter = props.propertyNames(); iter.hasMoreElements(); ) {
            String key = (String) iter.nextElement();
            result.add(new StringRefAddr(key, props.getProperty(key)));
        }
    } catch (Exception e) {
        throw new NamingException(e.getMessage());
    }
    return result;
}
Also used : Enumeration(java.util.Enumeration) StringRefAddr(javax.naming.StringRefAddr) Reference(javax.naming.Reference) NamingException(javax.naming.NamingException) Properties(java.util.Properties) NamingException(javax.naming.NamingException)

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