Search in sources :

Example 1 with URLObjectFactoryFinder

use of org.apache.aries.jndi.urls.URLObjectFactoryFinder in project aries by apache.

the class ContextHelper method getURLObjectFactory.

public static final ServicePair<ObjectFactory> getURLObjectFactory(final BundleContext ctx, String urlScheme, Hashtable<?, ?> environment) throws NamingException {
    ServicePair<ObjectFactory> result = null;
    ServiceReference ref = ServiceTrackerCustomizers.URL_FACTORY_CACHE.find(urlScheme);
    if (ref == null) {
        ServiceReference[] refs = AccessController.doPrivileged(new PrivilegedAction<ServiceReference[]>() {

            public ServiceReference[] run() {
                return Activator.getURLObectFactoryFinderServices();
            }
        });
        if (refs != null) {
            for (final ServiceReference finderRef : refs) {
                URLObjectFactoryFinder finder = (URLObjectFactoryFinder) Utils.getServicePrivileged(ctx, finderRef);
                if (finder != null) {
                    ObjectFactory f = finder.findFactory(urlScheme, environment);
                    if (f != null) {
                        result = new ServicePair<ObjectFactory>(ctx, finderRef, f);
                        break;
                    } else {
                        ctx.ungetService(finderRef);
                    }
                }
            }
        }
    } else {
        result = new ServicePair<ObjectFactory>(ctx, ref);
    }
    return result;
}
Also used : ObjectFactory(javax.naming.spi.ObjectFactory) URLObjectFactoryFinder(org.apache.aries.jndi.urls.URLObjectFactoryFinder) ServiceReference(org.osgi.framework.ServiceReference)

Example 2 with URLObjectFactoryFinder

use of org.apache.aries.jndi.urls.URLObjectFactoryFinder in project aries by apache.

the class ObjectFactoryTest method testURLReferenceUsingURLObjectFactoryFinder.

@Test
public void testURLReferenceUsingURLObjectFactoryFinder() throws Exception {
    String testObject = "Test object";
    URLObjectFactoryFinder factory = Skeleton.newMock(URLObjectFactoryFinder.class);
    Skeleton.getSkeleton(factory).setReturnValue(new MethodCall(ObjectFactory.class, "getObjectInstance", Object.class, Name.class, Context.class, Hashtable.class), testObject);
    bc.registerService(URLObjectFactoryFinder.class.getName(), factory, (Dictionary) new Properties());
    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) URLObjectFactoryFinder(org.apache.aries.jndi.urls.URLObjectFactoryFinder) Properties(java.util.Properties) MethodCall(org.apache.aries.unittest.mocks.MethodCall) Name(javax.naming.Name) Test(org.junit.Test)

Aggregations

ObjectFactory (javax.naming.spi.ObjectFactory)2 URLObjectFactoryFinder (org.apache.aries.jndi.urls.URLObjectFactoryFinder)2 Hashtable (java.util.Hashtable)1 Properties (java.util.Properties)1 Context (javax.naming.Context)1 Name (javax.naming.Name)1 Reference (javax.naming.Reference)1 StringRefAddr (javax.naming.StringRefAddr)1 MethodCall (org.apache.aries.unittest.mocks.MethodCall)1 Test (org.junit.Test)1 BundleContext (org.osgi.framework.BundleContext)1 ServiceReference (org.osgi.framework.ServiceReference)1