1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.catalog.api.util;
18
19 import javax.servlet.ServletContext;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.util.Properties;
23
24
25
26
27 public final class ProfileUtil {
28
29 private static final String SPRING_PROFILE = "spring.profiles.active";
30
31 private ProfileUtil() {
32 }
33
34
35
36
37
38 public static void setActiveProfile(ServletContext servletContext) {
39 String springProfile = System.getProperty(SPRING_PROFILE);
40 if (springProfile == null) {
41 springProfile = System.getenv(SPRING_PROFILE);
42 }
43 if (springProfile == null) {
44 springProfile = servletContext.getInitParameter(SPRING_PROFILE);
45 }
46 if (springProfile == null) {
47 Properties props = loadProperties(servletContext);
48 String activeProfile = props.getProperty(SPRING_PROFILE);
49 if (activeProfile == null) {
50 servletContext.setInitParameter(SPRING_PROFILE, "eclipseLink");
51 }
52 }
53 }
54
55 private static Properties loadProperties(ServletContext servletContext) {
56 Properties props = new Properties();
57 String env = System.getProperty("env");
58 if (env == null) {
59 env = System.getenv("env");
60 }
61 StringBuilder sb = new StringBuilder("catalog-");
62 if (env != null) {
63 sb.append(env);
64 }
65 sb.append("config.properties");
66 InputStream is = ProfileUtil.class.getClassLoader().getResourceAsStream(sb.toString());
67 if (is != null) {
68 try {
69 props.load(is);
70 } catch (IOException ioe) {
71 servletContext.log("Unable to load " + sb.toString() + ": " + ioe.getMessage());
72 }
73 }
74 return props;
75 }
76 }