From e93ee9cf6aa0c7a993ec59c527d115e25e0fa2e7 Mon Sep 17 00:00:00 2001 From: Mattias Hellborg Arthursson Date: Mon, 9 Sep 2013 08:27:07 +0200 Subject: [PATCH] LDAP-258: General Java 5/6 cleanup throughout the code base. --- ...AndResponseControlDirContextProcessor.java | 31 ++-- .../ldap/control/PagedResult.java | 7 +- .../PagedResultsDirContextProcessor.java | 9 +- .../SortControlDirContextProcessor.java | 10 +- .../ldap/core/DirContextAdapter.java | 145 +++++++++--------- .../ldap/core/DirContextOperations.java | 5 +- .../ldap/core/LdapEncoder.java | 6 +- .../LdapEntryIdentificationContextMapper.java | 17 +- .../ldap/core/LdapOperations.java | 4 +- .../ldap/core/LdapTemplate.java | 30 +--- .../core/support/AbstractContextSource.java | 83 +++++----- ...ctTlsDirContextAuthenticationStrategy.java | 23 ++- .../support/AggregateDirContextProcessor.java | 25 ++- .../core/support/DefaultDirObjectFactory.java | 18 ++- .../DefaultIncrementalAttributesMapper.java | 8 +- ...stMd5DirContextAuthenticationStrategy.java | 7 +- .../DirContextAuthenticationStrategy.java | 11 +- ...alTlsDirContextAuthenticationStrategy.java | 16 ++ .../ldap/core/support/LdapContextSource.java | 7 +- ...impleDirContextAuthenticationStrategy.java | 7 +- .../core/support/SingleContextSource.java | 21 ++- .../ldap/filter/AbstractFilter.java | 6 +- .../ldap/filter/BinaryLogicalFilter.java | 19 +-- .../filter/GreaterThanOrEqualsFilter.java | 5 +- .../ldap/filter/HardcodedFilter.java | 4 - .../ldap/filter/LessThanOrEqualsFilter.java | 5 +- .../ldap/filter/LikeFilter.java | 12 +- .../ldap/filter/NotFilter.java | 4 +- .../springframework/ldap/filter/OrFilter.java | 5 +- .../filter/WhitespaceWildcardsFilter.java | 3 - .../ldap/pool/DelegatingContext.java | 12 +- .../ldap/pool/DelegatingDirContext.java | 17 +- .../ldap/pool/DirContextType.java | 9 +- .../pool/MutableDelegatingLdapContext.java | 13 +- .../DefaultDirContextValidator.java | 4 +- .../ldap/support/LdapNameBuilder.java | 2 +- .../ldap/support/LdapUtils.java | 54 +++++-- .../ModifyAttributesOperationExecutor.java | 12 +- .../ModifyAttributesOperationRecorder.java | 13 +- ...ntextSourceTransactionManagerDelegate.java | 3 +- ...ctionAwareDirContextInvocationHandler.java | 15 +- ...ompensatingTransactionManagerDelegate.java | 8 +- ...mpensatingTransactionOperationManager.java | 21 ++- .../ldap/core/DirContextAdapterTest.java | 2 +- 44 files changed, 347 insertions(+), 391 deletions(-) diff --git a/core/src/main/java/org/springframework/ldap/control/AbstractFallbackRequestAndResponseControlDirContextProcessor.java b/core/src/main/java/org/springframework/ldap/control/AbstractFallbackRequestAndResponseControlDirContextProcessor.java index 87499ed3..48a18fc2 100644 --- a/core/src/main/java/org/springframework/ldap/control/AbstractFallbackRequestAndResponseControlDirContextProcessor.java +++ b/core/src/main/java/org/springframework/ldap/control/AbstractFallbackRequestAndResponseControlDirContextProcessor.java @@ -86,9 +86,9 @@ public abstract class AbstractFallbackRequestAndResponseControlDirContextProcess private static final boolean CRITICAL_CONTROL = true; - protected Class responseControlClass; + protected Class responseControlClass; - protected Class requestControlClass; + protected Class requestControlClass; protected boolean critical = CRITICAL_CONTROL; @@ -129,11 +129,11 @@ public abstract class AbstractFallbackRequestAndResponseControlDirContextProcess * * @param responseControlClass Class of the expected response control. */ - public void setResponseControlClass(Class responseControlClass) { + public void setResponseControlClass(Class responseControlClass) { this.responseControlClass = responseControlClass; } - public void setRequestControlClass(Class requestControlClass) { + public void setRequestControlClass(Class requestControlClass) { this.requestControlClass = requestControlClass; } @@ -144,7 +144,7 @@ public abstract class AbstractFallbackRequestAndResponseControlDirContextProcess * @param control Instance that the method should be invoked on * @return the invocation result, if any */ - protected Object invokeMethod(String method, Class clazz, Object control) { + protected Object invokeMethod(String method, Class clazz, Object control) { Method actualMethod = ReflectionUtils.findMethod(clazz, method); return ReflectionUtils.invokeMethod(actualMethod, control); } @@ -156,8 +156,8 @@ public abstract class AbstractFallbackRequestAndResponseControlDirContextProcess * @param params Actual constructor parameters * @return Control to be used by the DirContextProcessor */ - public Control createRequestControl(Class[] paramTypes, Object[] params) { - Constructor constructor = ClassUtils.getConstructorIfAvailable(requestControlClass, paramTypes); + public Control createRequestControl(Class[] paramTypes, Object[] params) { + Constructor constructor = ClassUtils.getConstructorIfAvailable(requestControlClass, paramTypes); if (constructor == null) { throw new IllegalArgumentException("Failed to find an appropriate RequestControl constructor"); } @@ -187,16 +187,13 @@ public abstract class AbstractFallbackRequestAndResponseControlDirContextProcess } // Go through response controls and get info, regardless of class - for (int i = 0; i < responseControls.length; i++) { - Control responseControl = responseControls[i]; - - // check for match, try fallback otherwise - if (responseControl.getClass().isAssignableFrom(responseControlClass)) { - Object control = responseControl; - handleResponse(control); - return; - } - } + for (Control responseControl : responseControls) { + // check for match, try fallback otherwise + if (responseControl.getClass().isAssignableFrom(responseControlClass)) { + handleResponse(responseControl); + return; + } + } log.info("No matching response control found - looking for '" + responseControlClass); } diff --git a/core/src/main/java/org/springframework/ldap/control/PagedResult.java b/core/src/main/java/org/springframework/ldap/control/PagedResult.java index b401724f..0e539242 100644 --- a/core/src/main/java/org/springframework/ldap/control/PagedResult.java +++ b/core/src/main/java/org/springframework/ldap/control/PagedResult.java @@ -23,10 +23,11 @@ import java.util.List; * * @author Mattias Hellborg Arthursson * @author Ulrik Sandberg + * @deprecated */ public class PagedResult { - private List resultList; + private List resultList; private PagedResultsCookie cookie; @@ -39,7 +40,7 @@ public class PagedResult { * @param cookie * the cookie. */ - public PagedResult(List resultList, PagedResultsCookie cookie) { + public PagedResult(List resultList, PagedResultsCookie cookie) { this.resultList = resultList; this.cookie = cookie; } @@ -58,7 +59,7 @@ public class PagedResult { * * @return the result list. */ - public List getResultList() { + public List getResultList() { return resultList; } diff --git a/core/src/main/java/org/springframework/ldap/control/PagedResultsDirContextProcessor.java b/core/src/main/java/org/springframework/ldap/control/PagedResultsDirContextProcessor.java index 7e89ec3d..1a815a00 100644 --- a/core/src/main/java/org/springframework/ldap/control/PagedResultsDirContextProcessor.java +++ b/core/src/main/java/org/springframework/ldap/control/PagedResultsDirContextProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -118,8 +118,8 @@ public class PagedResultsDirContextProcessor extends AbstractFallbackRequestAndR if (cookie != null) { actualCookie = cookie.getCookie(); } - return super.createRequestControl(new Class[] { int.class, byte[].class, boolean.class }, - new Object[] {new Integer(pageSize), actualCookie, Boolean.valueOf(critical)}); + return super.createRequestControl(new Class[] { int.class, byte[].class, boolean.class }, + new Object[] {pageSize, actualCookie, critical}); } /* @@ -130,7 +130,6 @@ public class PagedResultsDirContextProcessor extends AbstractFallbackRequestAndR protected void handleResponse(Object control) { byte[] result = (byte[]) invokeMethod("getCookie", responseControlClass, control); this.cookie = new PagedResultsCookie(result); - Integer wrapper = (Integer) invokeMethod("getResultSize", responseControlClass, control); - this.resultSize = wrapper.intValue(); + this.resultSize = (Integer) invokeMethod("getResultSize", responseControlClass, control); } } diff --git a/core/src/main/java/org/springframework/ldap/control/SortControlDirContextProcessor.java b/core/src/main/java/org/springframework/ldap/control/SortControlDirContextProcessor.java index c4282776..7900494b 100644 --- a/core/src/main/java/org/springframework/ldap/control/SortControlDirContextProcessor.java +++ b/core/src/main/java/org/springframework/ldap/control/SortControlDirContextProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -104,7 +104,7 @@ public class SortControlDirContextProcessor extends AbstractFallbackRequestAndRe */ public Control createRequestControl() { return super.createRequestControl(new Class[] { String[].class, boolean.class }, new Object[] { - new String[] { sortKey }, Boolean.valueOf(critical) }); + new String[] { sortKey }, critical}); } /* @@ -113,9 +113,7 @@ public class SortControlDirContextProcessor extends AbstractFallbackRequestAndRe * #handleResponse(java.lang.Object) */ protected void handleResponse(Object control) { - Boolean result = (Boolean) invokeMethod("isSorted", responseControlClass, control); - this.sorted = result.booleanValue(); - Integer code = (Integer) invokeMethod("getResultCode", responseControlClass, control); - this.resultCode = code.intValue(); + this.sorted = (Boolean) invokeMethod("isSorted", responseControlClass, control); + this.resultCode = (Integer) invokeMethod("getResultCode", responseControlClass, control); } } diff --git a/core/src/main/java/org/springframework/ldap/core/DirContextAdapter.java b/core/src/main/java/org/springframework/ldap/core/DirContextAdapter.java index 4fe8a9a0..03bdb5fc 100644 --- a/core/src/main/java/org/springframework/ldap/core/DirContextAdapter.java +++ b/core/src/main/java/org/springframework/ldap/core/DirContextAdapter.java @@ -22,9 +22,11 @@ import org.springframework.ldap.support.LdapUtils; import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; +import javax.naming.Binding; import javax.naming.Context; import javax.naming.InvalidNameException; import javax.naming.Name; +import javax.naming.NameClassPair; import javax.naming.NameNotFoundException; import javax.naming.NameParser; import javax.naming.NamingEnumeration; @@ -36,6 +38,7 @@ import javax.naming.directory.BasicAttributes; import javax.naming.directory.DirContext; import javax.naming.directory.ModificationItem; import javax.naming.directory.SearchControls; +import javax.naming.directory.SearchResult; import javax.naming.ldap.LdapName; import java.util.ArrayList; import java.util.Hashtable; @@ -223,9 +226,9 @@ public class DirContextAdapter implements DirContextOperations { */ public String[] getNamesOfModifiedAttributes() { - List tmpList = new ArrayList(); + List tmpList = new ArrayList(); - NamingEnumeration attributesEnumeration; + NamingEnumeration attributesEnumeration; if (isUpdateMode()) { attributesEnumeration = updatedAttrs.getAll(); } @@ -235,7 +238,7 @@ public class DirContextAdapter implements DirContextOperations { try { while (attributesEnumeration.hasMore()) { - Attribute oneAttribute = (Attribute) attributesEnumeration + Attribute oneAttribute = attributesEnumeration .next(); tmpList.add(oneAttribute.getID()); } @@ -247,10 +250,10 @@ public class DirContextAdapter implements DirContextOperations { closeNamingEnumeration(attributesEnumeration); } - return (String[]) tmpList.toArray(new String[0]); + return tmpList.toArray(new String[tmpList.size()]); } - private void closeNamingEnumeration(NamingEnumeration enumeration) { + private void closeNamingEnumeration(NamingEnumeration enumeration) { try { if (enumeration != null) { enumeration.close(); @@ -270,14 +273,14 @@ public class DirContextAdapter implements DirContextOperations { return new ModificationItem[0]; } - List tmpList = new LinkedList(); - NamingEnumeration attributesEnumeration = null; + List tmpList = new LinkedList(); + NamingEnumeration attributesEnumeration = null; try { attributesEnumeration = updatedAttrs.getAll(); // find attributes that have been changed, removed or added while (attributesEnumeration.hasMore()) { - Attribute oneAttr = (Attribute) attributesEnumeration.next(); + Attribute oneAttr = attributesEnumeration.next(); collectModifications(oneAttr, tmpList); } @@ -293,8 +296,7 @@ public class DirContextAdapter implements DirContextOperations { log.debug("Number of modifications:" + tmpList.size()); } - return (ModificationItem[]) tmpList - .toArray(new ModificationItem[tmpList.size()]); + return tmpList.toArray(new ModificationItem[tmpList.size()]); } /** @@ -312,7 +314,7 @@ public class DirContextAdapter implements DirContextOperations { * @throws NamingException if thrown by called Attribute methods. */ private void collectModifications(Attribute changedAttr, - List modificationList) throws NamingException { + List modificationList) throws NamingException { Attribute currentAttribute = originalAttrs.get(changedAttr.getID()); if (changedAttr.equals(currentAttribute)) { @@ -346,7 +348,7 @@ public class DirContextAdapter implements DirContextOperations { else if (changedAttr.size() > 0) { // Change of multivalue Attribute. Collect additions and removals // individually. - List myModifications = new LinkedList(); + List myModifications = new LinkedList(); collectModifications(currentAttribute, changedAttr, myModifications); if (myModifications.isEmpty()) { @@ -362,7 +364,7 @@ public class DirContextAdapter implements DirContextOperations { } private void collectModifications(Attribute originalAttr, - Attribute changedAttr, List modificationList) + Attribute changedAttr, List modificationList) throws NamingException { Attribute originalClone = (Attribute) originalAttr.clone(); @@ -443,11 +445,8 @@ public class DirContextAdapter implements DirContextOperations { // removed) // TODO Also include prev in null check // TODO Also check if there is a single null element - if (orig != null) { - return true; - } - return false; - } + return orig != null; + } // NOT setting to empty ------------------- @@ -585,11 +584,7 @@ public class DirContextAdapter implements DirContextOperations { */ public boolean attributeExists(String name) { Attribute oneAttr = originalAttrs.get(name); - if (oneAttr == null) { - return false; - } else { - return true; - } + return oneAttr != null; } /* @@ -729,14 +724,14 @@ public class DirContextAdapter implements DirContextOperations { * @see org.springframework.ldap.support.DirContextOperations#update() */ public void update() { - NamingEnumeration attributesEnumeration = null; + NamingEnumeration attributesEnumeration = null; try { attributesEnumeration = updatedAttrs.getAll(); // find what to update while (attributesEnumeration.hasMore()) { - Attribute a = (Attribute) attributesEnumeration.next(); + Attribute a = attributesEnumeration.next(); // if it does not exist it should be added if (isEmptyAttribute(a)) { @@ -766,8 +761,8 @@ public class DirContextAdapter implements DirContextOperations { */ public String[] getStringAttributes(String name) { try { - return (String[]) collectAttributeValuesAsList(name).toArray( - new String[0]); + List objects = collectAttributeValuesAsList(name, String.class); + return objects.toArray(new String[objects.size()]); } catch (NoSuchAttributeException e) { // The attribute does not exist - contract says to return null. @@ -784,7 +779,8 @@ public class DirContextAdapter implements DirContextOperations { */ public Object[] getObjectAttributes(String name) { try { - return collectAttributeValuesAsList(name).toArray(new Object[0]); + List list = collectAttributeValuesAsList(name, Object.class); + return list.toArray(new Object[list.size()]); } catch (NoSuchAttributeException e) { // The attribute does not exist - contract says to return null. @@ -792,20 +788,17 @@ public class DirContextAdapter implements DirContextOperations { } } - private List collectAttributeValuesAsList(String name) { - List list = new LinkedList(); - LdapUtils.collectAttributeValues(originalAttrs, name, list); + private List collectAttributeValuesAsList(String name, Class clazz) { + List list = new LinkedList(); + LdapUtils.collectAttributeValues(originalAttrs, name, list, clazz); return list; } - /* - * @seeorg.springframework.ldap.support.DirContextOperations# - * getAttributeSortedStringSet(java.lang.String) - */ - public SortedSet getAttributeSortedStringSet(String name) { + @Override + public SortedSet getAttributeSortedStringSet(String name) { try { - TreeSet attrSet = new TreeSet(); - LdapUtils.collectAttributeValues(originalAttrs, name, attrSet); + TreeSet attrSet = new TreeSet(); + LdapUtils.collectAttributeValues(originalAttrs, name, attrSet, String.class); return attrSet; } catch (NoSuchAttributeException e) { @@ -873,12 +866,12 @@ public class DirContextAdapter implements DirContextOperations { Attributes a = new BasicAttributes(true); Attribute target; - for (int i = 0; i < attrIds.length; i++) { - target = originalAttrs.get(attrIds[i]); - if (target != null) { - a.put(target); - } - } + for (String attrId : attrIds) { + target = originalAttrs.get(attrId); + if (target != null) { + a.put(target); + } + } return a; } @@ -1001,7 +994,7 @@ public class DirContextAdapter implements DirContextOperations { /** * @see javax.naming.directory.DirContext#search(Name, Attributes, String[]) */ - public NamingEnumeration search(Name name, Attributes matchingAttributes, + public NamingEnumeration search(Name name, Attributes matchingAttributes, String[] attributesToReturn) throws NamingException { throw new UnsupportedOperationException("Not implemented."); } @@ -1010,7 +1003,7 @@ public class DirContextAdapter implements DirContextOperations { * @see javax.naming.directory.DirContext#search(String, Attributes, * String[]) */ - public NamingEnumeration search(String name, Attributes matchingAttributes, + public NamingEnumeration search(String name, Attributes matchingAttributes, String[] attributesToReturn) throws NamingException { throw new UnsupportedOperationException("Not implemented."); } @@ -1018,7 +1011,7 @@ public class DirContextAdapter implements DirContextOperations { /** * @see javax.naming.directory.DirContext#search(Name, Attributes) */ - public NamingEnumeration search(Name name, Attributes matchingAttributes) + public NamingEnumeration search(Name name, Attributes matchingAttributes) throws NamingException { throw new UnsupportedOperationException("Not implemented."); } @@ -1026,7 +1019,7 @@ public class DirContextAdapter implements DirContextOperations { /** * @see javax.naming.directory.DirContext#search(String, Attributes) */ - public NamingEnumeration search(String name, Attributes matchingAttributes) + public NamingEnumeration search(String name, Attributes matchingAttributes) throws NamingException { throw new UnsupportedOperationException("Not implemented."); } @@ -1035,7 +1028,7 @@ public class DirContextAdapter implements DirContextOperations { * @see javax.naming.directory.DirContext#search(Name, String, * SearchControls) */ - public NamingEnumeration search(Name name, String filter, + public NamingEnumeration search(Name name, String filter, SearchControls cons) throws NamingException { throw new UnsupportedOperationException("Not implemented."); } @@ -1044,7 +1037,7 @@ public class DirContextAdapter implements DirContextOperations { * @see javax.naming.directory.DirContext#search(String, String, * SearchControls) */ - public NamingEnumeration search(String name, String filter, + public NamingEnumeration search(String name, String filter, SearchControls cons) throws NamingException { throw new UnsupportedOperationException("Not implemented."); } @@ -1053,7 +1046,7 @@ public class DirContextAdapter implements DirContextOperations { * @see javax.naming.directory.DirContext#search(Name, String, Object[], * SearchControls) */ - public NamingEnumeration search(Name name, String filterExpr, + public NamingEnumeration search(Name name, String filterExpr, Object[] filterArgs, SearchControls cons) throws NamingException { throw new UnsupportedOperationException("Not implemented."); } @@ -1062,7 +1055,7 @@ public class DirContextAdapter implements DirContextOperations { * @see javax.naming.directory.DirContext#search(String, String, Object[], * SearchControls) */ - public NamingEnumeration search(String name, String filterExpr, + public NamingEnumeration search(String name, String filterExpr, Object[] filterArgs, SearchControls cons) throws NamingException { throw new UnsupportedOperationException("Not implemented."); } @@ -1140,28 +1133,28 @@ public class DirContextAdapter implements DirContextOperations { /** * @see javax.naming.Context#list(Name) */ - public NamingEnumeration list(Name name) throws NamingException { + public NamingEnumeration list(Name name) throws NamingException { throw new UnsupportedOperationException("Not implemented."); } /** * @see javax.naming.Context#list(String) */ - public NamingEnumeration list(String name) throws NamingException { + public NamingEnumeration list(String name) throws NamingException { throw new UnsupportedOperationException("Not implemented."); } /** * @see javax.naming.Context#listBindings(Name) */ - public NamingEnumeration listBindings(Name name) throws NamingException { + public NamingEnumeration listBindings(Name name) throws NamingException { throw new UnsupportedOperationException("Not implemented."); } /** * @see javax.naming.Context#listBindings(String) */ - public NamingEnumeration listBindings(String name) throws NamingException { + public NamingEnumeration listBindings(String name) throws NamingException { throw new UnsupportedOperationException("Not implemented."); } @@ -1254,7 +1247,7 @@ public class DirContextAdapter implements DirContextOperations { /** * @see javax.naming.Context#getEnvironment() */ - public Hashtable getEnvironment() throws NamingException { + public Hashtable getEnvironment() throws NamingException { throw new UnsupportedOperationException("Not implemented."); } @@ -1342,46 +1335,46 @@ public class DirContextAdapter implements DirContextOperations { * @see java.lang.Object#toString() */ public String toString() { - StringBuffer buf = new StringBuffer(); - buf.append(getClass().getName()); - buf.append(":"); + StringBuilder builder = new StringBuilder(); + builder.append(getClass().getName()); + builder.append(":"); if (dn != null) { - buf.append(" dn=" + dn); + builder.append(" dn=").append(dn); } - buf.append(" {"); + builder.append(" {"); try { - for (NamingEnumeration i = originalAttrs.getAll(); i.hasMore();) { - Attribute attribute = (Attribute) i.next(); + for (NamingEnumeration i = originalAttrs.getAll(); i.hasMore();) { + Attribute attribute = i.next(); if (attribute.size() == 1) { - buf.append(attribute.getID()); - buf.append('='); - buf.append(attribute.get()); + builder.append(attribute.getID()); + builder.append('='); + builder.append(attribute.get()); } else { for (int j = 0; j < attribute.size(); j++) { if (j > 0) { - buf.append(", "); + builder.append(", "); } - buf.append(attribute.getID()); - buf.append('['); - buf.append(j); - buf.append("]="); - buf.append(attribute.get(j)); + builder.append(attribute.getID()); + builder.append('['); + builder.append(j); + builder.append("]="); + builder.append(attribute.get(j)); } } if (i.hasMore()) { - buf.append(", "); + builder.append(", "); } } } catch (NamingException e) { log.warn("Error in toString()"); } - buf.append('}'); + builder.append('}'); - return buf.toString(); + return builder.toString(); } /* diff --git a/core/src/main/java/org/springframework/ldap/core/DirContextOperations.java b/core/src/main/java/org/springframework/ldap/core/DirContextOperations.java index 52aab5ab..22acbf0f 100644 --- a/core/src/main/java/org/springframework/ldap/core/DirContextOperations.java +++ b/core/src/main/java/org/springframework/ldap/core/DirContextOperations.java @@ -182,7 +182,7 @@ public interface DirContextOperations extends DirContext, * @return a (possibly empty) array containing all registered values of the * attribute as Strings if the attribute is defined or null * otherwise. - * @throws ArrayStoreException if any of the attribute values is not a + * @throws IllegalArgumentException if any of the attribute values is not a * String. */ String[] getStringAttributes(String name); @@ -203,8 +203,9 @@ public interface DirContextOperations extends DirContext, * @param name name of the attribute. * @return a SortedSet containing all values of the attribute, * or null if the attribute does not exist. + * @throws IllegalArgumentException if one of the found attribute values cannot be cast to a String. */ - SortedSet getAttributeSortedStringSet(String name); + SortedSet getAttributeSortedStringSet(String name); /** * Returns the DN relative to the base path. diff --git a/core/src/main/java/org/springframework/ldap/core/LdapEncoder.java b/core/src/main/java/org/springframework/ldap/core/LdapEncoder.java index 199ea19b..46e74686 100644 --- a/core/src/main/java/org/springframework/ldap/core/LdapEncoder.java +++ b/core/src/main/java/org/springframework/ldap/core/LdapEncoder.java @@ -94,7 +94,7 @@ public class LdapEncoder { return null; // make buffer roomy - StringBuffer encodedValue = new StringBuffer(value.length() * 2); + StringBuilder encodedValue = new StringBuilder(value.length() * 2); int length = value.length(); @@ -132,7 +132,7 @@ public class LdapEncoder { return null; // make buffer roomy - StringBuffer encodedValue = new StringBuffer(value.length() * 2); + StringBuilder encodedValue = new StringBuilder(value.length() * 2); int length = value.length(); int last = length - 1; @@ -181,7 +181,7 @@ public class LdapEncoder { return null; // make buffer same size - StringBuffer decoded = new StringBuffer(value.length()); + StringBuilder decoded = new StringBuilder(value.length()); int i = 0; while (i < value.length()) { diff --git a/core/src/main/java/org/springframework/ldap/core/LdapEntryIdentificationContextMapper.java b/core/src/main/java/org/springframework/ldap/core/LdapEntryIdentificationContextMapper.java index 8e4f9ac5..988763dd 100644 --- a/core/src/main/java/org/springframework/ldap/core/LdapEntryIdentificationContextMapper.java +++ b/core/src/main/java/org/springframework/ldap/core/LdapEntryIdentificationContextMapper.java @@ -15,21 +15,22 @@ */ package org.springframework.ldap.core; +import org.springframework.ldap.core.support.AbstractContextMapper; import org.springframework.ldap.support.LdapUtils; /** * ContextMapper implementation that maps the found entries to the * {@link LdapEntryIdentification} of each respective entry. - * + * * @author Mattias Hellborg Arthursson * @since 1.3 */ -public class LdapEntryIdentificationContextMapper implements ContextMapper { +public class LdapEntryIdentificationContextMapper extends AbstractContextMapper { - public LdapEntryIdentification mapFromContext(Object ctx) { - DirContextOperations adapter = (DirContextOperations) ctx; - return new LdapEntryIdentification( - LdapUtils.newLdapName(adapter.getNameInNamespace()), - LdapUtils.newLdapName(adapter.getDn())); - } + @Override + public LdapEntryIdentification doMapFromContext(DirContextOperations adapter) { + return new LdapEntryIdentification( + LdapUtils.newLdapName(adapter.getNameInNamespace()), + LdapUtils.newLdapName(adapter.getDn())); + } } diff --git a/core/src/main/java/org/springframework/ldap/core/LdapOperations.java b/core/src/main/java/org/springframework/ldap/core/LdapOperations.java index 9cb724c8..9b19577b 100644 --- a/core/src/main/java/org/springframework/ldap/core/LdapOperations.java +++ b/core/src/main/java/org/springframework/ldap/core/LdapOperations.java @@ -262,7 +262,7 @@ public interface LdapOperations { * NameNotFoundException will be ignored. Instead this is * interpreted that no entries were found. */ - List search(String base, String filter, SearchControls controls, ContextMapper mapper, DirContextProcessor processor) + List search(String base, String filter, SearchControls controls, ContextMapper mapper, DirContextProcessor processor) throws NamingException; /** @@ -289,7 +289,7 @@ public interface LdapOperations { * NameNotFoundException will be ignored. Instead this is * interpreted that no entries were found. */ - List search(Name base, String filter, SearchControls controls, ContextMapper mapper, DirContextProcessor processor) + List search(Name base, String filter, SearchControls controls, ContextMapper mapper, DirContextProcessor processor) throws NamingException; /** diff --git a/core/src/main/java/org/springframework/ldap/core/LdapTemplate.java b/core/src/main/java/org/springframework/ldap/core/LdapTemplate.java index 2351917b..abb114f2 100644 --- a/core/src/main/java/org/springframework/ldap/core/LdapTemplate.java +++ b/core/src/main/java/org/springframework/ldap/core/LdapTemplate.java @@ -590,46 +590,24 @@ public class LdapTemplate implements LdapOperations, InitializingBean { return handler.getList(); } - /* - * (non-Javadoc) - * - * @see - * org.springframework.ldap.core.LdapOperations#search(java.lang.String, - * java.lang.String, javax.naming.directory.SearchControls, - * org.springframework.ldap.core.ContextMapper, - * org.springframework.ldap.core.DirContextProcessor) - */ - public List search(String base, String filter, SearchControls controls, ContextMapper mapper, + public List search(String base, String filter, SearchControls controls, ContextMapper mapper, DirContextProcessor processor) { assureReturnObjFlagSet(controls); - ContextMapperCallbackHandler handler = new ContextMapperCallbackHandler(mapper); + ContextMapperCallbackHandler handler = new ContextMapperCallbackHandler(mapper); search(base, filter, controls, handler, processor); return handler.getList(); } - /* - * (non-Javadoc) - * - * @see - * org.springframework.ldap.core.LdapOperations#search(javax.naming.Name, - * java.lang.String, javax.naming.directory.SearchControls, - * org.springframework.ldap.core.ContextMapper, - * org.springframework.ldap.core.DirContextProcessor) - */ - public List search(Name base, String filter, SearchControls controls, ContextMapper mapper, + public List search(Name base, String filter, SearchControls controls, ContextMapper mapper, DirContextProcessor processor) { assureReturnObjFlagSet(controls); - ContextMapperCallbackHandler handler = new ContextMapperCallbackHandler(mapper); + ContextMapperCallbackHandler handler = new ContextMapperCallbackHandler(mapper); search(base, filter, controls, handler, processor); return handler.getList(); } - /* - * @see org.springframework.ldap.core.LdapOperations#list(java.lang.String, - * org.springframework.ldap.core.NameClassPairCallbackHandler) - */ public void list(final String base, NameClassPairCallbackHandler handler) { SearchExecutor searchExecutor = new SearchExecutor() { public NamingEnumeration executeSearch(DirContext ctx) throws javax.naming.NamingException { diff --git a/core/src/main/java/org/springframework/ldap/core/support/AbstractContextSource.java b/core/src/main/java/org/springframework/ldap/core/support/AbstractContextSource.java index 3757cda8..0386f2dd 100644 --- a/core/src/main/java/org/springframework/ldap/core/support/AbstractContextSource.java +++ b/core/src/main/java/org/springframework/ldap/core/support/AbstractContextSource.java @@ -74,15 +74,16 @@ import java.util.Map; */ public abstract class AbstractContextSource implements BaseLdapPathContextSource, InitializingBean { - private static final Class DEFAULT_CONTEXT_FACTORY = com.sun.jndi.ldap.LdapCtxFactory.class; + private static final Class DEFAULT_CONTEXT_FACTORY + = com.sun.jndi.ldap.LdapCtxFactory.class; - private static final Class DEFAULT_DIR_OBJECT_FACTORY = DefaultDirObjectFactory.class; + private static final Class DEFAULT_DIR_OBJECT_FACTORY = DefaultDirObjectFactory.class; private static final boolean DONT_DISABLE_POOLING = false; private static final boolean EXPLICITLY_DISABLE_POOLING = true; - private Class dirObjectFactory = DEFAULT_DIR_OBJECT_FACTORY; + private Class dirObjectFactory = DEFAULT_DIR_OBJECT_FACTORY; - private Class contextFactory = DEFAULT_CONTEXT_FACTORY; + private Class contextFactory = DEFAULT_CONTEXT_FACTORY; private LdapName base = LdapUtils.emptyLdapName(); @@ -94,9 +95,9 @@ public abstract class AbstractContextSource implements BaseLdapPathContextSource private boolean pooled = false; - private Hashtable baseEnv = new Hashtable(); + private Hashtable baseEnv = new Hashtable(); - private Hashtable anonymousEnv; + private Hashtable anonymousEnv; private AuthenticationSource authenticationSource; @@ -121,7 +122,7 @@ public abstract class AbstractContextSource implements BaseLdapPathContextSource } private DirContext doGetContext(String principal, String credentials, boolean explicitlyDisablePooling) { - Hashtable env = getAuthenticatedEnv(principal, credentials); + Hashtable env = getAuthenticatedEnv(principal, credentials); if(explicitlyDisablePooling) { env.remove(SUN_LDAP_POOLING_FLAG); } @@ -179,7 +180,7 @@ public abstract class AbstractContextSource implements BaseLdapPathContextSource * @see DirContextAuthenticationStrategy * @see #setAuthenticationStrategy(DirContextAuthenticationStrategy) */ - protected void setupAuthenticatedEnvironment(Hashtable env, String principal, String credentials) { + protected void setupAuthenticatedEnvironment(Hashtable env, String principal, String credentials) { try { authenticationStrategy.setupEnvironment(env, principal, credentials); } @@ -199,6 +200,7 @@ public abstract class AbstractContextSource implements BaseLdapPathContextSource ctx.close(); } catch (Exception e) { + log.debug("Exception closing context", e); } } } @@ -211,17 +213,17 @@ public abstract class AbstractContextSource implements BaseLdapPathContextSource * @return the full url String */ protected String assembleProviderUrlString(String[] ldapUrls) { - StringBuffer providerUrlBuffer = new StringBuffer(1024); - for (int i = 0; i < ldapUrls.length; i++) { - providerUrlBuffer.append(ldapUrls[i]); - if (!base.isEmpty()) { - if (!ldapUrls[i].endsWith("/")) { - providerUrlBuffer.append("/"); - } - } - providerUrlBuffer.append(formatForUrl(base)); - providerUrlBuffer.append(' '); - } + StringBuilder providerUrlBuffer = new StringBuilder(1024); + for (String ldapUrl : ldapUrls) { + providerUrlBuffer.append(ldapUrl); + if (!base.isEmpty()) { + if (!ldapUrl.endsWith("/")) { + providerUrlBuffer.append("/"); + } + } + providerUrlBuffer.append(formatForUrl(base)); + providerUrlBuffer.append(' '); + } return providerUrlBuffer.toString().trim(); } @@ -300,7 +302,6 @@ public abstract class AbstractContextSource implements BaseLdapPathContextSource } /** - * @return * @deprecated {@link DistinguishedName} and associated classes and methods are deprecated as of 2.0. */ @Override @@ -313,12 +314,7 @@ public abstract class AbstractContextSource implements BaseLdapPathContextSource return (LdapName) base.clone(); } - /* - * (non-Javadoc) - * - * @seeorg.springframework.ldap.core.support.BaseLdapPathSource# - * getBaseLdapPathAsString() - */ + @Override public String getBaseLdapPathAsString() { return getBaseLdapName().toString(); } @@ -331,14 +327,14 @@ public abstract class AbstractContextSource implements BaseLdapPathContextSource * @return a new DirContext implementation initialized with the supplied * environment. */ - protected DirContext createContext(Hashtable environment) { + protected DirContext createContext(Hashtable environment) { DirContext ctx = null; try { ctx = getDirContextInstance(environment); if (log.isInfoEnabled()) { - Hashtable ctxEnv = ctx.getEnvironment(); + Hashtable ctxEnv = ctx.getEnvironment(); String ldapUrl = (String) ctxEnv.get(Context.PROVIDER_URL); log.debug("Got Ldap context on server '" + ldapUrl + "'"); } @@ -356,7 +352,7 @@ public abstract class AbstractContextSource implements BaseLdapPathContextSource * * @param contextFactory the context factory used when creating Contexts. */ - public void setContextFactory(Class contextFactory) { + public void setContextFactory(Class contextFactory) { this.contextFactory = contextFactory; } @@ -365,7 +361,7 @@ public abstract class AbstractContextSource implements BaseLdapPathContextSource * * @return the context factory used when creating Contexts. */ - public Class getContextFactory() { + public Class getContextFactory() { return contextFactory; } @@ -379,7 +375,7 @@ public abstract class AbstractContextSource implements BaseLdapPathContextSource * @param dirObjectFactory the DirObjectFactory to be used. Null means that * no DirObjectFactory will be used. */ - public void setDirObjectFactory(Class dirObjectFactory) { + public void setDirObjectFactory(Class dirObjectFactory) { this.dirObjectFactory = dirObjectFactory; } @@ -389,7 +385,7 @@ public abstract class AbstractContextSource implements BaseLdapPathContextSource * @return the DirObjectFactory to be used. null means that no * DirObjectFactory will be used. */ - public Class getDirObjectFactory() { + public Class getDirObjectFactory() { return dirObjectFactory; } @@ -424,7 +420,7 @@ public abstract class AbstractContextSource implements BaseLdapPathContextSource } } - private Hashtable setupAnonymousEnv() { + private Hashtable setupAnonymousEnv() { if (pooled) { baseEnv.put(SUN_LDAP_POOLING_FLAG, "true"); log.debug("Using LDAP pooling."); @@ -434,7 +430,7 @@ public abstract class AbstractContextSource implements BaseLdapPathContextSource log.debug("Not using LDAP pooling"); } - Hashtable env = new Hashtable(baseEnv); + Hashtable env = new Hashtable(baseEnv); env.put(Context.INITIAL_CONTEXT_FACTORY, contextFactory.getName()); env.put(Context.PROVIDER_URL, assembleProviderUrlString(urls)); @@ -483,7 +479,7 @@ public abstract class AbstractContextSource implements BaseLdapPathContextSource * @param urls the urls of all servers. */ public void setUrls(String[] urls) { - this.urls = (String[]) urls.clone(); + this.urls = urls.clone(); } /** @@ -492,7 +488,7 @@ public abstract class AbstractContextSource implements BaseLdapPathContextSource * @return the urls of all servers. */ public String[] getUrls() { - return (String[]) urls.clone(); + return urls.clone(); } /** @@ -540,17 +536,18 @@ public abstract class AbstractContextSource implements BaseLdapPathContextSource * If any custom environment properties are needed, these can be set using * this method. * - * @param baseEnvironmentProperties + * @param baseEnvironmentProperties the base environment properties that should always be used when + * creating new Context instances. */ - public void setBaseEnvironmentProperties(Map baseEnvironmentProperties) { - this.baseEnv = new Hashtable(baseEnvironmentProperties); + public void setBaseEnvironmentProperties(Map baseEnvironmentProperties) { + this.baseEnv = new Hashtable(baseEnvironmentProperties); } String getJdkVersion() { return JdkVersion.getJavaVersion(); } - protected Hashtable getAnonymousEnv() { + protected Hashtable getAnonymousEnv() { if (cacheEnvironmentProperties) { return anonymousEnv; } @@ -559,9 +556,9 @@ public abstract class AbstractContextSource implements BaseLdapPathContextSource } } - protected Hashtable getAuthenticatedEnv(String principal, String credentials) { + protected Hashtable getAuthenticatedEnv(String principal, String credentials) { // The authenticated environment should always be rebuilt. - Hashtable env = new Hashtable(getAnonymousEnv()); + Hashtable env = new Hashtable(getAnonymousEnv()); setupAuthenticatedEnvironment(env, principal, credentials); return env; } @@ -657,7 +654,7 @@ public abstract class AbstractContextSource implements BaseLdapPathContextSource * @return a new DirContext instance. * @throws NamingException if one is encountered when creating the instance. */ - protected abstract DirContext getDirContextInstance(Hashtable environment) throws NamingException; + protected abstract DirContext getDirContextInstance(Hashtable environment) throws NamingException; class SimpleAuthenticationSource implements AuthenticationSource { diff --git a/core/src/main/java/org/springframework/ldap/core/support/AbstractTlsDirContextAuthenticationStrategy.java b/core/src/main/java/org/springframework/ldap/core/support/AbstractTlsDirContextAuthenticationStrategy.java index d79c7fa3..db85f51d 100755 --- a/core/src/main/java/org/springframework/ldap/core/support/AbstractTlsDirContextAuthenticationStrategy.java +++ b/core/src/main/java/org/springframework/ldap/core/support/AbstractTlsDirContextAuthenticationStrategy.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,11 +15,9 @@ */ package org.springframework.ldap.core.support; -import java.io.IOException; -import java.lang.reflect.InvocationHandler; -import java.lang.reflect.Method; -import java.lang.reflect.Proxy; -import java.util.Hashtable; +import org.springframework.ldap.UncategorizedLdapException; +import org.springframework.ldap.core.DirContextProxy; +import org.springframework.ldap.support.LdapUtils; import javax.naming.NamingException; import javax.naming.directory.DirContext; @@ -28,10 +26,11 @@ import javax.naming.ldap.StartTlsRequest; import javax.naming.ldap.StartTlsResponse; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLSocketFactory; - -import org.springframework.ldap.UncategorizedLdapException; -import org.springframework.ldap.core.DirContextProxy; -import org.springframework.ldap.support.LdapUtils; +import java.io.IOException; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; +import java.util.Hashtable; /** * Abstract superclass for {@link DirContextAuthenticationStrategy} @@ -117,7 +116,7 @@ public abstract class AbstractTlsDirContextAuthenticationStrategy implements Dir /* (non-Javadoc) * @see org.springframework.ldap.core.support.DirContextAuthenticationStrategy#setupEnvironment(java.util.Hashtable, java.lang.String, java.lang.String) */ - public final void setupEnvironment(Hashtable env, String userDn, String password) { + public final void setupEnvironment(Hashtable env, String userDn, String password) { // Nothing to do in this implementation - authentication should take // place after TLS has been negotiated. } @@ -142,7 +141,7 @@ public abstract class AbstractTlsDirContextAuthenticationStrategy implements Dir // Wrap the target context in a proxy to intercept any calls // to 'close', so that we can shut down the TLS connection // gracefully first. - return (DirContext) Proxy.newProxyInstance(DirContextProxy.class.getClassLoader(), new Class[] { + return (DirContext) Proxy.newProxyInstance(DirContextProxy.class.getClassLoader(), new Class[] { LdapContext.class, DirContextProxy.class }, new TlsAwareDirContextProxy(ldapCtx, tlsResponse)); } diff --git a/core/src/main/java/org/springframework/ldap/core/support/AggregateDirContextProcessor.java b/core/src/main/java/org/springframework/ldap/core/support/AggregateDirContextProcessor.java index e2973ae1..dd626501 100644 --- a/core/src/main/java/org/springframework/ldap/core/support/AggregateDirContextProcessor.java +++ b/core/src/main/java/org/springframework/ldap/core/support/AggregateDirContextProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,14 +16,13 @@ package org.springframework.ldap.core.support; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; +import org.springframework.ldap.core.DirContextProcessor; import javax.naming.NamingException; import javax.naming.directory.DirContext; - -import org.springframework.ldap.core.DirContextProcessor; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; /** * Manages a sequence of {@link DirContextProcessor} instances. Applies @@ -35,7 +34,7 @@ import org.springframework.ldap.core.DirContextProcessor; */ public class AggregateDirContextProcessor implements DirContextProcessor { - private List dirContextProcessors = new LinkedList(); + private List dirContextProcessors = new LinkedList(); /** * Add the supplied DirContextProcessor to the list of managed objects. @@ -52,7 +51,7 @@ public class AggregateDirContextProcessor implements DirContextProcessor { * * @return the managed list of {@link DirContextProcessor} instances. */ - public List getDirContextProcessors() { + public List getDirContextProcessors() { return dirContextProcessors; } @@ -62,16 +61,15 @@ public class AggregateDirContextProcessor implements DirContextProcessor { * @param dirContextProcessors * the list of {@link DirContextProcessor} instances to set. */ - public void setDirContextProcessors(List dirContextProcessors) { - this.dirContextProcessors = dirContextProcessors; + public void setDirContextProcessors(List dirContextProcessors) { + this.dirContextProcessors = new ArrayList(dirContextProcessors); } /* * @see org.springframework.ldap.core.DirContextProcessor#preProcess(javax.naming.directory.DirContext) */ public void preProcess(DirContext ctx) throws NamingException { - for (Iterator iter = dirContextProcessors.iterator(); iter.hasNext();) { - DirContextProcessor processor = (DirContextProcessor) iter.next(); + for (DirContextProcessor processor : dirContextProcessors) { processor.preProcess(ctx); } } @@ -80,8 +78,7 @@ public class AggregateDirContextProcessor implements DirContextProcessor { * @see org.springframework.ldap.core.DirContextProcessor#postProcess(javax.naming.directory.DirContext) */ public void postProcess(DirContext ctx) throws NamingException { - for (Iterator iter = dirContextProcessors.iterator(); iter.hasNext();) { - DirContextProcessor processor = (DirContextProcessor) iter.next(); + for (DirContextProcessor processor : dirContextProcessors) { processor.postProcess(ctx); } } diff --git a/core/src/main/java/org/springframework/ldap/core/support/DefaultDirObjectFactory.java b/core/src/main/java/org/springframework/ldap/core/support/DefaultDirObjectFactory.java index c70f3400..038dea5b 100644 --- a/core/src/main/java/org/springframework/ldap/core/support/DefaultDirObjectFactory.java +++ b/core/src/main/java/org/springframework/ldap/core/support/DefaultDirObjectFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -45,7 +45,7 @@ public class DefaultDirObjectFactory implements DirObjectFactory { * Key to use in the ContextSource implementation to store the value of the * base path suffix, if any, in the Ldap Environment. * - * @deprecated Use {@link BaseLdapPathAware} and + * @deprecated Use {@link BaseLdapNameAware} and * {@link BaseLdapPathBeanPostProcessor} instead. */ public static final String JNDI_ENV_BASE_PATH_KEY = "org.springframework.ldap.base.path"; @@ -62,11 +62,15 @@ public class DefaultDirObjectFactory implements DirObjectFactory { * javax.naming.Name, javax.naming.Context, java.util.Hashtable, * javax.naming.directory.Attributes) */ - public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment, Attributes attrs) - throws Exception { + public final Object getObjectInstance( + Object obj, + Name name, + Context nameCtx, + Hashtable environment, + Attributes attrs) throws Exception { try { - String nameInNamespace = null; + String nameInNamespace; if (nameCtx != null) { nameInNamespace = nameCtx.getNameInNamespace(); } @@ -114,7 +118,7 @@ public class DefaultDirObjectFactory implements DirObjectFactory { * information. */ DirContextAdapter constructAdapterFromName(Attributes attrs, Name name, String nameInNamespace) { - String nameString = ""; + String nameString; String referralUrl = ""; if (name instanceof CompositeName) { @@ -181,7 +185,7 @@ public class DefaultDirObjectFactory implements DirObjectFactory { * @see javax.naming.spi.ObjectFactory#getObjectInstance(java.lang.Object, * javax.naming.Name, javax.naming.Context, java.util.Hashtable) */ - public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment) throws Exception { + public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment) throws Exception { return null; } diff --git a/core/src/main/java/org/springframework/ldap/core/support/DefaultIncrementalAttributesMapper.java b/core/src/main/java/org/springframework/ldap/core/support/DefaultIncrementalAttributesMapper.java index 3fb5066d..5e943274 100644 --- a/core/src/main/java/org/springframework/ldap/core/support/DefaultIncrementalAttributesMapper.java +++ b/core/src/main/java/org/springframework/ldap/core/support/DefaultIncrementalAttributesMapper.java @@ -173,9 +173,9 @@ public class DefaultIncrementalAttributesMapper implements IncrementalAttributes // Reset the affected attributes. rangedAttributesInNextIteration = new HashSet(); - NamingEnumeration attributeNameEnum = attributes.getIDs(); + NamingEnumeration attributeNameEnum = attributes.getIDs(); while (attributeNameEnum.hasMore()) { - String attributeName = (String) attributeNameEnum.next(); + String attributeName = attributeNameEnum.next(); String[] attributeNameSplit = attributeName.split(";"); IncrementalAttributeState state = getState(attributeNameSplit[0]); @@ -222,7 +222,7 @@ public class DefaultIncrementalAttributesMapper implements IncrementalAttributes Set attributeNames = stateMap.keySet(); for (String attributeName : attributeNames) { BasicAttribute oneAttribute = new BasicAttribute(attributeName); - List values = getValues(attributeName); + List values = getValues(attributeName); if (values != null) { for (Object oneValue : values) { oneAttribute.add(oneValue); @@ -400,7 +400,7 @@ public class DefaultIncrementalAttributesMapper implements IncrementalAttributes @Override public void processValues(Attributes attributes, String attributeName) throws NamingException { Attribute attribute = attributes.get(attributeName); - NamingEnumeration valueEnum = attribute.getAll(); + NamingEnumeration valueEnum = attribute.getAll(); initValuesIfApplicable(); while (valueEnum.hasMore()) { diff --git a/core/src/main/java/org/springframework/ldap/core/support/DigestMd5DirContextAuthenticationStrategy.java b/core/src/main/java/org/springframework/ldap/core/support/DigestMd5DirContextAuthenticationStrategy.java index e952bbe0..530552ba 100644 --- a/core/src/main/java/org/springframework/ldap/core/support/DigestMd5DirContextAuthenticationStrategy.java +++ b/core/src/main/java/org/springframework/ldap/core/support/DigestMd5DirContextAuthenticationStrategy.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,10 +15,9 @@ */ package org.springframework.ldap.core.support; -import java.util.Hashtable; - import javax.naming.Context; import javax.naming.directory.DirContext; +import java.util.Hashtable; /** * Authentication strategy for LDAP DIGEST-MD5 SASL mechanism. @@ -45,7 +44,7 @@ public class DigestMd5DirContextAuthenticationStrategy implements DirContextAuth * @see org.springframework.ldap.core.support.DirContextAuthenticationStrategy#setupEnvironment(java.util.Hashtable, * java.lang.String, java.lang.String) */ - public void setupEnvironment(Hashtable env, String userDn, String password) { + public void setupEnvironment(Hashtable env, String userDn, String password) { env.put(Context.SECURITY_AUTHENTICATION, DIGEST_MD5_AUTHENTICATION); // userDn should be a bare username for DIGEST-MD5 env.put(Context.SECURITY_PRINCIPAL, userDn); diff --git a/core/src/main/java/org/springframework/ldap/core/support/DirContextAuthenticationStrategy.java b/core/src/main/java/org/springframework/ldap/core/support/DirContextAuthenticationStrategy.java index f28ffec5..7d64d2f3 100755 --- a/core/src/main/java/org/springframework/ldap/core/support/DirContextAuthenticationStrategy.java +++ b/core/src/main/java/org/springframework/ldap/core/support/DirContextAuthenticationStrategy.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,13 +15,12 @@ */ package org.springframework.ldap.core.support; -import java.util.Hashtable; +import org.springframework.ldap.core.AuthenticationSource; +import org.springframework.ldap.core.ContextSource; import javax.naming.NamingException; import javax.naming.directory.DirContext; - -import org.springframework.ldap.core.AuthenticationSource; -import org.springframework.ldap.core.ContextSource; +import java.util.Hashtable; /** * A strategy to use when authenticating LDAP connections on creation. When @@ -57,7 +56,7 @@ public interface DirContextAuthenticationStrategy { * DirContext creation to be aborted and the exception to be * translated and rethrown. */ - public void setupEnvironment(Hashtable env, String userDn, String password) throws NamingException; + public void setupEnvironment(Hashtable env, String userDn, String password) throws NamingException; /** * This method is responsible for post-processing the diff --git a/core/src/main/java/org/springframework/ldap/core/support/ExternalTlsDirContextAuthenticationStrategy.java b/core/src/main/java/org/springframework/ldap/core/support/ExternalTlsDirContextAuthenticationStrategy.java index 4475339e..c60442b5 100755 --- a/core/src/main/java/org/springframework/ldap/core/support/ExternalTlsDirContextAuthenticationStrategy.java +++ b/core/src/main/java/org/springframework/ldap/core/support/ExternalTlsDirContextAuthenticationStrategy.java @@ -1,3 +1,19 @@ +/* + * Copyright 2005-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.springframework.ldap.core.support; import javax.naming.Context; diff --git a/core/src/main/java/org/springframework/ldap/core/support/LdapContextSource.java b/core/src/main/java/org/springframework/ldap/core/support/LdapContextSource.java index 0670b483..aed68bf0 100644 --- a/core/src/main/java/org/springframework/ldap/core/support/LdapContextSource.java +++ b/core/src/main/java/org/springframework/ldap/core/support/LdapContextSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,11 +16,10 @@ package org.springframework.ldap.core.support; -import java.util.Hashtable; - import javax.naming.NamingException; import javax.naming.directory.DirContext; import javax.naming.ldap.InitialLdapContext; +import java.util.Hashtable; /** * ContextSource implementation which creates an InitialLdapContext @@ -38,7 +37,7 @@ public class LdapContextSource extends AbstractContextSource { /* * @see org.springframework.ldap.support.AbstractContextSource#getDirContextInstance(java.util.Hashtable) */ - protected DirContext getDirContextInstance(Hashtable environment) + protected DirContext getDirContextInstance(Hashtable environment) throws NamingException { return new InitialLdapContext(environment, null); } diff --git a/core/src/main/java/org/springframework/ldap/core/support/SimpleDirContextAuthenticationStrategy.java b/core/src/main/java/org/springframework/ldap/core/support/SimpleDirContextAuthenticationStrategy.java index a73f7a30..10b92dc8 100755 --- a/core/src/main/java/org/springframework/ldap/core/support/SimpleDirContextAuthenticationStrategy.java +++ b/core/src/main/java/org/springframework/ldap/core/support/SimpleDirContextAuthenticationStrategy.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,10 +15,9 @@ */ package org.springframework.ldap.core.support; -import java.util.Hashtable; - import javax.naming.Context; import javax.naming.directory.DirContext; +import java.util.Hashtable; /** * The default {@link DirContextAuthenticationStrategy} implementation, setting @@ -38,7 +37,7 @@ public class SimpleDirContextAuthenticationStrategy implements DirContextAuthent * @see org.springframework.ldap.core.support.DirContextAuthenticationStrategy#setupEnvironment(java.util.Hashtable, * java.lang.String, java.lang.String) */ - public void setupEnvironment(Hashtable env, String userDn, String password) { + public void setupEnvironment(Hashtable env, String userDn, String password) { env.put(Context.SECURITY_AUTHENTICATION, SIMPLE_AUTHENTICATION); env.put(Context.SECURITY_PRINCIPAL, userDn); env.put(Context.SECURITY_CREDENTIALS, password); diff --git a/core/src/main/java/org/springframework/ldap/core/support/SingleContextSource.java b/core/src/main/java/org/springframework/ldap/core/support/SingleContextSource.java index ad90b922..2ba7e439 100644 --- a/core/src/main/java/org/springframework/ldap/core/support/SingleContextSource.java +++ b/core/src/main/java/org/springframework/ldap/core/support/SingleContextSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,20 +15,19 @@ */ package org.springframework.ldap.core.support; -import java.lang.reflect.InvocationHandler; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.lang.reflect.Proxy; - -import javax.naming.directory.DirContext; - import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.springframework.beans.factory.DisposableBean; import org.springframework.ldap.NamingException; import org.springframework.ldap.core.ContextSource; import org.springframework.ldap.core.DirContextProxy; import org.springframework.ldap.support.LdapUtils; -import org.springframework.beans.factory.DisposableBean; + +import javax.naming.directory.DirContext; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; /** * A {@link ContextSource} to be used as a decorator around a target ContextSource @@ -68,7 +67,7 @@ public class SingleContextSource implements ContextSource, DisposableBean { private DirContext getNonClosingDirContextProxy(DirContext context) { return (DirContext) Proxy.newProxyInstance(DirContextProxy.class - .getClassLoader(), new Class[]{ + .getClassLoader(), new Class[]{ LdapUtils.getActualTargetClass(context), DirContextProxy.class}, new SingleContextSource.NonClosingDirContextInvocationHandler( @@ -125,7 +124,7 @@ public class SingleContextSource implements ContextSource, DisposableBean { return (proxy == args[0] ? Boolean.TRUE : Boolean.FALSE); } else if (methodName.equals("hashCode")) { // Use hashCode of Connection proxy. - return new Integer(proxy.hashCode()); + return proxy.hashCode(); } else if (methodName.equals("close")) { // Never close the target context, as this class will only be // used for operations concerning the compensating transactions. diff --git a/core/src/main/java/org/springframework/ldap/filter/AbstractFilter.java b/core/src/main/java/org/springframework/ldap/filter/AbstractFilter.java index e1d23b61..bce3ad58 100644 --- a/core/src/main/java/org/springframework/ldap/filter/AbstractFilter.java +++ b/core/src/main/java/org/springframework/ldap/filter/AbstractFilter.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,10 +24,6 @@ package org.springframework.ldap.filter; */ public abstract class AbstractFilter implements Filter { - protected AbstractFilter() { - super(); - } - /* * @see org.springframework.ldap.filter.Filter#encode(java.lang.StringBuffer) */ diff --git a/core/src/main/java/org/springframework/ldap/filter/BinaryLogicalFilter.java b/core/src/main/java/org/springframework/ldap/filter/BinaryLogicalFilter.java index da7fded0..f0cf5dd8 100644 --- a/core/src/main/java/org/springframework/ldap/filter/BinaryLogicalFilter.java +++ b/core/src/main/java/org/springframework/ldap/filter/BinaryLogicalFilter.java @@ -15,7 +15,6 @@ */ package org.springframework.ldap.filter; -import java.util.Iterator; import java.util.LinkedList; import java.util.List; @@ -27,13 +26,8 @@ import java.util.List; */ public abstract class BinaryLogicalFilter extends AbstractFilter { - protected List queryList = new LinkedList(); + protected List queryList = new LinkedList(); - /* - * @see - * org.springframework.ldap.filter.AbstractFilter#encode(java.lang.StringBuffer - * ) - */ public StringBuffer encode(StringBuffer buff) { if (queryList.size() <= 0) { @@ -44,17 +38,16 @@ public abstract class BinaryLogicalFilter extends AbstractFilter { else if (queryList.size() == 1) { // don't add the & - Filter query = (Filter) queryList.get(0); + Filter query = queryList.get(0); return query.encode(buff); } else { - buff.append("(" + getLogicalOperator()); + buff.append("(").append(getLogicalOperator()); - for (Iterator i = queryList.iterator(); i.hasNext();) { - Filter query = (Filter) i.next(); - buff = query.encode(buff); - } + for (Filter query : queryList) { + buff = query.encode(buff); + } buff.append(")"); diff --git a/core/src/main/java/org/springframework/ldap/filter/GreaterThanOrEqualsFilter.java b/core/src/main/java/org/springframework/ldap/filter/GreaterThanOrEqualsFilter.java index cfddf0ee..274b6580 100644 --- a/core/src/main/java/org/springframework/ldap/filter/GreaterThanOrEqualsFilter.java +++ b/core/src/main/java/org/springframework/ldap/filter/GreaterThanOrEqualsFilter.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -45,9 +45,6 @@ public class GreaterThanOrEqualsFilter extends CompareFilter { super(attribute, value); } - /* - * @see org.springframework.ldap.filter.CompareFilter#getCompareString() - */ protected String getCompareString() { return GREATER_THAN_OR_EQUALS; } diff --git a/core/src/main/java/org/springframework/ldap/filter/HardcodedFilter.java b/core/src/main/java/org/springframework/ldap/filter/HardcodedFilter.java index c6c588f9..42007d32 100644 --- a/core/src/main/java/org/springframework/ldap/filter/HardcodedFilter.java +++ b/core/src/main/java/org/springframework/ldap/filter/HardcodedFilter.java @@ -58,10 +58,6 @@ public class HardcodedFilter extends AbstractFilter { this.filter = filter; } - /* - * (non-Javadoc) - * @see org.springframework.ldap.filter.AbstractFilter#encode(java.lang.StringBuffer) - */ public StringBuffer encode(StringBuffer buff) { if (!StringUtils.hasLength(filter)) { return buff; diff --git a/core/src/main/java/org/springframework/ldap/filter/LessThanOrEqualsFilter.java b/core/src/main/java/org/springframework/ldap/filter/LessThanOrEqualsFilter.java index 17a146e9..062cbd69 100644 --- a/core/src/main/java/org/springframework/ldap/filter/LessThanOrEqualsFilter.java +++ b/core/src/main/java/org/springframework/ldap/filter/LessThanOrEqualsFilter.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -45,9 +45,6 @@ public class LessThanOrEqualsFilter extends CompareFilter { super(attribute, value); } - /* - * @see org.springframework.ldap.filter.CompareFilter#getCompareString() - */ protected String getCompareString() { return LESS_THAN_OR_EQUALS; } diff --git a/core/src/main/java/org/springframework/ldap/filter/LikeFilter.java b/core/src/main/java/org/springframework/ldap/filter/LikeFilter.java index 183f8a87..01a99862 100644 --- a/core/src/main/java/org/springframework/ldap/filter/LikeFilter.java +++ b/core/src/main/java/org/springframework/ldap/filter/LikeFilter.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,9 +42,6 @@ public class LikeFilter extends EqualsFilter { super(attribute, value); } - /* - * @see org.springframework.ldap.filter.CompareFilter#encodeValue(java.lang.String) - */ protected String encodeValue(String value) { // just return if blank string if (value == null) { @@ -57,17 +54,12 @@ public class LikeFilter extends EqualsFilter { return LdapEncoder.filterEncode(substrings[0]); } - StringBuffer buff = new StringBuffer(); + StringBuilder buff = new StringBuilder(); for (int i = 0; i < substrings.length; i++) { buff.append(LdapEncoder.filterEncode(substrings[i])); if (i < substrings.length - 1) { buff.append("*"); } - else { - if (substrings[i].equals("")) { - continue; - } - } } return buff.toString(); diff --git a/core/src/main/java/org/springframework/ldap/filter/NotFilter.java b/core/src/main/java/org/springframework/ldap/filter/NotFilter.java index 4992f5e7..121443d9 100644 --- a/core/src/main/java/org/springframework/ldap/filter/NotFilter.java +++ b/core/src/main/java/org/springframework/ldap/filter/NotFilter.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.ldap.filter; import org.springframework.util.Assert; @@ -47,9 +48,6 @@ public class NotFilter extends AbstractFilter { this.filter = filter; } - /* - * @see org.springframework.ldap.filter.AbstractFilter#encode(java.lang.StringBuffer) - */ public StringBuffer encode(StringBuffer buff) { buff.append("(!"); diff --git a/core/src/main/java/org/springframework/ldap/filter/OrFilter.java b/core/src/main/java/org/springframework/ldap/filter/OrFilter.java index cf328826..497c7a10 100644 --- a/core/src/main/java/org/springframework/ldap/filter/OrFilter.java +++ b/core/src/main/java/org/springframework/ldap/filter/OrFilter.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -47,9 +47,6 @@ public class OrFilter extends BinaryLogicalFilter { return this; } - /* - * @see org.springframework.ldap.filter.BinaryLogicalFilter#getLogicalOperator() - */ protected String getLogicalOperator() { return PIPE_SIGN; } diff --git a/core/src/main/java/org/springframework/ldap/filter/WhitespaceWildcardsFilter.java b/core/src/main/java/org/springframework/ldap/filter/WhitespaceWildcardsFilter.java index e70b8655..75442d5e 100644 --- a/core/src/main/java/org/springframework/ldap/filter/WhitespaceWildcardsFilter.java +++ b/core/src/main/java/org/springframework/ldap/filter/WhitespaceWildcardsFilter.java @@ -42,9 +42,6 @@ public class WhitespaceWildcardsFilter extends EqualsFilter { super(attribute, value); } - /* - * @see org.springframework.ldap.filter.CompareFilter#encodeValue(java.lang.String) - */ protected String encodeValue(String value) { // blank string means just ONE star diff --git a/core/src/main/java/org/springframework/ldap/pool/DelegatingContext.java b/core/src/main/java/org/springframework/ldap/pool/DelegatingContext.java index d88df0b1..1bca7781 100644 --- a/core/src/main/java/org/springframework/ldap/pool/DelegatingContext.java +++ b/core/src/main/java/org/springframework/ldap/pool/DelegatingContext.java @@ -19,8 +19,10 @@ import org.apache.commons.pool.KeyedObjectPool; import org.springframework.ldap.pool.factory.PoolingContextSource; import org.springframework.util.Assert; +import javax.naming.Binding; import javax.naming.Context; import javax.naming.Name; +import javax.naming.NameClassPair; import javax.naming.NameParser; import javax.naming.NamingEnumeration; import javax.naming.NamingException; @@ -229,7 +231,7 @@ public class DelegatingContext implements Context { /** * @see javax.naming.Context#getEnvironment() */ - public Hashtable getEnvironment() throws NamingException { + public Hashtable getEnvironment() throws NamingException { this.assertOpen(); return this.getDelegateContext().getEnvironment(); } @@ -261,7 +263,7 @@ public class DelegatingContext implements Context { /** * @see javax.naming.Context#list(javax.naming.Name) */ - public NamingEnumeration list(Name name) throws NamingException { + public NamingEnumeration list(Name name) throws NamingException { this.assertOpen(); return this.getDelegateContext().list(name); } @@ -269,7 +271,7 @@ public class DelegatingContext implements Context { /** * @see javax.naming.Context#list(java.lang.String) */ - public NamingEnumeration list(String name) throws NamingException { + public NamingEnumeration list(String name) throws NamingException { this.assertOpen(); return this.getDelegateContext().list(name); } @@ -277,7 +279,7 @@ public class DelegatingContext implements Context { /** * @see javax.naming.Context#listBindings(javax.naming.Name) */ - public NamingEnumeration listBindings(Name name) throws NamingException { + public NamingEnumeration listBindings(Name name) throws NamingException { this.assertOpen(); return this.getDelegateContext().listBindings(name); } @@ -285,7 +287,7 @@ public class DelegatingContext implements Context { /** * @see javax.naming.Context#listBindings(java.lang.String) */ - public NamingEnumeration listBindings(String name) throws NamingException { + public NamingEnumeration listBindings(String name) throws NamingException { this.assertOpen(); return this.getDelegateContext().listBindings(name); } diff --git a/core/src/main/java/org/springframework/ldap/pool/DelegatingDirContext.java b/core/src/main/java/org/springframework/ldap/pool/DelegatingDirContext.java index 686e536a..80d42de1 100644 --- a/core/src/main/java/org/springframework/ldap/pool/DelegatingDirContext.java +++ b/core/src/main/java/org/springframework/ldap/pool/DelegatingDirContext.java @@ -28,6 +28,7 @@ import javax.naming.directory.Attributes; import javax.naming.directory.DirContext; import javax.naming.directory.ModificationItem; import javax.naming.directory.SearchControls; +import javax.naming.directory.SearchResult; /** @@ -285,7 +286,7 @@ public class DelegatingDirContext extends DelegatingContext implements DirContex /** * @see javax.naming.directory.DirContext#search(javax.naming.Name, javax.naming.directory.Attributes, java.lang.String[]) */ - public NamingEnumeration search(Name name, Attributes matchingAttributes, String[] attributesToReturn) throws NamingException { + public NamingEnumeration search(Name name, Attributes matchingAttributes, String[] attributesToReturn) throws NamingException { this.assertOpen(); return this.getDelegateDirContext().search(name, matchingAttributes, attributesToReturn); } @@ -293,7 +294,7 @@ public class DelegatingDirContext extends DelegatingContext implements DirContex /** * @see javax.naming.directory.DirContext#search(javax.naming.Name, javax.naming.directory.Attributes) */ - public NamingEnumeration search(Name name, Attributes matchingAttributes) throws NamingException { + public NamingEnumeration search(Name name, Attributes matchingAttributes) throws NamingException { this.assertOpen(); return this.getDelegateDirContext().search(name, matchingAttributes); } @@ -301,7 +302,7 @@ public class DelegatingDirContext extends DelegatingContext implements DirContex /** * @see javax.naming.directory.DirContext#search(javax.naming.Name, java.lang.String, java.lang.Object[], javax.naming.directory.SearchControls) */ - public NamingEnumeration search(Name name, String filterExpr, Object[] filterArgs, SearchControls cons) throws NamingException { + public NamingEnumeration search(Name name, String filterExpr, Object[] filterArgs, SearchControls cons) throws NamingException { this.assertOpen(); return this.getDelegateDirContext().search(name, filterExpr, filterArgs, cons); } @@ -309,7 +310,7 @@ public class DelegatingDirContext extends DelegatingContext implements DirContex /** * @see javax.naming.directory.DirContext#search(javax.naming.Name, java.lang.String, javax.naming.directory.SearchControls) */ - public NamingEnumeration search(Name name, String filter, SearchControls cons) throws NamingException { + public NamingEnumeration search(Name name, String filter, SearchControls cons) throws NamingException { this.assertOpen(); return this.getDelegateDirContext().search(name, filter, cons); } @@ -317,7 +318,7 @@ public class DelegatingDirContext extends DelegatingContext implements DirContex /** * @see javax.naming.directory.DirContext#search(java.lang.String, javax.naming.directory.Attributes, java.lang.String[]) */ - public NamingEnumeration search(String name, Attributes matchingAttributes, String[] attributesToReturn) throws NamingException { + public NamingEnumeration search(String name, Attributes matchingAttributes, String[] attributesToReturn) throws NamingException { this.assertOpen(); return this.getDelegateDirContext().search(name, matchingAttributes, attributesToReturn); } @@ -325,7 +326,7 @@ public class DelegatingDirContext extends DelegatingContext implements DirContex /** * @see javax.naming.directory.DirContext#search(java.lang.String, javax.naming.directory.Attributes) */ - public NamingEnumeration search(String name, Attributes matchingAttributes) throws NamingException { + public NamingEnumeration search(String name, Attributes matchingAttributes) throws NamingException { this.assertOpen(); return this.getDelegateDirContext().search(name, matchingAttributes); } @@ -333,7 +334,7 @@ public class DelegatingDirContext extends DelegatingContext implements DirContex /** * @see javax.naming.directory.DirContext#search(java.lang.String, java.lang.String, java.lang.Object[], javax.naming.directory.SearchControls) */ - public NamingEnumeration search(String name, String filterExpr, Object[] filterArgs, SearchControls cons) throws NamingException { + public NamingEnumeration search(String name, String filterExpr, Object[] filterArgs, SearchControls cons) throws NamingException { this.assertOpen(); return this.getDelegateDirContext().search(name, filterExpr, filterArgs, cons); } @@ -341,7 +342,7 @@ public class DelegatingDirContext extends DelegatingContext implements DirContex /** * @see javax.naming.directory.DirContext#search(java.lang.String, java.lang.String, javax.naming.directory.SearchControls) */ - public NamingEnumeration search(String name, String filter, SearchControls cons) throws NamingException { + public NamingEnumeration search(String name, String filter, SearchControls cons) throws NamingException { this.assertOpen(); return this.getDelegateDirContext().search(name, filter, cons); } diff --git a/core/src/main/java/org/springframework/ldap/pool/DirContextType.java b/core/src/main/java/org/springframework/ldap/pool/DirContextType.java index b6bd0c98..ac0de7a2 100644 --- a/core/src/main/java/org/springframework/ldap/pool/DirContextType.java +++ b/core/src/main/java/org/springframework/ldap/pool/DirContextType.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,10 +16,10 @@ package org.springframework.ldap.pool; -import javax.naming.directory.DirContext; - import org.springframework.ldap.core.ContextSource; +import javax.naming.directory.DirContext; + /** * An enum representing the two types of {@link DirContext}s that can be returned by a @@ -34,9 +34,6 @@ public final class DirContextType { this.name = name; } - /* (non-Javadoc) - * @see java.lang.Object#toString() - */ public String toString() { return name; } diff --git a/core/src/main/java/org/springframework/ldap/pool/MutableDelegatingLdapContext.java b/core/src/main/java/org/springframework/ldap/pool/MutableDelegatingLdapContext.java index 482ff686..d483d896 100644 --- a/core/src/main/java/org/springframework/ldap/pool/MutableDelegatingLdapContext.java +++ b/core/src/main/java/org/springframework/ldap/pool/MutableDelegatingLdapContext.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,13 +16,13 @@ package org.springframework.ldap.pool; +import org.apache.commons.pool.KeyedObjectPool; +import org.springframework.ldap.pool.factory.MutablePoolingContextSource; + import javax.naming.NamingException; import javax.naming.ldap.Control; import javax.naming.ldap.LdapContext; -import org.apache.commons.pool.KeyedObjectPool; -import org.springframework.ldap.pool.factory.MutablePoolingContextSource; - /** * Used by {@link MutablePoolingContextSource} to wrap a {@link LdapContext}, * delegating most methods to the underlying context. This class extends @@ -49,11 +49,6 @@ public class MutableDelegatingLdapContext extends DelegatingLdapContext { super(keyedObjectPool, delegateLdapContext, dirContextType); } - /* - * @see - * org.springframework.ldap.pool.DelegatingLdapContext#setRequestControls - * (javax.naming.ldap.Control[]) - */ public void setRequestControls(Control[] requestControls) throws NamingException { assertOpen(); getDelegateLdapContext().setRequestControls(requestControls); diff --git a/core/src/main/java/org/springframework/ldap/pool/validation/DefaultDirContextValidator.java b/core/src/main/java/org/springframework/ldap/pool/validation/DefaultDirContextValidator.java index f5939954..12440d99 100644 --- a/core/src/main/java/org/springframework/ldap/pool/validation/DefaultDirContextValidator.java +++ b/core/src/main/java/org/springframework/ldap/pool/validation/DefaultDirContextValidator.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.ldap.pool.validation; import org.apache.commons.logging.Log; @@ -23,6 +24,7 @@ import org.springframework.util.Assert; import javax.naming.NamingEnumeration; import javax.naming.directory.DirContext; import javax.naming.directory.SearchControls; +import javax.naming.directory.SearchResult; /** * Default {@link DirContext} validator that executes {@link DirContext#search(String, String, SearchControls)}. The @@ -162,7 +164,7 @@ public class DefaultDirContextValidator implements DirContextValidator { Assert.notNull(dirContext, "dirContext may not be null"); try { - final NamingEnumeration searchResults = dirContext.search(this.base, this.filter, this.searchControls); + final NamingEnumeration searchResults = dirContext.search(this.base, this.filter, this.searchControls); if (searchResults.hasMore()) { if (this.logger.isDebugEnabled()) { diff --git a/core/src/main/java/org/springframework/ldap/support/LdapNameBuilder.java b/core/src/main/java/org/springframework/ldap/support/LdapNameBuilder.java index 461361e6..f851c801 100644 --- a/core/src/main/java/org/springframework/ldap/support/LdapNameBuilder.java +++ b/core/src/main/java/org/springframework/ldap/support/LdapNameBuilder.java @@ -24,7 +24,7 @@ import javax.naming.ldap.LdapName; import javax.naming.ldap.Rdn; /** - * Helper class for building {@javax.naming.ldap.LdapName} instances. + * Helper class for building {@link javax.naming.ldap.LdapName} instances. * * @author Mattias Hellborg Arthursson * @since 2.0 diff --git a/core/src/main/java/org/springframework/ldap/support/LdapUtils.java b/core/src/main/java/org/springframework/ldap/support/LdapUtils.java index b15a447e..753166ed 100644 --- a/core/src/main/java/org/springframework/ldap/support/LdapUtils.java +++ b/core/src/main/java/org/springframework/ldap/support/LdapUtils.java @@ -251,16 +251,39 @@ public final class LdapUtils { * exists. * @since 1.3 */ - public static void collectAttributeValues(Attributes attributes, String name, Collection collection) { - Assert.notNull(attributes, "Attributes must not be null"); - Attribute attribute = attributes.get(name); - if (attribute == null) { - throw new NoSuchAttributeException("No attribute with name '" + name + "'"); - } - - iterateAttributeValues(attribute, new CollectingAttributeValueCallbackHandler(collection)); + public static void collectAttributeValues(Attributes attributes, String name, Collection collection) { + collectAttributeValues(attributes, name, collection, Object.class); } + /** + * Collect all the values of a the specified attribute from the supplied + * Attributes as the specified class. + * + * @param attributes The Attributes; not null. + * @param name The name of the Attribute to get values for. + * @param collection the collection to collect the values in. + * @param clazz the class of the collected attribute values + * @throws NoSuchAttributeException if no attribute with the specified name + * exists. + * @throws IllegalArgumentException if an attribute value cannot be cast to the specified class. + * @since 2.0 + */ + public static void collectAttributeValues( + Attributes attributes, String name, Collection collection, Class clazz) { + + Assert.notNull(attributes, "Attributes must not be null"); + Assert.hasText(name, "Name must not be empty"); + Assert.notNull(collection, "Collection must not be null"); + + Attribute attribute = attributes.get(name); + if (attribute == null) { + throw new NoSuchAttributeException("No attribute with name '" + name + "'"); + } + + iterateAttributeValues(attribute, new CollectingAttributeValueCallbackHandler(collection, clazz)); + + } + /** * Iterate through all the values of the specified Attribute calling back to * the specified callbackHandler. @@ -288,16 +311,21 @@ public final class LdapUtils { * * @author Mattias Hellborg Arthursson */ - private final static class CollectingAttributeValueCallbackHandler implements AttributeValueCallbackHandler { - private final Collection collection; + private final static class CollectingAttributeValueCallbackHandler implements AttributeValueCallbackHandler { + private final Collection collection; + private final Class clazz; + + public CollectingAttributeValueCallbackHandler(Collection collection, Class clazz) { + Assert.notNull(collection, "Collection must not be null"); + Assert.notNull(clazz, "Clazz parameter must not be null"); - public CollectingAttributeValueCallbackHandler(Collection collection) { - Assert.notNull(collection, "Collection must not be null"); this.collection = collection; + this.clazz = clazz; } public final void handleAttributeValue(String attributeName, Object attributeValue, int index) { - collection.add(attributeValue); + Assert.isTrue(attributeName == null || clazz.isAssignableFrom(attributeValue.getClass())); + collection.add(clazz.cast(attributeValue)); } } diff --git a/core/src/main/java/org/springframework/ldap/transaction/compensating/ModifyAttributesOperationExecutor.java b/core/src/main/java/org/springframework/ldap/transaction/compensating/ModifyAttributesOperationExecutor.java index 8e531c75..e8726014 100644 --- a/core/src/main/java/org/springframework/ldap/transaction/compensating/ModifyAttributesOperationExecutor.java +++ b/core/src/main/java/org/springframework/ldap/transaction/compensating/ModifyAttributesOperationExecutor.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,14 +15,14 @@ */ package org.springframework.ldap.transaction.compensating; -import javax.naming.Name; -import javax.naming.directory.ModificationItem; - import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.ldap.core.LdapOperations; import org.springframework.transaction.compensating.CompensatingTransactionOperationExecutor; +import javax.naming.Name; +import javax.naming.directory.ModificationItem; + /** * A {@link CompensatingTransactionOperationExecutor} to manage a * modifyAttributes operation. Performs a @@ -65,8 +65,8 @@ public class ModifyAttributesOperationExecutor implements ModificationItem[] compensatingModifications) { this.ldapOperations = ldapOperations; this.dn = dn; - this.actualModifications = (ModificationItem[]) actualModifications.clone(); - this.compensatingModifications = (ModificationItem[]) compensatingModifications.clone(); + this.actualModifications = actualModifications.clone(); + this.compensatingModifications = compensatingModifications.clone(); } /* diff --git a/core/src/main/java/org/springframework/ldap/transaction/compensating/ModifyAttributesOperationRecorder.java b/core/src/main/java/org/springframework/ldap/transaction/compensating/ModifyAttributesOperationRecorder.java index e895f12f..b6c0ffa8 100644 --- a/core/src/main/java/org/springframework/ldap/transaction/compensating/ModifyAttributesOperationRecorder.java +++ b/core/src/main/java/org/springframework/ldap/transaction/compensating/ModifyAttributesOperationRecorder.java @@ -64,19 +64,18 @@ public class ModifyAttributesOperationRecorder implements ModificationItem[] incomingModifications = (ModificationItem[]) args[1]; - Set set = new HashSet(); - for (int i = 0; i < incomingModifications.length; i++) { - set.add(incomingModifications[i].getAttribute().getID()); + Set set = new HashSet(); + for (ModificationItem incomingModification : incomingModifications) { + set.add(incomingModification.getAttribute().getID()); } // Get the current values of all referred Attributes. - String[] attributeNameArray = (String[]) set.toArray(new String[set - .size()]); + String[] attributeNameArray = set.toArray(new String[set.size()]); // LDAP-234: We need to explicitly an IncrementalAttributesMapper in // case we're working against AD and there are too many attribute values to be returned // by one query. - IncrementalAttributesMapper attributesMapper = getAttributesMapper(attributeNameArray); + IncrementalAttributesMapper attributesMapper = getAttributesMapper(attributeNameArray); while (attributesMapper.hasMore()) { ldapOperations.lookup(dn, attributesMapper.getAttributesForLookup(), attributesMapper); } @@ -102,7 +101,7 @@ public class ModifyAttributesOperationRecorder implements * @return the {@link AttributesMapper} to use for getting the current * Attributes of the target DN. */ - IncrementalAttributesMapper getAttributesMapper(String[] attributeNames) { + IncrementalAttributesMapper getAttributesMapper(String[] attributeNames) { return new DefaultIncrementalAttributesMapper(attributeNames); } diff --git a/core/src/main/java/org/springframework/ldap/transaction/compensating/manager/ContextSourceTransactionManagerDelegate.java b/core/src/main/java/org/springframework/ldap/transaction/compensating/manager/ContextSourceTransactionManagerDelegate.java index 31deabef..e39de8d0 100644 --- a/core/src/main/java/org/springframework/ldap/transaction/compensating/manager/ContextSourceTransactionManagerDelegate.java +++ b/core/src/main/java/org/springframework/ldap/transaction/compensating/manager/ContextSourceTransactionManagerDelegate.java @@ -85,11 +85,10 @@ public class ContextSourceTransactionManagerDelegate extends */ protected CompensatingTransactionHolderSupport getNewHolder() { DirContext newCtx = getContextSource().getReadWriteContext(); - DirContextHolder contextHolder = new DirContextHolder( + return new DirContextHolder( new DefaultCompensatingTransactionOperationManager( new LdapCompensatingTransactionOperationFactory( renamingStrategy)), newCtx); - return contextHolder; } /* diff --git a/core/src/main/java/org/springframework/ldap/transaction/compensating/manager/TransactionAwareDirContextInvocationHandler.java b/core/src/main/java/org/springframework/ldap/transaction/compensating/manager/TransactionAwareDirContextInvocationHandler.java index 77e1ab91..7377e97d 100644 --- a/core/src/main/java/org/springframework/ldap/transaction/compensating/manager/TransactionAwareDirContextInvocationHandler.java +++ b/core/src/main/java/org/springframework/ldap/transaction/compensating/manager/TransactionAwareDirContextInvocationHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * Copyright 2005-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,12 +15,6 @@ */ package org.springframework.ldap.transaction.compensating.manager; -import java.lang.reflect.InvocationHandler; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; - -import javax.naming.directory.DirContext; - import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.ldap.NamingException; @@ -29,6 +23,11 @@ import org.springframework.ldap.transaction.compensating.LdapTransactionUtils; import org.springframework.transaction.compensating.support.CompensatingTransactionUtils; import org.springframework.transaction.support.TransactionSynchronizationManager; +import javax.naming.directory.DirContext; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + /** * Proxy implementation for DirContext, making sure that the instance is not * closed during a transaction, and that all modifying operations are recorded, @@ -77,7 +76,7 @@ public class TransactionAwareDirContextInvocationHandler implements return (proxy == args[0] ? Boolean.TRUE : Boolean.FALSE); } else if (methodName.equals("hashCode")) { // Use hashCode of Connection proxy. - return new Integer(hashCode()); + return hashCode(); } else if (methodName.equals("close")) { doCloseConnection(target, contextSource); return null; diff --git a/core/src/main/java/org/springframework/transaction/compensating/support/AbstractCompensatingTransactionManagerDelegate.java b/core/src/main/java/org/springframework/transaction/compensating/support/AbstractCompensatingTransactionManagerDelegate.java index 7967ffc6..25796373 100644 --- a/core/src/main/java/org/springframework/transaction/compensating/support/AbstractCompensatingTransactionManagerDelegate.java +++ b/core/src/main/java/org/springframework/transaction/compensating/support/AbstractCompensatingTransactionManagerDelegate.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -68,8 +68,7 @@ public abstract class AbstractCompensatingTransactionManagerDelegate { public Object doGetTransaction() throws TransactionException { CompensatingTransactionHolderSupport holder = (CompensatingTransactionHolderSupport) TransactionSynchronizationManager .getResource(getTransactionSynchronizationKey()); - CompensatingTransactionObject txObject = new CompensatingTransactionObject(holder); - return txObject; + return new CompensatingTransactionObject(holder); } /* @@ -122,8 +121,7 @@ public abstract class AbstractCompensatingTransactionManagerDelegate { TransactionSynchronizationManager.unbindResource(getTransactionSynchronizationKey()); CompensatingTransactionObject txObject = (CompensatingTransactionObject) transaction; - CompensatingTransactionHolderSupport transactionHolderSupport = (CompensatingTransactionHolderSupport) txObject - .getHolder(); + CompensatingTransactionHolderSupport transactionHolderSupport = txObject.getHolder(); closeTargetResource(transactionHolderSupport); diff --git a/core/src/main/java/org/springframework/transaction/compensating/support/DefaultCompensatingTransactionOperationManager.java b/core/src/main/java/org/springframework/transaction/compensating/support/DefaultCompensatingTransactionOperationManager.java index f22ba26e..3507322e 100644 --- a/core/src/main/java/org/springframework/transaction/compensating/support/DefaultCompensatingTransactionOperationManager.java +++ b/core/src/main/java/org/springframework/transaction/compensating/support/DefaultCompensatingTransactionOperationManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,9 +15,6 @@ */ package org.springframework.transaction.compensating.support; -import java.util.Iterator; -import java.util.Stack; - import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.transaction.TransactionSystemException; @@ -26,6 +23,8 @@ import org.springframework.transaction.compensating.CompensatingTransactionOpera import org.springframework.transaction.compensating.CompensatingTransactionOperationManager; import org.springframework.transaction.compensating.CompensatingTransactionOperationRecorder; +import java.util.Stack; + /** * Default implementation of {@link CompensatingTransactionOperationManager}. * Manages a stack of {@link CompensatingTransactionOperationExecutor} objects @@ -40,7 +39,8 @@ public class DefaultCompensatingTransactionOperationManager implements private static Log log = LogFactory .getLog(DefaultCompensatingTransactionOperationManager.class); - private Stack operationExecutors = new Stack(); + private Stack operationExecutors = + new Stack(); private CompensatingTransactionOperationFactory operationFactory; @@ -78,8 +78,7 @@ public class DefaultCompensatingTransactionOperationManager implements public void rollback() { log.debug("Performing rollback"); while (!operationExecutors.isEmpty()) { - CompensatingTransactionOperationExecutor rollbackOperation = (CompensatingTransactionOperationExecutor) operationExecutors - .pop(); + CompensatingTransactionOperationExecutor rollbackOperation = operationExecutors.pop(); try { rollbackOperation.rollback(); } catch (Exception e) { @@ -94,7 +93,7 @@ public class DefaultCompensatingTransactionOperationManager implements * * @return the rollback operations. */ - protected Stack getOperationExecutors() { + protected Stack getOperationExecutors() { return operationExecutors; } @@ -105,7 +104,7 @@ public class DefaultCompensatingTransactionOperationManager implements * @param operationExecutors * the rollback operations. */ - void setOperationExecutors(Stack operationExecutors) { + void setOperationExecutors(Stack operationExecutors) { this.operationExecutors = operationExecutors; } @@ -114,9 +113,7 @@ public class DefaultCompensatingTransactionOperationManager implements */ public void commit() { log.debug("Performing commit"); - for (Iterator iter = operationExecutors.iterator(); iter.hasNext();) { - CompensatingTransactionOperationExecutor operationExecutor = (CompensatingTransactionOperationExecutor) iter - .next(); + for (CompensatingTransactionOperationExecutor operationExecutor : operationExecutors) { try { operationExecutor.commit(); } catch (Exception e) { diff --git a/core/src/test/java/org/springframework/ldap/core/DirContextAdapterTest.java b/core/src/test/java/org/springframework/ldap/core/DirContextAdapterTest.java index ff4809be..548047b9 100644 --- a/core/src/test/java/org/springframework/ldap/core/DirContextAdapterTest.java +++ b/core/src/test/java/org/springframework/ldap/core/DirContextAdapterTest.java @@ -174,7 +174,7 @@ public class DirContextAdapterTest { tested.getStringAttributes("abc"); fail("ClassCastException expected"); } - catch (ArrayStoreException expected) { + catch (IllegalArgumentException expected) { assertTrue(true); } }