Search in sources :

Example 11 with Reference

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

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

the class ContextFactory method newNamingContext.

/**
     * Create a new NamingContext.
     * @param obj the object to create
     * @param loader the classloader for the naming context
     * @param env the jndi env for the entry
     * @param name the name of the entry
     * @param parentCtx the parent context of the entry
     * @return the newly created naming context
     * @throws Exception if unable to create a new naming context
     */
public NamingContext newNamingContext(Object obj, ClassLoader loader, Hashtable env, Name name, Context parentCtx) throws Exception {
    Reference ref = (Reference) obj;
    StringRefAddr parserAddr = (StringRefAddr) ref.get("parser");
    String parserClassName = (parserAddr == null ? null : (String) parserAddr.getContent());
    NameParser parser = (NameParser) (parserClassName == null ? null : loader.loadClass(parserClassName).newInstance());
    return new NamingContext(env, name.get(0), (NamingContext) parentCtx, parser);
}
Also used : StringRefAddr(javax.naming.StringRefAddr) Reference(javax.naming.Reference) NameParser(javax.naming.NameParser)

Example 13 with Reference

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

Example 14 with Reference

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

the class NamingContext method dump.

/* ------------------------------------------------------------ */
public void dump(Appendable out, String indent) throws IOException {
    out.append(this.getClass().getSimpleName()).append("@").append(Long.toHexString(this.hashCode())).append("\n");
    int size = _bindings.size();
    int i = 0;
    for (Map.Entry<String, Binding> entry : ((Map<String, Binding>) _bindings).entrySet()) {
        boolean last = ++i == size;
        out.append(indent).append(" +- ").append(entry.getKey()).append(": ");
        Binding binding = entry.getValue();
        Object value = binding.getObject();
        if ("comp".equals(entry.getKey()) && value instanceof Reference && "org.eclipse.jetty.jndi.ContextFactory".equals(((Reference) value).getFactoryClassName())) {
            ContextFactory.dump(out, indent + (last ? "    " : " |  "));
        } else if (value instanceof Dumpable) {
            ((Dumpable) value).dump(out, indent + (last ? "    " : " |  "));
        } else {
            out.append(value.getClass().getSimpleName()).append("=");
            out.append(String.valueOf(value).replace('\n', '|').replace('\r', '|'));
            out.append("\n");
        }
    }
}
Also used : Binding(javax.naming.Binding) Reference(javax.naming.Reference) HashMap(java.util.HashMap) Map(java.util.Map) Dumpable(org.eclipse.jetty.util.component.Dumpable)

Example 15 with Reference

use of javax.naming.Reference in project tomcat by apache.

the class NamingContext method bind.

/**
     * Binds a name to an object. All intermediate contexts and the target
     * context (that named by all but terminal atomic component of the name)
     * must already exist.
     *
     * @param name the name to bind; may not be empty
     * @param obj the object to bind; possibly null
     * @param rebind if true, then perform a rebind (ie, overwrite)
     * @exception NameAlreadyBoundException if name is already bound
     * @exception javax.naming.directory.InvalidAttributesException if object
     * did not supply all mandatory attributes
     * @exception NamingException if a naming exception is encountered
     */
protected void bind(Name name, Object obj, boolean rebind) throws NamingException {
    if (!checkWritable()) {
        return;
    }
    while ((!name.isEmpty()) && (name.get(0).length() == 0)) name = name.getSuffix(1);
    if (name.isEmpty())
        throw new NamingException(sm.getString("namingContext.invalidName"));
    NamingEntry entry = bindings.get(name.get(0));
    if (name.size() > 1) {
        if (entry == null) {
            throw new NameNotFoundException(sm.getString("namingContext.nameNotBound", name, name.get(0)));
        }
        if (entry.type == NamingEntry.CONTEXT) {
            if (rebind) {
                ((Context) entry.value).rebind(name.getSuffix(1), obj);
            } else {
                ((Context) entry.value).bind(name.getSuffix(1), obj);
            }
        } else {
            throw new NamingException(sm.getString("namingContext.contextExpected"));
        }
    } else {
        if ((!rebind) && (entry != null)) {
            throw new NameAlreadyBoundException(sm.getString("namingContext.alreadyBound", name.get(0)));
        } else {
            // Getting the type of the object and wrapping it within a new
            // NamingEntry
            Object toBind = NamingManager.getStateToBind(obj, name, this, env);
            if (toBind instanceof Context) {
                entry = new NamingEntry(name.get(0), toBind, NamingEntry.CONTEXT);
            } else if (toBind instanceof LinkRef) {
                entry = new NamingEntry(name.get(0), toBind, NamingEntry.LINK_REF);
            } else if (toBind instanceof Reference) {
                entry = new NamingEntry(name.get(0), toBind, NamingEntry.REFERENCE);
            } else if (toBind instanceof Referenceable) {
                toBind = ((Referenceable) toBind).getReference();
                entry = new NamingEntry(name.get(0), toBind, NamingEntry.REFERENCE);
            } else {
                entry = new NamingEntry(name.get(0), toBind, NamingEntry.ENTRY);
            }
            bindings.put(name.get(0), entry);
        }
    }
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) Referenceable(javax.naming.Referenceable) NameNotFoundException(javax.naming.NameNotFoundException) Reference(javax.naming.Reference) NamingException(javax.naming.NamingException) LinkRef(javax.naming.LinkRef)

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