1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.util.main;
17
18 import java.util.Collections;
19 import java.util.Map;
20
21 import org.kuali.common.util.spring.SpringExecutable;
22 import org.kuali.common.util.spring.service.SpringContext;
23 import org.kuali.common.util.spring.service.SpringService;
24
25 public class MainUtils {
26
27 public static final String MAIN_CONTEXT_BEAN_NAME = "mainContext";
28 public static final String MAIN_PROFILE_NAME = "main";
29
30
31
32
33 public static void runAndExit(Class<?> mainClass, String[] args) {
34 runAndExit(mainClass, args, false);
35 }
36
37
38
39
40 public static void runAndExit(Class<?> mainClass, String[] args, boolean stacktrace) {
41 run(mainClass, args, stacktrace, true);
42 }
43
44
45
46
47 public static void run(Class<?> mainClass, String[] args, boolean stacktrace) {
48 run(mainClass, args, stacktrace, false);
49 }
50
51
52
53
54 public static void run(Class<?> mainClass, String[] args) {
55 run(mainClass, args, true, false);
56 }
57
58
59
60
61 public static void run(Class<?> mainClass, String[] args, boolean stacktrace, boolean exit) {
62 try {
63
64 MainContext mainContext = new MainContext(mainClass, args);
65
66
67 Map<String, Object> beans = Collections.singletonMap(MAIN_CONTEXT_BEAN_NAME, (Object) mainContext);
68
69
70 SpringContext context = new SpringContext(beans, mainClass, MAIN_PROFILE_NAME);
71
72
73 SpringService service = SpringExecutable.DEFAULT_SPRING_SERVICE;
74
75
76 new SpringExecutable(service, context).execute();
77
78
79 if (exit) {
80 System.exit(Status.SUCCESS.getValue());
81 }
82 } catch (Exception e) {
83 handleException(e, stacktrace, exit);
84 }
85 }
86
87 protected static void handleException(Exception e, boolean stacktrace, boolean exit) {
88 if (stacktrace) {
89 e.printStackTrace();
90 } else {
91 System.err.print(e.getMessage());
92 }
93 if (exit) {
94 System.exit(Status.FAILURE.getValue());
95 }
96 }
97
98 }