Pet
Feb 4th, 2007, 06:36 AM
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.
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.
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.
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;
}
}
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.
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.
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.
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;
}
}