Components that implement atg.naming.NameContext are recognized by Nucleus as containers of other components. This means that Nucleus can traverse through these components when it is resolving names. It also means that the Component Browser allows the administrator to walk through the children of that component, in the same way that a web browser allows a user to walk through the files in a directory.

The NameContext interface resembles java.util.Dictionary in that it has methods for getting, putting, removing, and listing elements. One possible implementation of NameContext is to use a Hashtable:

Hashtable elements = new Hashtable ();

public Object getElement (String name) {
  return elements.get (name);
}
public void putElement (String name,
                        Object element) {
  removeElement (name);
  elements.put (name, element);
}
public void removeElement (String name) {
  elements.remove (name);
}
public boolean isElementBound (String name) {
  return getElement (name) != null;
}
public Enumeration listElementNames () {
  return elements.keys ();
}
public Enumeration listElements () {
  return elements.elements ();
}

Some implementations, however, might not wish to implement all of this functionality. For example, a NameContext can be hard-coded to have three elements: name, price, and availability:

public Object getElement (String name) {
  if (name.equals ("name")) return "PowerCenter Pro 180";
  else if (name.equals ("price")) return new Integer (1995);
  else if (name.equals ("availability")) return new Boolean (true);
  else return null;
}
public void putElement (String name,
                        Object element) {
}
public void removeElement (String name) {
}
public boolean isElementBound (String name) {
  return getElement (name) != null;
}
public Enumeration listElementNames () {
  return new Enumeration () {
    int i = 0;
    public boolean hasMoreElements () {
      return i < 3;
    }
    public Object nextElement () {
      if (!hasMoreElements ()) throw new NoSuchElementException ();
      switch (i++) {
        case 0: return "name"; break;
        case 1: return "price"; break;
        case 2: return "availability"; break;
      }
    }
  };
}
public Enumeration listElements () {
  return new Enumeration () {
    Enumeration e = listElementNames ();
    public boolean hasMoreElements () {
      return e.hasMoreElements ();
    }
    public Object nextElement () {
      return getElement (e.nextElement ());
    }
  };
}

Notice how the putElement and removeElement methods do nothing. Also notice the use of inner classes to implement the methods that return Enumerations. This is a common technique for satisfying these types of interfaces.

Remember that NameContext extends NameContextElement, so your implementations of NameContext must also implement all the methods for NameContextElement.


Copyright © 1997, 2015 Oracle and/or its affiliates. All rights reserved. Legal Notices