Search in sources :

Example 1 with NamingContext

use of org.eclipse.jetty.jndi.NamingContext 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 NamingContext

use of org.eclipse.jetty.jndi.NamingContext in project jetty.project by eclipse.

the class localContextRoot method lookupLink.

/**
     *
     *
     * @see javax.naming.Context#lookupLink(javax.naming.Name)
     */
public Object lookupLink(Name name) throws NamingException {
    synchronized (__root) {
        //return __root.lookupLink(getSuffix(name));
        Name cname = __root.toCanonicalName(name);
        if (cname == null) {
            //If no name create copy of this context with same bindings, but with copy of the environment so it can be modified
            NamingContext ctx = new NamingContext(_env, null, null, __root.getNameParser(""));
            ctx.setBindings(__root.getBindings());
            return ctx;
        }
        if (cname.size() == 0)
            throw new NamingException("Name is empty");
        if (cname.size() == 1) {
            Binding binding = __root.getBinding(cname);
            if (binding == null)
                throw new NameNotFoundException();
            Object o = binding.getObject();
            //handle links by looking up the link
            if (o instanceof Reference) {
                //deference the object
                try {
                    return NamingManager.getObjectInstance(o, cname.getPrefix(1), __root, _env);
                } catch (NamingException e) {
                    throw e;
                } catch (Exception e) {
                    __log.warn("", e);
                    throw new NamingException(e.getMessage());
                }
            } else {
                //or a plain object in which case spec says we return it
                return o;
            }
        }
        //it is a multipart name, recurse to the first subcontext
        String firstComponent = cname.get(0);
        Object ctx = null;
        if (firstComponent.equals(""))
            ctx = this;
        else {
            Binding binding = __root.getBinding(firstComponent);
            if (binding == null)
                throw new NameNotFoundException();
            ctx = binding.getObject();
            if (ctx instanceof Reference) {
                //deference the object
                try {
                    ctx = NamingManager.getObjectInstance(ctx, getNameParser("").parse(firstComponent), __root, _env);
                } catch (NamingException e) {
                    throw e;
                } catch (Exception e) {
                    __log.warn("", e);
                    throw new NamingException(e.getMessage());
                }
            }
        }
        if (!(ctx instanceof Context))
            throw new NotContextException();
        return ((Context) ctx).lookup(cname.getSuffix(1));
    }
}
Also used : Binding(javax.naming.Binding) Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) NamingContext(org.eclipse.jetty.jndi.NamingContext) NotContextException(javax.naming.NotContextException) NameNotFoundException(javax.naming.NameNotFoundException) Reference(javax.naming.Reference) NamingException(javax.naming.NamingException) NamingContext(org.eclipse.jetty.jndi.NamingContext) NamingException(javax.naming.NamingException) NameNotFoundException(javax.naming.NameNotFoundException) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) NotContextException(javax.naming.NotContextException) OperationNotSupportedException(javax.naming.OperationNotSupportedException) CompoundName(javax.naming.CompoundName) Name(javax.naming.Name)

Example 3 with NamingContext

use of org.eclipse.jetty.jndi.NamingContext in project jetty.project by eclipse.

the class localContextRoot method createSubcontext.

/**
     *
     *
     * @see javax.naming.Context#createSubcontext(javax.naming.Name)
     */
public Context createSubcontext(Name name) throws NamingException {
    synchronized (__root) {
        if (__root.isLocked()) {
            NamingException ne = new NamingException("This context is immutable");
            ne.setRemainingName(name);
            throw ne;
        }
        Name cname = __root.toCanonicalName(name);
        if (cname == null)
            throw new NamingException("Name is null");
        if (cname.size() == 0)
            throw new NamingException("Name is empty");
        if (cname.size() == 1) {
            //not permitted to bind if something already bound at that name
            Binding binding = __root.getBinding(cname);
            if (binding != null)
                throw new NameAlreadyBoundException(cname.toString());
            //make a new naming context with the root as the parent
            Context ctx = new NamingContext((Hashtable) _env.clone(), cname.get(0), __root, __root.getNameParser(""));
            __root.addBinding(cname, ctx);
            return ctx;
        }
        //If the name has multiple subcontexts, walk the hierarchy by
        //fetching the first one. All intermediate subcontexts in the
        //name must already exist.
        String firstComponent = cname.get(0);
        Object ctx = null;
        if (firstComponent.equals(""))
            ctx = this;
        else {
            Binding binding = __root.getBinding(firstComponent);
            if (binding == null)
                throw new NameNotFoundException(firstComponent + " is not bound");
            ctx = binding.getObject();
            if (ctx instanceof Reference) {
                //deference the object
                if (__log.isDebugEnabled())
                    __log.debug("Object bound at " + firstComponent + " is a Reference");
                try {
                    ctx = NamingManager.getObjectInstance(ctx, getNameParser("").parse(firstComponent), __root, _env);
                } catch (NamingException e) {
                    throw e;
                } catch (Exception e) {
                    __log.warn("", e);
                    throw new NamingException(e.getMessage());
                }
            }
        }
        if (ctx instanceof Context) {
            return ((Context) ctx).createSubcontext(cname.getSuffix(1));
        } else
            throw new NotContextException(firstComponent + " is not a Context");
    }
}
Also used : Binding(javax.naming.Binding) Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) NamingContext(org.eclipse.jetty.jndi.NamingContext) NotContextException(javax.naming.NotContextException) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) NameNotFoundException(javax.naming.NameNotFoundException) Reference(javax.naming.Reference) NamingException(javax.naming.NamingException) NamingContext(org.eclipse.jetty.jndi.NamingContext) NamingException(javax.naming.NamingException) NameNotFoundException(javax.naming.NameNotFoundException) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) NotContextException(javax.naming.NotContextException) OperationNotSupportedException(javax.naming.OperationNotSupportedException) CompoundName(javax.naming.CompoundName) Name(javax.naming.Name)

Example 4 with NamingContext

use of org.eclipse.jetty.jndi.NamingContext in project jetty.project by eclipse.

the class TestJNDI method testJavaNameParsing.

@Test
public void testJavaNameParsing() throws Exception {
    Thread currentThread = Thread.currentThread();
    ClassLoader currentLoader = currentThread.getContextClassLoader();
    ClassLoader childLoader1 = new URLClassLoader(new URL[0], currentLoader);
    //set the current thread's classloader
    currentThread.setContextClassLoader(childLoader1);
    try {
        InitialContext initCtx = new InitialContext();
        Context sub0 = (Context) initCtx.lookup("java:");
        if (LOG.isDebugEnabled())
            LOG.debug("------ Looked up java: --------------");
        Name n = sub0.getNameParser("").parse("/red/green/");
        if (LOG.isDebugEnabled())
            LOG.debug("get(0)=" + n.get(0));
        if (LOG.isDebugEnabled())
            LOG.debug("getPrefix(1)=" + n.getPrefix(1));
        n = n.getSuffix(1);
        if (LOG.isDebugEnabled())
            LOG.debug("getSuffix(1)=" + n);
        if (LOG.isDebugEnabled())
            LOG.debug("get(0)=" + n.get(0));
        if (LOG.isDebugEnabled())
            LOG.debug("getPrefix(1)=" + n.getPrefix(1));
        n = n.getSuffix(1);
        if (LOG.isDebugEnabled())
            LOG.debug("getSuffix(1)=" + n);
        if (LOG.isDebugEnabled())
            LOG.debug("get(0)=" + n.get(0));
        if (LOG.isDebugEnabled())
            LOG.debug("getPrefix(1)=" + n.getPrefix(1));
        n = n.getSuffix(1);
        if (LOG.isDebugEnabled())
            LOG.debug("getSuffix(1)=" + n);
        n = sub0.getNameParser("").parse("pink/purple/");
        if (LOG.isDebugEnabled())
            LOG.debug("get(0)=" + n.get(0));
        if (LOG.isDebugEnabled())
            LOG.debug("getPrefix(1)=" + n.getPrefix(1));
        n = n.getSuffix(1);
        if (LOG.isDebugEnabled())
            LOG.debug("getSuffix(1)=" + n);
        if (LOG.isDebugEnabled())
            LOG.debug("get(0)=" + n.get(0));
        if (LOG.isDebugEnabled())
            LOG.debug("getPrefix(1)=" + n.getPrefix(1));
        NamingContext ncontext = (NamingContext) sub0;
        Name nn = ncontext.toCanonicalName(ncontext.getNameParser("").parse("/yellow/blue/"));
        LOG.debug(nn.toString());
        assertEquals(2, nn.size());
        nn = ncontext.toCanonicalName(ncontext.getNameParser("").parse("/yellow/blue"));
        LOG.debug(nn.toString());
        assertEquals(2, nn.size());
        nn = ncontext.toCanonicalName(ncontext.getNameParser("").parse("/"));
        if (LOG.isDebugEnabled())
            LOG.debug("/ parses as: " + nn + " with size=" + nn.size());
        LOG.debug(nn.toString());
        assertEquals(1, nn.size());
        nn = ncontext.toCanonicalName(ncontext.getNameParser("").parse(""));
        LOG.debug(nn.toString());
        assertEquals(0, nn.size());
        Context fee = ncontext.createSubcontext("fee");
        fee.bind("fi", "88");
        assertEquals("88", initCtx.lookup("java:/fee/fi"));
        assertEquals("88", initCtx.lookup("java:/fee/fi/"));
        assertTrue(initCtx.lookup("java:/fee/") instanceof javax.naming.Context);
    } finally {
        InitialContext ic = new InitialContext();
        Context java = (Context) ic.lookup("java:");
        java.destroySubcontext("fee");
        currentThread.setContextClassLoader(currentLoader);
    }
}
Also used : Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) NamingContext(org.eclipse.jetty.jndi.NamingContext) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) Context(javax.naming.Context) NamingContext(org.eclipse.jetty.jndi.NamingContext) InitialContext(javax.naming.InitialContext) Name(javax.naming.Name) Test(org.junit.Test)

Example 5 with NamingContext

use of org.eclipse.jetty.jndi.NamingContext in project jetty.project by eclipse.

the class localContextRoot method lookup.

/**
     *
     *
     * @see javax.naming.Context#lookup(javax.naming.Name)
     */
public Object lookup(Name name) throws NamingException {
    synchronized (__root) {
        if (__log.isDebugEnabled())
            __log.debug("Looking up name=\"" + name + "\"");
        Name cname = __root.toCanonicalName(name);
        if ((cname == null) || (cname.size() == 0)) {
            __log.debug("Null or empty name, returning copy of this context");
            NamingContext ctx = new NamingContext(_env, null, null, __root.getNameParser(""));
            ctx.setBindings(__root.getBindings());
            return ctx;
        }
        if (cname.size() == 1) {
            Binding binding = __root.getBinding(cname);
            if (binding == null) {
                NameNotFoundException nnfe = new NameNotFoundException();
                nnfe.setRemainingName(cname);
                throw nnfe;
            }
            Object o = binding.getObject();
            //handle links by looking up the link
            if (o instanceof LinkRef) {
                //if link name starts with ./ it is relative to current context
                String linkName = ((LinkRef) o).getLinkName();
                if (linkName.startsWith("./"))
                    return lookup(linkName.substring(2));
                else {
                    //link name is absolute
                    InitialContext ictx = new InitialContext();
                    return ictx.lookup(linkName);
                }
            } else if (o instanceof Reference) {
                //deference the object
                try {
                    return NamingManager.getObjectInstance(o, cname, __root, _env);
                } catch (NamingException e) {
                    throw e;
                } catch (final Exception e) {
                    throw new NamingException(e.getMessage()) {

                        {
                            initCause(e);
                        }
                    };
                }
            } else
                return o;
        }
        //it is a multipart name, get the first subcontext
        String firstComponent = cname.get(0);
        Object ctx = null;
        if (firstComponent.equals(""))
            ctx = this;
        else {
            Binding binding = __root.getBinding(firstComponent);
            if (binding == null) {
                NameNotFoundException nnfe = new NameNotFoundException();
                nnfe.setRemainingName(cname);
                throw nnfe;
            }
            //as we have bound a reference to an object factory
            //for the component specific contexts
            //at "comp" we need to resolve the reference
            ctx = binding.getObject();
            if (ctx instanceof Reference) {
                //deference the object
                try {
                    ctx = NamingManager.getObjectInstance(ctx, getNameParser("").parse(firstComponent), __root, _env);
                } catch (NamingException e) {
                    throw e;
                } catch (Exception e) {
                    __log.warn("", e);
                    throw new NamingException(e.getMessage());
                }
            }
        }
        if (!(ctx instanceof Context))
            throw new NotContextException();
        return ((Context) ctx).lookup(cname.getSuffix(1));
    }
}
Also used : Binding(javax.naming.Binding) Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) NamingContext(org.eclipse.jetty.jndi.NamingContext) NotContextException(javax.naming.NotContextException) NameNotFoundException(javax.naming.NameNotFoundException) Reference(javax.naming.Reference) NamingContext(org.eclipse.jetty.jndi.NamingContext) InitialContext(javax.naming.InitialContext) NamingException(javax.naming.NamingException) NameNotFoundException(javax.naming.NameNotFoundException) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) NotContextException(javax.naming.NotContextException) OperationNotSupportedException(javax.naming.OperationNotSupportedException) CompoundName(javax.naming.CompoundName) Name(javax.naming.Name) NamingException(javax.naming.NamingException) LinkRef(javax.naming.LinkRef)

Aggregations

NamingContext (org.eclipse.jetty.jndi.NamingContext)7 Context (javax.naming.Context)5 InitialContext (javax.naming.InitialContext)5 NameNotFoundException (javax.naming.NameNotFoundException)5 NamingException (javax.naming.NamingException)5 Binding (javax.naming.Binding)4 Name (javax.naming.Name)4 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)4 Reference (javax.naming.Reference)4 CompoundName (javax.naming.CompoundName)3 NotContextException (javax.naming.NotContextException)3 OperationNotSupportedException (javax.naming.OperationNotSupportedException)3 URLClassLoader (java.net.URLClassLoader)2 LinkRef (javax.naming.LinkRef)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 NameClassPair (javax.naming.NameClassPair)1 NamingEnumeration (javax.naming.NamingEnumeration)1 StringRefAddr (javax.naming.StringRefAddr)1