diff --git a/spring-ldap-odm/.springBeans b/spring-ldap-odm/.springBeans index dbf77410..09cf3198 100644 --- a/spring-ldap-odm/.springBeans +++ b/spring-ldap-odm/.springBeans @@ -4,7 +4,6 @@ xml - src/itest/java/conf/beans.xml @@ -12,7 +11,6 @@ true false - src/itest/java/conf/beans.xml diff --git a/spring-ldap-odm/src/itest/java/conf/apacheDsContext.xml b/spring-ldap-odm/src/itest/java/conf/apacheDsContext.xml deleted file mode 100644 index 80910e7e..00000000 --- a/spring-ldap-odm/src/itest/java/conf/apacheDsContext.xml +++ /dev/null @@ -1,84 +0,0 @@ - - - - - - - - - - simple - - - ${userDn} - - - ${password} - - - - - - - - - - - - - - - - - - - - - objectClass: top - objectClass: domain - objectClass: extensibleObject - dc: example - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/spring-ldap-odm/src/itest/java/conf/beans.xml b/spring-ldap-odm/src/itest/java/conf/beans.xml deleted file mode 100644 index 057ad706..00000000 --- a/spring-ldap-odm/src/itest/java/conf/beans.xml +++ /dev/null @@ -1,43 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/spring-ldap-odm/src/itest/java/conf/ldap.properties b/spring-ldap-odm/src/itest/java/conf/ldap.properties deleted file mode 100644 index a0bbef1a..00000000 --- a/spring-ldap-odm/src/itest/java/conf/ldap.properties +++ /dev/null @@ -1,4 +0,0 @@ -urls=ldap://127.0.0.1:3900 -userDn=uid=admin,ou=system -password=secret -base=dc=example,dc=com diff --git a/spring-ldap-odm/src/itest/java/conf/log4j.xml b/spring-ldap-odm/src/itest/java/conf/log4j.xml deleted file mode 100644 index 23d78f02..00000000 --- a/spring-ldap-odm/src/itest/java/conf/log4j.xml +++ /dev/null @@ -1,50 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/spring-ldap-odm/src/itest/java/conf/setup_data.ldif b/spring-ldap-odm/src/itest/java/conf/setup_data.ldif deleted file mode 100644 index 560cffb9..00000000 --- a/spring-ldap-odm/src/itest/java/conf/setup_data.ldif +++ /dev/null @@ -1,19 +0,0 @@ -dn: ou=people,dc=example,dc=com -objectclass: top -objectclass: organizationalUnit -ou: people - -dn: ou=roles,dc=example,dc=com -objectclass: top -objectclass: organizationalUnit -ou: roles - -dn: cn=webUser,ou=roles,dc=example,dc=com -objectclass: top -objectclass: organizationalRole -cn: webUser -description: Everyday site visitor - - - - diff --git a/spring-ldap-odm/src/itest/java/conf/teardown_data.ldif b/spring-ldap-odm/src/itest/java/conf/teardown_data.ldif deleted file mode 100644 index 16f41052..00000000 --- a/spring-ldap-odm/src/itest/java/conf/teardown_data.ldif +++ /dev/null @@ -1,9 +0,0 @@ -dn: ou=people,dc=example,dc=com -objectclass: top -objectclass: organizationalUnit -ou: people - -dn: ou=roles,dc=example,dc=com -objectclass: top -objectclass: organizationalUnit -ou: roles \ No newline at end of file diff --git a/spring-ldap-odm/src/itest/java/org/springframework/ldap/odm/contextmapping/AnnotatedClassContextMapperTest.java b/spring-ldap-odm/src/itest/java/org/springframework/ldap/odm/contextmapping/AnnotatedClassContextMapperTest.java deleted file mode 100644 index 16932b56..00000000 --- a/spring-ldap-odm/src/itest/java/org/springframework/ldap/odm/contextmapping/AnnotatedClassContextMapperTest.java +++ /dev/null @@ -1,349 +0,0 @@ -/* - * 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.contextmapping; - -import java.util.Date; -import java.util.Map; - -import javax.naming.Name; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.springframework.ldap.core.DirContextAdapter; -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.ldap.odm.attributetypes.LdapTypeConverter; -import org.springframework.ldap.odm.attributetypes.ReferencedEntryEditorFactory; -import org.springframework.ldap.odm.contextmapping.exception.ContextMapperException; -import org.springframework.ldap.odm.entity.MockEntity; -import org.springframework.test.AbstractDependencyInjectionSpringContextTests; -import org.testng.Assert; -import org.testng.annotations.Test; - -public class AnnotatedClassContextMapperTest extends - AbstractDependencyInjectionSpringContextTests { - private static final Log LOGGER = LogFactory - .getLog(AnnotatedClassContextMapperTest.class); - - private LdapTypeConverter typeConverter; - - private ReferencedEntryEditorFactory refEditorFactory; - - public void setRefEditorFactory( - ReferencedEntryEditorFactory refEditorFactory) { - this.refEditorFactory = refEditorFactory; - } - - public void setTypeConverter(LdapTypeConverter typeConverter) { - this.typeConverter = typeConverter; - } - - @Override - protected String[] getConfigLocations() { - return new String[] { "conf/beans.xml" }; - } - - public void testMapsDirectoryAttributesToBeanProperties() { - try { - AnnotatedClassContextMapper contextMapper = new AnnotatedClassContextMapper( - MockEntity.class, typeConverter, refEditorFactory); - ContextMap contextMap = contextMapper.getContextMap(); - Assert.assertEquals("cn", contextMap.attributeNameFor("Name"), - "Name mapping not as expected"); - Assert.assertEquals("mail", contextMap.attributeNameFor("Mail"), - "Mail mapping not as expected"); - Assert.assertEquals("addr", contextMap.attributeNameFor("Address"), - "Address mapping not as expected"); - Assert.assertEquals("userpassword", contextMap - .attributeNameFor("Password"), - "Password mapping not as expected"); - Assert.assertEquals("desc", contextMap - .attributeNameFor("Description"), - "Description mapping not as expected"); - } catch (ContextMapperException e) { - LOGGER.debug(e.getMessage()); - Assert.fail(e.getMessage(), e); - } - } - - public void testReturnsCorrectNamingAttribute() { - try { - AnnotatedClassContextMapper contextMapper = new AnnotatedClassContextMapper( - MockEntity.class, typeConverter, refEditorFactory); - Assert.assertEquals("cn", contextMapper.getNamingAttribute(), - "Context mapper naming attribute not as expected"); - - } catch (ContextMapperException e) { - LOGGER.debug(e.getMessage()); - Assert.fail(e.getMessage(), e); - } - } - - public void testReturnsCorrectObjectClasses() { - try { - AnnotatedClassContextMapper contextMapper = new AnnotatedClassContextMapper( - MockEntity.class, typeConverter, refEditorFactory); - Assert.assertEquals(new String[] { "top", "person", - "organizationalPerson", "inetorgperson" }, contextMapper - .getObjectClasses(), - "Context mapper naming attribute not as expected"); - - } catch (ContextMapperException e) { - LOGGER.debug(e.getMessage()); - Assert.fail(e.getMessage()); - } - } - - public void testBuildDn() { - try { - AnnotatedClassContextMapper contextMapper = new AnnotatedClassContextMapper( - MockEntity.class, typeConverter, refEditorFactory); - MockEntity entity = new MockEntity(); - entity.setName("Mr Bean"); - entity.setAddress("Somewhere in england"); - entity.setDescription(new String[] { "The quick brown fox jumped", - "over the lazy dog" }); - Name dn = contextMapper.buildDn(entity); - Assert.assertEquals("cn=Mr Bean, ou=people, dc=example, dc=com", dn - .toString(), "dn not as expected"); - - } catch (ContextMapperException e) { - LOGGER.debug(e.getMessage()); - Assert.fail(e.getMessage()); - } - } - - public void testMapToContext() { - try { - AnnotatedClassContextMapper contextMapper = new AnnotatedClassContextMapper( - MockEntity.class, typeConverter, refEditorFactory); - MockEntity entity = new MockEntity(); - entity.setName("Mr Bean"); - entity.setMail("bean@bean.com"); - entity.setAddress("Somewhere in england"); - entity.setPassword("fred".getBytes()); - entity.setDescription(new String[] { "The quick brown fox jumped", - "over the lazy dog" }); - entity.setAcceptEmails(false); - entity.setResetLogin(new Date(1L)); - entity.setCreator(new DistinguishedName( - "uid=amAdmin,ou=people,dc=myretsu,dc=com")); - entity.setFailedLogins(3); - DirContextAdapter ctxAdapter = new DirContextAdapter(); - contextMapper.mapToContext(entity, ctxAdapter); - - Assert.assertEquals("Mr Bean", ctxAdapter.getStringAttribute("cn"), - "Name mapping not as expected"); - Assert - .assertEquals("bean@bean.com", ctxAdapter - .getStringAttribute("mail"), - "Mail mapping not as expected"); - Assert.assertEquals("Somewhere in england", ctxAdapter - .getObjectAttribute("addr"), - "Address mapping not as expected"); - Assert.assertEquals("fred", new String((byte[]) ctxAdapter - .getObjectAttribute("userpassword")), - "Password mapping not as expected"); - Assert.assertEquals("The quick brown fox jumped", ctxAdapter - .getStringAttributes("desc")[0], - "Description mapping not as expected"); - Assert.assertEquals("false", ctxAdapter - .getStringAttribute("acceptemails"), - "AcceptEmails mapping not as expected"); - Assert.assertEquals("19700101100000.1", ctxAdapter - .getStringAttribute("loginresettime"), - "ResetLogin mapping not as expected"); - Assert.assertEquals("3", ctxAdapter - .getStringAttribute("failedlogins"), - "FailedLogins mapping not as expected"); - Assert.assertEquals("uid=amAdmin, ou=people, dc=myretsu, dc=com", - ctxAdapter.getStringAttribute("creatorname"), - "Creator mapping not as expected"); - } catch (ContextMapperException e) { - LOGGER.debug(e.getMessage()); - Assert.fail(e.getMessage(), e); - } - } - - public void testMapFromContext() throws ContextMapperException { - AnnotatedClassContextMapper contextMapper = new AnnotatedClassContextMapper( - MockEntity.class, typeConverter, refEditorFactory); - DirContextAdapter ctxAdapter = new DirContextAdapter(); - ctxAdapter.setAttributeValue("cn", "Mr Bean"); - ctxAdapter.setAttributeValue("mail", "bean@bean.com"); - ctxAdapter.setAttributeValue("addr", "Somewhere in england"); - ctxAdapter.setAttributeValue("userpassword", "fred".getBytes()); - ctxAdapter - .setAttributeValues("desc", new String[] { "elem1", "elem2" }); - ctxAdapter.setAttributeValue("userpassword", "fred".getBytes()); - ctxAdapter.setAttributeValue("acceptemails", "true"); - ctxAdapter.setAttributeValue("loginresettime", "19700101100000.1"); - ctxAdapter.setAttributeValue("failedlogins", "3"); - ctxAdapter.setAttributeValue("creatorname", - "uid=amAdmin,ou=people,dc=myretsu,dc=com"); - - MockEntity entity = (MockEntity) contextMapper - .mapFromContext(ctxAdapter); - Assert.assertEquals(entity.getMail(), "bean@bean.com", - "The mocked entity should match what has been mapped"); - Assert.assertEquals("Mr Bean", entity.getName(), - "Name mapping not as expected"); - Assert.assertEquals(entity.getMail(), "bean@bean.com", - "Mail mapping not as expected"); - Assert.assertEquals(entity.getAddress(), "Somewhere in england", - "Address mapping not as expected"); - Assert.assertEquals("fred", new String(entity.getPassword()), - "Password mapping not as expected"); - Assert.assertEquals("elem1", entity.getDescription()[0], - "Description mapping not as expected"); - Assert.assertEquals(Boolean.TRUE, entity.getAcceptEmails(), - "AcceptEmails mapping not as expected"); - Assert.assertEquals(new Date(1L), entity.getResetLogin(), - "ResetLogin mapping not as expected"); - Assert.assertEquals(new Integer(3), entity.getFailedLogins(), - "FailedLogin mapping not as expected"); - Assert.assertEquals(new DistinguishedName( - "uid=amAdmin,ou=people,dc=myretsu,dc=com"), - entity.getCreator(), "Creator mapping not as expected"); - - } - - @Test(groups = { "AnnotatedClassContextMapper" }, expectedExceptions = ContextMapperException.class) - public void throwsExceptionWhenNamingSuffixHasSyntaxError() - throws ContextMapperException { - AnnotatedClassContextMapper contextMapper = new AnnotatedClassContextMapper( - MockModelNamingSuffixSyntaxError.class, typeConverter, - refEditorFactory); - Assert - .fail("AnnotatedClassContextMapper should throw an exception when naming path annotation syntax is " - + "incorrect."); - } - - @Test(groups = { "AnnotatedClassContextMapper" }, expectedExceptions = ContextMapperException.class) - public void throwsExceptionWhenNamingSuffixNotInSeparateElements() - throws ContextMapperException { - AnnotatedClassContextMapper contextMapper = new AnnotatedClassContextMapper( - MockModelNamingSuffixNotInSeparateElements.class, - typeConverter, refEditorFactory); - Assert - .fail("AnnotatedClassContextMapper should throw an exception when naming path annotation syntax is " - + "incorrect."); - } - - @Test(groups = { "AnnotatedClassContextMapper" }, expectedExceptions = ContextMapperException.class) - public void throwsExceptionWhenModelObjectMissingGetterOrSetter() - throws ContextMapperException { - AnnotatedClassContextMapper contextMapper = new AnnotatedClassContextMapper( - MockModelMissingSetter.class, typeConverter, refEditorFactory); - Assert.fail("Should've thrown exception due to missing getter/setter"); - } - - @Test(groups = { "AnnotatedClassContextMapper" }) - public void throwsExceptionWhenBeanPropertyNotAValidTranslationType() { - try { - AnnotatedClassContextMapper contextMapper = new AnnotatedClassContextMapper( - MockModelInvalidTranslationType.class, typeConverter, - refEditorFactory); - Assert - .fail("Should've thrown exception due to missing getter/setter"); - } catch (ContextMapperException e) { - Assert - .assertTrue(e - .getMessage() - .contains( - "MockModelInvalidTranslationType.getMap() has invalid return type")); - } - - } - - @NamingAttribute("cn") - @NamingSuffix( { "ou=people", "dc=example", "dc=com" }) - @ObjectClasses( { "top", "person", "organizationalPerson", "inetorgperson" }) - public class MockModelMissingSetter { - @DirAttribute("cn") - private String name; - - @DirAttribute("addr") - private String address; - - @DirAttribute("mail") - private String mail; - - public String getName() { - return name; - } - - public String getAddress() { - return address; - } - - public String getMail() { - return mail; - } - - public void setName(String name) { - this.name = name; - } - - public void setAddress(String address) { - this.address = address; - } - } - - @NamingAttribute("cn") - @NamingSuffix( { "ou=people", "dc=example", "dc=com" }) - @ObjectClasses( { "top", "person", "organizationalPerson", "inetorgperson" }) - public class MockModelBadMethodName { - - @DirAttribute("cn") - private String name; - - @DirAttribute("addr") - private String address; - - public String zzzName() { - return name; - } - - public String getAddress() { - return address; - } - } - - @NamingAttribute("cn") - @NamingSuffix( { "ou=people,dc=example ,dc=com" }) - @ObjectClasses( { "top", "person", "organizationalPerson", "inetorgperson" }) - public class MockModelNamingSuffixNotInSeparateElements { - } - - @NamingAttribute("cn") - @NamingSuffix( { "ou=people", "dc clazz; - private String namingAttribute; - private String[] objectClasses; - private String[] namingSuffixElements; - private DistinguishedName namingSuffix; - - private ContextMap contextMap; - private Map propertyGetters; - private Map propertySetters; - - - public AnnotatedClassContextMapper(Class clazz, LdapTypeConverter typeConverter, - ReferencedEntryEditorFactory refEditorFactory) throws ContextMapperException - { - this.clazz = clazz; - this.typeConverter = typeConverter; - this.refEditorFactory = refEditorFactory; - checkConfigured(); - checkRequiredMetaData(); - buildNamingSuffix(); - mapAttributesToBeanProperties(); - cacheGettersAndSetters(); - registerReferencedEntries(); - } - - private void checkConfigured() throws ContextMapperException - { - if (clazz == null) - { - throw new ContextMapperException("Error creating context mapper." - + ". Class arugment is null"); - } - - if (this.typeConverter == null) - { - throw new ContextMapperException("Error creating context mapper for class: " - + clazz.getSimpleName() - + ". AttributeTypeTranslator arugment is null"); - } - - if (this.refEditorFactory == null) - { - throw new ContextMapperException("Error creating context mapper for class: " - + clazz.getSimpleName() - + ". ReferencedEntryEditorFactory arugment is null"); - } - } - - private void checkRequiredMetaData() throws ContextMapperException - { - NamingAttribute attnNamingAttr = clazz.getAnnotation(NamingAttribute.class); - ObjectClasses attnObjectClasses = clazz.getAnnotation(ObjectClasses.class); - NamingSuffix attnNamingSuffix = clazz.getAnnotation(NamingSuffix.class); - this.namingAttribute = attnNamingAttr != null ? attnNamingAttr.value() : null; - this.objectClasses = attnObjectClasses != null ? attnObjectClasses.value() : null; - this.namingSuffixElements = attnNamingSuffix != null ? attnNamingSuffix.value() : null; - - if (this.namingAttribute == null) - { - throw new ContextMapperException("Error creating context mapper for class: " - + clazz.getSimpleName() - + ". The @NamingAttribute annotation is required."); - } - - if (this.objectClasses == null) - { - throw new ContextMapperException("Error creating context mapper for class: " - + clazz.getSimpleName() - + ". The @ObjectClasses annotation is required."); - } - - if (namingSuffixElements == null || namingSuffixElements.length == 0) - { - throw new ContextMapperException("Error creating context mapper for class: " - + clazz.getSimpleName() - + ". The @NamingSuffix annotation is required."); - } - - if (namingSuffixElements[0].contains(",")) - { - throw new ContextMapperException("Error creating context mapper for class: " - + clazz.getSimpleName() - + ". Naming path nodes must be defined as separate elements, " - + "for example: @NamingSuffix({\"ou=people\", \"dc=example\", \"dc=com\"}), and not: " - + "@NamingSuffix({\"ou=people,dc=example,dc=com\"})"); - } - } - - private void buildNamingSuffix() throws ContextMapperException - { - namingSuffix = new DistinguishedName(); - for (int i = namingSuffixElements.length - 1; i >= 0; i--) - { - String[] nameValue = namingSuffixElements[i].split("="); - if (nameValue.length != 2) - { - throw new ContextMapperException("Error creating context mapper for class: " + clazz.getSimpleName() - + ". Syntax error in naming suffix element: " + namingSuffixElements[i]); - } - namingSuffix.add(nameValue[0].trim(), nameValue[1].trim()); - } - } - - private void mapAttributesToBeanProperties() - { - contextMap = new ContextMap(); - 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(); - contextMap.map(beanPropertyName, attributeName); - } - } - } - - private void cacheGettersAndSetters() throws ContextMapperException - { - propertyGetters = new HashMap(); - propertySetters = new HashMap(); - - for (String beanPropertyName : contextMap.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 ContextMapperException("Error creating context mapper for class: " - + clazz.getSimpleName() - + ". " + clazz.getSimpleName() + " is missing a getter/setter for property: " - + beanPropertyName, e); - } - } - } - - private void registerReferencedEntries() throws ContextMapperException - { - 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 ContextMapperException(clazz.getSimpleName() + "." - + getter.getName() - + "() has invalid return type. Return type must be a context mapped " - + "directory object, or one of the following: " - + ValidConversionType.listTypes(), e); - } - } - } - } - - public Object mapFromContext(Object ctx) - { - DirContextAdapter ctxAdapter = (DirContextAdapter) ctx; - Object instance; - try - { - instance = clazz.newInstance(); - for (String beanPropertyName : contextMap.beanPropertyNames()) - { - Class attributeType = propertyGetters.get(beanPropertyName).getReturnType(); - Attribute attribute = - ctxAdapter.getAttributes().get(contextMap.attributeNameFor(beanPropertyName)); - if (attribute != null) - { - try - { - Object beanPropertyValue = typeConverter.convertIfNecessary( - new AttributeDecorator(attribute).getAllAsObject(), attributeType); - propertySetters.get(beanPropertyName).invoke(instance, beanPropertyValue); - } - catch (TypeMismatchException e) - { - throw new ContextMapperException("Cannot set bean property '" + beanPropertyName - + "' in class " + clazz.getSimpleName() + ", " + e.getMessage(), e); - } - } - } - } - catch (Exception e) - { - throw new RuntimeException(e.getMessage(), e); - } - return instance; - } - - // TODO: Simplify this method: When bug with DirContextAdapter.setAttribute() (LDAP-15) is fixed, - // TODO: register a type converter to convert to type Attribute - public void mapToContext(Object beanInstance, Object ctx) - { - DirContextAdapter ctxAdapter = (DirContextAdapter) ctx; - ctxAdapter.setAttributeValues("objectclass", objectClasses); - for (String attributeName : contextMap.attributeNames()) - { - try - { - Object beanPropertyValue = propertyGetters.get( - contextMap.beanPropertyNameFor(attributeName)).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 ContextMapperException - { - if (beanInstance == null) - { - throw new ContextMapperException("Can't build Dn from beanInstance, beanInstance is null"); - } - return buildDn(namingAttributeValue(beanInstance)); - } - - public Name buildDn(String namingAttributeValue) throws ContextMapperException - { - if (namingAttributeValue == null) - { - throw new ContextMapperException("Can't build Dn from namingAttributeValue, namingAttributeValue is null"); - } - DistinguishedName dn = (DistinguishedName) namingSuffix.clone(); - dn.add(namingAttribute, namingAttributeValue); - LOGGER.trace("buildDn(): " + dn); - return dn; - } - - public String namingAttributeValue(Object beanInstance) - { - Method method = propertyGetters.get(contextMap.beanPropertyNameFor(namingAttribute)); - try - { - return (String) method.invoke(clazz.cast(beanInstance)); - } - catch (Exception e) - { - throw new RuntimeException(e.getMessage(), e); - } - } - - public ContextMap getContextMap() - { - return contextMap; - } - - public String[] getObjectClasses() - { - return objectClasses; - } - - public String getNamingAttribute() - { - return namingAttribute; - } - - public DistinguishedName getNamingSuffix() - { - return namingSuffix; - } -} \ No newline at end of file diff --git a/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/contextmapping/AnnotationObjectDirectoryMapper.java b/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/contextmapping/AnnotationObjectDirectoryMapper.java deleted file mode 100644 index 3d6fbaae..00000000 --- a/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/contextmapping/AnnotationObjectDirectoryMapper.java +++ /dev/null @@ -1,332 +0,0 @@ -/* - * 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.contextmapping; - -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.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.ldap.odm.attributetypes.LdapTypeConverter; -import org.springframework.ldap.odm.attributetypes.ValidConversionType; -import org.springframework.ldap.odm.attributetypes.ReferencedEntryEditorFactory; -import org.springframework.ldap.odm.attributetypes.exception.ReferencedEntryEditorCreationException; -import org.springframework.ldap.odm.contextmapping.exception.ContextMapperException; -import org.springframework.util.StringUtils; - -import javax.naming.Name; -import javax.naming.directory.Attribute; -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.util.HashMap; -import java.util.Map; - -public class AnnotationObjectDirectoryMapper implements ObjectDirectoryMapper -{ - private static final Log LOGGER = LogFactory.getLog(AnnotationObjectDirectoryMapper.class); - - private LdapTypeConverter typeConverter; - private ReferencedEntryEditorFactory refEditorFactory; - - private Class clazz; - private String namingAttribute; - private String[] objectClasses; - private String[] namingSuffixElements; - private DistinguishedName namingSuffix; - - private ContextMap contextMap; - private Map propertyGetters; - private Map propertySetters; - - - public AnnotationObjectDirectoryMapper(Class clazz, LdapTypeConverter typeConverter, - ReferencedEntryEditorFactory refEditorFactory) throws ContextMapperException - { - this.clazz = clazz; - this.typeConverter = typeConverter; - this.refEditorFactory = refEditorFactory; - checkConfigured(); - checkRequiredMetaData(); - buildNamingSuffix(); - mapAttributesToBeanProperties(); - cacheGettersAndSetters(); - registerReferencedEntries(); - } - - private void checkConfigured() throws ContextMapperException - { - if (clazz == null) - { - throw new ContextMapperException("Error creating context mapper." - + ". Class arugment is null"); - } - - if (this.typeConverter == null) - { - throw new ContextMapperException("Error creating context mapper for class: " - + clazz.getSimpleName() - + ". AttributeTypeTranslator arugment is null"); - } - - if (this.refEditorFactory == null) - { - throw new ContextMapperException("Error creating context mapper for class: " - + clazz.getSimpleName() - + ". ReferencedEntryEditorFactory arugment is null"); - } - } - - private void checkRequiredMetaData() throws ContextMapperException - { - NamingAttribute attnNamingAttr = clazz.getAnnotation(NamingAttribute.class); - ObjectClasses attnObjectClasses = clazz.getAnnotation(ObjectClasses.class); - NamingSuffix attnNamingSuffix = clazz.getAnnotation(NamingSuffix.class); - this.namingAttribute = attnNamingAttr != null ? attnNamingAttr.value() : null; - this.objectClasses = attnObjectClasses != null ? attnObjectClasses.value() : null; - this.namingSuffixElements = attnNamingSuffix != null ? attnNamingSuffix.value() : null; - - if (this.namingAttribute == null) - { - throw new ContextMapperException("Error creating context mapper for class: " - + clazz.getSimpleName() - + ". The @NamingAttribute annotation is required."); - } - - if (this.objectClasses == null) - { - throw new ContextMapperException("Error creating context mapper for class: " - + clazz.getSimpleName() - + ". The @ObjectClasses annotation is required."); - } - - if (namingSuffixElements == null || namingSuffixElements.length == 0) - { - throw new ContextMapperException("Error creating context mapper for class: " - + clazz.getSimpleName() - + ". The @NamingSuffix annotation is required."); - } - - if (namingSuffixElements[0].contains(",")) - { - throw new ContextMapperException("Error creating context mapper for class: " - + clazz.getSimpleName() - + ". Naming path nodes must be defined as separate elements, " - + "for example: @NamingSuffix({\"ou=people\", \"dc=example\", \"dc=com\"}), and not: " - + "@NamingSuffix({\"ou=people,dc=example,dc=com\"})"); - } - } - - private void buildNamingSuffix() throws ContextMapperException - { - namingSuffix = new DistinguishedName(); - for (int i = namingSuffixElements.length - 1; i >= 0; i--) - { - String[] nameValue = namingSuffixElements[i].split("="); - if (nameValue.length != 2) - { - throw new ContextMapperException("Error creating context mapper for class: " + clazz.getSimpleName() - + ". Syntax error in naming suffix element: " + namingSuffixElements[i]); - } - namingSuffix.add(nameValue[0].trim(), nameValue[1].trim()); - } - } - - private void mapAttributesToBeanProperties() - { - contextMap = new ContextMap(); - 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(); - contextMap.map(beanPropertyName, attributeName); - } - } - } - - private void cacheGettersAndSetters() throws ContextMapperException - { - propertyGetters = new HashMap(); - propertySetters = new HashMap(); - - for (String beanPropertyName : contextMap.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 ContextMapperException("Error creating context mapper for class: " - + clazz.getSimpleName() - + ". " + clazz.getSimpleName() + " is missing a getter/setter for property: " - + beanPropertyName, e); - } - } - } - - private void registerReferencedEntries() throws ContextMapperException - { - 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 ContextMapperException(clazz.getSimpleName() + "." - + getter.getName() - + "() has invalid return type. Return type must be a context mapped " - + "directory object, or one of the following: " - + ValidConversionType.listTypes(), e); - } - } - } - } - - public Object mapFromContext(Object ctx) - { - DirContextAdapter ctxAdapter = (DirContextAdapter) ctx; - Object instance; - try - { - instance = clazz.newInstance(); - for (String beanPropertyName : contextMap.beanPropertyNames()) - { - Class attributeType = propertyGetters.get(beanPropertyName).getReturnType(); - Attribute attribute = - ctxAdapter.getAttributes().get(contextMap.attributeNameFor(beanPropertyName)); - if (attribute != null) - { - try - { - Object beanPropertyValue = typeConverter.convertIfNecessary( - new AttributeDecorator(attribute).getAllAsObject(), attributeType); - propertySetters.get(beanPropertyName).invoke(instance, beanPropertyValue); - } - catch (TypeMismatchException e) - { - throw new ContextMapperException("Cannot set bean property '" + beanPropertyName - + "' in class " + clazz.getSimpleName() + ", " + e.getMessage(), e); - } - } - } - } - catch (Exception e) - { - throw new RuntimeException(e.getMessage(), e); - } - return instance; - } - - // TODO: Simplify this method: When bug with DirContextAdapter.setAttribute() (LDAP-15) is fixed, - // TODO: register a type converter to convert to type Attribute - public void mapToContext(Object beanInstance, Object ctx) - { - DirContextAdapter ctxAdapter = (DirContextAdapter) ctx; - ctxAdapter.setAttributeValues("objectclass", objectClasses); - for (String attributeName : contextMap.attributeNames()) - { - try - { - Object beanPropertyValue = propertyGetters.get( - contextMap.beanPropertyNameFor(attributeName)).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 ContextMapperException - { - if (beanInstance == null) - { - throw new ContextMapperException("Can't build Dn from beanInstance, beanInstance is null"); - } - return buildDn(namingAttributeValue(beanInstance)); - } - - public Name buildDn(String namingAttributeValue) throws ContextMapperException - { - if (namingAttributeValue == null) - { - throw new ContextMapperException("Can't build Dn from namingAttributeValue, namingAttributeValue is null"); - } - DistinguishedName dn = (DistinguishedName) namingSuffix.clone(); - dn.add(namingAttribute, namingAttributeValue); - LOGGER.trace("buildDn(): " + dn); - return dn; - } - - public String namingAttributeValue(Object beanInstance) - { - Method method = propertyGetters.get(contextMap.beanPropertyNameFor(namingAttribute)); - try - { - return (String) method.invoke(clazz.cast(beanInstance)); - } - catch (Exception e) - { - throw new RuntimeException(e.getMessage(), e); - } - } - - public ContextMap getContextMap() - { - return contextMap; - } - - public String[] getObjectClasses() - { - return objectClasses; - } - - public String getNamingAttribute() - { - return namingAttribute; - } - - public DistinguishedName getNamingSuffix() - { - return namingSuffix; - } -} \ No newline at end of file diff --git a/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/contextmapping/AttributeDecorator.java b/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/contextmapping/AttributeDecorator.java deleted file mode 100644 index ccd3ec96..00000000 --- a/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/contextmapping/AttributeDecorator.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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.contextmapping; - -import javax.naming.NamingException; -import javax.naming.directory.Attribute; - -public class AttributeDecorator -{ - private Attribute attribute; - - public AttributeDecorator(Attribute attribute) - { - this.attribute = attribute; - } - - public Object getAllAsObject() throws NamingException - { - if (attribute != null) - { - 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; - } - } - else - { - return null; - } - - } -} diff --git a/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/contextmapping/ContextMap.java b/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/contextmapping/ContextMap.java deleted file mode 100644 index aae1d046..00000000 --- a/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/contextmapping/ContextMap.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * 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.contextmapping; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import java.util.HashMap; -import java.util.Map; -import java.util.Set; - -/** A bi-directional map of bean property names to attribute names */ -public class ContextMap -{ - private static final Log LOGGER = LogFactory.getLog(ContextMap.class); - private Map beanPropertyNameKeys; - private Map attributeNameKeys; - - public ContextMap() - { - beanPropertyNameKeys = new HashMap(); - attributeNameKeys = new HashMap(); - } - - public 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 beanPropertyNames() - { - return beanPropertyNameKeys.keySet(); - } - - public Set attributeNames() - { - return attributeNameKeys.keySet(); - } -} diff --git a/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/contextmapping/ContextMapperFactory.java b/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/contextmapping/ContextMapperFactory.java deleted file mode 100644 index f4893d82..00000000 --- a/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/contextmapping/ContextMapperFactory.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * 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.contextmapping; - -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 org.springframework.ldap.odm.contextmapping.exception.ContextMapperException; - -import java.util.HashMap; -import java.util.Map; - -public class ContextMapperFactory -{ - private static final Log LOGGER = LogFactory.getLog(ContextMapperFactory.class); - private Map contextMappers; - private LdapTypeConverter typeConverter; - private ReferencedEntryEditorFactory referencedEntryEditorFactory; - - public ContextMapperFactory(LdapTypeConverter typeConverter, - ReferencedEntryEditorFactory referencedEntryEditorFactory) - { - contextMappers = new HashMap(); - this.typeConverter = typeConverter; - this.referencedEntryEditorFactory = referencedEntryEditorFactory; - this.referencedEntryEditorFactory.setContextMapperFactory(this); - } - - public ObjectDirectoryMapper contextMapperForClass(Class clazz) - throws ContextMapperException - { - if (contextMappers.containsKey(clazz)) - { - LOGGER.debug("Returning cached context mapper for class: " + clazz.getSimpleName()); - return contextMappers.get(clazz); - } - else - { - LOGGER.debug("Attempting to create a context mapper for class: " + clazz.getSimpleName()); - AnnotationObjectDirectoryMapper contextMapper = - new AnnotationObjectDirectoryMapper(clazz, typeConverter, referencedEntryEditorFactory); - contextMappers.put(clazz, contextMapper); - return contextMapper; - } - } -} diff --git a/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/contextmapping/ObjectDirectoryMapper.java b/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/contextmapping/ObjectDirectoryMapper.java deleted file mode 100644 index f4ddd451..00000000 --- a/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/contextmapping/ObjectDirectoryMapper.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * 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.contextmapping; - -import org.springframework.ldap.core.ContextMapper; -import org.springframework.ldap.core.ContextAssembler; -import org.springframework.ldap.core.DistinguishedName; -import org.springframework.ldap.odm.contextmapping.exception.ContextMapperException; - -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 ContextMapperException; - - Name buildDn(String namingAttributeValue) throws ContextMapperException; - - String namingAttributeValue(Object beanInstance); - - ContextMap getContextMap(); - - String[] getObjectClasses(); - - String getNamingAttribute(); - - DistinguishedName getNamingSuffix(); -} diff --git a/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/contextmapping/exception/ContextMapperException.java b/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/contextmapping/exception/ContextMapperException.java deleted file mode 100644 index 66863897..00000000 --- a/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/contextmapping/exception/ContextMapperException.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * 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.contextmapping.exception; - -public class ContextMapperException extends Exception -{ - - - public ContextMapperException(String message) - { - super(message); - } - - public ContextMapperException(String message, Throwable cause) - { - super(message, cause); - } - - - - -} diff --git a/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/dao/exception/DaoException.java b/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/dao/exception/DaoException.java deleted file mode 100644 index 9ca7abfd..00000000 --- a/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/dao/exception/DaoException.java +++ /dev/null @@ -1,19 +0,0 @@ -/* - * 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.exception; - -public class DaoException extends RuntimeException -{ - public DaoException(String message) - { - super(message); - } - - public DaoException(String message, Throwable cause) - { - super(message, cause); - } -} diff --git a/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/dao/exception/DataIntegrityViolationException.java b/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/dao/exception/DataIntegrityViolationException.java deleted file mode 100644 index d2d015b7..00000000 --- a/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/dao/exception/DataIntegrityViolationException.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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.exception; - -import java.util.List; - -/** - * A Serializable version of org.springframework.dao.DataIntegrityViolationException - */ -public class DataIntegrityViolationException extends DaoException -{ - - private List nonUniqueAttributes; - - public DataIntegrityViolationException(String message) - { - super(message); - } - - public DataIntegrityViolationException(String message, Throwable cause) - { - super(message, cause); - } - - - - -} diff --git a/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/dao/exception/EntryNotFoundException.java b/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/dao/exception/EntryNotFoundException.java deleted file mode 100644 index 0274c8f0..00000000 --- a/spring-ldap-odm/src/main/java/org/springframework/ldap/odm/dao/exception/EntryNotFoundException.java +++ /dev/null @@ -1,19 +0,0 @@ -/* - * 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.exception; - -/** - * A Serializable version of org.springframework.dao.EntryNotFoundException - */ -public class EntryNotFoundException extends DaoException -{ - - public EntryNotFoundException(String message) - { - super(message); - } - -}