Search in sources :

Example 1 with Binding

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

the class localContextRoot method unbind.

/**
     *
     *
     * @see javax.naming.Context#unbind(javax.naming.Name)
     */
public void unbind(Name name) throws NamingException {
    synchronized (__root) {
        if (name.size() == 0)
            return;
        if (__root.isLocked())
            throw new NamingException("This context is immutable");
        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 no subcontexts, just unbind it
        if (cname.size() == 1) {
            __root.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 = __root.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), __root, _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) 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)

Example 2 with Binding

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

the class localContextRoot method list.

/**
     *
     *
     * @see javax.naming.Context#list(javax.naming.Name)
     */
public NamingEnumeration list(Name name) throws NamingException {
    synchronized (__root) {
        //return __root.list(getSuffix(name));
        Name cname = __root.toCanonicalName(name);
        if (cname == null) {
            List<Binding> empty = Collections.emptyList();
            return new NameEnumeration(empty.iterator());
        }
        if (cname.size() == 0) {
            return new NameEnumeration(__root.getBindings().values().iterator());
        }
        //multipart name
        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
                if (__log.isDebugEnabled())
                    __log.debug("Dereferencing Reference for " + name.get(0));
                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).list(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) NameEnumeration(org.eclipse.jetty.jndi.NameEnumeration) 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)

Example 3 with Binding

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

the class localContextRoot method bind.

/**
     *
     *
     * @see javax.naming.Context#bind(javax.naming.Name, java.lang.Object)
     */
public void bind(Name name, Object obj) throws NamingException {
    synchronized (__root) {
        if (__root.isLocked())
            throw new NamingException("This context is immutable");
        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 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
            __root.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 = __root.getBinding(firstComponent);
                if (binding == null)
                    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) NamingContext(org.eclipse.jetty.jndi.NamingContext) 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) NotContextException(javax.naming.NotContextException) OperationNotSupportedException(javax.naming.OperationNotSupportedException) CompoundName(javax.naming.CompoundName) Name(javax.naming.Name)

Example 4 with Binding

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

the class NamingContext method lookup.

/*------------------------------------------------*/
/**
     * Lookup a binding by name
     *
     * @param name name of bound object
     * @exception NamingException if an error occurs
     */
public Object lookup(Name name) throws NamingException {
    if (__log.isDebugEnabled())
        __log.debug("Looking up name=\"" + name + "\"");
    Name cname = toCanonicalName(name);
    if ((cname == null) || (cname.size() == 0)) {
        __log.debug("Null or empty name, returning copy of this context");
        NamingContext ctx = new NamingContext(_env, _name, _parent, _parser);
        ctx._bindings = _bindings;
        return ctx;
    }
    if (cname.size() == 1) {
        Binding binding = 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 this.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, this, _env);
            } catch (NamingException e) {
                throw e;
            } catch (Exception e) {
                __log.warn("", e);
                throw new NamingException(e.getMessage());
            }
        } else
            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 = 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), this, _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) NotContextException(javax.naming.NotContextException) NameNotFoundException(javax.naming.NameNotFoundException) Reference(javax.naming.Reference) InitialContext(javax.naming.InitialContext) 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) NamingException(javax.naming.NamingException) LinkRef(javax.naming.LinkRef)

Example 5 with Binding

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

the class NamingContext method createSubcontext.

/*------------------------------------------------*/
/**
     * Create a context as a child of this one
     *
     * @param name a <code>Name</code> value
     * @return a <code>Context</code> value
     * @exception NamingException if an error occurs
     */
public Context createSubcontext(Name name) throws NamingException {
    if (isLocked()) {
        NamingException ne = new NamingException("This context is immutable");
        ne.setRemainingName(name);
        throw ne;
    }
    Name cname = 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 = getBinding(cname);
        if (binding != null)
            throw new NameAlreadyBoundException(cname.toString());
        Context ctx = new NamingContext((Hashtable) _env.clone(), cname.get(0), this, _parser);
        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 = 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), this, _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) NotContextException(javax.naming.NotContextException) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) 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

Binding (javax.naming.Binding)70 NamingException (javax.naming.NamingException)37 Context (javax.naming.Context)31 InitialContext (javax.naming.InitialContext)29 NameNotFoundException (javax.naming.NameNotFoundException)22 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)20 Name (javax.naming.Name)19 NotContextException (javax.naming.NotContextException)18 OperationNotSupportedException (javax.naming.OperationNotSupportedException)18 Reference (javax.naming.Reference)17 CompoundName (javax.naming.CompoundName)16 IOException (java.io.IOException)12 ArrayList (java.util.ArrayList)10 NamingContext (org.eclipse.jetty.jndi.NamingContext)9 CompositeName (javax.naming.CompositeName)8 Test (org.junit.Test)8 HashMap (java.util.HashMap)7 NamingEnumeration (javax.naming.NamingEnumeration)5 ServletContext (javax.servlet.ServletContext)5 MalformedURLException (java.net.MalformedURLException)4