aboutsummaryrefslogtreecommitdiffstats
path: root/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/common/ExceptionCollector.java
blob: b810e873537dcc0408630c728f5b0c3111f3a4b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package org.openecomp.sdc.toscaparser.api.common;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// Perfectly good enough... 

public class ExceptionCollector {

	private static Logger log = LoggerFactory.getLogger(ExceptionCollector.class.getName());

	//private static boolean isCollecting = false;
	private static ArrayList<String> exceptionStrings = new ArrayList<>();
	private static ArrayList<String> exceptionTraceStrings = new ArrayList<>();
	private static ArrayList<String> warningStrings = new ArrayList<>();
	private static ArrayList<String> warningTraceStrings = new ArrayList<>();
	private static boolean bWantTrace = true;

	/*public static void start() {
		if(exceptionStrings == null) {
			exceptionStrings = new ArrayList<String>();
			exceptionTraceStrings = new ArrayList<String>();
		}
		isCollecting = true;
	}*/

	/*public static void stop() {
		isCollecting = false;
	}*/

	public static void clear() {
		exceptionStrings = new ArrayList<>();
		exceptionTraceStrings = new ArrayList<>();
		warningStrings = new ArrayList<>();
		warningTraceStrings = new ArrayList<>();
	}

	public static void appendException(String strExc) { // throws Exception {

		/*if(!isCollecting) {
			// throw new Exception("Can't append exception " + strExc);
			log.error("ExceptionCollector - appendException - Can't append exception {}", strExc);
		}*/

		if(!exceptionStrings.contains(strExc)) {
			exceptionStrings.add(strExc);
			// get stack trace
			StackTraceElement[] ste =  Thread.currentThread().getStackTrace();
			StringBuilder sb = new StringBuilder();
			// skip the last 2 (getStackTrace and this)
			for(int i=2; i<ste.length; i++) {
				sb.append(String.format("  %s(%s:%d)%s",ste[i].getClassName(),ste[i].getFileName(),
												ste[i].getLineNumber(),i==ste.length-1?" ":"\n"));
			}
			exceptionTraceStrings.add(sb.toString());
		}
	}
	
	public static void appendWarning(String strExc) { // throws Exception {

		/*if(!isCollecting) {
			// throw new Exception("Can't append exception " + strExc);
			log.error("ExceptionCollector - appendException - Can't append exception {}", strExc);
		}*/

		if(!warningStrings.contains(strExc)) {
			warningStrings.add(strExc);
			// get stack trace
			StackTraceElement[] ste =  Thread.currentThread().getStackTrace();
			StringBuilder sb = new StringBuilder();
			// skip the last 2 (getStackTrace and this)
			for(int i=2; i<ste.length; i++) {
				sb.append(String.format("  %s(%s:%d)%s",ste[i].getClassName(),ste[i].getFileName(),
												ste[i].getLineNumber(),i==ste.length-1?" ":"\n"));
			}
			warningTraceStrings.add(sb.toString());
		}
	}

	public static List<String> getCriticalsReport() {
		
		List<String> res = new ArrayList<>();
		if(exceptionStrings.size() > 0) {
			for(int i=0; i<exceptionStrings.size(); i++) {
				res.add(exceptionStrings.get(i));
				if(bWantTrace) {
					res.add(exceptionTraceStrings.get(i));
				}
			}
		}
		return res;
	}
	
	public static List<String> getWarningsReport() {
		
		List<String> res = new ArrayList<>();
		if(warningStrings.size() > 0) {
			for(int i=0; i<warningStrings.size(); i++) {
				res.add(warningStrings.get(i));
				if(bWantTrace) {
					res.add(warningTraceStrings.get(i));
				}
			}
		}
		return res;
	}
	
	public static int errorsCaught() {
		return exceptionStrings.size();
	}
	
	public static int warningsCaught() {
		return warningStrings.size();
	}
	
	public static void setWantTrace(boolean b) {
		bWantTrace = b;
	}

}