Search in sources :

Example 46 with Context

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

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

the class NamingUtil method flattenBindings.

/**
     * Do a deep listing of the bindings for a context.
     * @param ctx the context containing the name for which to list the bindings
     * @param name the name in the context to list
     * @return map: key is fully qualified name, value is the bound object
     * @throws NamingException if unable to flatten bindings
     */
public static Map flattenBindings(Context ctx, String name) throws NamingException {
    HashMap map = new HashMap();
    //the context representation of name arg
    Context c = (Context) ctx.lookup(name);
    NameParser parser = c.getNameParser("");
    NamingEnumeration enm = ctx.listBindings(name);
    while (enm.hasMore()) {
        Binding b = (Binding) enm.next();
        if (b.getObject() instanceof Context) {
            map.putAll(flattenBindings(c, b.getName()));
        } else {
            Name compoundName = parser.parse(c.getNameInNamespace());
            compoundName.add(b.getName());
            map.put(compoundName.toString(), b.getObject());
        }
    }
    return map;
}
Also used : Context(javax.naming.Context) Binding(javax.naming.Binding) HashMap(java.util.HashMap) NamingEnumeration(javax.naming.NamingEnumeration) NameParser(javax.naming.NameParser) Name(javax.naming.Name)

Example 48 with Context

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

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

the class ContextFactory method associateContext.

/**
     * Associate the given Context with the current thread.
     * disassociate method should be called to reset the context.
     * @param ctx the context to associate to the current thread.
     * @return the previous context associated on the thread (can be null)
     */
public static Context associateContext(final Context ctx) {
    Context previous = (Context) __threadContext.get();
    __threadContext.set(ctx);
    return previous;
}
Also used : Context(javax.naming.Context)

Example 50 with Context

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

the class NamingContext method listBindings.

/*------------------------------------------------*/
/**
     * List all Bindings present 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 listBindings(Name name) throws NamingException {
    Name cname = toCanonicalName(name);
    if (cname == null) {
        return new BindingEnumeration(__empty.iterator());
    }
    if (cname.size() == 0) {
        return new BindingEnumeration(_bindings.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 = 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).listBindings(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)

Aggregations

Context (javax.naming.Context)507 InitialContext (javax.naming.InitialContext)250 Test (org.junit.Test)173 NamingException (javax.naming.NamingException)156 DataSource (javax.sql.DataSource)72 Properties (java.util.Properties)67 Connection (java.sql.Connection)62 NameNotFoundException (javax.naming.NameNotFoundException)52 UserTransaction (javax.transaction.UserTransaction)48 SQLException (java.sql.SQLException)46 IOException (java.io.IOException)44 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)44 Statement (java.sql.Statement)42 Name (javax.naming.Name)38 Hashtable (java.util.Hashtable)35 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)27 BeanContext (org.apache.openejb.BeanContext)27 Binding (javax.naming.Binding)25 Reference (javax.naming.Reference)25 NotContextException (javax.naming.NotContextException)23