View Javadoc
1   package fr.ifremer.quadrige2.ui.swing.common;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 UI Common
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  
27  import fr.ifremer.quadrige2.ui.swing.common.action.AbstractAction;
28  import jaxx.runtime.swing.JAXXRuntimeException;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.nuiton.jaxx.application.ApplicationTechnicalException;
32  import org.nuiton.jaxx.application.swing.action.ApplicationActionException;
33  import org.springframework.security.access.AccessDeniedException;
34  import org.springframework.security.core.AuthenticationException;
35  
36  import java.lang.reflect.InvocationTargetException;
37  
38  /**
39   * <p>ExceptionHandler class.</p>
40   *
41   * @author Ludovic Pecquot <ludovic.pecquot@e-is.pro>
42   */
43  public class ExceptionHandler implements Thread.UncaughtExceptionHandler {
44  
45      private static final Log LOG = LogFactory.getLog(ExceptionHandler.class);
46  
47      /** {@inheritDoc} */
48      @Override
49      public void uncaughtException(Thread t, Throwable ex) {
50          handleException(t.getName(), ex);
51      }
52  
53      /**
54       * <p>handle.</p>
55       *
56       * @param thrown a {@link java.lang.Throwable} object.
57       */
58      public void handle(Throwable thrown) {
59          // for EDT exceptions
60          handleException(Thread.currentThread().getName(), thrown);
61      }
62  
63      /**
64       * <p>handleException.</p>
65       *
66       * @param tname a {@link java.lang.String} object.
67       * @param ex a {@link java.lang.Throwable} object.
68       */
69      protected void handleException(String tname, Throwable ex) {
70          Throwable cause = ex;
71  
72          // if exception coming from reflection, try to get the target exception
73          while (cause.getCause() instanceof InvocationTargetException) {
74              // try to get inner comprehensive cause
75              cause = cause.getCause();
76          }
77          if (cause instanceof InvocationTargetException) {
78              cause = ((InvocationTargetException) cause).getTargetException();
79          }
80  
81          // threat ApplicationActionException inner exception
82          if (cause instanceof ApplicationActionException) {
83              ApplicationActionException actionException = (ApplicationActionException) cause;
84              Throwable innerException = actionException.getCause();
85  
86              // if inner exception is already managed by the action, avoid it
87              if (actionException.getAction() instanceof AbstractAction
88                  && (innerException instanceof AuthenticationException
89                  || innerException instanceof AccessDeniedException)) {
90                  return;
91              }
92  
93              // try to get next exception
94              cause = innerException;
95          } else {
96              // ApplicationActionException already logged, but log all others
97              if (LOG.isErrorEnabled()) {
98                  LOG.error("Global application exception [" + tname + "]", cause);
99              }
100 
101         }
102         // default exception message
103         String message = cause.getMessage();
104 
105         if (cause instanceof ApplicationTechnicalException) {
106             cause = cause.getCause();
107         }
108 
109         if (cause instanceof JAXXRuntimeException) {
110             cause = cause.getCause();
111             message = cause.getMessage();
112         }
113 
114         // TODO à tester
115         ApplicationUIContext.getInstance().getErrorHelper().showErrorDialog(message, cause);
116 //        JOptionPane.showMessageDialog(null, message, "Global application exception", JOptionPane.ERROR_MESSAGE);
117 
118     }
119 }