Search in sources :

Example 1 with Referenceable

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

Example 2 with Referenceable

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

use of javax.naming.Referenceable in project aries by apache.

the class ObjectFactoryHelper method getObjectInstance.

public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
    // Step 1 ensure we have a reference rather than a referenceable
    if (obj instanceof Referenceable) {
        obj = ((Referenceable) obj).getReference();
    }
    if (logger.isLoggable(Level.FINE))
        logger.log(Level.FINE, "obj = " + obj);
    Object result = obj;
    // Step 2 - if we have a reference process it as a reference
    if (obj instanceof Reference) {
        Reference ref = (Reference) obj;
        String className = ref.getFactoryClassName();
        if (className != null) {
            // Step 3 - use the class name in the reference to get the factory class name
            result = getObjectInstanceUsingClassName(obj, className, obj, name, nameCtx, environment);
        } else {
            // Step 4 - look, assuming url string ref addrs, for a url context object factory.
            result = getObjectInstanceUsingRefAddress(ref.getAll(), obj, name, nameCtx, environment);
        }
    }
    if (logger.isLoggable(Level.FINE))
        logger.log(Level.FINE, "Step 4: result = " + result);
    // Step 5 - if we still don't have a resolved object goto the object factory builds in the SR.
    if (result == null || result == obj) {
        result = getObjectInstanceUsingObjectFactoryBuilders(obj, name, nameCtx, environment);
    }
    if (logger.isLoggable(Level.FINE))
        logger.log(Level.FINE, "Step 5: result = " + result);
    // Step 6 - Attempt to use all the registered ObjectFactories in the SR.
    if (result == null || result == obj) {
        if ((obj instanceof Reference && ((Reference) obj).getFactoryClassName() == null) || !(obj instanceof Reference)) {
            result = getObjectInstanceUsingObjectFactories(obj, name, nameCtx, environment);
        }
    }
    if (logger.isLoggable(Level.FINE))
        logger.log(Level.FINE, "Step 6: result = " + result);
    // to https://www.osgi.org/bugzilla/show_bug.cgi?id=138 
    if (result == null || result == obj) {
        result = getObjectInstanceViaContextDotObjectFactories(obj, name, nameCtx, environment);
    }
    if (logger.isLoggable(Level.FINE))
        logger.log(Level.FINE, "Step 7: result = " + result);
    return (result == null) ? obj : result;
}
Also used : Referenceable(javax.naming.Referenceable) Reference(javax.naming.Reference) ServiceReference(org.osgi.framework.ServiceReference)

Example 4 with Referenceable

use of javax.naming.Referenceable in project wildfly by wildfly.

the class NamingContext method bind.

/** {@inheritDoc} */
public void bind(final Name name, final Object object) throws NamingException {
    check(name, JndiPermission.ACTION_BIND);
    if (namingStore instanceof WritableNamingStore) {
        final Name absoluteName = getAbsoluteName(name);
        final Object value;
        if (object instanceof Referenceable) {
            value = ((Referenceable) object).getReference();
        } else {
            value = object;
        }
        if (System.getSecurityManager() == null) {
            getWritableNamingStore().bind(absoluteName, value);
        } else {
            // The permissions check has already happened for the binding further permissions should be allowed
            final NamingException e = AccessController.doPrivileged(new PrivilegedAction<NamingException>() {

                @Override
                public NamingException run() {
                    try {
                        getWritableNamingStore().bind(absoluteName, value);
                    } catch (NamingException e) {
                        return e;
                    }
                    return null;
                }
            });
            // Check that a NamingException wasn't thrown during the bind
            if (e != null) {
                throw e;
            }
        }
    } else {
        throw NamingLogger.ROOT_LOGGER.readOnlyNamingContext();
    }
}
Also used : Referenceable(javax.naming.Referenceable) NamingException(javax.naming.NamingException) CompositeName(javax.naming.CompositeName) Name(javax.naming.Name)

Example 5 with Referenceable

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

the class localContextRoot method rebind.

/**
     *
     *
     * @see javax.naming.Context#rebind(javax.naming.Name, java.lang.Object)
     */
public void rebind(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) {
            //check if it is a Referenceable
            Object objToBind = NamingManager.getStateToBind(obj, name, __root, _env);
            if (objToBind instanceof Referenceable) {
                objToBind = ((Referenceable) objToBind).getReference();
            }
            __root.removeBinding(cname);
            __root.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 = __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).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) 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)

Aggregations

Referenceable (javax.naming.Referenceable)11 Reference (javax.naming.Reference)9 Name (javax.naming.Name)6 NamingException (javax.naming.NamingException)6 Context (javax.naming.Context)5 InitialContext (javax.naming.InitialContext)5 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)5 NameNotFoundException (javax.naming.NameNotFoundException)5 Binding (javax.naming.Binding)4 CompoundName (javax.naming.CompoundName)4 NotContextException (javax.naming.NotContextException)4 OperationNotSupportedException (javax.naming.OperationNotSupportedException)4 IOException (java.io.IOException)2 CompositeName (javax.naming.CompositeName)2 NamingContext (org.eclipse.jetty.jndi.NamingContext)2 ServiceReference (org.osgi.framework.ServiceReference)2 LinkRef (javax.naming.LinkRef)1 RefAddr (javax.naming.RefAddr)1 StringRefAddr (javax.naming.StringRefAddr)1