1 package org.sapia.archie.jndi; 2 3 import java.util.Iterator ; 4 5 import javax.naming.Binding ; 6 import javax.naming.Context ; 7 import javax.naming.NameClassPair ; 8 import javax.naming.NamingEnumeration ; 9 import javax.naming.NamingException ; 10 11 import org.sapia.archie.Entry; 12 import org.sapia.archie.Node; 13 14 22 public class JndiNamingEnum implements NamingEnumeration { 23 24 public static final int LIST_OBJECTS = 0; 25 public static final int LIST_BINDINGS = 1; 26 public static final int LIST_NAMECLASS_PAIRS = 2; 27 28 protected Iterator _entries, _childNodes; 29 protected int _listType; 30 31 public JndiNamingEnum(Iterator entries, Iterator childNodes, int listType){ 32 _entries = entries; 33 _childNodes = childNodes; 34 _listType = listType; 35 } 36 39 public void close() throws NamingException { 40 } 41 42 45 public boolean hasMore() throws NamingException { 46 return _entries.hasNext() || _childNodes.hasNext(); 47 } 48 49 52 public Object next() throws NamingException { 53 if(_listType == LIST_BINDINGS){ 54 if(_entries.hasNext()){ 55 return toBinding((Entry)_entries.next()); 56 } 57 else{ 58 return toBinding((Node)_childNodes.next()); 59 } 60 61 } 62 else if(_listType == LIST_NAMECLASS_PAIRS){ 63 if(_entries.hasNext()){ 64 Entry entry = (Entry)_entries.next(); 65 return new NameClassPair (entry.getName (), entry.getValue().getClass().getName(), true); 66 } 67 else{ 68 Node node = (Node)_childNodes.next(); 69 Context ctx = newJndiContext(node); 70 return new NameClassPair (node.getName().asString(), ctx.getClass().getName(), true); 71 } 72 } 73 else { 74 if(_entries.hasNext()){ 75 return ((Entry)_entries.next()).getValue(); 76 } 77 else{ 78 return newJndiContext((Node)_childNodes.next()); 79 } 80 } 81 } 82 83 86 public boolean hasMoreElements() { 87 return _entries.hasNext() || _childNodes.hasNext(); 88 } 89 90 93 public Object nextElement() { 94 try{ 95 return next(); 96 }catch(NamingException e){ 97 throw new IllegalStateException (e.getClass() + " caught; message: " + e.getMessage()); 98 } 99 } 100 101 protected Binding toBinding(Entry entry){ 102 return new Binding (entry.getName(), entry.getValue().getClass().getName(), entry.getValue(), true); 103 } 104 105 protected Binding toBinding(Node node){ 106 Context ctx = newJndiContext(node); 107 return new Binding (node.getName().asString(), ctx.getClass().getName(), ctx); 108 } 109 110 protected Context newJndiContext(Node node){ 111 return new JndiContext(node); 112 } 113 } 114 | Popular Tags |