Search in sources :

Example 6 with Reference

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

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

Example 8 with Reference

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

the class NamingContext method list.

/*------------------------------------------------*/
/**
     * List all names bound at Context named by Name
     *
     * @param name a <code>Name</code> value
     * @return a <code>NamingEnumeration</code> value
     * @exception NamingException if an error occurs
     */
public NamingEnumeration list(Name name) throws NamingException {
    if (__log.isDebugEnabled())
        __log.debug("list() on Context=" + getName() + " for name=" + name);
    Name cname = toCanonicalName(name);
    if (cname == null) {
        return new NameEnumeration(__empty.iterator());
    }
    if (cname.size() == 0) {
        return new NameEnumeration(_bindings.values().iterator());
    }
    //multipart name
    String firstComponent = cname.get(0);
    Object ctx = null;
    if (firstComponent.equals(""))
        ctx = this;
    else {
        Binding binding = 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), 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).list(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) 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 9 with Reference

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

the class NamingContext method lookupLink.

/*------------------------------------------------*/
/**
     * Lookup link bound to name
     *
     * @param name name of link binding
     * @return LinkRef or plain object bound at name
     * @exception NamingException if an error occurs
     */
public Object lookupLink(Name name) throws NamingException {
    Name cname = toCanonicalName(name);
    if (cname == null) {
        NamingContext ctx = new NamingContext(_env, _name, _parent, _parser);
        ctx._bindings = _bindings;
        return ctx;
    }
    if (cname.size() == 0)
        throw new NamingException("Name is empty");
    if (cname.size() == 1) {
        Binding binding = 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), this, _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 = 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), 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) 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 10 with Reference

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

the class MailSessionReference method getObjectInstance.

/**
     * Create a javax.mail.Session instance based on the information passed in the Reference
     * @see javax.naming.spi.ObjectFactory#getObjectInstance(java.lang.Object, javax.naming.Name, javax.naming.Context, java.util.Hashtable)
     * @param ref the Reference
     * @param arg1 not used
     * @param arg2 not used
     * @param arg3 not used
     * @return the object found
     * @throws Exception if unable to get object instance
     */
public Object getObjectInstance(Object ref, Name arg1, Context arg2, Hashtable arg3) throws Exception {
    if (ref == null)
        return null;
    Reference reference = (Reference) ref;
    Properties props = new Properties();
    String user = null;
    String password = null;
    Enumeration refs = reference.getAll();
    while (refs.hasMoreElements()) {
        RefAddr refAddr = (RefAddr) refs.nextElement();
        String name = refAddr.getType();
        String value = (String) refAddr.getContent();
        if (name.equalsIgnoreCase("user"))
            user = value;
        else if (name.equalsIgnoreCase("pwd"))
            password = value;
        else
            props.put(name, value);
    }
    if (password == null)
        return Session.getInstance(props);
    else
        return Session.getInstance(props, new PasswordAuthenticator(user, password));
}
Also used : RefAddr(javax.naming.RefAddr) StringRefAddr(javax.naming.StringRefAddr) Enumeration(java.util.Enumeration) Reference(javax.naming.Reference) Properties(java.util.Properties)

Aggregations

Reference (javax.naming.Reference)226 NamingException (javax.naming.NamingException)79 RefAddr (javax.naming.RefAddr)59 StringRefAddr (javax.naming.StringRefAddr)54 Context (javax.naming.Context)41 NameNotFoundException (javax.naming.NameNotFoundException)40 InitialContext (javax.naming.InitialContext)37 Test (org.junit.Test)33 Name (javax.naming.Name)30 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)28 Properties (java.util.Properties)26 OperationNotSupportedException (javax.naming.OperationNotSupportedException)25 NotContextException (javax.naming.NotContextException)24 ObjectFactory (javax.naming.spi.ObjectFactory)23 CompositeName (javax.naming.CompositeName)21 Referenceable (javax.naming.Referenceable)18 Binding (javax.naming.Binding)17 CompoundName (javax.naming.CompoundName)16 LinkRef (javax.naming.LinkRef)15 Hashtable (java.util.Hashtable)12