View Javadoc
1   /*
2    * Copyright 2002-2005 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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       * (non-Javadoc)
44       * 
45       * @see net.sf.ldaptemplate.NamingExceptionTranslator#translate(java.lang.String,
46       *      java.lang.String, java.lang.String, javax.naming.NamingException)
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          // Fallback - other type of NamingException encountered.
93          return new UncategorizedLdapException("Operation failed",
94                  namingException);
95      }
96  }