1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
32
33
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 }