View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.config;
8   
9   import org.mule.api.config.ExceptionReader;
10  
11  import java.util.Collections;
12  import java.util.HashMap;
13  import java.util.Map;
14  
15  import javax.naming.Name;
16  import javax.naming.NamingException;
17  
18  public class NamingExceptionReader implements ExceptionReader
19  {
20      /**
21       * Displayed when no remaining or resolved name found.
22       */
23      protected static final String MISSING_NAME_DISPLAY_VALUE = "<none>";
24  
25      public String getMessage(Throwable t)
26      {
27          return (t instanceof NamingException ? ((NamingException) t).toString(true) : "<unknown>");
28      }
29  
30      public Throwable getCause(Throwable t)
31      {
32          return (t instanceof NamingException ? ((NamingException) t).getCause() : null);
33      }
34  
35      public Class<?> getExceptionType()
36      {
37          return NamingException.class;
38      }
39  
40      /**
41       * Returns a map of the non-stanard information stored on the exception
42       * 
43       * @param t the exception to extract the information from
44       * @return a map of the non-stanard information stored on the exception
45       */
46      public Map<?, ?> getInfo(Throwable t)
47      {
48          if (t instanceof NamingException)
49          {
50              NamingException e = (NamingException) t;
51              
52              Map<String, Object> info = new HashMap<String, Object>();
53              final Name remainingName = e.getRemainingName();
54              final Name resolvedName = e.getResolvedName();
55              info.put("Remaining Name", remainingName == null ?
56                      MISSING_NAME_DISPLAY_VALUE : remainingName.toString());
57              info.put("Resolved Name", resolvedName == null ? 
58                  MISSING_NAME_DISPLAY_VALUE : resolvedName.toString());
59              return info;
60          }
61          else
62          {
63              return Collections.EMPTY_MAP;
64          }
65      }
66  }