Example usage for javax.naming.spi NamingManager getStateToBind

List of usage examples for javax.naming.spi NamingManager getStateToBind

Introduction

In this page you can find the example usage for javax.naming.spi NamingManager getStateToBind.

Prototype

public static Object getStateToBind(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment)
        throws NamingException 

Source Link

Document

Retrieves the state of an object for binding.

Usage

From source file:org.apache.naming.NamingContext.java

/**
 * 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.//w  w w  .  ja  v  a 2 s .  co  m
 * 
 * @param name the name to bind; may not be empty
 * @param object the object to bind; possibly null
 * @param rebind if true, then perform a rebind (ie, overwrite)
 * @exception NameAlreadyBoundException if name is already bound
 * @exception 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 {

    checkWritable();

    while ((!name.isEmpty()) && (name.get(0).length() == 0))
        name = name.getSuffix(1);
    if (name.isEmpty())
        throw new NamingException(sm.getString("namingContext.invalidName"));

    NamingEntry entry = (NamingEntry) bindings.get(name.get(0));

    if (name.size() > 1) {
        if (entry == null) {
            throw new NameNotFoundException(sm.getString("namingContext.nameNotBound", 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 NamingException(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);
        }
    }

}