Search in sources :

Example 56 with Name

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

the class localContextRoot method listBindings.

/**
     *
     *
     * @see javax.naming.Context#listBindings(javax.naming.Name)
     */
public NamingEnumeration listBindings(Name name) throws NamingException {
    synchronized (__root) {
        //return __root.listBindings(getSuffix(name));
        Name cname = __root.toCanonicalName(name);
        if (cname == null) {
            List<Binding> empty = Collections.emptyList();
            return new BindingEnumeration(empty.iterator());
        }
        if (cname.size() == 0) {
            return new BindingEnumeration(__root.getBindings().values().iterator());
        }
        //multipart name
        String firstComponent = cname.get(0);
        Object ctx = null;
        //at this level in the tree
        if (firstComponent.equals(""))
            ctx = this;
        else {
            //it is a non-empty name component
            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).listBindings(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) 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) BindingEnumeration(org.eclipse.jetty.jndi.BindingEnumeration)

Example 57 with Name

use of javax.naming.Name 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 58 with Name

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

the class TestLocalJNDI method testLocal.

@Test
public void testLocal() throws Exception {
    InitialContext ic = new InitialContext();
    NameParser parser = ic.getNameParser("");
    ic.bind("foo", "xxx");
    Object o = ic.lookup("foo");
    assertNotNull(o);
    assertEquals("xxx", (String) o);
    ic.unbind("foo");
    try {
        ic.lookup("foo");
        fail("Foo exists");
    } catch (NameNotFoundException e) {
    //expected
    }
    Name name = parser.parse("a");
    name.addAll(parser.parse("b/c/d"));
    NamingUtil.bind(ic, name.toString(), "333");
    assertNotNull(ic.lookup("a"));
    assertNotNull(ic.lookup("a/b"));
    assertNotNull(ic.lookup("a/b/c"));
    Context c = (Context) ic.lookup("a/b/c");
    o = c.lookup("d");
    assertNotNull(o);
    assertEquals("333", (String) o);
    assertEquals("333", ic.lookup(name));
    ic.destroySubcontext("a");
    try {
        ic.lookup("a");
        fail("context a was not destroyed");
    } catch (NameNotFoundException e) {
    //expected
    }
    name = parser.parse("");
    name.add("x");
    Name suffix = parser.parse("y/z");
    name.addAll(suffix);
    NamingUtil.bind(ic, name.toString(), "555");
    assertEquals("555", ic.lookup(name));
    ic.destroySubcontext("x");
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) NameNotFoundException(javax.naming.NameNotFoundException) InitialContext(javax.naming.InitialContext) NameParser(javax.naming.NameParser) Name(javax.naming.Name) Test(org.junit.Test)

Example 59 with Name

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

the class NamingContext method unbind.

/*------------------------------------------------*/
/**
     * Not supported.
     *
     * @param name a <code>String</code> value
     * @exception NamingException if an error occurs
     */
public void unbind(Name name) throws NamingException {
    if (name.size() == 0)
        return;
    if (isLocked())
        throw new NamingException("This context is immutable");
    Name cname = toCanonicalName(name);
    if (cname == null)
        throw new NamingException("Name is null");
    if (cname.size() == 0)
        throw new NamingException("Name is empty");
    //if no subcontexts, just unbind it
    if (cname.size() == 1) {
        removeBinding(cname);
    } else {
        //walk down the subcontext hierarchy
        if (__log.isDebugEnabled())
            __log.debug("Checking for existing binding for name=" + cname + " for first element of name=" + cname.get(0));
        String firstComponent = cname.get(0);
        Object ctx = null;
        if (firstComponent.equals(""))
            ctx = this;
        else {
            Binding binding = getBinding(name.get(0));
            if (binding == null)
                throw new NameNotFoundException(name.get(0) + " is not bound");
            ctx = binding.getObject();
            if (ctx instanceof Reference) {
                //deference the object
                try {
                    ctx = NamingManager.getObjectInstance(ctx, getNameParser("").parse(firstComponent), this, _env);
                } catch (NamingException e) {
                    throw e;
                } catch (Exception e) {
                    __log.warn("", e);
                    throw new NamingException(e.getMessage());
                }
            }
        }
        if (ctx instanceof Context) {
            ((Context) ctx).unbind(cname.getSuffix(1));
        } else
            throw new NotContextException("Object bound at " + firstComponent + " is not a Context");
    }
}
Also used : Binding(javax.naming.Binding) Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) NotContextException(javax.naming.NotContextException) NameNotFoundException(javax.naming.NameNotFoundException) Reference(javax.naming.Reference) NamingException(javax.naming.NamingException) NamingException(javax.naming.NamingException) NameNotFoundException(javax.naming.NameNotFoundException) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) IOException(java.io.IOException) NotContextException(javax.naming.NotContextException) OperationNotSupportedException(javax.naming.OperationNotSupportedException) CompoundName(javax.naming.CompoundName) Name(javax.naming.Name)

Example 60 with Name

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

the class NamingContext method bind.

/*------------------------------------------------*/
/**
     * Bind a name to an object
     *
     * @param name Name of the object
     * @param obj object to bind
     * @exception NamingException if an error occurs
     */
public void bind(Name name, Object obj) throws NamingException {
    if (isLocked())
        throw new NamingException("This context is immutable");
    Name cname = toCanonicalName(name);
    if (cname == null)
        throw new NamingException("Name is null");
    if (cname.size() == 0)
        throw new NamingException("Name is empty");
    //if no subcontexts, just bind it
    if (cname.size() == 1) {
        //get the object to be bound
        Object objToBind = NamingManager.getStateToBind(obj, name, this, _env);
        // Check for Referenceable
        if (objToBind instanceof Referenceable) {
            objToBind = ((Referenceable) objToBind).getReference();
        }
        //anything else we should be able to bind directly
        addBinding(cname, objToBind);
    } else {
        if (__log.isDebugEnabled())
            __log.debug("Checking for existing binding for name=" + cname + " for first element of name=" + cname.get(0));
        //walk down the subcontext hierarchy
        //need to ignore trailing empty "" name components
        String firstComponent = cname.get(0);
        Object ctx = null;
        if (firstComponent.equals(""))
            ctx = this;
        else {
            Binding binding = getBinding(firstComponent);
            if (binding == null) {
                if (_supportDeepBinding) {
                    Name subname = _parser.parse(firstComponent);
                    Context subctx = new NamingContext((Hashtable) _env.clone(), firstComponent, this, _parser);
                    addBinding(subname, subctx);
                    binding = getBinding(subname);
                } else {
                    throw new NameNotFoundException(firstComponent + " is not bound");
                }
            }
            ctx = binding.getObject();
            if (ctx instanceof Reference) {
                //deference the object
                try {
                    ctx = NamingManager.getObjectInstance(ctx, getNameParser("").parse(firstComponent), this, _env);
                } catch (NamingException e) {
                    throw e;
                } catch (Exception e) {
                    __log.warn("", e);
                    throw new NamingException(e.getMessage());
                }
            }
        }
        if (ctx instanceof Context) {
            ((Context) ctx).bind(cname.getSuffix(1), obj);
        } else
            throw new NotContextException("Object bound at " + firstComponent + " is not a Context");
    }
}
Also used : Binding(javax.naming.Binding) Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) NotContextException(javax.naming.NotContextException) Referenceable(javax.naming.Referenceable) NameNotFoundException(javax.naming.NameNotFoundException) Reference(javax.naming.Reference) NamingException(javax.naming.NamingException) NamingException(javax.naming.NamingException) NameNotFoundException(javax.naming.NameNotFoundException) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) IOException(java.io.IOException) NotContextException(javax.naming.NotContextException) OperationNotSupportedException(javax.naming.OperationNotSupportedException) CompoundName(javax.naming.CompoundName) Name(javax.naming.Name)

Aggregations

Name (javax.naming.Name)107 CompositeName (javax.naming.CompositeName)48 NamingException (javax.naming.NamingException)38 Context (javax.naming.Context)37 InitialContext (javax.naming.InitialContext)36 NameNotFoundException (javax.naming.NameNotFoundException)34 Test (org.junit.Test)32 Reference (javax.naming.Reference)24 NotContextException (javax.naming.NotContextException)22 Binding (javax.naming.Binding)19 CompoundName (javax.naming.CompoundName)19 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)19 NameParser (javax.naming.NameParser)17 OperationNotSupportedException (javax.naming.OperationNotSupportedException)16 InvalidNameException (javax.naming.InvalidNameException)10 NamingContext (org.eclipse.jetty.jndi.NamingContext)10 IOException (java.io.IOException)8 LinkRef (javax.naming.LinkRef)7 ServiceName (org.jboss.msc.service.ServiceName)7 Referenceable (javax.naming.Referenceable)6