1 /*
   2  * Copyright (c) 1999, 2011, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * 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
  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");
 164                                 e2.setRootCause(e);
 165                                 throw e2;
 166                             }
 167                         }
 168                         answer = new SearchResult(next.getName(),
 169                             next.getClassName(), obj,
 170                             SearchFilter.selectAttributes(targetAttrs,
 171                                 cons.getReturningAttributes()),
 172                             true);
 173                         if (!remove)
 174                             nextMatch = answer;
 175                         return answer;
 176                     }
 177                 }
 178             }
 179             return null;
 180         }
 181     }
 182 }