Results 1 to 5 of 5

Thread: extending DirContextAdapter

  1. #1
    Join Date
    Feb 2007
    Posts
    6

    Default extending DirContextAdapter

    Hi,
    I would like to post spring-ldap team my extension of DirContextAdapter.
    When item in the ldap is not set, method context.getStringAttribute throw
    exception NullPointerException. In practise application is it obstruction.

    When getStringAttribute throw exception, method get default value. You can
    get other the string values such as double,boolean, long, int.

    This code may behave only for new implementation of DirContextAdapter.

    This is example source code without extension.
    Code:
        private static class UserContextMapper implements ContextMapper {
            public Object mapFromContext(Object ctx) {
                DirContextAdapter context = (DirContextAdapter)ctx;
    
                User user = new User();
                try{
                    user.setUid(context.getStringAttributes("uid"));
                } catch(java.lang.NullPointerException npe) {}
                ...
                return user;
            }
        }
    This is example source code of extension.
    Code:
        private static class UserContextMapper implements ContextMapper {
            public Object mapFromContext(Object ctx) {
                DirContextAdapter context = (DirContextAdapter)ctx;
    
                User user = new User();
                    user.setUid(context.getStringAttribute("uid",""));
                    ...
                return user;
            }
        }

    This is source code of extension.

    Code:
    package net.itsynapse2.utils.data;
     
    import org.springframework.ldap.support.DirContextAdapter;
     
    public class DirContextAdapter {
        public String getStringAttribute(String attrName, String defaultValue) {
            String result = defaultValue;
     
            try {
                result = getStringAttribute(attrName);
            } catch(java.lang.NullPointerException npe) {}
     
            return result;
        }
        public String[] getStringAttributes(String attrName) {
            String[] result = new String[0];
    
            try {
                int length = getStringAttributes(attrName).length;
                result = new String[length];
                
                for(int i=0; i<length ; i++) {
                    result[i] = getStringAttributes(attrName)[i];
                }
                
            } catch(java.lang.NullPointerException npe) {}
    
            return result;
        }
    
        public boolean getBooleanAttribute(String attrName, boolean defaultValue) {
            boolean result = defaultValue;
     
            try {
                result = Boolean.parseBoolean(getStringAttribute(attrName));
            } catch(java.lang.NullPointerException npe) {}
     
            return result;
        }
        public boolean[] getBooleanAttributes(String attrName) {
            boolean[] result = new boolean[0];
    
            try {
                int length = getStringAttributes(attrName).length;
                result = new boolean[length];
                
                for(int i=0; i<length ; i++) {
                    result[i] = Boolean.parseBoolean(getStringAttributes(attrName)[i]);
                }
                
            } catch(java.lang.NullPointerException npe) {}
    
            return result;
            
        }
    
        public long getIntegerAttribute(String attrName, int defaultValue) {
            int result = defaultValue;
     
            try {
                result = Integer.parseInt(getStringAttribute(attrName));
            } catch(java.lang.NullPointerException npe) {}
     
            return result;
     
        }
        public int[] getIntegerAttributes(String attrName) {
            int[] result = new int[0];
    
            try {
                int length = getStringAttributes(attrName).length;
                result = new int[length];
                
                for(int i=0; i<length ; i++) {
                    result[i] = Integer.parseInt(getStringAttributes(attrName)[i]);
                }
                
            } catch(java.lang.NullPointerException npe) {}
    
            return result;
            
        }
    
        public long getLongAttribute(String attrName, long defaultValue) {
            long result = defaultValue;
     
            try {
                result = Long.parseLong(getStringAttribute(attrName));
            } catch(java.lang.NullPointerException npe) {}
     
            return result;
     
        }
        public long[] getLongAttributes(String attrName) {
            long[] result = new long[0];
    
            try {
                int length = getStringAttributes(attrName).length;
                result = new long[length];
                
                for(int i=0; i<length ; i++) {
                    result[i] = Long.parseLong(getStringAttributes(attrName)[i]);
                }
                
            } catch(java.lang.NullPointerException npe) {}
    
            return result;
            
        }
    
        public double getDoubleAttribute(String attrName, double defaultValue) {
            double result = defaultValue;
     
            try {
                result = Double.parseDouble(getStringAttribute(attrName));
            } catch(java.lang.NullPointerException npe) {}
     
            return result;
        }
        public double[] getDoubleAttributes(String attrName) {
            double[] result = new double[0];
    
            try {
                int length = getStringAttributes(attrName).length;
                result = new double[length];
                
                for(int i=0; i<length ; i++) {
                    result[i] = Double.parseDouble(getStringAttributes(attrName)[i]);
                }
                
            } catch(java.lang.NullPointerException npe) {}
    
            return result;
            
        }
        public byte[] getByteArrayAttribute(String attrName) {
            byte[] result = null;
    
            try {
                result = (byte[])getObjectAttribute(attrName);
            } catch(java.lang.NullPointerException npe) {}
    
            return result;
        }
    
        public Object getObjectAttribute(String attrName) {
            Object result = null;
    
            try {
                result = (Object)getObjectAttribute(attrName);
            } catch(java.lang.NullPointerException npe) {}
    
            return result;
        } 
    }
    Last edited by Pet; Feb 4th, 2007 at 06:53 AM.

  2. #2
    Join Date
    Mar 2005
    Location
    Landskrona, Sweden
    Posts
    505

    Default

    Actually, I'm unable to reproduce your problem. The following test case runs just fine:
    Code:
    public void testGetStringAttribute_NullValue(){
      DirContextAdapter ctx = new DirContextAdapter();
      String result = ctx.getStringAttribute("someAbsentAttribute");
      assertNull(result);
    }
    So the getStringAttribute() method returns null for an Attribte that is not set, which is to be expected.

    The same goes for the getStringAttributes method.

    Maybe I misinterpreted your problem; could you please provide a test case that points out the problem?
    Last edited by rasky; Feb 5th, 2007 at 11:35 AM.
    Mattias Arthursson
    Jayway AB (www.jayway.se)
    Spring-LDAP project member

  3. #3
    Join Date
    Feb 2007
    Posts
    6

    Default

    Hi rasky,
    when attribute exist in ldap, such as itbNN but not set ( has no value ), it throw NullPointerException. When you can get any value from ldap and value not set, you would like set default value and its code repetition. When you can get numeric value or logic value, its the same problem.

    May I show you complete example?

    Pet

  4. #4
    Join Date
    Mar 2005
    Location
    Landskrona, Sweden
    Posts
    505

    Default

    Well, I still don't understand. Could you please send me the stacktrace of the NullPointerException, so that I can pinpoint the exact line where it occurs?
    Mattias Arthursson
    Jayway AB (www.jayway.se)
    Spring-LDAP project member

  5. #5
    Join Date
    Feb 2007
    Posts
    6

    Default

    Quote Originally Posted by rasky View Post
    Well, I still don't understand. Could you please send me the stacktrace of the NullPointerException, so that I can pinpoint the exact line where it occurs?
    Hi, problem is at user.setUid(context.getStringAttributes("uid")); line. But this reason is well for other operations. When property not set, you get default value and you get concrete data type and don't have to retype each accessed attribute.

    I go to simulate this problem and at next reply I copy complete stack.

    Have a nice day

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •