All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.michaelo.dirctxsrc.spring.DirContextSource Maven / Gradle / Ivy

Go to download

A JNDI DirContext factory which works very much like a JDBC DataSource returning ready-to-use DirContext objects.

There is a newer version: 2.3.1
Show newest version
/*
 * Copyright 2012–2019 Michael Osipov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.sf.michaelo.dirctxsrc.spring;

import java.util.Map;

import javax.naming.NamingException;
import javax.naming.directory.DirContext;

import net.sf.michaelo.dirctxsrc.DirContextSource.Auth;
import net.sf.michaelo.dirctxsrc.DirContextSource.Builder;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.ldap.core.ContextSource;

/**
 * A Spring LDAP {@link ContextSource} wrapper for {@link net.sf.michaelo.dirctxsrc.DirContextSource
 * DirContextSource}.
 * 

* Here is a minimal example how to create a {@code DirContextSource} with the supplied builder: * *

 * <beans:bean class="net.sf.michaelo.dirctxsrc.spring.DirContextSource">
 *  <beans:constructor-arg>
 *    <beans:array>
 *      <beans:value>ldap://hostname</beans:value>
 *    </beans:array>
 *  </beans:constructor-arg>
 * </beans:bean>
 * 
* *

* A {@code DirContextSource} object will be initially preconfigured by its builder for you: *

    *
  1. The object factory is set by default to * {@code org.springframework.ldap.core.support.DefaultDirObjectFactory}.
  2. *
* * @see net.sf.michaelo.dirctxsrc.DirContextSource * @version $Id: DirContextSource.java 237 2019-02-18 22:06:45Z michael-o $ */ public class DirContextSource implements ContextSource, InitializingBean { private Builder builder; private net.sf.michaelo.dirctxsrc.DirContextSource contextSource; public DirContextSource(String... urls) { builder = new Builder(urls); builder.objectFactories("org.springframework.ldap.core.support.DefaultDirObjectFactory"); } @Override public void afterPropertiesSet() throws Exception { contextSource = builder.build(); } @Override public DirContext getReadOnlyContext() { try { return contextSource.getDirContext(); } catch (NamingException e) { throw org.springframework.ldap.support.LdapUtils.convertLdapException(e); } } @Override public DirContext getReadWriteContext() { try { return contextSource.getDirContext(); } catch (NamingException e) { throw org.springframework.ldap.support.LdapUtils.convertLdapException(e); } } @Override public DirContext getContext(String principal, String credentials) { throw new UnsupportedOperationException( "a dir context can only be created explicitly with a login config name"); } /** * @see Builder#contextFactory(String) */ public void setContextFactory(String contextFactory) { builder.contextFactory(contextFactory); } /** * @see Builder#auth(Auth) */ public void setAuth(Auth auth) { builder.auth(auth); } /** * @see Builder#loginEntryName(String) */ public void setLoginEntryName(String loginEntryName) { builder.loginEntryName(loginEntryName); } /** * @see Builder#debug(boolean) */ public void setDebug(boolean debug) { builder.debug(debug); } /** * @see Builder#qop(String...) */ public void setQop(String... qop) { builder.qop(qop); } /** * @see Builder#mutualAuth(boolean) */ public void setMutualAuth(boolean mutualAuth) { builder.mutualAuth(mutualAuth); } /** * @see Builder#retries(int) */ public void setRetries(int retries) { builder.retries(retries); } /** * @see Builder#retryWait(int) */ public void setRetryWait(int retryWait) { builder.retryWait(retryWait); } /** * @see Builder#binaryAttributes(String...) */ public void setBinaryAttributes(String... binaryAttributes) { builder.binaryAttributes(binaryAttributes); } /** * @see Builder#referral(String) */ public void setReferral(String referral) { builder.referral(referral); } /** * @see Builder#additionalProperty(String, Object) */ public void setAdditionalProperties(Map additionalProperties) { for (Map.Entry additionalProperty : additionalProperties.entrySet()) builder.additionalProperty(additionalProperty.getKey(), additionalProperty.getValue()); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy