Patch from Jasper on July 23.

This commit is contained in:
Ulrik Sandberg
2007-07-23 09:16:20 +00:00
parent c937c97cbb
commit ec150b1303
44 changed files with 3160 additions and 153 deletions

View File

@@ -16,6 +16,22 @@
<classpathentry kind="lib" path="lib/test/testng.jar"/>
<classpathentry kind="lib" path="lib/test/junit.jar"/>
<classpathentry kind="lib" path="lib/test/spring-mock.jar"/>
<classpathentry kind="lib" path="lib/test/easymockclassextension.jar"/>
<classpathentry kind="lib" path="lib/test/easymock.jar"/>
<classpathentry kind="lib" path="lib/test/shared-ldap.jar"/>
<classpathentry kind="lib" path="lib/test/shared-asn1.jar"/>
<classpathentry kind="lib" path="lib/test/shared-asn1-codec.jar"/>
<classpathentry kind="lib" path="lib/test/mina-filter-ssl.jar"/>
<classpathentry kind="lib" path="lib/test/mina-core.jar"/>
<classpathentry kind="lib" path="lib/test/apacheds-server-main.jar"/>
<classpathentry kind="lib" path="lib/test/apacheds-server-jndi.jar"/>
<classpathentry kind="lib" path="lib/test/apacheds-protocol-shared.jar"/>
<classpathentry kind="lib" path="lib/test/apacheds-protocol-ldap.jar"/>
<classpathentry kind="lib" path="lib/test/apacheds-protocol-kerberos.jar"/>
<classpathentry kind="lib" path="lib/test/apacheds-protocol-changepw.jar"/>
<classpathentry kind="lib" path="lib/test/apacheds-kerberos-shared.jar"/>
<classpathentry kind="lib" path="lib/test/apacheds-core.jar"/>
<classpathentry kind="lib" path="lib/test/apacheds-core-shared.jar"/>
<classpathentry combineaccessrules="false" kind="src" path="/spring-ldap"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

@@ -48,8 +48,8 @@
<dependency org="junit" name="junit" rev="3.8.1" conf="test->default"/>
<dependency org="junit-addons" name="junit-addons" rev="1.4" conf="test->default"/>
<dependency org="junitperf" name="junitperf" rev="1.8" conf="test->default"/>
<dependency org="easymock" name="easymock" rev="2.0" conf="test->default"/>
<dependency org="easymock" name="easymockclassextension" rev="1.2" conf="test->default"/>
<dependency org="org.easymock" name="easymock" rev="2.2" conf="test->default"/>
<dependency org="org.easymock" name="easymockclassextension" rev="2.2.2" conf="test->default"/>
<dependency org="cglib" name="cglib-nodep" rev="2.1_3" conf="test->default"/>
<dependency org="org.testng" name="testng" rev="5.0" conf="test->default" />

View File

@@ -29,7 +29,7 @@
</bean>
<bean id="ctxMapperFactory" singleton="true"
class="org.springframework.ldap.odm.contextmapping.ContextMapperFactory">
class="org.springframework.ldap.odm.mapping.ObjectDirectoryMapperFactory">
<constructor-arg ref="typeConverter"/>
<constructor-arg ref="referencedEntryEditorFactory"/>
</bean>

View File

@@ -18,17 +18,20 @@ package org.springframework.ldap;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
public abstract class AbstractLdapTemplateIntegrationTest extends
AbstractDependencyInjectionSpringContextTests {
AbstractDependencyInjectionSpringContextTests
{
private LdapServerManager ldapServerManager;
protected void onSetUp() throws Exception {
protected void onSetUp() throws Exception
{
super.onSetUp();
ldapServerManager.cleanAndSetup("setup_data.ldif");
}
public void setLdapServerManager(LdapServerManager ldapServerManager) {
public void setLdapServerManager(LdapServerManager ldapServerManager)
{
this.ldapServerManager = ldapServerManager;
}
}

View File

@@ -16,21 +16,21 @@
package org.springframework.ldap;
import java.io.File;
import java.util.Hashtable;
import javax.naming.Context;
import org.apache.directory.server.configuration.MutableServerStartupConfiguration;
import org.apache.directory.server.jndi.ServerContextFactory;
import org.springframework.beans.factory.InitializingBean;
import javax.naming.Context;
import java.io.File;
import java.util.Hashtable;
/**
* Helper class to simplify Spring configuration of ApacheDS.
*
* @author Mattias Arthursson
*/
public class ConfigEnvHelper implements InitializingBean {
public class ConfigEnvHelper implements InitializingBean
{
private final MutableServerStartupConfiguration configuration;
private final Hashtable initialEnv;
@@ -40,17 +40,20 @@ public class ConfigEnvHelper implements InitializingBean {
+ File.separator + "ldaptemplate_apacheds");
public ConfigEnvHelper(Hashtable initialEnv,
MutableServerStartupConfiguration configuration) {
MutableServerStartupConfiguration configuration)
{
this.initialEnv = initialEnv;
this.configuration = configuration;
}
public Hashtable getEnv() {
public Hashtable getEnv()
{
return initialEnv;
}
public void afterPropertiesSet() throws Exception {
public void afterPropertiesSet() throws Exception
{
initialEnv.put(Context.INITIAL_CONTEXT_FACTORY, ServerContextFactory.class.getName());
configuration.setWorkingDirectory(workingDir);
initialEnv.putAll(configuration.toJndiEnvironment());

View File

@@ -16,17 +16,6 @@
package org.springframework.ldap;
import java.util.Properties;
import javax.naming.Binding;
import javax.naming.Context;
import javax.naming.ContextNotEmptyException;
import javax.naming.InitialContext;
import javax.naming.Name;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.directory.server.core.configuration.ShutdownConfiguration;
@@ -37,23 +26,31 @@ import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.support.DefaultDirObjectFactory;
import javax.naming.*;
import javax.naming.ContextNotEmptyException;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import java.util.Properties;
/**
* Utility class to initialize the apache directory server for use in the
* integration tests.
*
* @author Mattias Arthursson
*
*/
public class LdapServerManager implements DisposableBean {
public class LdapServerManager implements DisposableBean
{
private static Log log = LogFactory.getLog(LdapServerManager.class);
private ContextSource contextSource;
public void setContextSource(ContextSource contextSource) {
public void setContextSource(ContextSource contextSource)
{
this.contextSource = contextSource;
}
public void destroy() throws Exception {
public void destroy() throws Exception
{
Properties env = new Properties();
env.setProperty(Context.INITIAL_CONTEXT_FACTORY,
ServerContextFactory.class.getName());
@@ -67,7 +64,8 @@ public class LdapServerManager implements DisposableBean {
new InitialContext(env);
}
public void cleanAndSetup(String ldifFile) throws Exception {
public void cleanAndSetup(String ldifFile) throws Exception
{
DirContext ctx = contextSource.getReadWriteContext();
// First of all, make sure the database is empty.
@@ -76,49 +74,68 @@ public class LdapServerManager implements DisposableBean {
// Different test cases have different base paths. This means that the
// starting point will be different.
if (ctx.getEnvironment().get(
DefaultDirObjectFactory.JNDI_ENV_BASE_PATH_KEY) != null) {
DefaultDirObjectFactory.JNDI_ENV_BASE_PATH_KEY) != null)
{
startingPoint = DistinguishedName.EMPTY_PATH;
} else {
}
else
{
startingPoint = new DistinguishedName("dc=jayway,dc=se");
}
try {
try
{
log.info("Cleaning all present data.");
clearSubContexts(ctx, startingPoint);
// Load the ldif to the recently started server
log.info("Loading setup data");
LdifFileLoader loader = new LdifFileLoader(ctx, ldifFile);
loader.execute();
} finally {
}
finally
{
ctx.close();
}
}
private void clearSubContexts(DirContext ctx, Name name)
throws NamingException {
throws NamingException
{
NamingEnumeration enumeration = null;
try {
try
{
enumeration = ctx.listBindings(name);
while (enumeration.hasMore()) {
while (enumeration.hasMore())
{
Binding element = (Binding) enumeration.next();
DistinguishedName childName = new DistinguishedName(element
.getName());
childName.prepend((DistinguishedName) name);
try {
try
{
ctx.destroySubcontext(childName);
} catch (ContextNotEmptyException e) {
}
catch (ContextNotEmptyException e)
{
clearSubContexts(ctx, childName);
ctx.destroySubcontext(childName);
}
}
} catch (NamingException e) {
}
catch (NamingException e)
{
e.printStackTrace();
} finally {
try {
}
finally
{
try
{
enumeration.close();
} catch (Exception e) {
}
catch (Exception e)
{
// Never mind this
}
}

View File

@@ -5,15 +5,15 @@
*/
package org.springframework.ldap.odm.dao;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.ldap.AbstractLdapTemplateIntegrationTest;
import org.springframework.ldap.NameAlreadyBoundException;
import org.springframework.ldap.NameNotFoundException;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.odm.entity.TestPerson;
import org.springframework.ldap.odm.entity.TestRole;
import org.springframework.ldap.odm.entity.ITestPerson;
import org.springframework.ldap.odm.entity.ITestRole;
import org.testng.Assert;
import java.util.List;
@@ -23,7 +23,7 @@ public class LdapDaoTest extends AbstractLdapTemplateIntegrationTest
{
protected static final Log LOGGER = LogFactory.getLog(LdapDaoTest.class);
protected LdapDao ldapDao;
private TestPerson testPerson;
private ITestPerson testPerson;
protected String[] getConfigLocations()
@@ -46,7 +46,7 @@ public class LdapDaoTest extends AbstractLdapTemplateIntegrationTest
public void createPerMethodConsumer()
{
LOGGER.debug("********************CREATE PER METHOD CONSUMER**************************");
testPerson = new TestPerson();
testPerson = new ITestPerson();
testPerson.setIdentifier("fred" + System.currentTimeMillis());
testPerson.setEmailAddress("fred@fred.com");
testPerson.setFullName("Fred (Freddy) Fredrickson");
@@ -85,7 +85,7 @@ public class LdapDaoTest extends AbstractLdapTemplateIntegrationTest
public void testFindByNamingAttribute()
{
ldapDao.create(testPerson);
TestPerson retrieved = (TestPerson) ldapDao.findByNamingAttribute(testPerson.getIdentifier(), TestPerson.class);
ITestPerson retrieved = (ITestPerson) ldapDao.findByNamingAttribute(testPerson.getIdentifier(), ITestPerson.class);
LOGGER.debug(retrieved);
Assert.assertNotNull(retrieved, "Should've found the created entity.");
}
@@ -93,11 +93,11 @@ public class LdapDaoTest extends AbstractLdapTemplateIntegrationTest
public void testLoadsReferencedEntities()
{
TestRole webUser = (TestRole) ldapDao.findByNamingAttribute("webUser", TestRole.class);
ITestRole webUser = (ITestRole) ldapDao.findByNamingAttribute("webUser", ITestRole.class);
ldapDao.create(testPerson);
webUser.setMembers(new TestPerson[]{testPerson});
webUser.setMembers(new ITestPerson[]{testPerson});
ldapDao.update(webUser);
TestRole updated = (TestRole) ldapDao.findByNamingAttribute("webUser", TestRole.class);
ITestRole updated = (ITestRole) ldapDao.findByNamingAttribute("webUser", ITestRole.class);
LOGGER.debug(updated);
Assert.assertTrue(ArrayUtils.contains(updated.getMembers(), testPerson));
}
@@ -107,7 +107,7 @@ public class LdapDaoTest extends AbstractLdapTemplateIntegrationTest
{
ldapDao.create(testPerson);
DistinguishedName dn = new DistinguishedName("uid=" + testPerson.getIdentifier() + ",ou=people");
TestPerson retrieved = (TestPerson) ldapDao.findByDn(dn, TestPerson.class);
ITestPerson retrieved = (ITestPerson) ldapDao.findByDn(dn, ITestPerson.class);
LOGGER.debug(retrieved);
Assert.assertNotNull(retrieved, "Should've found the created entity.");
}
@@ -132,14 +132,14 @@ public class LdapDaoTest extends AbstractLdapTemplateIntegrationTest
public void testFindAll()
{
ldapDao.create(testPerson);
List results = ldapDao.findAll(TestPerson.class);
List results = ldapDao.findAll(ITestPerson.class);
Assert.assertTrue(results.size() > 0);
}
public void testFilterByBeanProperty()
{
ldapDao.create(testPerson);
List results = ldapDao.filterByBeanProperty(testPerson.getEmailAddress(), "EmailAddress", TestPerson.class);
List results = ldapDao.filterByBeanProperty(testPerson.getEmailAddress(), "EmailAddress", ITestPerson.class);
LOGGER.debug("filterByBeanProperty returned: " + results.size() + " results.");
Assert.assertTrue(results.size() > 0, "Filter should return collection containing more than one result. ");
}
@@ -152,8 +152,8 @@ public class LdapDaoTest extends AbstractLdapTemplateIntegrationTest
String newEmail = "new@email.com";
testPerson.setEmailAddress(newEmail);
ldapDao.update(testPerson);
TestPerson retrieved = (TestPerson) ldapDao.findByNamingAttribute(testPerson.getIdentifier(),
TestPerson.class);
ITestPerson retrieved = (ITestPerson) ldapDao.findByNamingAttribute(testPerson.getIdentifier(),
ITestPerson.class);
Assert.assertEquals(retrieved.getEmailAddress(), newEmail, "Email name not updated.");
}
catch (Exception unexpected)
@@ -182,7 +182,7 @@ public class LdapDaoTest extends AbstractLdapTemplateIntegrationTest
ldapDao.create(testPerson);
LOGGER.debug(testPerson);
ldapDao.delete(testPerson);
TestPerson retrieved = (TestPerson) ldapDao.findByNamingAttribute(testPerson.getIdentifier(), TestPerson.class);
ITestPerson retrieved = (ITestPerson) ldapDao.findByNamingAttribute(testPerson.getIdentifier(), ITestPerson.class);
Assert.assertNull(retrieved, "Entity should've been deleted.");
}

View File

@@ -0,0 +1,138 @@
/*
* Copyright 2006 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.entity;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.odm.annotations.DirAttribute;
import org.springframework.ldap.odm.annotations.NamingAttribute;
import org.springframework.ldap.odm.annotations.NamingSuffix;
import org.springframework.ldap.odm.annotations.ObjectClasses;
import java.util.Date;
@NamingAttribute("cn")
@NamingSuffix({"ou = people", "dc = example", "dc=com"})
@ObjectClasses({"top", "person", "organizationalPerson", "inetorgperson"})
public class ITestEntity
{
@DirAttribute("cn")
private String name;
@DirAttribute("addr")
private String address;
@DirAttribute("mail")
private String mail;
@DirAttribute("userpassword")
private byte[] password;
@DirAttribute("desc")
private String[] description;
@DirAttribute("acceptemails")
private Boolean acceptEmails;
@DirAttribute("loginresettime")
private Date resetLogin;
@DirAttribute("failedlogins")
private Integer failedLogins;
@DirAttribute("creatorname")
private DistinguishedName creator;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
public String getMail()
{
return mail;
}
public void setMail(String mail)
{
this.mail = mail;
}
public byte[] getPassword()
{
return password;
}
public void setPassword(byte[] password)
{
this.password = password;
}
public String[] getDescription()
{
return description;
}
public void setDescription(String[] description)
{
this.description = description;
}
public Boolean getAcceptEmails()
{
return acceptEmails;
}
public void setAcceptEmails(Boolean acceptEmails)
{
this.acceptEmails = acceptEmails;
}
public Date getResetLogin()
{
return resetLogin;
}
public void setResetLogin(Date resetLogin)
{
this.resetLogin = resetLogin;
}
public Integer getFailedLogins()
{
return failedLogins;
}
public void setFailedLogins(Integer failedLogins)
{
this.failedLogins = failedLogins;
}
public DistinguishedName getCreator()
{
return creator;
}
public void setCreator(DistinguishedName creator)
{
this.creator = creator;
}
}

View File

@@ -0,0 +1,119 @@
/*
* Copyright 2006 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.entity;
import com.thoughtworks.xstream.XStream;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.springframework.ldap.odm.annotations.DirAttribute;
import org.springframework.ldap.odm.annotations.NamingAttribute;
import org.springframework.ldap.odm.annotations.NamingSuffix;
import org.springframework.ldap.odm.annotations.ObjectClasses;
@NamingAttribute("uid")
@NamingSuffix({"ou=people"})
@ObjectClasses({"person", "organizationalPerson", "inetorgperson"})
public class ITestPerson
{
/**
* ******************************** Directory mapped fields ***********************************
*/
@DirAttribute("uid")
private String identifier;
@DirAttribute("sn")
private String lastName;
@DirAttribute("givenName")
private String givenName;
@DirAttribute("cn")
private String fullName;
@DirAttribute("mail")
private String emailAddress;
@DirAttribute("userpassword")
private byte[] password;
/**
* ********************************************************************************************
*/
public String getIdentifier()
{
return identifier;
}
public void setIdentifier(String identifier)
{
this.identifier = identifier;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public String getGivenName()
{
return givenName;
}
public void setGivenName(String givenName)
{
this.givenName = givenName;
}
public String getFullName()
{
return fullName;
}
public void setFullName(String fullName)
{
this.fullName = fullName;
}
public String getEmailAddress()
{
return emailAddress;
}
public void setEmailAddress(String emailAddress)
{
this.emailAddress = emailAddress;
}
public byte[] getPassword()
{
return password;
}
public void setPassword(byte[] password)
{
this.password = password;
}
public String toString()
{
XStream xStream = new XStream();
xStream.alias("ITestPerson", ITestPerson.class);
return "\n" + xStream.toXML(this);
}
public boolean equals(Object o)
{
return EqualsBuilder.reflectionEquals(this, o);
}
}

View File

@@ -0,0 +1,73 @@
/*
* Copyright 2006 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.entity;
import com.thoughtworks.xstream.XStream;
import org.springframework.ldap.odm.annotations.DirAttribute;
import org.springframework.ldap.odm.annotations.NamingAttribute;
import org.springframework.ldap.odm.annotations.NamingSuffix;
import org.springframework.ldap.odm.annotations.ObjectClasses;
@NamingAttribute("cn")
@NamingSuffix({"ou=roles"})
@ObjectClasses({"top", "organizationalRole"})
public class ITestRole
{
/**
* ******************************** Directory mapped fields ***********************************
*/
@DirAttribute("cn")
private String roleName;
@DirAttribute
private String description;
@DirAttribute("roleOccupant")
private ITestPerson[] members;
/**
* ********************************************************************************************
*/
public String getRoleName()
{
return roleName;
}
public void setRoleName(String roleName)
{
this.roleName = roleName;
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
public ITestPerson[] getMembers()
{
return members;
}
public void setMembers(ITestPerson[] members)
{
this.members = members;
}
public String toString()
{
XStream xStream = new XStream();
xStream.alias("ITestRole", ITestRole.class);
xStream.alias("ITestPerson", ITestPerson.class);
return "\n" + xStream.toXML(this);
}
}

View File

@@ -11,8 +11,8 @@ import org.springframework.ldap.core.DistinguishedName;
import javax.naming.ldap.LdapName;
import java.beans.PropertyEditor;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
@@ -37,7 +37,9 @@ public class LdapTypeConverter extends SimpleTypeConverter
registerCustomEditor(DistinguishedName.class, new NameEditor(DistinguishedName.class));
}
/** Convert Object to String */
/**
* Convert Object to String
*/
public String getAsText(Object value)
{
if (value instanceof String)
@@ -56,7 +58,9 @@ public class LdapTypeConverter extends SimpleTypeConverter
}
}
/** Convert Object array to String array */
/**
* Convert Object array to String array
*/
public String[] getAllAsText(Object[] values)
{
String[] textValues = new String[values.length];

View File

@@ -7,10 +7,11 @@ package org.springframework.ldap.odm.attributetypes;
import org.springframework.ldap.core.DistinguishedName;
import java.beans.PropertyEditorSupport;
import javax.naming.ldap.LdapName;
import java.beans.PropertyEditorSupport;
/** NameEditor is responsible for converting Strings to the type LdapName or DistinguishedName.
/**
* NameEditor is responsible for converting Strings to the type LdapName or DistinguishedName.
* And, conversely LdapNames and DistinguishedNames to the type String.
*/
public class NameEditor extends PropertyEditorSupport
@@ -19,7 +20,7 @@ public class NameEditor extends PropertyEditorSupport
/**
* @param conversionClass <code>LdapName.class</code> for String <--> LdapName and
* <code>DistinguishedName.class</code> for String <--> DistinguishedName
* <code>DistinguishedName.class</code> for String <--> DistinguishedName
*/
public NameEditor(Class conversionClass)
{
@@ -31,14 +32,18 @@ public class NameEditor extends PropertyEditorSupport
}
}
/** Invokes toString() on the LdapName or DistinguishedName */
/**
* Invokes toString() on the LdapName or DistinguishedName
*/
public String getAsText()
{
Object value = conversionClass.cast(getValue());
return value != null ? value.toString() : "";
}
/** Attempts to parse and String and return an LdapName or DistinguishedName */
/**
* Attempts to parse and String and return an LdapName or DistinguishedName
*/
public void setAsText(String text) throws IllegalArgumentException
{
try

View File

@@ -6,8 +6,8 @@
package org.springframework.ldap.odm.attributetypes;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.odm.contextmapping.ObjectDirectoryMapper;
import org.springframework.ldap.odm.contextmapping.exception.ContextMapperException;
import org.springframework.ldap.odm.mapping.MappingException;
import org.springframework.ldap.odm.mapping.ObjectDirectoryMapper;
import javax.naming.InvalidNameException;
import javax.naming.ldap.LdapName;
@@ -16,23 +16,25 @@ import java.beans.PropertyEditorSupport;
public class ReferencedEntryEditor extends PropertyEditorSupport
{
private LdapTemplate ldapTemplate;
private ObjectDirectoryMapper contextMapper;
private ObjectDirectoryMapper objectDirectoryMapper;
public ReferencedEntryEditor(LdapTemplate ldapTemplate, ObjectDirectoryMapper contextMapper)
public ReferencedEntryEditor(LdapTemplate ldapTemplate,
ObjectDirectoryMapper objectDirectoryMapper)
{
this.ldapTemplate = ldapTemplate;
this.contextMapper = contextMapper;
this.objectDirectoryMapper = objectDirectoryMapper;
}
public String getAsText()
{
try
{
return contextMapper.buildDn(getValue()).toString();
return objectDirectoryMapper.buildDn(getValue()).toString();
}
catch (ContextMapperException e)
catch (MappingException e)
{
throw new RuntimeException("No context mapper for class: " + getValue().getClass().getSimpleName());
throw new RuntimeException(
"Mapping exception: " + getValue().getClass().getSimpleName());
}
}
@@ -40,7 +42,7 @@ public class ReferencedEntryEditor extends PropertyEditorSupport
{
try
{
setValue(ldapTemplate.lookup(new LdapName(text), contextMapper));
setValue(ldapTemplate.lookup(new LdapName(text), objectDirectoryMapper));
}
catch (InvalidNameException e)
{

View File

@@ -0,0 +1,15 @@
/*
* Copyright 2005 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.attributetypes;
public class ReferencedEntryEditorCreationException extends Exception
{
public ReferencedEntryEditorCreationException(String message, Exception cause)
{
super(message, cause);
}
}

View File

@@ -5,27 +5,24 @@
*/
package org.springframework.ldap.odm.attributetypes;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.ldap.odm.attributetypes.exception.ReferencedEntryEditorCreationException;
import org.springframework.ldap.odm.contextmapping.ContextMapperFactory;
import org.springframework.ldap.odm.contextmapping.ObjectDirectoryMapper;
import org.springframework.ldap.odm.contextmapping.exception.ContextMapperException;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.odm.mapping.MappingException;
import org.springframework.ldap.odm.mapping.ObjectDirectoryMapper;
import org.springframework.ldap.odm.mapping.ObjectDirectoryMapperFactory;
import java.util.HashMap;
import java.util.Map;
public class ReferencedEntryEditorFactory
{
private static final Log LOGGER = LogFactory.getLog(ReferencedEntryEditorFactory.class);
private ContextMapperFactory contextMapperFactory;
private ObjectDirectoryMapperFactory odmFactory;
private LdapTemplate ldapTemplate;
private Map<Class, ReferencedEntryEditor> referencedEntryEditors;
public ReferencedEntryEditorFactory(
LdapTemplate ldapTemplate)
public ReferencedEntryEditorFactory(LdapTemplate ldapTemplate)
{
this.ldapTemplate = ldapTemplate;
this.referencedEntryEditors = new HashMap();
@@ -43,24 +40,25 @@ public class ReferencedEntryEditorFactory
{
LOGGER.debug("Attempting to create a referenced entry editor for class: "
+ clazz.getSimpleName());
ObjectDirectoryMapper contextMapper = null;
try
{
contextMapper = contextMapperFactory.contextMapperForClass(clazz);
ObjectDirectoryMapper odm = odmFactory.objectDirectoryMapperForClass(clazz);
ReferencedEntryEditor referencedEntryEditor =
new ReferencedEntryEditor(ldapTemplate, contextMapper);
new ReferencedEntryEditor(ldapTemplate, odm);
referencedEntryEditors.put(clazz, referencedEntryEditor);
return referencedEntryEditor;
}
catch (ContextMapperException e)
catch (MappingException e)
{
throw new ReferencedEntryEditorCreationException(e.getMessage(), e);
}
}
}
public void setContextMapperFactory(ContextMapperFactory contextMapperFactory)
public void setObjectDirectoryMapperFactory(ObjectDirectoryMapperFactory
mapperFactory)
{
this.contextMapperFactory = contextMapperFactory;
this.odmFactory = mapperFactory;
}
}

View File

@@ -0,0 +1,19 @@
/*
* Copyright 2006 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.dao;
public class DaoException extends RuntimeException
{
public DaoException(String message)
{
super(message);
}
public DaoException(String message, Throwable cause)
{
super(message, cause);
}
}

View File

@@ -5,9 +5,8 @@
*/
package org.springframework.ldap.odm.dao;
import java.util.List;
import javax.naming.Name;
import java.util.List;
public interface LdapDao
{
@@ -23,7 +22,7 @@ public interface LdapDao
public Object findByDn(Name dn, Class returnType);
List findAll(Class ofType);
List findAll(Class ofType);
List filterByBeanProperty(String value, String beanPropertyName, Class returnType);
}

View File

@@ -8,13 +8,11 @@ package org.springframework.ldap.odm.dao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.odm.contextmapping.ContextMapperFactory;
import org.springframework.ldap.odm.contextmapping.ObjectDirectoryMapper;
import org.springframework.ldap.odm.contextmapping.exception.ContextMapperException;
import org.springframework.ldap.odm.dao.exception.DaoException;
import org.springframework.ldap.odm.dao.exception.DataIntegrityViolationException;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.odm.mapping.MappingException;
import org.springframework.ldap.odm.mapping.ObjectDirectoryMapper;
import org.springframework.ldap.odm.mapping.ObjectDirectoryMapperFactory;
import javax.naming.Name;
import java.util.List;
@@ -23,12 +21,12 @@ public class LdapDaoImpl implements LdapDao
{
static final Log LOGGER = LogFactory.getLog(LdapDaoImpl.class);
LdapTemplate ldapTemplate;
ContextMapperFactory ctxMapperFactory;
ObjectDirectoryMapperFactory odmFactory;
public LdapDaoImpl(LdapTemplate ldapTemplate, ContextMapperFactory ctxMapperFactory)
public LdapDaoImpl(LdapTemplate ldapTemplate, ObjectDirectoryMapperFactory odmFactory)
{
this.ldapTemplate = ldapTemplate;
this.ctxMapperFactory = ctxMapperFactory;
this.odmFactory = odmFactory;
}
public void create(Object dirObject)
@@ -36,15 +34,12 @@ public class LdapDaoImpl implements LdapDao
DirContextAdapter context = new DirContextAdapter();
try
{
ObjectDirectoryMapper ctxMapper = ctxMapperFactory.contextMapperForClass(dirObject.getClass());
ctxMapper.mapToContext(dirObject, context);
ldapTemplate.bind(ctxMapper.buildDn(dirObject), context, null);
ObjectDirectoryMapper mapper =
odmFactory.objectDirectoryMapperForClass(dirObject.getClass());
mapper.mapToContext(dirObject, context);
ldapTemplate.bind(mapper.buildDn(dirObject), context, null);
}
catch (org.springframework.dao.DataIntegrityViolationException dive)
{
throw new DataIntegrityViolationException(dive.getMessage());
}
catch (ContextMapperException e)
catch (MappingException e)
{
throw new DaoException(e.getMessage(), e);
}
@@ -54,13 +49,14 @@ public class LdapDaoImpl implements LdapDao
{
try
{
ObjectDirectoryMapper ctxMapper = ctxMapperFactory.contextMapperForClass(dirObject.getClass());
Name dn = ctxMapper.buildDn(dirObject);
ObjectDirectoryMapper mapper =
odmFactory.objectDirectoryMapperForClass(dirObject.getClass());
Name dn = mapper.buildDn(dirObject);
DirContextAdapter contextAdapter = (DirContextAdapter) ldapTemplate.lookup(dn);
ctxMapper.mapToContext(dirObject, contextAdapter);
mapper.mapToContext(dirObject, contextAdapter);
ldapTemplate.modifyAttributes(dn, contextAdapter.getModificationItems());
}
catch (ContextMapperException e)
catch (MappingException e)
{
throw new DaoException(e.getMessage(), e);
}
@@ -70,8 +66,9 @@ public class LdapDaoImpl implements LdapDao
{
try
{
ObjectDirectoryMapper ctxMapper = ctxMapperFactory.contextMapperForClass(dirObject.getClass());
if (findByDn(ctxMapper.buildDn(dirObject), dirObject.getClass()) == null)
ObjectDirectoryMapper mapper =
odmFactory.objectDirectoryMapperForClass(dirObject.getClass());
if (findByDn(mapper.buildDn(dirObject), dirObject.getClass()) == null)
{
create(dirObject);
}
@@ -80,7 +77,7 @@ public class LdapDaoImpl implements LdapDao
update(dirObject);
}
}
catch (ContextMapperException e)
catch (MappingException e)
{
throw new DaoException(e.getMessage(), e);
}
@@ -91,10 +88,11 @@ public class LdapDaoImpl implements LdapDao
{
try
{
ObjectDirectoryMapper ctxMapper = ctxMapperFactory.contextMapperForClass(dirObject.getClass());
ldapTemplate.unbind(ctxMapper.buildDn(dirObject));
ObjectDirectoryMapper mapper =
odmFactory.objectDirectoryMapperForClass(dirObject.getClass());
ldapTemplate.unbind(mapper.buildDn(dirObject));
}
catch (ContextMapperException e)
catch (MappingException e)
{
e.printStackTrace();
throw new DaoException(e.getMessage(), e);
@@ -105,11 +103,12 @@ public class LdapDaoImpl implements LdapDao
{
try
{
ObjectDirectoryMapper ctxMapper = ctxMapperFactory.contextMapperForClass(returnType);
Name dn = ctxMapper.buildDn(namingAttributeValue);
ObjectDirectoryMapper mapper =
odmFactory.objectDirectoryMapperForClass(returnType);
Name dn = mapper.buildDn(namingAttributeValue);
return findByDn(dn, returnType);
}
catch (ContextMapperException e)
catch (MappingException e)
{
throw new DaoException(e.getMessage(), e);
}
@@ -119,10 +118,11 @@ public class LdapDaoImpl implements LdapDao
{
try
{
ObjectDirectoryMapper ctxMapper = ctxMapperFactory.contextMapperForClass(returnType);
return ldapTemplate.lookup(dn, ctxMapper);
ObjectDirectoryMapper mapper =
odmFactory.objectDirectoryMapperForClass(returnType);
return ldapTemplate.lookup(dn, mapper);
}
catch (ContextMapperException e)
catch (MappingException e)
{
throw new DaoException(e.getMessage(), e);
}
@@ -138,24 +138,13 @@ public class LdapDaoImpl implements LdapDao
try
{
LOGGER.debug("Filtering on property: " + beanPropertyName + ", for value: " + value);
ObjectDirectoryMapper ctxMapper =
ctxMapperFactory.contextMapperForClass(returnType);
try
{
String attributeName = ctxMapper.getContextMap().attributeNameFor(beanPropertyName);
String filter = attributeName + "=" + value;
List results = ldapTemplate.search(ctxMapper.getNamingSuffix().toString(), filter,
ctxMapperFactory.contextMapperForClass(returnType));
LOGGER.debug(results.size() + " results found for filter: " + filter);
return results;
}
catch (ContextMapperException e)
{
throw new DaoException(e.getMessage(), e);
}
ObjectDirectoryMapper mapper = odmFactory.objectDirectoryMapperForClass(returnType);
String filter = mapper.getObjectDirectoryMap().attributeNameFor(beanPropertyName) + "=" + value;
List results = ldapTemplate.search(mapper.getObjectDirectoryMap().getNamingSuffix().toString(), filter, mapper);
LOGGER.debug(results.size() + " results found for filter: " + filter);
return results;
}
catch (ContextMapperException e)
catch (MappingException e)
{
throw new DaoException(e.getMessage(), e);
}
@@ -165,21 +154,22 @@ public class LdapDaoImpl implements LdapDao
{
try
{
ObjectDirectoryMapper ctxMapper = ctxMapperFactory.contextMapperForClass(ofType);
ObjectDirectoryMapper mapper = odmFactory.objectDirectoryMapperForClass(ofType);
StringBuilder filterBuilder = new StringBuilder();
filterBuilder.append("(&");
for (String objectClass : ctxMapper.getObjectClasses())
for (String objectClass : mapper.getObjectDirectoryMap().getObjectClasses())
{
filterBuilder.append("(objectClass=");
filterBuilder.append(objectClass);
filterBuilder.append(")");
}
filterBuilder.append(")");
List results = ldapTemplate.search(DistinguishedName.EMPTY_PATH, filterBuilder.toString(), ctxMapper);
List results = ldapTemplate.search(
DistinguishedName.EMPTY_PATH, filterBuilder.toString(), mapper);
LOGGER.debug("Found " + results.size() + " entries of type " + ofType.getSimpleName());
return results;
}
catch (ContextMapperException e)
catch (MappingException e)
{
throw new DaoException(e.getMessage(), e);
}

View File

@@ -0,0 +1,107 @@
/*
* Copyright 2005 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.mapping;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.ldap.core.DistinguishedName;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public abstract class AbstractObjectDirectoryMap implements ObjectDirectoryMap
{
protected static final Log LOGGER = LogFactory.getLog(AnnotationObjectDirectoryMap.class);
protected Class clazz;
protected String namingAttribute;
protected String[] objectClasses;
protected DistinguishedName namingSuffix;
protected Map<String, String> beanPropertyNameKeys;
protected Map<String, String> attributeNameKeys;
public AbstractObjectDirectoryMap(Class<?> clazz) throws MappingException
{
this.clazz = clazz;
if (clazz == null)
{
throw new IllegalArgumentException("Unable to create mapping parser. Clazz argument is null.");
}
beanPropertyNameKeys = new HashMap();
attributeNameKeys = new HashMap();
parseNamingAttribute();
parseObjectClasses();
parseNamingSuffix();
mapAttributesToBeanProperties();
}
/**
* ********************************Template *****************************************
*/
protected abstract void parseNamingAttribute() throws MappingException;
protected abstract void parseObjectClasses() throws MappingException;
protected abstract void parseNamingSuffix() throws MappingException;
protected abstract void mapAttributesToBeanProperties() throws MappingException;
/**
* **********************************************************************************
*/
protected void map(String beanPropertyName, String toAttributeName)
{
LOGGER.info("Bean property '" + beanPropertyName + "' maps to ldap attribute '" + toAttributeName + "'");
beanPropertyNameKeys.put(beanPropertyName, toAttributeName);
attributeNameKeys.put(toAttributeName, beanPropertyName);
}
public String beanPropertyNameFor(String attributeName)
{
return attributeNameKeys.get(attributeName);
}
public String attributeNameFor(String beanPropertyName)
{
return beanPropertyNameKeys.get(beanPropertyName);
}
public Set<String> beanPropertyNames()
{
return beanPropertyNameKeys.keySet();
}
public Set<String> attributeNames()
{
return attributeNameKeys.keySet();
}
public Class getClazz()
{
return clazz;
}
public String getNamingAttribute()
{
return namingAttribute;
}
public String[] getObjectClasses()
{
return objectClasses;
}
public DistinguishedName getNamingSuffix()
{
return namingSuffix;
}
}

View File

@@ -0,0 +1,106 @@
/*
* Copyright 2006 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.mapping;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.odm.annotations.DirAttribute;
import org.springframework.ldap.odm.annotations.NamingAttribute;
import org.springframework.ldap.odm.annotations.NamingSuffix;
import org.springframework.ldap.odm.annotations.ObjectClasses;
import org.springframework.util.StringUtils;
import java.lang.reflect.Field;
public class AnnotationObjectDirectoryMap extends AbstractObjectDirectoryMap
{
public AnnotationObjectDirectoryMap(Class<?> clazz) throws MappingException
{
super(clazz);
}
protected void parseNamingAttribute() throws MappingException
{
NamingAttribute attnNamingAttr = (NamingAttribute) clazz.getAnnotation(NamingAttribute.class);
namingAttribute = attnNamingAttr != null ? attnNamingAttr.value() : null;
if (namingAttribute == null)
{
throw new MappingException("Error creating context mapper for class: "
+ clazz.getSimpleName()
+ ". The @NamingAttribute annotation is required.");
}
}
protected void parseObjectClasses() throws MappingException
{
ObjectClasses attnObjectClasses = (ObjectClasses) clazz.getAnnotation(ObjectClasses.class);
objectClasses = attnObjectClasses != null ? attnObjectClasses.value() : null;
if (objectClasses == null)
{
throw new MappingException("Error creating context mapper for class: "
+ clazz.getSimpleName()
+ ". The @ObjectClasses annotation is required.");
}
if (objectClasses[0].contains(","))
{
throw new MappingException("Error creating context mapper for class: "
+ clazz.getSimpleName()
+ ". Object classes must be defined as separate elements, "
+ "for example: @ObjectClasses({\"top\", \"inetorgperson\"}), and not: "
+ "@ObjectClasses({\"top,inetorgperson\"})");
}
}
protected void parseNamingSuffix() throws MappingException
{
NamingSuffix attnNamingSuffix = (NamingSuffix) clazz.getAnnotation(NamingSuffix.class);
String[] namingSuffixElements = attnNamingSuffix != null ? attnNamingSuffix.value() : null;
if (namingSuffixElements == null || namingSuffixElements.length == 0)
{
throw new MappingException("Error creating context mapper for class: "
+ clazz.getSimpleName()
+ ". The @NamingSuffix annotation is required.");
}
if (namingSuffixElements[0].contains(","))
{
throw new MappingException("Error creating context mapper for class: "
+ clazz.getSimpleName()
+ ". Naming suffix elements must be declared separately, "
+ "for example: @NamingSuffix({\"ou=people\", \"dc=example\", \"dc=com\"}), and not: "
+ "@NamingSuffix({\"ou=people,dc=example,dc=com\"})");
}
namingSuffix = new DistinguishedName();
for (int i = namingSuffixElements.length - 1; i >= 0; i--)
{
String[] nameValue = namingSuffixElements[i].split("=");
if (nameValue.length != 2)
{
throw new MappingException("Error creating context mapper for class: " + clazz.getSimpleName()
+ ". Syntax error in naming suffix element: " + namingSuffixElements[i]);
}
namingSuffix.add(nameValue[0].trim(), nameValue[1].trim());
}
}
protected void mapAttributesToBeanProperties()
{
for (Field field : clazz.getDeclaredFields())
{
DirAttribute dirAttribute = field.getAnnotation(DirAttribute.class);
if (dirAttribute != null)
{
String beanPropertyName = StringUtils.capitalize(field.getName());
String attributeName = dirAttribute.value().equals("") ? field.getName() : dirAttribute.value();
map(beanPropertyName, attributeName);
}
}
}
}

View File

@@ -0,0 +1,22 @@
/*
* Copyright 2006 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.mapping;
public class MappingException extends Exception
{
public MappingException(String message)
{
super(message);
}
public MappingException(String message, Throwable cause)
{
super(message, cause);
}
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2005 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.mapping;
import org.springframework.ldap.core.DistinguishedName;
import java.util.Set;
public interface ObjectDirectoryMap
{
String attributeNameFor(String beanPropertyName);
Set<String> attributeNames();
String beanPropertyNameFor(String attributeName);
Set<String> beanPropertyNames();
Class getClazz();
String getNamingAttribute();
DistinguishedName getNamingSuffix();
String[] getObjectClasses();
}

View File

@@ -0,0 +1,25 @@
/*
* Copyright 2005 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.mapping;
import org.springframework.ldap.core.ContextAssembler;
import org.springframework.ldap.core.ContextMapper;
import javax.naming.Name;
public interface ObjectDirectoryMapper extends ContextMapper, ContextAssembler
{
Object mapFromContext(Object ctx);
void mapToContext(Object beanInstance, Object ctx);
Name buildDn(Object beanInstance) throws MappingException;
Name buildDn(String namingAttributeValue) throws MappingException;
ObjectDirectoryMap getObjectDirectoryMap();
}

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2006 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.mapping;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.ldap.odm.attributetypes.LdapTypeConverter;
import org.springframework.ldap.odm.attributetypes.ReferencedEntryEditorFactory;
import java.util.HashMap;
import java.util.Map;
public class ObjectDirectoryMapperFactory
{
private static final Log LOGGER = LogFactory.getLog(ObjectDirectoryMapperFactory.class);
private Map<Class, ObjectDirectoryMapper> mappers;
private LdapTypeConverter typeConverter;
private ReferencedEntryEditorFactory referencedEntryEditorFactory;
public ObjectDirectoryMapperFactory(LdapTypeConverter typeConverter,
ReferencedEntryEditorFactory referencedEntryEditorFactory)
{
mappers = new HashMap<Class, ObjectDirectoryMapper>();
this.typeConverter = typeConverter;
this.referencedEntryEditorFactory = referencedEntryEditorFactory;
this.referencedEntryEditorFactory.setObjectDirectoryMapperFactory(this);
}
public ObjectDirectoryMapper objectDirectoryMapperForClass(Class clazz)
throws MappingException
{
if (mappers.containsKey(clazz))
{
LOGGER.debug("Returning cached object directory mapper for class: " + clazz.getSimpleName());
return mappers.get(clazz);
}
else
{
LOGGER.debug("Attempting to create an object directory mapper for class: " + clazz.getSimpleName());
AnnotationObjectDirectoryMap map = new AnnotationObjectDirectoryMap(clazz);
ObjectDirectoryMapper mapper =
new ObjectDirectoryMapperImpl(map, typeConverter, referencedEntryEditorFactory);
mappers.put(clazz, mapper);
return mapper;
}
}
}

View File

@@ -0,0 +1,237 @@
/*
* Copyright 2005 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.mapping;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.TypeMismatchException;
import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.odm.attributetypes.LdapTypeConverter;
import org.springframework.ldap.odm.attributetypes.ReferencedEntryEditorCreationException;
import org.springframework.ldap.odm.attributetypes.ReferencedEntryEditorFactory;
import org.springframework.ldap.odm.attributetypes.ValidConversionType;
import org.springframework.ldap.odm.util.AttributeWrapper;
import javax.naming.Name;
import javax.naming.directory.Attribute;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
public class ObjectDirectoryMapperImpl implements ObjectDirectoryMapper
{
private static final Log LOGGER = LogFactory.getLog(ObjectDirectoryMapperImpl.class);
private ObjectDirectoryMap odm;
private LdapTypeConverter typeConverter;
private ReferencedEntryEditorFactory refEditorFactory;
private Map<String, Method> propertyGetters;
private Map<String, Method> propertySetters;
public ObjectDirectoryMapperImpl(ObjectDirectoryMap odm, LdapTypeConverter typeConverter,
ReferencedEntryEditorFactory refEditorFactory) throws MappingException
{
this.odm = odm;
this.typeConverter = typeConverter;
this.refEditorFactory = refEditorFactory;
checkConfigured();
cacheGettersAndSetters();
mapReferencedEntries();
}
public Object mapFromContext(Object ctx)
{
DirContextAdapter ctxAdapter = (DirContextAdapter) ctx;
Object instance;
try
{
instance = odm.getClazz().newInstance();
for (String beanPropertyName : odm.beanPropertyNames())
{
String attributeName = odm.attributeNameFor(beanPropertyName);
Attribute attribute = ctxAdapter.getAttributes().get(attributeName);
if (attribute != null)
{
try
{
Class attributeType = propertyGetters.get(beanPropertyName).getReturnType();
Object beanPropertyValue = typeConverter.convertIfNecessary(
new AttributeWrapper(attribute).getAllAsObject(), attributeType);
propertySetters.get(beanPropertyName).invoke(instance, beanPropertyValue);
}
catch (TypeMismatchException e)
{
throw new MappingException("Cannot set bean property '" + beanPropertyName
+ "' in class " + odm.getClazz().getSimpleName() + ", " + e.getMessage(), e);
}
}
}
}
catch (Exception e)
{
throw new RuntimeException(e.getMessage(), e);
}
return instance;
}
public void mapToContext(Object beanInstance, Object ctx)
{
DirContextAdapter ctxAdapter = (DirContextAdapter) ctx;
ctxAdapter.setAttributeValues("objectclass", odm.getObjectClasses());
for (String attributeName : odm.attributeNames())
{
try
{
String beanPropertyName = odm.beanPropertyNameFor(attributeName);
Object beanPropertyValue = propertyGetters.get(beanPropertyName).invoke(beanInstance);
LOGGER.trace("mapToContext() attribute:" + attributeName + ", value: " + beanPropertyValue);
if (beanPropertyValue != null)
{
if (beanPropertyValue instanceof byte[])
{
ctxAdapter.setAttributeValue(attributeName, beanPropertyValue);
}
else if (beanPropertyValue instanceof Object[])
{
ctxAdapter.setAttributeValues(attributeName,
typeConverter.getAllAsText((Object[]) beanPropertyValue));
}
else
{
ctxAdapter.setAttributeValue(attributeName,
typeConverter.getAsText(beanPropertyValue));
}
}
}
catch (Exception e)
{
throw new RuntimeException(e.getMessage(), e);
}
}
}
public Name buildDn(Object beanInstance) throws MappingException
{
if (beanInstance == null)
{
throw new MappingException("Can't build Dn from beanInstance, beanInstance is null");
}
return buildDn(namingAttributeValue(beanInstance));
}
public Name buildDn(String namingAttributeValue) throws MappingException
{
if (namingAttributeValue == null)
{
throw new MappingException("Can't build Dn from namingAttributeValue, namingAttributeValue is null");
}
DistinguishedName dn = (DistinguishedName) odm.getNamingSuffix().clone();
dn.add(odm.getNamingAttribute(), namingAttributeValue);
LOGGER.trace("buildDn(): " + dn);
return dn;
}
public ObjectDirectoryMap getObjectDirectoryMap()
{
return odm;
}
/**
* ****************Private Methods *******************************************
*/
private void checkConfigured()
{
if (odm == null)
{
throw new IllegalArgumentException("Error creating mapper."
+ ". AbstractObjectDirectoryMap argugment is null");
}
if (this.typeConverter == null)
{
throw new IllegalArgumentException("Error creating mapper for class: "
+ odm.getClazz().getSimpleName()
+ ". LdapTypeConverter arugment is null");
}
if (this.refEditorFactory == null)
{
throw new IllegalArgumentException("Error creating context mapper for class: "
+ odm.getClazz().getSimpleName()
+ ". ReferencedEntryEditorFactory arugment is null");
}
}
//TODO: handle non 'get' style method names eg 'isFoo()', 'hasBar()'
private void cacheGettersAndSetters() throws MappingException
{
propertyGetters = new HashMap<String, Method>();
propertySetters = new HashMap<String, Method>();
Class clazz = odm.getClazz();
for (String beanPropertyName : odm.beanPropertyNames())
{
try
{
Method getter = clazz.getMethod("get" + beanPropertyName);
propertyGetters.put(beanPropertyName, getter);
Method setter = clazz.getMethod("set" + beanPropertyName, getter.getReturnType());
propertySetters.put(beanPropertyName, setter);
}
catch (NoSuchMethodException e)
{
throw new MappingException("Error creating context mapper for class: "
+ clazz.getSimpleName()
+ ". " + clazz.getSimpleName() + " is missing a getter/setter for property: "
+ beanPropertyName, e);
}
}
}
private void mapReferencedEntries() throws MappingException
{
for (Method getter : propertyGetters.values())
{
Class<?> returnType = getter.getReturnType();
if (!ValidConversionType.isValidConversionType(returnType))
{
try
{
Class componentType = returnType.isArray() ? returnType.getComponentType() : returnType;
typeConverter.registerCustomEditor(componentType,
refEditorFactory.referencedEntryEditorForClass(componentType));
}
catch (ReferencedEntryEditorCreationException e)
{
throw new MappingException(odm.getClazz().getSimpleName() + "."
+ getter.getName()
+ "() has invalid return type. Return type must be another mapped "
+ "directory object, or one of the following: "
+ ValidConversionType.listTypes(), e);
}
}
}
}
private String namingAttributeValue(Object beanInstance)
{
Method method = propertyGetters.get(odm.beanPropertyNameFor(odm.getNamingAttribute()));
try
{
return (String) method.invoke(odm.getClazz().cast(beanInstance));
}
catch (Exception e)
{
throw new RuntimeException(e.getMessage(), e);
}
}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2006 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.util;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
public class AttributeWrapper
{
private Attribute attribute;
public AttributeWrapper(Attribute attribute)
{
this.attribute = attribute;
}
public Object getAllAsObject() throws NamingException
{
if (attribute.size() == 1)
{
return attribute.get(0);
}
else
{
Object[] o = new Object[attribute.size()];
for (int i = 0; i < attribute.size(); i++)
{
o[i] = attribute.get(i);
}
return o;
}
}
}

View File

@@ -27,7 +27,7 @@ public class NameEditorTest extends TestCase
}
}
public void testGetAsText()
public void testGetAsText()
{
NameEditor editor = new NameEditor(DistinguishedName.class);
DistinguishedName name = new DistinguishedName("uid=zzz, ou=people");

View File

@@ -0,0 +1,110 @@
/*
* Copyright 2005 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.attributetypes;
import junit.framework.TestCase;
import org.easymock.classextension.EasyMock;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.odm.mapping.MappingException;
import org.springframework.ldap.odm.mapping.ObjectDirectoryMapper;
import org.springframework.ldap.odm.mapping.ObjectDirectoryMapperFactory;
public class ReferencedEntryEditorFactoryTest extends TestCase
{
private LdapTemplate ldapTemplate;
private ObjectDirectoryMapperFactory odmFactory;
private ObjectDirectoryMapper odm;
private ReferencedEntryEditorFactory editorFactory;
protected void setUp() throws Exception
{
super.setUp();
ldapTemplate = EasyMock.createStrictMock(LdapTemplate.class);
odmFactory = EasyMock.createStrictMock(ObjectDirectoryMapperFactory.class);
odm = EasyMock.createStrictMock(ObjectDirectoryMapper.class);
editorFactory = new ReferencedEntryEditorFactory(ldapTemplate);
editorFactory.setObjectDirectoryMapperFactory(odmFactory);
}
/**
* Asserts that a referenced entry editor is created, after successful mapping of
* the referenced entity.
*
* @throws MappingException
* @throws ReferencedEntryEditorCreationException
*
*/
public void testReferencedEditorForClass_successfulMapping()
throws MappingException
{
EasyMock.expect(odmFactory.objectDirectoryMapperForClass(TestReferencedEntry.class)).andReturn(odm);
replayMocks();
try
{
editorFactory.referencedEntryEditorForClass(TestReferencedEntry.class);
//second call to return cached editor, verified by the fact that objectDirectoryMapperForClass
//is only called once.
editorFactory.referencedEntryEditorForClass(TestReferencedEntry.class);
}
catch (ReferencedEntryEditorCreationException e)
{
fail();
}
verifyMocks();
}
/**
* Asserts that an exception is thrown when the referenced entity cannot be mapped
*
* @throws org.springframework.ldap.odm.mapping.MappingException
*
* @throws ReferencedEntryEditorCreationException
*
*/
public void testReferencedEditorForClass_unsuccessfulMapping()
throws MappingException
{
ReferencedEntryEditorFactory editorFactory = new ReferencedEntryEditorFactory(ldapTemplate);
editorFactory.setObjectDirectoryMapperFactory(odmFactory);
EasyMock.expect(odmFactory.objectDirectoryMapperForClass(TestReferencedEntry.class))
.andThrow(new MappingException("unsuccessful mapping"));
replayMocks();
try
{
editorFactory.referencedEntryEditorForClass(TestReferencedEntry.class);
fail("Should've thrown exception");
}
catch (ReferencedEntryEditorCreationException e)
{
//expected behaviour
}
verifyMocks();
}
private void verifyMocks()
{
EasyMock.verify(ldapTemplate);
EasyMock.verify(odmFactory);
EasyMock.verify(odm);
}
private void replayMocks()
{
EasyMock.replay(ldapTemplate);
EasyMock.replay(odmFactory);
EasyMock.replay(odm);
}
}

View File

@@ -0,0 +1,104 @@
/*
* Copyright 2005 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.attributetypes;
import junit.framework.TestCase;
import org.easymock.classextension.EasyMock;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.odm.mapping.MappingException;
import org.springframework.ldap.odm.mapping.ObjectDirectoryMapper;
import javax.naming.InvalidNameException;
import javax.naming.ldap.LdapName;
public class ReferencedEntryEditorTest extends TestCase
{
private LdapTemplate ldapTemplate;
private ObjectDirectoryMapper objectDirectoryMapper;
private ReferencedEntryEditor editor;
protected void setUp() throws Exception
{
super.setUp();
ldapTemplate = EasyMock.createStrictMock(LdapTemplate.class);
objectDirectoryMapper = EasyMock.createStrictMock(ObjectDirectoryMapper.class);
editor = new ReferencedEntryEditor(ldapTemplate, objectDirectoryMapper);
}
public void testGetAsText() throws MappingException
{
TestReferencedEntry referencedEntry = new TestReferencedEntry();
editor.setValue(referencedEntry);
EasyMock.expect(objectDirectoryMapper.buildDn(referencedEntry))
.andReturn(new DistinguishedName("uid=referencedEntry, ou=foobars"));
replayMocks();
editor.getAsText();
verifyMocks();
}
public void testGetAsTextThrowsRuntimeExceptionWhenMappingExceptionOccurs()
throws MappingException
{
TestReferencedEntry referencedEntry = new TestReferencedEntry();
editor.setValue(referencedEntry);
EasyMock.expect(objectDirectoryMapper.buildDn(referencedEntry))
.andThrow(new MappingException("no mapper"));
replayMocks();
try
{
editor.getAsText();
fail("Should've propogated mapping exception.");
}
catch (RuntimeException e)
{
//expected
}
verifyMocks();
}
public void testSetAsText()
{
LdapName referenceName = null;
try
{
referenceName = new LdapName("uid = referencedEntry, ou=foobars");
}
catch (InvalidNameException e)
{
}
TestReferencedEntry referencedEntry = new TestReferencedEntry();
EasyMock.expect(ldapTemplate.lookup(referenceName, objectDirectoryMapper))
.andReturn(referencedEntry);
replayMocks();
editor.setAsText("uid = referencedEntry, ou=foobars");
verifyMocks();
}
private void verifyMocks()
{
EasyMock.verify(ldapTemplate);
EasyMock.verify(objectDirectoryMapper);
}
private void replayMocks()
{
EasyMock.replay(ldapTemplate);
EasyMock.replay(objectDirectoryMapper);
}
}

View File

@@ -0,0 +1,57 @@
/*
* Copyright 2005 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.attributetypes;
import org.springframework.ldap.odm.annotations.DirAttribute;
import org.springframework.ldap.odm.annotations.NamingAttribute;
import org.springframework.ldap.odm.annotations.NamingSuffix;
import org.springframework.ldap.odm.annotations.ObjectClasses;
@NamingAttribute("cn")
@NamingSuffix({"ou = people", "dc = example", "dc=com"})
@ObjectClasses({"top", "person", "organizationalPerson", "inetorgperson"})
public class TestReferencedEntry
{
@DirAttribute("cn")
private String name;
@DirAttribute("addr")
private String address;
@DirAttribute("mail")
private String mail;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
public String getMail()
{
return mail;
}
public void setMail(String mail)
{
this.mail = mail;
}
}

View File

@@ -9,12 +9,12 @@ package org.springframework.ldap.odm.attributetypes;
import junit.framework.TestCase;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.testng.Assert;
import org.springframework.ldap.core.DistinguishedName;
import org.testng.Assert;
import javax.naming.ldap.LdapName;
import java.util.List;
import java.util.Date;
import java.util.List;
public class ValidConversionTypeTest extends TestCase
{

View File

@@ -0,0 +1,174 @@
/*
* Copyright 2005 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.entity;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.odm.annotations.DirAttribute;
import org.springframework.ldap.odm.annotations.NamingAttribute;
import org.springframework.ldap.odm.annotations.NamingSuffix;
import org.springframework.ldap.odm.annotations.ObjectClasses;
import java.util.Date;
@NamingAttribute("uid")
@NamingSuffix({"ou=people"})
@ObjectClasses({"person", "organizationalPerson", "inetorgperson"})
public class UnitTestPerson
{
/**
* ******************************** Directory mapped fields ***********************************
*/
@DirAttribute("uid")
private String identifier;
@DirAttribute("sn")
private String lastName;
@DirAttribute("givenName")
private String givenName;
@DirAttribute("cn")
private String fullName;
@DirAttribute("mail")
private String emailAddress;
@DirAttribute("userpassword")
private byte[] password;
@DirAttribute()
private String[] description;
@DirAttribute("acceptemails")
private Boolean acceptEmails;
@DirAttribute("loginresettime")
private Date resetLogin;
@DirAttribute("failedlogins")
private Integer failedLogins;
@DirAttribute("creatorname")
private DistinguishedName creator;
/**
* ********************************************************************************************
*/
private String fred; //A non directory mapped field.
public String getIdentifier()
{
return identifier;
}
public void setIdentifier(String identifier)
{
this.identifier = identifier;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public String getGivenName()
{
return givenName;
}
public void setGivenName(String givenName)
{
this.givenName = givenName;
}
public String getFullName()
{
return fullName;
}
public void setFullName(String fullName)
{
this.fullName = fullName;
}
public String getEmailAddress()
{
return emailAddress;
}
public void setEmailAddress(String emailAddress)
{
this.emailAddress = emailAddress;
}
public byte[] getPassword()
{
return password;
}
public void setPassword(byte[] password)
{
this.password = password;
}
public String[] getDescription()
{
return description;
}
public void setDescription(String[] description)
{
this.description = description;
}
public Boolean getAcceptEmails()
{
return acceptEmails;
}
public void setAcceptEmails(Boolean acceptEmails)
{
this.acceptEmails = acceptEmails;
}
public Date getResetLogin()
{
return resetLogin;
}
public void setResetLogin(Date resetLogin)
{
this.resetLogin = resetLogin;
}
public Integer getFailedLogins()
{
return failedLogins;
}
public void setFailedLogins(Integer failedLogins)
{
this.failedLogins = failedLogins;
}
public DistinguishedName getCreator()
{
return creator;
}
public void setCreator(DistinguishedName creator)
{
this.creator = creator;
}
}

View File

@@ -0,0 +1,170 @@
/*
* Copyright 2005 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.entity;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.odm.annotations.DirAttribute;
import org.springframework.ldap.odm.annotations.NamingSuffix;
import org.springframework.ldap.odm.annotations.ObjectClasses;
import java.util.Date;
@NamingSuffix({"ou=people"})
@ObjectClasses({"person", "organizationalPerson", "inetorgperson"})
public class UnitTestPersonMissingNamingAttributeAnnotation
{
/**
* ******************************** Directory mapped fields ***********************************
*/
@DirAttribute("uid")
private String identifier;
@DirAttribute("sn")
private String lastName;
@DirAttribute("givenName")
private String givenName;
@DirAttribute("cn")
private String fullName;
@DirAttribute("mail")
private String emailAddress;
@DirAttribute("userpassword")
private byte[] password;
@DirAttribute
private String[] description;
@DirAttribute("acceptemails")
private Boolean acceptEmails;
@DirAttribute("loginresettime")
private Date resetLogin;
@DirAttribute("failedlogins")
private Integer failedLogins;
@DirAttribute("creatorname")
private DistinguishedName creator;
/**
* ********************************************************************************************
*/
public String getIdentifier()
{
return identifier;
}
public void setIdentifier(String identifier)
{
this.identifier = identifier;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public String getGivenName()
{
return givenName;
}
public void setGivenName(String givenName)
{
this.givenName = givenName;
}
public String getFullName()
{
return fullName;
}
public void setFullName(String fullName)
{
this.fullName = fullName;
}
public String getEmailAddress()
{
return emailAddress;
}
public void setEmailAddress(String emailAddress)
{
this.emailAddress = emailAddress;
}
public byte[] getPassword()
{
return password;
}
public void setPassword(byte[] password)
{
this.password = password;
}
public String[] getDescription()
{
return description;
}
public void setDescription(String[] description)
{
this.description = description;
}
public Boolean getAcceptEmails()
{
return acceptEmails;
}
public void setAcceptEmails(Boolean acceptEmails)
{
this.acceptEmails = acceptEmails;
}
public Date getResetLogin()
{
return resetLogin;
}
public void setResetLogin(Date resetLogin)
{
this.resetLogin = resetLogin;
}
public Integer getFailedLogins()
{
return failedLogins;
}
public void setFailedLogins(Integer failedLogins)
{
this.failedLogins = failedLogins;
}
public DistinguishedName getCreator()
{
return creator;
}
public void setCreator(DistinguishedName creator)
{
this.creator = creator;
}
}

View File

@@ -0,0 +1,169 @@
/*
* Copyright 2005 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.entity;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.odm.annotations.DirAttribute;
import org.springframework.ldap.odm.annotations.NamingAttribute;
import org.springframework.ldap.odm.annotations.ObjectClasses;
import java.util.Date;
@NamingAttribute("uid")
@ObjectClasses({"person", "organizationalPerson", "inetorgperson"})
public class UnitTestPersonMissingNamingSuffixAnnotation
{
/**
* ******************************** Directory mapped fields ***********************************
*/
@DirAttribute("uid")
private String identifier;
@DirAttribute("sn")
private String lastName;
@DirAttribute("givenName")
private String givenName;
@DirAttribute("cn")
private String fullName;
@DirAttribute("mail")
private String emailAddress;
@DirAttribute("userpassword")
private byte[] password;
@DirAttribute
private String[] description;
@DirAttribute("acceptemails")
private Boolean acceptEmails;
@DirAttribute("loginresettime")
private Date resetLogin;
@DirAttribute("failedlogins")
private Integer failedLogins;
@DirAttribute("creatorname")
private DistinguishedName creator;
/**
* ********************************************************************************************
*/
public String getIdentifier()
{
return identifier;
}
public void setIdentifier(String identifier)
{
this.identifier = identifier;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public String getGivenName()
{
return givenName;
}
public void setGivenName(String givenName)
{
this.givenName = givenName;
}
public String getFullName()
{
return fullName;
}
public void setFullName(String fullName)
{
this.fullName = fullName;
}
public String getEmailAddress()
{
return emailAddress;
}
public void setEmailAddress(String emailAddress)
{
this.emailAddress = emailAddress;
}
public byte[] getPassword()
{
return password;
}
public void setPassword(byte[] password)
{
this.password = password;
}
public String[] getDescription()
{
return description;
}
public void setDescription(String[] description)
{
this.description = description;
}
public Boolean getAcceptEmails()
{
return acceptEmails;
}
public void setAcceptEmails(Boolean acceptEmails)
{
this.acceptEmails = acceptEmails;
}
public Date getResetLogin()
{
return resetLogin;
}
public void setResetLogin(Date resetLogin)
{
this.resetLogin = resetLogin;
}
public Integer getFailedLogins()
{
return failedLogins;
}
public void setFailedLogins(Integer failedLogins)
{
this.failedLogins = failedLogins;
}
public DistinguishedName getCreator()
{
return creator;
}
public void setCreator(DistinguishedName creator)
{
this.creator = creator;
}
}

View File

@@ -0,0 +1,168 @@
/*
* Copyright 2005 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.entity;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.odm.annotations.DirAttribute;
import org.springframework.ldap.odm.annotations.NamingAttribute;
import org.springframework.ldap.odm.annotations.NamingSuffix;
import java.util.Date;
@NamingAttribute("uid")
@NamingSuffix({"ou=people"})
public class UnitTestPersonMissingObjectClassesAnnotation
{
/**
* ******************************** Directory mapped fields ***********************************
*/
@DirAttribute("uid")
private String identifier;
@DirAttribute("sn")
private String lastName;
@DirAttribute("givenName")
private String givenName;
@DirAttribute("cn")
private String fullName;
@DirAttribute("mail")
private String emailAddress;
@DirAttribute("userpassword")
private byte[] password;
@DirAttribute
private String[] description;
@DirAttribute("acceptemails")
private Boolean acceptEmails;
@DirAttribute("loginresettime")
private Date resetLogin;
@DirAttribute("failedlogins")
private Integer failedLogins;
@DirAttribute("creatorname")
private DistinguishedName creator;
/**
* ********************************************************************************************
*/
public String getIdentifier()
{
return identifier;
}
public void setIdentifier(String identifier)
{
this.identifier = identifier;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public String getGivenName()
{
return givenName;
}
public void setGivenName(String givenName)
{
this.givenName = givenName;
}
public String getFullName()
{
return fullName;
}
public void setFullName(String fullName)
{
this.fullName = fullName;
}
public String getEmailAddress()
{
return emailAddress;
}
public void setEmailAddress(String emailAddress)
{
this.emailAddress = emailAddress;
}
public byte[] getPassword()
{
return password;
}
public void setPassword(byte[] password)
{
this.password = password;
}
public String[] getDescription()
{
return description;
}
public void setDescription(String[] description)
{
this.description = description;
}
public Boolean getAcceptEmails()
{
return acceptEmails;
}
public void setAcceptEmails(Boolean acceptEmails)
{
this.acceptEmails = acceptEmails;
}
public Date getResetLogin()
{
return resetLogin;
}
public void setResetLogin(Date resetLogin)
{
this.resetLogin = resetLogin;
}
public Integer getFailedLogins()
{
return failedLogins;
}
public void setFailedLogins(Integer failedLogins)
{
this.failedLogins = failedLogins;
}
public DistinguishedName getCreator()
{
return creator;
}
public void setCreator(DistinguishedName creator)
{
this.creator = creator;
}
}

View File

@@ -0,0 +1,172 @@
/*
* Copyright 2005 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.entity;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.odm.annotations.DirAttribute;
import org.springframework.ldap.odm.annotations.NamingAttribute;
import org.springframework.ldap.odm.annotations.NamingSuffix;
import org.springframework.ldap.odm.annotations.ObjectClasses;
import java.util.Date;
@NamingAttribute("uid")
@NamingSuffix({"ou=people, dc=example, dc=com"})
//Should be declared as separate elements
@ObjectClasses({"person", "organizationalPerson", "inetorgperson"})
public class UnitTestPersonSyntaxError1InNamingSuffix
{
/**
* ******************************** Directory mapped fields ***********************************
*/
@DirAttribute("uid")
private String identifier;
@DirAttribute("sn")
private String lastName;
@DirAttribute("givenName")
private String givenName;
@DirAttribute("cn")
private String fullName;
@DirAttribute("mail")
private String emailAddress;
@DirAttribute("userpassword")
private byte[] password;
@DirAttribute
private String[] description;
@DirAttribute("acceptemails")
private Boolean acceptEmails;
@DirAttribute("loginresettime")
private Date resetLogin;
@DirAttribute("failedlogins")
private Integer failedLogins;
@DirAttribute("creatorname")
private DistinguishedName creator;
/**
* ********************************************************************************************
*/
public String getIdentifier()
{
return identifier;
}
public void setIdentifier(String identifier)
{
this.identifier = identifier;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public String getGivenName()
{
return givenName;
}
public void setGivenName(String givenName)
{
this.givenName = givenName;
}
public String getFullName()
{
return fullName;
}
public void setFullName(String fullName)
{
this.fullName = fullName;
}
public String getEmailAddress()
{
return emailAddress;
}
public void setEmailAddress(String emailAddress)
{
this.emailAddress = emailAddress;
}
public byte[] getPassword()
{
return password;
}
public void setPassword(byte[] password)
{
this.password = password;
}
public String[] getDescription()
{
return description;
}
public void setDescription(String[] description)
{
this.description = description;
}
public Boolean getAcceptEmails()
{
return acceptEmails;
}
public void setAcceptEmails(Boolean acceptEmails)
{
this.acceptEmails = acceptEmails;
}
public Date getResetLogin()
{
return resetLogin;
}
public void setResetLogin(Date resetLogin)
{
this.resetLogin = resetLogin;
}
public Integer getFailedLogins()
{
return failedLogins;
}
public void setFailedLogins(Integer failedLogins)
{
this.failedLogins = failedLogins;
}
public DistinguishedName getCreator()
{
return creator;
}
public void setCreator(DistinguishedName creator)
{
this.creator = creator;
}
}

View File

@@ -0,0 +1,171 @@
/*
* Copyright 2005 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.entity;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.odm.annotations.DirAttribute;
import org.springframework.ldap.odm.annotations.NamingAttribute;
import org.springframework.ldap.odm.annotations.NamingSuffix;
import org.springframework.ldap.odm.annotations.ObjectClasses;
import java.util.Date;
@NamingAttribute("uid")
@NamingSuffix({"ou#Dpeople"})
@ObjectClasses({"person", "organizationalPerson", "inetorgperson"})
public class UnitTestPersonSyntaxError2InNamingSuffix
{
/**
* ******************************** Directory mapped fields ***********************************
*/
@DirAttribute("uid")
private String identifier;
@DirAttribute("sn")
private String lastName;
@DirAttribute("givenName")
private String givenName;
@DirAttribute("cn")
private String fullName;
@DirAttribute("mail")
private String emailAddress;
@DirAttribute("userpassword")
private byte[] password;
@DirAttribute
private String[] description;
@DirAttribute("acceptemails")
private Boolean acceptEmails;
@DirAttribute("loginresettime")
private Date resetLogin;
@DirAttribute("failedlogins")
private Integer failedLogins;
@DirAttribute("creatorname")
private DistinguishedName creator;
/**
* ********************************************************************************************
*/
public String getIdentifier()
{
return identifier;
}
public void setIdentifier(String identifier)
{
this.identifier = identifier;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public String getGivenName()
{
return givenName;
}
public void setGivenName(String givenName)
{
this.givenName = givenName;
}
public String getFullName()
{
return fullName;
}
public void setFullName(String fullName)
{
this.fullName = fullName;
}
public String getEmailAddress()
{
return emailAddress;
}
public void setEmailAddress(String emailAddress)
{
this.emailAddress = emailAddress;
}
public byte[] getPassword()
{
return password;
}
public void setPassword(byte[] password)
{
this.password = password;
}
public String[] getDescription()
{
return description;
}
public void setDescription(String[] description)
{
this.description = description;
}
public Boolean getAcceptEmails()
{
return acceptEmails;
}
public void setAcceptEmails(Boolean acceptEmails)
{
this.acceptEmails = acceptEmails;
}
public Date getResetLogin()
{
return resetLogin;
}
public void setResetLogin(Date resetLogin)
{
this.resetLogin = resetLogin;
}
public Integer getFailedLogins()
{
return failedLogins;
}
public void setFailedLogins(Integer failedLogins)
{
this.failedLogins = failedLogins;
}
public DistinguishedName getCreator()
{
return creator;
}
public void setCreator(DistinguishedName creator)
{
this.creator = creator;
}
}

View File

@@ -0,0 +1,171 @@
/*
* Copyright 2005 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.entity;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.odm.annotations.DirAttribute;
import org.springframework.ldap.odm.annotations.NamingAttribute;
import org.springframework.ldap.odm.annotations.NamingSuffix;
import org.springframework.ldap.odm.annotations.ObjectClasses;
import java.util.Date;
@NamingAttribute("uid")
@NamingSuffix({"ou=people"})
@ObjectClasses({"person, organizationalPerson, inetorgperson"})
public class UnitTestPersonSyntaxErrorInObjectClasses
{
/**
* ******************************** Directory mapped fields ***********************************
*/
@DirAttribute("uid")
private String identifier;
@DirAttribute("sn")
private String lastName;
@DirAttribute("givenName")
private String givenName;
@DirAttribute("cn")
private String fullName;
@DirAttribute("mail")
private String emailAddress;
@DirAttribute("userpassword")
private byte[] password;
@DirAttribute
private String[] description;
@DirAttribute("acceptemails")
private Boolean acceptEmails;
@DirAttribute("loginresettime")
private Date resetLogin;
@DirAttribute("failedlogins")
private Integer failedLogins;
@DirAttribute("creatorname")
private DistinguishedName creator;
/**
* ********************************************************************************************
*/
public String getIdentifier()
{
return identifier;
}
public void setIdentifier(String identifier)
{
this.identifier = identifier;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public String getGivenName()
{
return givenName;
}
public void setGivenName(String givenName)
{
this.givenName = givenName;
}
public String getFullName()
{
return fullName;
}
public void setFullName(String fullName)
{
this.fullName = fullName;
}
public String getEmailAddress()
{
return emailAddress;
}
public void setEmailAddress(String emailAddress)
{
this.emailAddress = emailAddress;
}
public byte[] getPassword()
{
return password;
}
public void setPassword(byte[] password)
{
this.password = password;
}
public String[] getDescription()
{
return description;
}
public void setDescription(String[] description)
{
this.description = description;
}
public Boolean getAcceptEmails()
{
return acceptEmails;
}
public void setAcceptEmails(Boolean acceptEmails)
{
this.acceptEmails = acceptEmails;
}
public Date getResetLogin()
{
return resetLogin;
}
public void setResetLogin(Date resetLogin)
{
this.resetLogin = resetLogin;
}
public Integer getFailedLogins()
{
return failedLogins;
}
public void setFailedLogins(Integer failedLogins)
{
this.failedLogins = failedLogins;
}
public DistinguishedName getCreator()
{
return creator;
}
public void setCreator(DistinguishedName creator)
{
this.creator = creator;
}
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright 2005 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.entity;
import org.springframework.ldap.odm.annotations.DirAttribute;
import org.springframework.ldap.odm.annotations.NamingAttribute;
import org.springframework.ldap.odm.annotations.NamingSuffix;
import org.springframework.ldap.odm.annotations.ObjectClasses;
@NamingAttribute("cn")
@NamingSuffix({"ou=roles"})
@ObjectClasses({"top", "organizationalRole"})
public class UnitTestRole
{
@DirAttribute("cn")
private String roleName;
@DirAttribute
private String description;
@DirAttribute("roleOccupant")
private UnitTestPerson[] members;
public String getRoleName()
{
return roleName;
}
public void setRoleName(String roleName)
{
this.roleName = roleName;
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
public UnitTestPerson[] getMembers()
{
return members;
}
public void setMembers(UnitTestPerson[] members)
{
this.members = members;
}
}

View File

@@ -0,0 +1,77 @@
/*
* Copyright 2005 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.mapping;
import junit.framework.Assert;
import junit.framework.TestCase;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.odm.entity.UnitTestPerson;
import java.util.Arrays;
import java.util.Set;
public abstract class AbstractObjectDirectoryMapTest extends TestCase
{
protected ObjectDirectoryMap odm;
public void testAttributeNameFor()
{
Assert.assertEquals(odm.attributeNameFor("Identifier"), "uid");
}
public void testThrowsExceptionWhenClassArgumentIsNull() throws MappingException
{
try
{
//Exercise code in the abstract base using something instantiable
odm = new AnnotationObjectDirectoryMap(null);
fail("Should've thrown exception");
}
catch (IllegalArgumentException e)
{
//Expected behaviour
}
}
public void testAttributeNames()
{
Set<String> attributes = odm.attributeNames();
Assert.assertTrue(attributes.size() == 11);
}
public void testbeanPropertyNameFor()
{
Assert.assertEquals(odm.beanPropertyNameFor("uid"), "Identifier");
}
public void testBeanPropertyNames()
{
Set<String> properties = odm.beanPropertyNames();
Assert.assertTrue(properties.size() == 11);
}
public void testGetClazz()
{
Assert.assertEquals(odm.getClazz(), UnitTestPerson.class);
}
public void testGetNamingAttribute()
{
Assert.assertEquals(odm.getNamingAttribute(), "uid");
}
public void testGetNamingSuffix()
{
Assert.assertEquals(odm.getNamingSuffix(), new DistinguishedName("ou=people"));
}
public void testGetObjectClasses()
{
Assert.assertTrue(Arrays.equals(odm.getObjectClasses(),
new String[]{"person", "organizationalPerson", "inetorgperson"}));
}
}

View File

@@ -0,0 +1,95 @@
/*
* Copyright 2005 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.mapping;
import junit.framework.Assert;
import org.springframework.ldap.odm.entity.*;
public class AnnotationObjectDirectoryMapTest extends AbstractObjectDirectoryMapTest
{
protected void setUp() throws Exception
{
super.setUp();
odm = new AnnotationObjectDirectoryMap(UnitTestPerson.class);
}
public void testThrowsExceptionWhenNamingAttributeAnnotationMissing()
{
try
{
odm = new AnnotationObjectDirectoryMap(UnitTestPersonMissingNamingAttributeAnnotation.class);
fail("Should've thrown exception");
}
catch (MappingException e)
{
Assert.assertTrue(e.getMessage().contains("The @NamingAttribute annotation is required."));
}
}
public void testThrowsExceptionWhenObjectClassesAnnotationMissing()
{
try
{
odm = new AnnotationObjectDirectoryMap(UnitTestPersonMissingObjectClassesAnnotation.class);
fail("Should've thrown exception");
}
catch (MappingException e)
{
Assert.assertTrue(e.getMessage().contains("The @ObjectClasses annotation is required."));
}
}
public void testThrowsExceptionWhenObjectClassesAnnotationHasSyntaxError()
{
try
{
odm = new AnnotationObjectDirectoryMap(UnitTestPersonSyntaxErrorInObjectClasses.class);
fail("Should've thrown exception");
}
catch (MappingException e)
{
Assert.assertTrue(e.getMessage().contains("Object classes must be defined as separate elements"));
}
}
public void testThrowsExceptionWhenNamingSuffixAnnotationIsMissing()
{
try
{
odm = new AnnotationObjectDirectoryMap(UnitTestPersonMissingNamingSuffixAnnotation.class);
fail("Should've thrown exception");
}
catch (MappingException e)
{
Assert.assertTrue(e.getMessage().contains("The @NamingSuffix annotation is required."));
}
}
public void testThrowsExceptionWhenNamingSuffixAnnotationHasSyntaxError()
{
try
{
odm = new AnnotationObjectDirectoryMap(UnitTestPersonSyntaxError1InNamingSuffix.class);
fail("Should've thrown exception");
}
catch (MappingException e)
{
Assert.assertTrue(e.getMessage().contains("Naming suffix elements must be declared separately"));
}
try
{
odm = new AnnotationObjectDirectoryMap(UnitTestPersonSyntaxError2InNamingSuffix.class);
fail("Should've thrown exception");
}
catch (MappingException e)
{
Assert.assertTrue(e.getMessage().contains("Syntax error in naming suffix"));
}
}
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2005 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.mapping;
import junit.framework.TestCase;
import org.easymock.classextension.EasyMock;
import org.springframework.ldap.odm.attributetypes.LdapTypeConverter;
import org.springframework.ldap.odm.attributetypes.ReferencedEntryEditorFactory;
import org.springframework.ldap.odm.entity.UnitTestPerson;
public class ObjectDirectoryMapperFactoryTest extends TestCase
{
private ObjectDirectoryMapperFactory odmFactory;
private LdapTypeConverter typeConverter;
private ReferencedEntryEditorFactory refEditorFactory;
protected void setUp() throws Exception
{
super.setUp();
typeConverter = EasyMock.createStrictMock(LdapTypeConverter.class);
refEditorFactory = EasyMock.createStrictMock(ReferencedEntryEditorFactory.class);
odmFactory = new ObjectDirectoryMapperFactory(typeConverter, refEditorFactory);
}
public void testObjectDirectoryMapperForClass() throws MappingException
{
replayMocks();
odmFactory.objectDirectoryMapperForClass(UnitTestPerson.class);
//second call to retrieve mapper from cache
odmFactory.objectDirectoryMapperForClass(UnitTestPerson.class);
}
private void replayMocks()
{
EasyMock.replay(typeConverter);
EasyMock.replay(refEditorFactory);
}
}

View File

@@ -0,0 +1,205 @@
/*
* Copyright 2005 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.mapping;
import junit.framework.Assert;
import junit.framework.TestCase;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.easymock.classextension.EasyMock;
import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.odm.attributetypes.LdapTypeConverter;
import org.springframework.ldap.odm.attributetypes.ReferencedEntryEditorFactory;
import org.springframework.ldap.odm.entity.UnitTestPerson;
import javax.naming.Name;
import java.util.Date;
public class ObjectDirectoryMapperImplTest extends TestCase
{
private static final Log LOGGER = LogFactory.getLog(ObjectDirectoryMapperImplTest.class);
private LdapTypeConverter typeConverter;
private ReferencedEntryEditorFactory refEditorFactory;
private AnnotationObjectDirectoryMap objectDirectoryMap;
protected void setUp() throws Exception
{
super.setUp();
this.typeConverter = new LdapTypeConverter();
this.refEditorFactory = EasyMock.createMock(ReferencedEntryEditorFactory.class);
this.objectDirectoryMap = new AnnotationObjectDirectoryMap(UnitTestPerson.class);
}
public void testThrowsIllegalArgumentExceptionWhenNotConfigured() throws MappingException
{
try
{
ObjectDirectoryMapperImpl mapper =
new ObjectDirectoryMapperImpl(null, typeConverter, refEditorFactory);
fail("Should've thrown exception.");
}
catch (IllegalArgumentException e)
{
e.printStackTrace();
}
try
{
ObjectDirectoryMapperImpl mapper =
new ObjectDirectoryMapperImpl(objectDirectoryMap, null, refEditorFactory);
fail("Should've thrown exception.");
}
catch (IllegalArgumentException e)
{
e.printStackTrace();
}
try
{
ObjectDirectoryMapperImpl mapper =
new ObjectDirectoryMapperImpl(objectDirectoryMap, typeConverter, null);
fail("Should've thrown exception.");
}
catch (IllegalArgumentException e)
{
e.printStackTrace();
}
}
public void testBuildDn()
{
try
{
ObjectDirectoryMapperImpl mapper =
new ObjectDirectoryMapperImpl(objectDirectoryMap, typeConverter, refEditorFactory);
UnitTestPerson person = new UnitTestPerson();
person.setIdentifier("x332");
person.setFullName("Mr Bean");
person.setEmailAddress("mrbean@bean.com");
person.setDescription(new String[]{"The quick brown fox jumped",
"over the lazy dog"});
Name dn = mapper.buildDn(person);
Assert.assertEquals("uid=x332, ou=people", dn.toString());
}
catch (MappingException e)
{
LOGGER.debug(e.getMessage());
Assert.fail(e.getMessage());
}
}
public void testBuildDnThrowsMappingExceptionWhenArgumentIsNull()
{
try
{
ObjectDirectoryMapperImpl mapper =
new ObjectDirectoryMapperImpl(objectDirectoryMap, typeConverter, refEditorFactory);
UnitTestPerson person = null;
mapper.buildDn(person);
fail("Should've thrown mapping exception - argument is null");
}
catch (MappingException e)
{
Assert.assertEquals(e.getMessage(), "Can't build Dn from beanInstance, beanInstance is null");
}
try
{
ObjectDirectoryMapperImpl mapper =
new ObjectDirectoryMapperImpl(objectDirectoryMap, typeConverter, refEditorFactory);
String namingAttributeValue = null;
mapper.buildDn(namingAttributeValue);
fail("Should've thrown mapping exception - argument is null");
}
catch (MappingException e)
{
Assert.assertEquals(e.getMessage(), "Can't build Dn from namingAttributeValue, namingAttributeValue is null");
}
}
public void testMapToContext()
{
try
{
ObjectDirectoryMapperImpl mapper =
new ObjectDirectoryMapperImpl(objectDirectoryMap, typeConverter, refEditorFactory);
UnitTestPerson entity = new UnitTestPerson();
entity.setIdentifier("x232");
entity.setFullName("Mr Bean");
entity.setEmailAddress("bean@bean.com");
entity.setPassword("fred".getBytes());
entity.setAcceptEmails(false);
entity.setResetLogin(new Date(1L));
entity.setCreator(new DistinguishedName(
"uid=amAdmin,ou=people,dc=myretsu,dc=com"));
entity.setFailedLogins(3);
replayMocks();
DirContextAdapter ctxAdapter = new DirContextAdapter();
mapper.mapToContext(entity, ctxAdapter);
Assert.assertEquals("x232", ctxAdapter.getStringAttribute("uid"));
Assert.assertEquals("Mr Bean", ctxAdapter.getStringAttribute("cn"));
Assert.assertEquals("bean@bean.com", ctxAdapter.getStringAttribute("mail"));
Assert.assertEquals("fred", new String((byte[]) ctxAdapter.getObjectAttribute("userpassword")));
Assert.assertEquals("false", ctxAdapter.getStringAttribute("acceptemails"));
Assert.assertEquals("19700101100000.1", ctxAdapter.getStringAttribute("loginresettime"));
Assert.assertEquals("3", ctxAdapter.getStringAttribute("failedlogins"));
Assert.assertEquals("uid=amAdmin, ou=people, dc=myretsu, dc=com",
ctxAdapter.getStringAttribute("creatorname"));
verifyMocks();
}
catch (MappingException e)
{
LOGGER.debug(e.getMessage());
Assert.fail(e.getMessage());
}
}
public void testMapFromContext() throws MappingException
{
ObjectDirectoryMapperImpl mapper =
new ObjectDirectoryMapperImpl(objectDirectoryMap, typeConverter, refEditorFactory);
DirContextAdapter ctxAdapter = new DirContextAdapter();
ctxAdapter.setAttributeValue("acceptemails", "false");
ctxAdapter.setAttributeValue("creatorname", "uid=admin, ou=people");
ctxAdapter.setAttributeValue("mail", "person@person.com");
replayMocks();
UnitTestPerson person = (UnitTestPerson) mapper.mapFromContext(ctxAdapter);
Assert.assertEquals(person.getAcceptEmails(), Boolean.FALSE);
Assert.assertEquals(person.getCreator(), new DistinguishedName("uid=admin, ou=people"));
Assert.assertEquals(person.getEmailAddress(), "person@person.com");
verifyMocks();
}
public void testGetObjectDirectoryMap() throws MappingException
{
ObjectDirectoryMapperImpl mapper =
new ObjectDirectoryMapperImpl(objectDirectoryMap, typeConverter, refEditorFactory);
Assert.assertNotNull(mapper.getObjectDirectoryMap());
}
private void verifyMocks()
{
EasyMock.verify(refEditorFactory);
}
private void replayMocks()
{
EasyMock.replay(refEditorFactory);
}
}

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2005 by Majitek. All Rights Reserved.
*
* This software is the proprietary information of Majitek. Use is subject to license terms.
*/
package org.springframework.ldap.odm.util;
import junit.framework.Assert;
import junit.framework.TestCase;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.BasicAttribute;
public class AttributeWrapperTest extends TestCase
{
public void testGetAllAsObject()
{
Attribute multiValueAttribute = new BasicAttribute("multiValueAttribute");
multiValueAttribute.add("value1");
multiValueAttribute.add("value2");
multiValueAttribute.add("value3");
AttributeWrapper attributeWrapper = new AttributeWrapper(multiValueAttribute);
try
{
Object[] all = (Object[]) attributeWrapper.getAllAsObject();
Assert.assertEquals(all[0], "value1");
Assert.assertEquals(all[1], "value2");
Assert.assertEquals(all[2], "value3");
}
catch (NamingException e)
{
Assert.fail();
}
Attribute singleValueAttribute = new BasicAttribute("attribute", "value");
attributeWrapper = new AttributeWrapper(singleValueAttribute);
try
{
Assert.assertEquals(attributeWrapper.getAllAsObject(), "value");
}
catch (NamingException e)
{
Assert.fail();
}
}
}