Search in sources :

Example 21 with ObjectFactory

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

the class ObjectFactoryTest method testSpecifiedFactoryWithMatchingFactory.

@Test
public void testSpecifiedFactoryWithMatchingFactory() 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", factory.getClass().getName(), "");
    bc.registerService(new String[] { ObjectFactory.class.getName(), factory.getClass().getName() }, factory, 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)

Example 22 with ObjectFactory

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

the class ObjectFactoryTest method testObjectFactoryThatThrowsIsIgnored.

@Test
public void testObjectFactoryThatThrowsIsIgnored() throws Exception {
    final ObjectFactory factory = Skeleton.newMock(ObjectFactory.class);
    bc.registerService(ObjectFactory.class.getName(), factory, null);
    final Skeleton skeleton = Skeleton.getSkeleton(factory);
    final MethodCall getObjectInstance = new MethodCall(ObjectFactory.class, "getObjectInstance", Object.class, Name.class, Context.class, Hashtable.class);
    skeleton.setThrows(getObjectInstance, new Exception());
    Object original = new Object() {
    };
    Object processed = NamingManager.getObjectInstance(original, null, null, env);
    skeleton.assertCalled(getObjectInstance);
    assertEquals("The original object should be returned despite the object factory throwing an exception", original, processed);
}
Also used : ObjectFactory(javax.naming.spi.ObjectFactory) Skeleton(org.apache.aries.unittest.mocks.Skeleton) MethodCall(org.apache.aries.unittest.mocks.MethodCall) Test(org.junit.Test)

Example 23 with ObjectFactory

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

the class InitialContextTest method registerURLObjectFactory.

/**
 * Registers an ObjectFactory to be used for creating URLContexts for the given scheme
 * @param of
 * @param scheme
 */
private void registerURLObjectFactory(ObjectFactory of, String scheme) {
    Properties props = new Properties();
    props.setProperty(JNDIConstants.JNDI_URLSCHEME, "test");
    bc.registerService(ObjectFactory.class.getName(), of, (Dictionary) props);
}
Also used : ObjectFactory(javax.naming.spi.ObjectFactory) Properties(java.util.Properties)

Example 24 with ObjectFactory

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

the class InitialContextTest method testURLContextErrorPropagation.

@Test
public void testURLContextErrorPropagation() throws Exception {
    ObjectFactory of = new ObjectFactory() {

        public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
            throw new Exception("doh");
        }
    };
    registerURLObjectFactory(of, "test");
    ic = initialContext();
    try {
        ic.lookup("test:something");
        Assert.fail("Expected NamingException");
    } catch (NamingException ne) {
        assertNotNull(ne.getCause());
        assertEquals("doh", ne.getCause().getMessage());
    }
}
Also used : InitialLdapContext(javax.naming.ldap.InitialLdapContext) BundleContext(org.osgi.framework.BundleContext) LdapContext(javax.naming.ldap.LdapContext) ObjectFactory(javax.naming.spi.ObjectFactory) Hashtable(java.util.Hashtable) Test(org.junit.Test)

Example 25 with ObjectFactory

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

the class ResourceFactory method getDefaultFactory.

@Override
protected ObjectFactory getDefaultFactory(Reference ref) throws NamingException {
    ObjectFactory factory = null;
    if (ref.getClassName().equals("javax.sql.DataSource")) {
        String javaxSqlDataSourceFactoryClassName = System.getProperty("javax.sql.DataSource.Factory", Constants.DBCP_DATASOURCE_FACTORY);
        try {
            factory = (ObjectFactory) Class.forName(javaxSqlDataSourceFactoryClassName).getConstructor().newInstance();
        } catch (Exception e) {
            NamingException ex = new NamingException(sm.getString("resourceFactory.factoryCreationError"));
            ex.initCause(e);
            throw ex;
        }
    } else if (ref.getClassName().equals("jakarta.mail.Session")) {
        String javaxMailSessionFactoryClassName = System.getProperty("jakarta.mail.Session.Factory", "org.apache.naming.factory.MailSessionFactory");
        try {
            factory = (ObjectFactory) Class.forName(javaxMailSessionFactoryClassName).getConstructor().newInstance();
        } catch (Throwable t) {
            if (t instanceof NamingException) {
                throw (NamingException) t;
            }
            if (t instanceof ThreadDeath) {
                throw (ThreadDeath) t;
            }
            if (t instanceof VirtualMachineError) {
                throw (VirtualMachineError) t;
            }
            NamingException ex = new NamingException(sm.getString("resourceFactory.factoryCreationError"));
            ex.initCause(t);
            throw ex;
        }
    }
    return factory;
}
Also used : ObjectFactory(javax.naming.spi.ObjectFactory) NamingException(javax.naming.NamingException) NamingException(javax.naming.NamingException)

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