View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.frontend.cli;
20  
21  import java.lang.reflect.InvocationTargetException;
22  import java.util.Collection;
23  import java.util.Map;
24  
25  import org.apache.logging.log4j.LogManager;
26  import org.apache.logging.log4j.Logger;
27  import org.mycore.common.MCRException;
28  import org.mycore.datamodel.common.MCRActiveLinkException;
29  
30  /**
31   * Shows details about an exception that occured during command processing
32   * 
33   * @author Frank L\u00FCtzenkirchen
34   */
35  public class MCRCLIExceptionHandler {
36  
37      private static final Logger LOGGER = LogManager.getLogger(MCRCLIExceptionHandler.class);
38  
39      public static void handleException(InvocationTargetException ex) {
40          handleException(ex.getTargetException());
41      }
42  
43      public static void handleException(ExceptionInInitializerError ex) {
44          handleException(ex.getCause());
45      }
46  
47      public static void handleException(MCRActiveLinkException ex) {
48          showActiveLinks(ex);
49          showException(ex);
50      }
51  
52      private static void showActiveLinks(MCRActiveLinkException activeLinks) {
53          MCRCommandLineInterface
54              .output("There are links active preventing the commit of work, see error message for details.");
55          MCRCommandLineInterface.output("The following links where affected:");
56  
57          Map<String, Collection<String>> links = activeLinks.getActiveLinks();
58  
59          for (String curDest : links.keySet()) {
60              LOGGER.debug("Current Destination: {}", curDest);
61              Collection<String> sources = links.get(curDest);
62              for (String source : sources) {
63                  MCRCommandLineInterface.output(source + " ==> " + curDest);
64              }
65          }
66      }
67  
68      public static void handleException(Throwable ex) {
69          showException(ex);
70      }
71  
72      private static void showException(Throwable ex) {
73          MCRCommandLineInterface.output("");
74          MCRCommandLineInterface.output("Exception occured: " + ex.getClass().getName());
75          MCRCommandLineInterface.output("Exception message: " + ex.getLocalizedMessage());
76          MCRCommandLineInterface.output("");
77          showStackTrace(ex);
78          showCauseOf(ex);
79      }
80  
81      private static void showStackTrace(Throwable ex) {
82          String trace = MCRException.getStackTraceAsString(ex);
83          if (LOGGER.isDebugEnabled()) {
84              LOGGER.debug(trace);
85          } else {
86              MCRCommandLineInterface.output(trace);
87          }
88      }
89  
90      private static void showCauseOf(Throwable ex) {
91          ex = ex.getCause();
92          if (ex == null) {
93              return;
94          }
95  
96          MCRCommandLineInterface.output("");
97          MCRCommandLineInterface.output("This exception was caused by:");
98          handleException(ex);
99      }
100 }