J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1999-2000 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | /** |
| 27 | * Given an enumeration of candidates, check whether each |
| 28 | * item in enumeration satifies the given filter. |
| 29 | * Each item is a Binding and the following is used to get its |
| 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"); |
| 162 | e2.setRootCause(e); |
| 163 | throw e2; |
| 164 | } |
| 165 | } |
| 166 | answer = new SearchResult(next.getName(), |
| 167 | next.getClassName(), obj, |
| 168 | SearchFilter.selectAttributes(targetAttrs, |
| 169 | cons.getReturningAttributes()), |
| 170 | true); |
| 171 | if (!remove) |
| 172 | nextMatch = answer; |
| 173 | return answer; |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | return null; |
| 178 | } |
| 179 | } |
| 180 | } |