Search in sources :

Example 16 with ObjectFactory

use of javax.naming.spi.ObjectFactory in project aries by apache.

the class ServiceRegistryContextTest method testJNDIRegistration.

/**
 * This test checks that we correctly register and deregister the url context
 * object factory in the service registry.
 */
@Test
public void testJNDIRegistration() {
    ServiceReference ref = bc.getServiceReference(ObjectFactory.class.getName());
    assertNotNull("The aries url context object factory was not registered", ref);
    ObjectFactory factory = (ObjectFactory) bc.getService(ref);
    assertNotNull("The aries url context object factory was null", factory);
}
Also used : ObjectFactory(javax.naming.spi.ObjectFactory) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 17 with ObjectFactory

use of javax.naming.spi.ObjectFactory in project aries by apache.

the class ObjectFactoryHelper method getObjectInstanceUsingObjectFactoryBuilders.

private Object getObjectInstanceUsingObjectFactoryBuilders(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment, Attributes attrs) throws Exception {
    ObjectFactory factory = null;
    for (ObjectFactoryBuilder ofb : Activator.getServices(callerContext, ObjectFactoryBuilder.class)) {
        try {
            factory = ofb.createObjectFactory(obj, environment);
        } catch (NamingException e) {
        // TODO: log it
        }
        if (factory != null) {
            break;
        }
    }
    Object result = null;
    if (factory != null) {
        result = getObjectFromFactory(obj, name, nameCtx, environment, attrs, factory);
    }
    return (result == null) ? obj : result;
}
Also used : DirObjectFactory(javax.naming.spi.DirObjectFactory) ObjectFactory(javax.naming.spi.ObjectFactory) ObjectFactoryBuilder(javax.naming.spi.ObjectFactoryBuilder)

Example 18 with ObjectFactory

use of javax.naming.spi.ObjectFactory in project aries by apache.

the class ObjectFactoryTest method testSpecifiedFactoryWithRegisteredButNotMatchingFactory.

@Test
public void testSpecifiedFactoryWithRegisteredButNotMatchingFactory() throws Exception {
    String testObject = "Test object";
    ObjectFactory factory = Skeleton.newMock(ObjectFactory.class);
    Skeleton.getSkeleton(factory).setReturnValue(new MethodCall(ObjectFactory.class, "getObjectInstance", Object.class, Name.class, Context.class, Hashtable.class), testObject);
    Reference ref = new Reference("dummy.class.name", "dummy.factory.class.name", "");
    bc.registerService(new String[] { ObjectFactory.class.getName(), factory.getClass().getName() }, factory, null);
    Object obj = NamingManager.getObjectInstance(ref, null, null, env);
    assertSame("The naming manager should have returned the reference object", ref, obj);
}
Also used : BundleContext(org.osgi.framework.BundleContext) Context(javax.naming.Context) ObjectFactory(javax.naming.spi.ObjectFactory) Hashtable(java.util.Hashtable) Reference(javax.naming.Reference) MethodCall(org.apache.aries.unittest.mocks.MethodCall) Name(javax.naming.Name) Test(org.junit.Test)

Example 19 with ObjectFactory

use of javax.naming.spi.ObjectFactory in project aries by apache.

the class ObjectFactoryTest method testFactoriesThatDoUnsafeCastsAreIgnored.

@Test
public void testFactoriesThatDoUnsafeCastsAreIgnored() throws Exception {
    Hashtable<String, Object> props = new Hashtable<String, Object>();
    props.put("aries.object.factory.requires.reference", Boolean.TRUE);
    bc.registerService(ObjectFactory.class.getName(), new ObjectFactory() {

        public Object getObjectInstance(Object arg0, Name arg1, Context arg2, Hashtable<?, ?> arg3) throws Exception {
            return (Reference) arg0;
        }
    }, props);
    NamingManager.getObjectInstance("Some dummy data", null, null, env);
}
Also used : BundleContext(org.osgi.framework.BundleContext) Context(javax.naming.Context) ObjectFactory(javax.naming.spi.ObjectFactory) Hashtable(java.util.Hashtable) Name(javax.naming.Name) Test(org.junit.Test)

Example 20 with ObjectFactory

use of javax.naming.spi.ObjectFactory in project aries by apache.

the class ObjectFactoryTest method testReferenceWithNoClassName.

@Test
public void testReferenceWithNoClassName() throws Exception {
    String testObject = "Test object";
    ObjectFactory factory = Skeleton.newMock(ObjectFactory.class);
    Skeleton.getSkeleton(factory).setReturnValue(new MethodCall(ObjectFactory.class, "getObjectInstance", Object.class, Name.class, Context.class, Hashtable.class), testObject);
    bc.registerService(ObjectFactory.class.getName(), factory, null);
    Reference ref = new Reference(null);
    Object obj = NamingManager.getObjectInstance(ref, null, null, env);
    assertEquals("The naming manager should have returned the test object", testObject, obj);
}
Also used : BundleContext(org.osgi.framework.BundleContext) Context(javax.naming.Context) ObjectFactory(javax.naming.spi.ObjectFactory) Hashtable(java.util.Hashtable) Reference(javax.naming.Reference) MethodCall(org.apache.aries.unittest.mocks.MethodCall) Name(javax.naming.Name) Test(org.junit.Test)

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