Search in sources :

Example 51 with ObjectFactory

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

the class ObjectFactoryHelper method getObjectInstanceUsingRefAddress.

private Object getObjectInstanceUsingRefAddress(Enumeration<RefAddr> addresses, Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment, Attributes attrs) throws Exception {
    while (addresses.hasMoreElements()) {
        RefAddr address = addresses.nextElement();
        if (address instanceof StringRefAddr && "URL".equals(address.getType())) {
            String urlScheme = getUrlScheme((String) address.getContent());
            ServicePair<ObjectFactory> factoryService = ContextHelper.getURLObjectFactory(callerContext, urlScheme, environment);
            if (factoryService != null) {
                ObjectFactory factory = factoryService.get();
                String value = (String) address.getContent();
                Object result = getObjectFromFactory(value, name, nameCtx, environment, attrs, factory);
                // loop we are in.
                if (result != null && result != obj) {
                    return result;
                }
            }
        }
    }
    return obj;
}
Also used : DirObjectFactory(javax.naming.spi.DirObjectFactory) ObjectFactory(javax.naming.spi.ObjectFactory)

Example 52 with ObjectFactory

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

the class ObjectFactoryHelper method getObjectInstanceViaContextDotObjectFactories.

/*
     * Attempt to obtain an Object instance via the java.naming.factory.object property
     */
private Object getObjectInstanceViaContextDotObjectFactories(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment, Attributes attrs) throws Exception {
    Object result = null;
    String factories = (String) environment.get(Context.OBJECT_FACTORIES);
    if (factories != null && factories.length() > 0) {
        String[] candidates = factories.split(":");
        ClassLoader cl = Utils.doPrivileged(Thread.currentThread()::getContextClassLoader);
        for (String cand : candidates) {
            ObjectFactory factory;
            try {
                @SuppressWarnings("unchecked") Class<ObjectFactory> clz = (Class<ObjectFactory>) cl.loadClass(cand);
                factory = clz.newInstance();
            } catch (Exception e) {
                if (logger.isLoggable(Level.FINE))
                    logger.log(Level.FINE, "Exception instantiating factory: " + e);
                continue;
            }
            if (logger.isLoggable(Level.FINE))
                logger.log(Level.FINE, "cand=" + cand + " factory=" + factory);
            if (factory != null) {
                result = getObjectFromFactory(obj, name, nameCtx, environment, attrs, factory);
            }
            if (result != null && result != obj)
                break;
        }
    }
    if (logger.isLoggable(Level.FINE))
        logger.log(Level.FINE, "result = " + result);
    return (result == null) ? obj : result;
}
Also used : DirObjectFactory(javax.naming.spi.DirObjectFactory) ObjectFactory(javax.naming.spi.ObjectFactory)

Example 53 with ObjectFactory

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

the class InitialContextTest method testURLLookup.

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

        public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
            return dummyContext("result");
        }
    };
    registerURLObjectFactory(of, "test");
    ic = initialContext();
    assertEquals("result", ic.lookup("test:something"));
}
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 54 with ObjectFactory

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

the class InitialContextTest method testLookupWithoutICFButWithURLLookup.

@Test
public void testLookupWithoutICFButWithURLLookup() throws NamingException {
    ObjectFactory factory = Skeleton.newMock(ObjectFactory.class);
    Context ctx = Skeleton.newMock(Context.class);
    Skeleton.getSkeleton(factory).setReturnValue(new MethodCall(ObjectFactory.class, "getObjectInstance", Object.class, Name.class, Context.class, Hashtable.class), ctx);
    Skeleton.getSkeleton(ctx).setReturnValue(new MethodCall(Context.class, "lookup", String.class), "someText");
    Properties props = new Properties();
    props.put(JNDIConstants.JNDI_URLSCHEME, "testURL");
    bc.registerService(ObjectFactory.class.getName(), factory, (Dictionary) props);
    props = new Properties();
    props.put(JNDIConstants.BUNDLE_CONTEXT, bc);
    InitialContext initialCtx = new InitialContext(props);
    Object someObject = initialCtx.lookup("testURL:somedata");
    assertEquals("Expected to be given a string, but got something else.", "someText", someObject);
}
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) Properties(java.util.Properties) MethodCall(org.apache.aries.unittest.mocks.MethodCall) Test(org.junit.Test)

Example 55 with ObjectFactory

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

the class InitialContextTest method testNoURLContextCaching.

@Test
public void testNoURLContextCaching() throws Exception {
    final AtomicBoolean second = new AtomicBoolean(false);
    final Context ctx = dummyContext("one");
    final Context ctx2 = dummyContext("two");
    ObjectFactory of = new ObjectFactory() {

        public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
            if (second.get())
                return ctx2;
            else {
                second.set(true);
                return ctx;
            }
        }
    };
    registerURLObjectFactory(of, "test");
    ic = initialContext();
    assertEquals("one", ic.lookup("test:something"));
    assertEquals("two", ic.lookup("test:something"));
}
Also used : InitialLdapContext(javax.naming.ldap.InitialLdapContext) BundleContext(org.osgi.framework.BundleContext) LdapContext(javax.naming.ldap.LdapContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ObjectFactory(javax.naming.spi.ObjectFactory) Hashtable(java.util.Hashtable) 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