1 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 ; 28 import javax.naming.NamingException ; 29 import javax.naming.directory.DirContext ; 30 import javax.naming.directory.InitialDirContext ; 31 import javax.naming.directory.SearchControls ; 32 import java.io.IOException ; 33 import java.io.Reader ; 34 import java.util.Hashtable ; 35 36 43 public class LdapConnection extends AbstractConnection { 44 private DirContext ctx; 45 private final SearchControls searchControls; private final Long maxFileLength; 47 private final String baseDn; 48 private final DriverContext driverContext; 49 50 51 57 public static final String SEARCH_SCOPE_KEY = "search.scope"; 58 59 64 public static final String SEARCH_BASEDN_KEY = "search.basedn"; 65 66 67 73 public static final String SEARCH_TIMELIMIT_KEY = "search.timelimit"; 74 75 82 public static final String SEARCH_COUNTLIMIT_KEY = "search.countlimit"; 83 84 89 public static final String 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 103 public LdapConnection(ConnectionParameters parameters) { 104 super(Driver.DIALECT, parameters); 105 Hashtable <String , Object > env = new Hashtable <String , Object >(); 106 env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 108 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 env.putAll(parameters.getProperties()); 121 try { 122 ctx = new InitialDirContext (env); 123 } catch (NamingException e) { 124 throw new LdapProviderException("Unable to establish directory connection", e); 125 } 126 searchControls = new SearchControls (); 128 String 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 baseDn = parameters.getStringProperty(SEARCH_SCOPE_KEY); 141 this.baseDn = baseDn == null ? "" : baseDn; 142 143 Integer tl = parameters.getIntegerProperty(SEARCH_TIMELIMIT_KEY); 144 if (tl != null) { 145 searchControls.setTimeLimit(tl); 146 } 147 Integer cl = parameters.getIntegerProperty(SEARCH_COUNTLIMIT_KEY); 148 if (cl != null) { 149 searchControls.setCountLimit(cl); 150 } 151 Number mfl = parameters.getNumberProperty(FILE_MAXLENGTH_KEY, null); 152 maxFileLength = mfl == null ? null : mfl.longValue(); 153 154 driverContext = parameters.getContext(); 155 } 156 157 DirContext getCtx() { 158 return ctx; 159 } 160 161 SearchControls getSearchControls() { 162 return searchControls; 163 } 164 165 Long getMaxFileLength() { 166 return maxFileLength; 167 } 168 169 DriverContext getDriversContext() { 170 return driverContext; 171 } 172 173 String 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 in; 183 try { 184 in = scriptContent.open(); 185 } catch (IOException 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 filter; 193 try { 194 filter = IOUtils.toString(queryContent.open()).trim(); 195 } catch (IOException 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 e) { 208 throw new LdapProviderException("Unable to close directory context", e); 209 } 210 } 211 } 212 213 } 214 | Popular Tags |