30 * attributes for used by the filter: 31 * 32 * ((DirContext)item.getObject()).getAttributes(""). 33 * If item.getObject() is not an DirContext, the item is skipped 34 * 35 * The items in the enumeration are obtained one at a time as 36 * items from the search enumeration are requested. 37 * 38 * @author Rosanna Lee 39 */ 40 41 package com.sun.jndi.toolkit.dir; 42 43 import javax.naming.*; 44 import javax.naming.directory.*; 45 import javax.naming.spi.DirectoryManager; 46 47 import java.util.NoSuchElementException; 48 import java.util.Hashtable; 49 50 final public class LazySearchEnumerationImpl implements NamingEnumeration { 51 private NamingEnumeration candidates; 52 private SearchResult nextMatch = null; 53 private SearchControls cons; 54 private AttrFilter filter; 55 private Context context; 56 private Hashtable env; 57 private boolean useFactory = true; 58 59 public LazySearchEnumerationImpl(NamingEnumeration candidates, 60 AttrFilter filter, SearchControls cons) throws NamingException { 61 this.candidates = candidates; 62 this.filter = filter; 63 64 if(cons == null) { 65 this.cons = new SearchControls(); 66 } else { 67 this.cons = cons; 68 } 69 } 70 71 public LazySearchEnumerationImpl(NamingEnumeration candidates, 72 AttrFilter filter, SearchControls cons, 73 Context ctx, Hashtable env, boolean useFactory) throws NamingException { 74 75 this.candidates = candidates; 76 this.filter = filter; 77 this.env = env; 78 this.context = ctx; 79 this.useFactory = useFactory; 80 81 if(cons == null) { 82 this.cons = new SearchControls(); 83 } else { 84 this.cons = cons; 85 } 86 } 87 88 89 public LazySearchEnumerationImpl(NamingEnumeration candidates, 90 AttrFilter filter, SearchControls cons, 91 Context ctx, Hashtable env) throws NamingException { 92 this(candidates, filter, cons, ctx, env, true); 93 } 94 95 public boolean hasMore() throws NamingException { 96 // find and do not remove from list 97 return findNextMatch(false) != null; 98 } 99 100 public boolean hasMoreElements() { 101 try { 102 return hasMore(); 103 } catch (NamingException e) { 104 return false; 105 } 106 } 107 108 public Object nextElement() { 109 try { 110 return findNextMatch(true); 111 } catch (NamingException e) { 112 throw new NoSuchElementException(e.toString()); 113 } 114 } 115 116 public Object next() throws NamingException { 117 // find and remove from list 118 return (findNextMatch(true)); 119 } 120 121 public void close() throws NamingException { 122 if (candidates != null) { 123 candidates.close(); 124 } 125 } 126 127 private SearchResult findNextMatch(boolean remove) throws NamingException { 128 SearchResult answer; 129 if (nextMatch != null) { 130 answer = nextMatch; 131 if (remove) { 132 nextMatch = null; 133 } 134 return answer; 135 } else { 136 // need to find next match 137 Binding next; 138 Object obj; 139 Attributes targetAttrs; 140 while (candidates.hasMore()) { 141 next = (Binding)candidates.next(); 142 obj = next.getObject(); 143 if (obj instanceof DirContext) { 144 targetAttrs = ((DirContext)(obj)).getAttributes(""); 145 if (filter.check(targetAttrs)) { 146 if (!cons.getReturningObjFlag()) { 147 obj = null; 148 } else if (useFactory) { 149 try { 150 // Give name only if context non-null, 151 // otherewise, name will be interpreted relative 152 // to initial context (not what we want) 153 Name nm = (context != null ? 154 new CompositeName(next.getName()) : null); 155 obj = DirectoryManager.getObjectInstance(obj, 156 nm, context, env, targetAttrs); 157 } catch (NamingException e) { 158 throw e; 159 } catch (Exception e) { 160 NamingException e2 = new NamingException( 161 "problem generating object using object factory"); | 30 * attributes for used by the filter: 31 * 32 * ((DirContext)item.getObject()).getAttributes(""). 33 * If item.getObject() is not an DirContext, the item is skipped 34 * 35 * The items in the enumeration are obtained one at a time as 36 * items from the search enumeration are requested. 37 * 38 * @author Rosanna Lee 39 */ 40 41 package com.sun.jndi.toolkit.dir; 42 43 import javax.naming.*; 44 import javax.naming.directory.*; 45 import javax.naming.spi.DirectoryManager; 46 47 import java.util.NoSuchElementException; 48 import java.util.Hashtable; 49 50 final public class LazySearchEnumerationImpl 51 implements NamingEnumeration<SearchResult> { 52 private NamingEnumeration<Binding> candidates; 53 private SearchResult nextMatch = null; 54 private SearchControls cons; 55 private AttrFilter filter; 56 private Context context; 57 private Hashtable<String, Object> env; 58 private boolean useFactory = true; 59 60 public LazySearchEnumerationImpl(NamingEnumeration<Binding> candidates, 61 AttrFilter filter, SearchControls cons) throws NamingException { 62 this.candidates = candidates; 63 this.filter = filter; 64 65 if(cons == null) { 66 this.cons = new SearchControls(); 67 } else { 68 this.cons = cons; 69 } 70 } 71 72 public LazySearchEnumerationImpl(NamingEnumeration<Binding> candidates, 73 AttrFilter filter, SearchControls cons, 74 Context ctx, Hashtable<String, Object> env, boolean useFactory) 75 throws NamingException { 76 77 this.candidates = candidates; 78 this.filter = filter; 79 this.env = env; 80 this.context = ctx; 81 this.useFactory = useFactory; 82 83 if(cons == null) { 84 this.cons = new SearchControls(); 85 } else { 86 this.cons = cons; 87 } 88 } 89 90 91 public LazySearchEnumerationImpl(NamingEnumeration<Binding> candidates, 92 AttrFilter filter, SearchControls cons, 93 Context ctx, Hashtable<String, Object> env) throws NamingException { 94 this(candidates, filter, cons, ctx, env, true); 95 } 96 97 public boolean hasMore() throws NamingException { 98 // find and do not remove from list 99 return findNextMatch(false) != null; 100 } 101 102 public boolean hasMoreElements() { 103 try { 104 return hasMore(); 105 } catch (NamingException e) { 106 return false; 107 } 108 } 109 110 public SearchResult nextElement() { 111 try { 112 return findNextMatch(true); 113 } catch (NamingException e) { 114 throw new NoSuchElementException(e.toString()); 115 } 116 } 117 118 public SearchResult next() throws NamingException { 119 // find and remove from list 120 return (findNextMatch(true)); 121 } 122 123 public void close() throws NamingException { 124 if (candidates != null) { 125 candidates.close(); 126 } 127 } 128 129 private SearchResult findNextMatch(boolean remove) throws NamingException { 130 SearchResult answer; 131 if (nextMatch != null) { 132 answer = nextMatch; 133 if (remove) { 134 nextMatch = null; 135 } 136 return answer; 137 } else { 138 // need to find next match 139 Binding next; 140 Object obj; 141 Attributes targetAttrs; 142 while (candidates.hasMore()) { 143 next = candidates.next(); 144 obj = next.getObject(); 145 if (obj instanceof DirContext) { 146 targetAttrs = ((DirContext)(obj)).getAttributes(""); 147 if (filter.check(targetAttrs)) { 148 if (!cons.getReturningObjFlag()) { 149 obj = null; 150 } else if (useFactory) { 151 try { 152 // Give name only if context non-null, 153 // otherewise, name will be interpreted relative 154 // to initial context (not what we want) 155 Name nm = (context != null ? 156 new CompositeName(next.getName()) : null); 157 obj = DirectoryManager.getObjectInstance(obj, 158 nm, context, env, targetAttrs); 159 } catch (NamingException e) { 160 throw e; 161 } catch (Exception e) { 162 NamingException e2 = new NamingException( 163 "problem generating object using object factory"); |