1 package cn.home1.oss.boot.autoconfigure;
2
3 import static java.util.stream.Collectors.toList;
4
5 import org.springframework.context.ApplicationContext;
6 import org.springframework.util.ClassUtils;
7
8 import java.util.Arrays;
9 import java.util.Map;
10
11
12
13
14 public abstract class AppUtils {
15
16 private AppUtils() {
17 }
18
19 public static void printApplicationInfo(final ApplicationContext ctx) {
20 System.out.println("Current environment is: " + ctx.getBean(AppProperties.class).getEnv());
21 System.out.println("Let's inspect the beans provided by Spring Boot:");
22
23 final String[] beanNames = ctx.getBeanDefinitionNames();
24 for (final String beanName : Arrays.stream(beanNames).sorted().collect(toList())) {
25 final Object bean = ctx.getBean(beanName);
26 final Class<?> clazz = bean != null ? bean.getClass() : null;
27 final String clazzName = clazz != null ? clazz.getName() : "null";
28 System.out.println(beanName + " - " + clazzName);
29 }
30 }
31
32 public static String appBasePackage(final String defaultValue) {
33 final Map.Entry<String, String> javaMainClass = System.getenv().entrySet()
34 .stream()
35 .filter(entry -> entry.getKey().startsWith("JAVA_MAIN_CLASS"))
36 .findFirst()
37 .orElse(null);
38
39 final String sunJavaCommand = System.getProperty("sun.java.command");
40
41 final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
42
43 final String result;
44 if (javaMainClass != null && ClassUtils.isPresent(javaMainClass.getValue(), classLoader)) {
45 result = ClassUtils.getPackageName(javaMainClass.getValue());
46 } else if (ClassUtils.isPresent(sunJavaCommand, classLoader)) {
47 result = ClassUtils.getPackageName(sunJavaCommand);
48 } else {
49 result = defaultValue;
50 }
51 return result;
52 }
53 }