Search in sources :

Example 46 with ObjectFactory

use of javax.naming.spi.ObjectFactory in project wildfly by wildfly.

the class ObjectFactoryBuilder method factoryFromReference.

private ObjectFactory factoryFromReference(final Reference reference, final ClassLoader classLoader, final Hashtable<?, ?> environment) throws Exception {
    try {
        final Class<?> factoryClass = classLoader.loadClass(reference.getFactoryClassName());
        ObjectFactory factory = ObjectFactory.class.cast(factoryClass.newInstance());
        if (factory instanceof ServiceAwareObjectFactory) {
            ((ServiceAwareObjectFactory) factory).injectServiceRegistry(currentServiceContainer());
        }
        return factory;
    } catch (Throwable t) {
        throw NamingLogger.ROOT_LOGGER.objectFactoryCreationFailure(t);
    }
}
Also used : DirObjectFactory(javax.naming.spi.DirObjectFactory) ServiceAwareObjectFactory(org.jboss.as.naming.ServiceAwareObjectFactory) ObjectFactory(javax.naming.spi.ObjectFactory) ServiceAwareObjectFactory(org.jboss.as.naming.ServiceAwareObjectFactory)

Example 47 with ObjectFactory

use of javax.naming.spi.ObjectFactory in project wildfly by wildfly.

the class InitialContext method removeUrlContextFactory.

/**
 * Remove an ObjectFactory from the map of registered ones. To make sure that not anybody can remove an
 * ObjectFactory both the scheme as well as the actual object factory itself need to be supplied. So you
 * can only remove the factory if you have the factory object.
 * @param scheme The URL scheme for which the handler is registered.
 * @param factory The factory object associated with the scheme
 * @throws IllegalArgumentException if the requested scheme/factory combination is not registered.
 */
public static synchronized void removeUrlContextFactory(final String scheme, ObjectFactory factory) {
    Map<String, ObjectFactory> factories = new HashMap<String, ObjectFactory>(urlContextFactories);
    ObjectFactory f = factories.get(scheme);
    if (f == factory) {
        factories.remove(scheme);
        urlContextFactories = Collections.unmodifiableMap(factories);
        return;
    } else {
        throw new IllegalArgumentException();
    }
}
Also used : ObjectFactory(javax.naming.spi.ObjectFactory) HashMap(java.util.HashMap)

Example 48 with ObjectFactory

use of javax.naming.spi.ObjectFactory in project wildfly by wildfly.

the class InitialContextTestCase method testRegisterURLSchemeHandler.

@Test
public void testRegisterURLSchemeHandler() throws Exception {
    InitialContext ictx = new InitialContext(null);
    try {
        ictx.lookup("foobar:something");
        Assert.fail("Precondition: the foobar: scheme should not yet be registered");
    } catch (NamingException ne) {
    // good
    }
    ObjectFactory tof = new TestObjectFactory();
    InitialContext.addUrlContextFactory("foobar", tof);
    String something = (String) ictx.lookup("foobar:something");
    Assert.assertTrue("The object should now be provided by our TestObjectFactory", something.startsWith("TestObject:"));
    try {
        InitialContext.removeUrlContextFactory("foobar:", new TestObjectFactory());
        Assert.fail("Should throw an IllegalArgumentException since the associated factory object doesn't match the registration");
    } catch (IllegalArgumentException iae) {
    // good;
    }
    Assert.assertEquals("The foobar: scheme should still be registered", something, ictx.lookup("foobar:something"));
    InitialContext.removeUrlContextFactory("foobar", tof);
    try {
        ictx.lookup("foobar:something");
        Assert.fail("The foobar: scheme should not be registered any more");
    } catch (NamingException ne) {
    // good
    }
}
Also used : ObjectFactory(javax.naming.spi.ObjectFactory) NamingException(javax.naming.NamingException) Test(org.junit.Test)

Example 49 with ObjectFactory

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

the class ObjectFactoryTest method testURLReferenceWithMatchingHandler.

@Test
public void testURLReferenceWithMatchingHandler() 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);
    Properties props = new Properties();
    props.setProperty("osgi.jndi.urlScheme", "wibble");
    bc.registerService(ObjectFactory.class.getName(), factory, (Dictionary) props);
    Reference ref = new Reference(null);
    ref.add(new StringRefAddr("URL", "wibble"));
    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) StringRefAddr(javax.naming.StringRefAddr) Hashtable(java.util.Hashtable) Reference(javax.naming.Reference) Properties(java.util.Properties) MethodCall(org.apache.aries.unittest.mocks.MethodCall) Name(javax.naming.Name) Test(org.junit.Test)

Example 50 with ObjectFactory

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

the class ObjectFactoryHelper method getObjectInstanceUsingClassName.

private Object getObjectInstanceUsingClassName(Object reference, String className, Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment, Attributes attrs) throws Exception {
    Object result = null;
    ObjectFactory factory = ObjectFactoryHelper.findObjectFactoryByClassName(defaultContext, className);
    if (factory != null) {
        result = getObjectFromFactory(reference, name, nameCtx, environment, attrs, factory);
    }
    return (result == null) ? obj : result;
}
Also used : DirObjectFactory(javax.naming.spi.DirObjectFactory) ObjectFactory(javax.naming.spi.ObjectFactory)

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