Search in sources :

Example 91 with NameNotFoundException

use of javax.naming.NameNotFoundException 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)

Example 92 with NameNotFoundException

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

the class NamingContext method rebind.

/*------------------------------------------------*/
/**
     * Overwrite or create a binding
     *
     * @param name a <code>Name</code> value
     * @param obj an <code>Object</code> value
     * @exception NamingException if an error occurs
     */
public void rebind(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) {
        //check if it is a Referenceable
        Object objToBind = NamingManager.getStateToBind(obj, name, this, _env);
        if (objToBind instanceof Referenceable) {
            objToBind = ((Referenceable) objToBind).getReference();
        }
        removeBinding(cname);
        addBinding(cname, objToBind);
    } 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).rebind(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)

Example 93 with NameNotFoundException

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

the class NamingUtil method bind.

/* ------------------------------------------------------------ */
/**
     * Bind an object to a context ensuring all sub-contexts
     * are created if necessary
     *
     * @param ctx the context into which to bind
     * @param nameStr the name relative to context to bind
     * @param obj the object to be bound
     * @return the bound context
     * @exception NamingException if an error occurs
     */
public static Context bind(Context ctx, String nameStr, Object obj) throws NamingException {
    Name name = ctx.getNameParser("").parse(nameStr);
    //no name, nothing to do
    if (name.size() == 0)
        return null;
    Context subCtx = ctx;
    //last component of the name will be the name to bind
    for (int i = 0; i < name.size() - 1; i++) {
        try {
            subCtx = (Context) subCtx.lookup(name.get(i));
            if (__log.isDebugEnabled())
                __log.debug("Subcontext " + name.get(i) + " already exists");
        } catch (NameNotFoundException e) {
            subCtx = subCtx.createSubcontext(name.get(i));
            if (__log.isDebugEnabled())
                __log.debug("Subcontext " + name.get(i) + " created");
        }
    }
    subCtx.rebind(name.get(name.size() - 1), obj);
    if (__log.isDebugEnabled())
        __log.debug("Bound object to " + name.get(name.size() - 1));
    return subCtx;
}
Also used : Context(javax.naming.Context) NameNotFoundException(javax.naming.NameNotFoundException) Name(javax.naming.Name)

Example 94 with NameNotFoundException

use of javax.naming.NameNotFoundException 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)

Example 95 with NameNotFoundException

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

the class EnvConfiguration method destroy.

/**
     * Remove all jndi setup
     * @throws Exception if unable to destroy
     */
@Override
public void destroy(WebAppContext context) throws Exception {
    try {
        //unbind any NamingEntries that were configured in this webapp's name space           
        NamingContext scopeContext = (NamingContext) NamingEntryUtil.getContextForScope(context);
        scopeContext.getParent().destroySubcontext(scopeContext.getName());
    } catch (NameNotFoundException e) {
        LOG.ignore(e);
        LOG.debug("No jndi entries scoped to webapp {}", context);
    } catch (NamingException e) {
        LOG.debug("Error unbinding jndi entries scoped to webapp " + context, e);
    }
}
Also used : NameNotFoundException(javax.naming.NameNotFoundException) NamingException(javax.naming.NamingException) NamingContext(org.eclipse.jetty.jndi.NamingContext)

Aggregations

NameNotFoundException (javax.naming.NameNotFoundException)168 NamingException (javax.naming.NamingException)81 InitialContext (javax.naming.InitialContext)75 Context (javax.naming.Context)71 Reference (javax.naming.Reference)40 Test (org.junit.Test)39 NotContextException (javax.naming.NotContextException)35 Name (javax.naming.Name)34 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)33 OperationNotSupportedException (javax.naming.OperationNotSupportedException)29 CompositeName (javax.naming.CompositeName)27 Binding (javax.naming.Binding)22 CompoundName (javax.naming.CompoundName)16 LinkRef (javax.naming.LinkRef)16 IOException (java.io.IOException)12 NamingContext (org.eclipse.jetty.jndi.NamingContext)12 ArrayList (java.util.ArrayList)10 InvalidNameException (javax.naming.InvalidNameException)10 Attributes (javax.naming.directory.Attributes)10 HashMap (java.util.HashMap)7