KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > driver > ldap > SearchFilterQuery


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package scriptella.driver.ldap;
17
18 import scriptella.expression.PropertiesSubstitutor;
19 import scriptella.spi.ParametersCallback;
20 import scriptella.spi.QueryCallback;
21
22 import javax.naming.NamingEnumeration JavaDoc;
23 import javax.naming.NamingException JavaDoc;
24 import javax.naming.directory.Attribute JavaDoc;
25 import javax.naming.directory.Attributes JavaDoc;
26 import javax.naming.directory.DirContext JavaDoc;
27 import javax.naming.directory.SearchResult JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.util.logging.Logger JavaDoc;
30
31 /**
32  * Represents an executor for LDAP search filter query(RFC 2254).
33  * <p>When {@link #execute(String)} is called a virtual
34  * row set based on {@link SearchResult search results} is produced.
35  * <p>The {@link javax.naming.directory.SearchResult#getAttributes()} produces
36  * columns for a virtual row. The virtual row also contains <code>dn</code> and <code>rdn</code>
37  * columns representing a found entry DN and a relative DN respectively.
38  *
39  * @author Fyodor Kupolov
40  * @version 1.0
41  */

42 public class SearchFilterQuery implements ParametersCallback {
43     private static final Logger JavaDoc LOG = Logger.getLogger(SearchFilterQuery.class.getName());
44     private QueryCallback queryCallback;
45
46     private SearchResult JavaDoc result;
47     private final LdapConnection connection;
48     private final PropertiesSubstitutor substitutor;
49
50
51     /**
52      * Instantiates an LDAP query.
53      *
54      * @param connection ldap connection.
55      * @param parameters parent parameters callback to get unresolved variables from.
56      * @param queryCallback query callback to notify for search results.
57      */

58     public SearchFilterQuery(final LdapConnection connection, final ParametersCallback parameters,
59                              final QueryCallback queryCallback) {
60
61         this.queryCallback = queryCallback;
62         this.connection = connection;
63         this.substitutor = new PropertiesSubstitutor(parameters);
64     }
65
66     public Object JavaDoc getParameter(final String JavaDoc name) {
67         final Attributes JavaDoc attributes = result.getAttributes();
68         final Attribute JavaDoc attribute = attributes.get(name);
69         if (attribute != null) {
70             try {
71                 return attribute.get(); //Currently only the first value is returned
72
} catch (NamingException JavaDoc e) {
73                 throw new LdapProviderException("Failed to get attribute " + name + " value", e);
74             }
75         } else if ("dn".equalsIgnoreCase(name)) {
76             return result.getNameInNamespace(); //JDK14: use getName
77
}
78         return substitutor.getParameters().getParameter(name);
79     }
80
81     /**
82      * Runs a search specified by filter on a {@link #connection}.
83      * <p>For each search result {@link QueryCallback#processRow(scriptella.spi.ParametersCallback)} is called.
84      *
85      * @param filter search filter according to RFC 2254
86      * @see DirContext#search(javax.naming.Name, String, javax.naming.directory.SearchControls)
87      */

88     public void execute(final String JavaDoc filter) {
89         //Using standard properties substitutor, may be change to something similar to JDBC parameters
90
final String JavaDoc sFilter = substitutor.substitute(filter);
91         if (LOG.isLoggable(Level.FINE)) {
92             LOG.fine("Running a query for search filter " + sFilter);
93         }
94         try {
95             iterate(query(connection, sFilter));
96         } catch (NamingException JavaDoc e) {
97             throw new LdapProviderException("Failed to execute query", e);
98         } catch (LdapProviderException e2) {
99             //Settings a filter as a poblem statement if it has n't been set.
100
if (e2.getErrorStatement() != null) {
101                 e2.setErrorStatement(sFilter);
102             }
103             throw e2;
104         }
105     }
106
107     /**
108      * Iterates naming enumeration
109      */

110     private void iterate(NamingEnumeration JavaDoc<SearchResult JavaDoc> ne) {
111         try {
112             while (ne.hasMoreElements()) {
113                 result = ne.nextElement();
114                 if (LOG.isLoggable(Level.FINE)) {
115                     LOG.fine("Processing search result: " + result);
116                 }
117                 queryCallback.processRow(this); //notifying a callback
118
}
119         } finally {
120             try {//closing naming enumeration in case of unexpected error
121
ne.close();
122             } catch (Exception JavaDoc e) {
123                 LOG.log(Level.FINE, "Failed to close naming enumeration", e);
124             }
125         }
126     }
127
128     protected NamingEnumeration JavaDoc<SearchResult JavaDoc> query(final LdapConnection connection, final String JavaDoc filter) throws NamingException JavaDoc {
129         NamingEnumeration JavaDoc<SearchResult JavaDoc> en = connection.getCtx().search(connection.getBaseDn(), filter, connection.getSearchControls());
130         connection.getStatementCounter().statements++;
131         return en;
132     }
133
134
135 }
136
Popular Tags