Previous | Next | Trail Map | Building a Service Provider | The Essential Components

Update Methods

A context is updated by using the following operations: Whether these methods are supported and how they are supported depend on the underlying naming/directory service. An actual context implementation would update the underlying naming/directory service. A context implementation for a read-only service, for example, would not support any of these methods. One for a flat namespace, such as the flat namespace example would not support createSubcontext() and destroySubcontext().

Implementation Tip: bind() and rebind() can accept null as the object parameter if the context implementation supports only adding a name to the namespace without an associated object.

In the flat namespace example, bind(), rebind(), and unbind() are implemented much like lookup(), except that these methods update the internal data structure instead of read from it. Here is the definition of rebind().

public void rebind(Name name, Object obj) throws NamingException {
    if (name.isEmpty()) {
        throw new InvalidNameException("Cannot bind empty name");
    }

    // Extract the components that belong to this namespace
    String nm = getMyComponents(name);

    // Add the object to the internal hash table
    bindings.put(nm, obj);
}

Rename

rename() allows you to rename across different contexts. An actual context implementation could have problems supporting this fully because the underlying naming/directory service might not support this feature. Some services might support only renaming within the same context. In that case, the error might be indicated by the service itself and would result in a service-specific exception; or, the context implementation might detect the problem and throw an OperationNotSupportedException(in the API reference documentation).

Here is the definition of rename() from the simple flat namespace example.

public void rename(Name oldname, Name newname) throws NamingException {
    if (oldname.isEmpty() || newname.isEmpty()) {
        throw new InvalidNameException("Cannot rename empty name");
    }

    // Extract the components that belong to this namespace
    String oldnm = getMyComponents(oldname);
    String newnm = getMyComponents(newname);

    // Check whether the new name exists
    if (bindings.get(newnm) != null) {
        throw new NameAlreadyBoundException(newname.toString() +
                                            " is already bound");
    }

    // Check whether the old name is bound
    Object oldBinding = bindings.remove(oldnm);
    if (oldBinding == null) {
        throw new NameNotFoundException(oldname.toString() + " not bound");
    }

    bindings.put(newnm, oldBinding);
}


Previous | Next | Trail Map | Building a Service Provider | The Essential Components