View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.xdbm.search.evaluator;
21  
22  
23  import org.apache.directory.api.ldap.model.entry.Entry;
24  import org.apache.directory.api.ldap.model.exception.LdapException;
25  import org.apache.directory.api.ldap.model.filter.ScopeNode;
26  import org.apache.directory.server.i18n.I18n;
27  import org.apache.directory.server.xdbm.IndexEntry;
28  import org.apache.directory.server.xdbm.Store;
29  import org.apache.directory.server.xdbm.search.Evaluator;
30  
31  
32  /**
33   * Evaluates base level scope assertions on candidates using an entry database.
34   * 
35   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36   */
37  public class BaseLevelScopeEvaluator<E> implements Evaluator<ScopeNode>
38  {
39      /** The ScopeNode containing initial search scope constraints */
40      private final ScopeNode node;
41  
42      /** The entry identifier of the scope base */
43      private final String baseId;
44  
45      /** True if the scope requires alias dereferencing while searching */
46      private final boolean dereferencing;
47  
48      /** the entry db storing entries */
49      private final Store db;
50  
51  
52      /**
53       * Creates a one level scope node Evaluator for search expressions.
54       *
55       * @param node the scope node
56       * @param db the database used to evaluate scope node
57       * @throws Exception on db access failure
58       */
59      public BaseLevelScopeEvaluator( Store db, ScopeNode node ) throws Exception
60      {
61          this.node = node;
62  
63          this.db = db;
64          baseId = node.getBaseId();
65          dereferencing = node.getDerefAliases().isDerefInSearching() || node.getDerefAliases().isDerefAlways();
66      }
67  
68  
69      /**
70       * Asserts whether or not a candidate has one level scope while taking
71       * alias dereferencing into account.
72       *
73       * TODO - terribly inefficient - would benefit from exposing the id of an
74       * entry within the Entry
75       *
76       * {@inheritDoc}
77       */
78      public boolean evaluate( Entry candidate ) throws Exception
79      {
80          throw new UnsupportedOperationException( I18n.err( I18n.ERR_721 ) );
81      }
82  
83  
84      /**
85       * Asserts whether or not a candidate has one level scope while taking
86       * alias dereferencing into account.
87       *
88       * @param candidate the candidate to assert
89       * @return true if the candidate is within one level scope
90       * @throws Exception if db lookups fail
91       * @see org.apache.directory.server.xdbm.search.Evaluator#evaluate(IndexEntry)
92       */
93      public boolean evaluate( IndexEntry<?, String> indexEntry ) throws LdapException
94      {
95          Entry entry = indexEntry.getEntry();
96  
97          // Fetch the entry
98          if ( null == entry )
99          {
100             entry = db.fetch( indexEntry.getId() );
101 
102             if ( null == entry )
103             {
104                 // The entry is not anymore present : get out
105                 return false;
106             }
107 
108             indexEntry.setEntry( entry );
109         }
110 
111         return true;
112     }
113 
114 
115     public ScopeNode getExpression()
116     {
117         return node;
118     }
119 
120 
121     /**
122      * Gets the id of the search base associated with the ScopeNode expression.
123      *
124      * @return identifier of the search base
125      */
126     public String getBaseId()
127     {
128         return baseId;
129     }
130 
131 
132     /**
133      * Gets whether or not dereferencing is enabled for this evaluator.
134      *
135      * @return true if dereferencing is enabled, false otherwise
136      */
137     public boolean isDereferencing()
138     {
139         return dereferencing;
140     }
141 
142 
143     /**
144      * @see Object#toString()
145      */
146     public String toString( String tabs )
147     {
148         StringBuilder sb = new StringBuilder();
149 
150         sb.append( tabs ).append( "BaseLevelScopEvaluator : " ).append( node ).append( "\n" );
151 
152         return sb.toString();
153     }
154 
155 
156     /**
157      * @see Object#toString()
158      */
159     public String toString()
160     {
161         return toString( "" );
162     }
163 }