Search in sources :

Example 1 with StringRefAddr

use of javax.naming.StringRefAddr in project jetty.project by eclipse.

the class TestJNDI method testIt.

@Test
public void testIt() throws Exception {
    //set up some classloaders
    Thread currentThread = Thread.currentThread();
    ClassLoader currentLoader = currentThread.getContextClassLoader();
    ClassLoader childLoader1 = new URLClassLoader(new URL[0], currentLoader);
    ClassLoader childLoader2 = new URLClassLoader(new URL[0], currentLoader);
    try {
        //Uncomment to aid with debug
        /*
            javaRootURLContext.getRoot().addListener(new NamingContext.Listener()
            {
                public void unbind(NamingContext ctx, Binding binding)
                {
                    System.err.println("java unbind "+binding+" from "+ctx.getName());
                }
                
                public Binding bind(NamingContext ctx, Binding binding)
                {
                    System.err.println("java bind "+binding+" to "+ctx.getName());
                    return binding;
                }
            });
            
            localContextRoot.getRoot().addListener(new NamingContext.Listener()
            {
                public void unbind(NamingContext ctx, Binding binding)
                {
                    System.err.println("local unbind "+binding+" from "+ctx.getName());
                }
                
                public Binding bind(NamingContext ctx, Binding binding)
                {
                    System.err.println("local bind "+binding+" to "+ctx.getName());
                    return binding;
                }
            });
            */
        //Set up the tccl before doing any jndi operations
        currentThread.setContextClassLoader(childLoader1);
        InitialContext initCtx = new InitialContext();
        //Test we can lookup the root java: naming tree
        Context sub0 = (Context) initCtx.lookup("java:");
        assertNotNull(sub0);
        //already be bound 
        try {
            Context sub1 = sub0.createSubcontext("comp");
            fail("Comp should already be bound");
        } catch (NameAlreadyBoundException e) {
        //expected exception
        }
        //check bindings at comp
        Context sub1 = (Context) initCtx.lookup("java:comp");
        assertNotNull(sub1);
        Context sub2 = sub1.createSubcontext("env");
        assertNotNull(sub2);
        initCtx.bind("java:comp/env/rubbish", "abc");
        assertEquals("abc", initCtx.lookup("java:comp/env/rubbish"));
        //check binding LinkRefs
        LinkRef link = new LinkRef("java:comp/env/rubbish");
        initCtx.bind("java:comp/env/poubelle", link);
        assertEquals("abc", initCtx.lookup("java:comp/env/poubelle"));
        //check binding References
        StringRefAddr addr = new StringRefAddr("blah", "myReferenceable");
        Reference ref = new Reference(java.lang.String.class.getName(), addr, MyObjectFactory.class.getName(), null);
        initCtx.bind("java:comp/env/quatsch", ref);
        assertEquals(MyObjectFactory.myString, initCtx.lookup("java:comp/env/quatsch"));
        //test binding something at java:
        Context sub3 = initCtx.createSubcontext("java:zero");
        initCtx.bind("java:zero/one", "ONE");
        assertEquals("ONE", initCtx.lookup("java:zero/one"));
        //change the current thread's classloader to check distinct naming
        currentThread.setContextClassLoader(childLoader2);
        Context otherSub1 = (Context) initCtx.lookup("java:comp");
        assertTrue(!(sub1 == otherSub1));
        try {
            initCtx.lookup("java:comp/env/rubbish");
            fail("env should not exist for this classloader");
        } catch (NameNotFoundException e) {
        //expected
        }
        //put the thread's classloader back
        currentThread.setContextClassLoader(childLoader1);
        //test rebind with existing binding
        initCtx.rebind("java:comp/env/rubbish", "xyz");
        assertEquals("xyz", initCtx.lookup("java:comp/env/rubbish"));
        //test rebind with no existing binding
        initCtx.rebind("java:comp/env/mullheim", "hij");
        assertEquals("hij", initCtx.lookup("java:comp/env/mullheim"));
        //test that the other bindings are already there
        assertEquals("xyz", initCtx.lookup("java:comp/env/poubelle"));
        //test java:/comp/env/stuff
        assertEquals("xyz", initCtx.lookup("java:/comp/env/poubelle/"));
        //test list Names
        NamingEnumeration nenum = initCtx.list("java:comp/env");
        HashMap results = new HashMap();
        while (nenum.hasMore()) {
            NameClassPair ncp = (NameClassPair) nenum.next();
            results.put(ncp.getName(), ncp.getClassName());
        }
        assertEquals(4, results.size());
        assertEquals("java.lang.String", results.get("rubbish"));
        assertEquals("javax.naming.LinkRef", results.get("poubelle"));
        assertEquals("java.lang.String", results.get("mullheim"));
        assertEquals("javax.naming.Reference", results.get("quatsch"));
        //test list Bindings
        NamingEnumeration benum = initCtx.list("java:comp/env");
        assertEquals(4, results.size());
        //test NameInNamespace
        assertEquals("comp/env", sub2.getNameInNamespace());
        //test close does nothing
        Context closeCtx = (Context) initCtx.lookup("java:comp/env");
        closeCtx.close();
        //test what happens when you close an initial context
        InitialContext closeInit = new InitialContext();
        closeInit.close();
        //check locking the context
        Context ectx = (Context) initCtx.lookup("java:comp");
        ectx.bind("crud", "xxx");
        ectx.addToEnvironment("org.eclipse.jndi.immutable", "TRUE");
        assertEquals("xxx", initCtx.lookup("java:comp/crud"));
        try {
            ectx.bind("crud2", "xxx2");
        } catch (NamingException ne) {
        //expected failure to modify immutable context
        }
        initCtx.close();
    } finally {
        //make some effort to clean up
        InitialContext ic = new InitialContext();
        Context java = (Context) ic.lookup("java:");
        java.destroySubcontext("zero");
        java.destroySubcontext("fee");
        currentThread.setContextClassLoader(childLoader1);
        Context comp = (Context) ic.lookup("java:comp");
        comp.destroySubcontext("env");
        comp.unbind("crud");
        comp.unbind("crud2");
        currentThread.setContextClassLoader(currentLoader);
    }
}
Also used : Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) NamingContext(org.eclipse.jetty.jndi.NamingContext) NameNotFoundException(javax.naming.NameNotFoundException) HashMap(java.util.HashMap) Reference(javax.naming.Reference) NamingEnumeration(javax.naming.NamingEnumeration) InitialContext(javax.naming.InitialContext) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) StringRefAddr(javax.naming.StringRefAddr) NameClassPair(javax.naming.NameClassPair) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) NamingException(javax.naming.NamingException) LinkRef(javax.naming.LinkRef) Test(org.junit.Test)

Example 2 with StringRefAddr

use of javax.naming.StringRefAddr in project jetty.project by eclipse.

the class MailSessionReference method setUser.

public void setUser(String user) {
    StringRefAddr addr = (StringRefAddr) get("user");
    if (addr != null) {
        throw new RuntimeException("user already set on SessionReference, can't be changed");
    }
    add(new StringRefAddr("user", user));
}
Also used : StringRefAddr(javax.naming.StringRefAddr)

Example 3 with StringRefAddr

use of javax.naming.StringRefAddr in project jetty.project by eclipse.

the class MailSessionReference method setPassword.

public void setPassword(String password) {
    StringRefAddr addr = (StringRefAddr) get("pwd");
    if (addr != null)
        throw new RuntimeException("password already set on SessionReference, can't be changed");
    add(new StringRefAddr("pwd", password));
}
Also used : StringRefAddr(javax.naming.StringRefAddr)

Example 4 with StringRefAddr

use of javax.naming.StringRefAddr in project jetty.project by eclipse.

the class ContextFactory method newNamingContext.

/**
     * Create a new NamingContext.
     * @param obj the object to create
     * @param loader the classloader for the naming context
     * @param env the jndi env for the entry
     * @param name the name of the entry
     * @param parentCtx the parent context of the entry
     * @return the newly created naming context
     * @throws Exception if unable to create a new naming context
     */
public NamingContext newNamingContext(Object obj, ClassLoader loader, Hashtable env, Name name, Context parentCtx) throws Exception {
    Reference ref = (Reference) obj;
    StringRefAddr parserAddr = (StringRefAddr) ref.get("parser");
    String parserClassName = (parserAddr == null ? null : (String) parserAddr.getContent());
    NameParser parser = (NameParser) (parserClassName == null ? null : loader.loadClass(parserClassName).newInstance());
    return new NamingContext(env, name.get(0), (NamingContext) parentCtx, parser);
}
Also used : StringRefAddr(javax.naming.StringRefAddr) Reference(javax.naming.Reference) NameParser(javax.naming.NameParser)

Example 5 with StringRefAddr

use of javax.naming.StringRefAddr in project aries by apache.

the class ObjectFactoryHelper method getObjectInstanceUsingRefAddress.

private Object getObjectInstanceUsingRefAddress(Enumeration<RefAddr> addresses, Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
    Object result = null;
    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();
                try {
                    result = factory.getObjectInstance(value, name, nameCtx, environment);
                } finally {
                    factoryService.unget();
                }
                // loop we are in.
                if (result != null && result != obj) {
                    break;
                }
            }
        }
    }
    return (result == null) ? obj : result;
}
Also used : RefAddr(javax.naming.RefAddr) StringRefAddr(javax.naming.StringRefAddr) DirObjectFactory(javax.naming.spi.DirObjectFactory) ObjectFactory(javax.naming.spi.ObjectFactory) StringRefAddr(javax.naming.StringRefAddr)

Aggregations

StringRefAddr (javax.naming.StringRefAddr)68 Reference (javax.naming.Reference)51 NamingException (javax.naming.NamingException)18 RefAddr (javax.naming.RefAddr)9 Test (org.junit.Test)8 Name (javax.naming.Name)7 Properties (java.util.Properties)6 CompositeName (javax.naming.CompositeName)5 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)5 ObjectFactory (javax.naming.spi.ObjectFactory)5 MalformedURLException (java.net.MalformedURLException)4 Enumeration (java.util.Enumeration)4 Hashtable (java.util.Hashtable)4 Iterator (java.util.Iterator)4 Context (javax.naming.Context)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 NameNotFoundException (javax.naming.NameNotFoundException)3 Context (org.apache.catalina.Context)3 NamingContext (org.apache.naming.NamingContext)3