1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.ldaptemplate;
18
19 import javax.naming.CommunicationException;
20 import javax.naming.ContextNotEmptyException;
21 import javax.naming.LimitExceededException;
22 import javax.naming.NameAlreadyBoundException;
23 import javax.naming.NameNotFoundException;
24 import javax.naming.NamingException;
25 import javax.naming.directory.InvalidAttributesException;
26 import javax.naming.directory.InvalidSearchControlsException;
27 import javax.naming.directory.InvalidSearchFilterException;
28
29 import org.springframework.dao.DataAccessException;
30 import org.springframework.dao.DataIntegrityViolationException;
31 import org.springframework.dao.DataRetrievalFailureException;
32 import org.springframework.dao.InvalidDataAccessApiUsageException;
33
34 /***
35 * The default implementation of NamingExceptionTranslator.
36 *
37 * @author Mattias Arthursson
38 */
39 public class DefaultNamingExceptionTranslator implements
40 NamingExceptionTranslator {
41
42
43
44
45
46
47
48 public DataAccessException translate(NamingException namingException) {
49
50 if (namingException instanceof NameNotFoundException) {
51 return new EntryNotFoundException("Entry not found",
52 namingException);
53 }
54
55 if (namingException instanceof InvalidSearchFilterException) {
56 return new BadLdapGrammarException("Invalid search filter",
57 namingException);
58 }
59
60 if (namingException instanceof InvalidSearchControlsException) {
61 return new InvalidDataAccessApiUsageException(
62 "Invalid search controls supplied by internal API",
63 namingException);
64 }
65
66 if (namingException instanceof NameAlreadyBoundException) {
67 return new DataIntegrityViolationException("Name already bound",
68 namingException);
69 }
70
71 if (namingException instanceof ContextNotEmptyException) {
72 return new DataIntegrityViolationException(
73 "The context needs to be empty in order to be removed",
74 namingException);
75 }
76
77 if (namingException instanceof InvalidAttributesException) {
78 return new AttributesIntegrityViolationException(
79 "Invalid attributes", namingException);
80 }
81
82 if (namingException instanceof LimitExceededException) {
83 return new SearchLimitExceededException("Too many objects found",
84 namingException);
85 }
86
87 if (namingException instanceof CommunicationException) {
88 throw new DataRetrievalFailureException(
89 "Unable to communicate with LDAP server", namingException);
90 }
91
92
93 return new UncategorizedLdapException("Operation failed",
94 namingException);
95 }
96 }