KickJava   Java API By Example, From Geeks To Geeks.

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


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.spi.AbstractConnection;
19 import scriptella.spi.ConnectionParameters;
20 import scriptella.spi.DriverContext;
21 import scriptella.spi.ParametersCallback;
22 import scriptella.spi.ProviderException;
23 import scriptella.spi.QueryCallback;
24 import scriptella.spi.Resource;
25 import scriptella.util.IOUtils;
26
27 import javax.naming.Context JavaDoc;
28 import javax.naming.NamingException JavaDoc;
29 import javax.naming.directory.DirContext JavaDoc;
30 import javax.naming.directory.InitialDirContext JavaDoc;
31 import javax.naming.directory.SearchControls JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.Reader JavaDoc;
34 import java.util.Hashtable JavaDoc;
35
36 /**
37  * Represents a connection to a directory context.
38  * <p>For configuration details and examples see <a HREF="package-summary.html">overview page</a>.
39  *
40  * @author Fyodor Kupolov
41  * @version 1.0
42  */

43 public class LdapConnection extends AbstractConnection {
44     private DirContext JavaDoc ctx;
45     private final SearchControls JavaDoc searchControls; //default search controls
46
private final Long JavaDoc maxFileLength;
47     private final String JavaDoc baseDn;
48     private final DriverContext driverContext;
49
50
51     /**
52      * Name of the <em>Search scope</em> connection property.
53      * <p>The value must be one of the: object, onelevel, subtree
54      *
55      * @see SearchControls#setSearchScope(int)
56      */

57     public static final String JavaDoc SEARCH_SCOPE_KEY = "search.scope";
58
59     /**
60      * Name of the <em>Search base DN</em> connection property.
61      *
62      * @see DirContext#search(String,javax.naming.directory.Attributes)
63      */

64     public static final String JavaDoc SEARCH_BASEDN_KEY = "search.basedn";
65
66
67     /**
68      * Name of the <em>Time Limit</em> connection property.
69      * <p>The value must be integer.
70      *
71      * @see SearchControls#setTimeLimit(int)
72      */

73     public static final String JavaDoc SEARCH_TIMELIMIT_KEY = "search.timelimit";
74
75     /**
76      * Name of the <em>Count Limit</em>(maximum number of entries to be returned)
77      * connection property.
78      * <p>The value must be integer.
79      *
80      * @see SearchControls#setCountLimit(long)
81      */

82     public static final String JavaDoc SEARCH_COUNTLIMIT_KEY = "search.countlimit";
83
84     /**
85      * Names of the Max File Length connection property.
86      * <p>This property specifies the maximum size in Kb of the external files referenced from LDIFs.
87      * The default value is 10000 (10MB)
88      */

89     public static final String JavaDoc FILE_MAXLENGTH_KEY = "file.maxlength";
90
91     public LdapConnection() {
92         this.searchControls = null;
93         this.maxFileLength = null;
94         this.baseDn = null;
95         this.driverContext = null;
96     }
97
98     /**
99      * Creates a connnection to a directory.
100      *
101      * @param parameters parameters to establish connection.
102      */

103     public LdapConnection(ConnectionParameters parameters) {
104         super(Driver.DIALECT, parameters);
105         Hashtable JavaDoc<String JavaDoc, Object JavaDoc> env = new Hashtable JavaDoc<String JavaDoc, Object JavaDoc>();
106         //Put default settings
107
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
108         //Put connection settings
109
if (parameters.getUrl() == null) {
110             throw new LdapProviderException("Connection URL is required");
111         }
112         env.put(Context.PROVIDER_URL, parameters.getUrl());
113         if (parameters.getUser() != null) {
114             env.put(Context.SECURITY_PRINCIPAL, parameters.getUser());
115         }
116         if (parameters.getPassword() != null) {
117             env.put(Context.SECURITY_CREDENTIALS, parameters.getPassword());
118         }
119         //Override env with user specified connection properties
120
env.putAll(parameters.getProperties());
121         try {
122             ctx = new InitialDirContext JavaDoc(env);
123         } catch (NamingException JavaDoc e) {
124             throw new LdapProviderException("Unable to establish directory connection", e);
125         }
126         //Set the search controls used for queries
127
searchControls = new SearchControls JavaDoc();
128         String JavaDoc scope = parameters.getStringProperty(SEARCH_SCOPE_KEY);
129         if (scope != null) {
130             if ("object".equalsIgnoreCase(scope)) {
131                 searchControls.setSearchScope(SearchControls.OBJECT_SCOPE);
132             } else if ("onelevel".equalsIgnoreCase(scope)) {
133                 searchControls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
134             } else if ("subtree".equalsIgnoreCase(scope)) {
135                 searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
136             } else {
137                 throw new LdapProviderException("Unsupported " + SEARCH_SCOPE_KEY + "=" + scope);
138             }
139         }
140         String JavaDoc baseDn = parameters.getStringProperty(SEARCH_SCOPE_KEY);
141         this.baseDn = baseDn == null ? "" : baseDn;
142
143         Integer JavaDoc tl = parameters.getIntegerProperty(SEARCH_TIMELIMIT_KEY);
144         if (tl != null) {
145             searchControls.setTimeLimit(tl);
146         }
147         Integer JavaDoc cl = parameters.getIntegerProperty(SEARCH_COUNTLIMIT_KEY);
148         if (cl != null) {
149             searchControls.setCountLimit(cl);
150         }
151         Number JavaDoc mfl = parameters.getNumberProperty(FILE_MAXLENGTH_KEY, null);
152         maxFileLength = mfl == null ? null : mfl.longValue();
153
154         driverContext = parameters.getContext();
155     }
156
157     DirContext JavaDoc getCtx() {
158         return ctx;
159     }
160
161     SearchControls JavaDoc getSearchControls() {
162         return searchControls;
163     }
164
165     Long JavaDoc getMaxFileLength() {
166         return maxFileLength;
167     }
168
169     DriverContext getDriversContext() {
170         return driverContext;
171     }
172
173     String JavaDoc getBaseDn() {
174         return baseDn;
175     }
176
177     StatementCounter getStatementCounter() {
178         return counter;
179     }
180
181     public void executeScript(final Resource scriptContent, final ParametersCallback parametersCallback) throws ProviderException {
182         Reader JavaDoc in;
183         try {
184             in = scriptContent.open();
185         } catch (IOException JavaDoc e) {
186             throw new LdapProviderException("Failed to read script", e);
187         }
188         new LdifScript(this).execute(in, ctx, parametersCallback);
189     }
190
191     public void executeQuery(final Resource queryContent, final ParametersCallback parametersCallback, final QueryCallback queryCallback) throws ProviderException {
192         String JavaDoc filter;
193         try {
194             filter = IOUtils.toString(queryContent.open()).trim();
195         } catch (IOException JavaDoc e) {
196             throw new LdapProviderException("Failed to read query filter", e);
197         }
198         SearchFilterQuery q = new SearchFilterQuery(this, parametersCallback, queryCallback);
199         q.execute(filter);
200     }
201
202     public void close() throws ProviderException {
203         if (ctx != null) {
204             try {
205                 ctx.close();
206                 ctx = null;
207             } catch (NamingException JavaDoc e) {
208                 throw new LdapProviderException("Unable to close directory context", e);
209             }
210         }
211     }
212
213 }
214
Popular Tags