1 | |
package org.kuali.spring.util; |
2 | |
|
3 | |
import org.springframework.util.Assert; |
4 | |
|
5 | |
public class SystemAwarePropertiesRetriever extends PropertiesRetriever { |
6 | 0 | SystemPropertiesMode mode = SystemPropertiesMode.SYSTEM_PROPERTIES_MODE_FALLBACK; |
7 | 0 | boolean searchEnvironment = true; |
8 | |
|
9 | |
public SystemAwarePropertiesRetriever() { |
10 | 0 | this(SystemPropertiesMode.SYSTEM_PROPERTIES_MODE_FALLBACK, true); |
11 | 0 | } |
12 | |
|
13 | |
public SystemAwarePropertiesRetriever(SystemPropertiesMode mode, boolean searchEnvironment) { |
14 | 0 | super(); |
15 | 0 | this.mode = mode; |
16 | 0 | this.searchEnvironment = searchEnvironment; |
17 | 0 | } |
18 | |
|
19 | |
@Override |
20 | |
public String retrieveValue(String key) { |
21 | 0 | Assert.notNull(mode); |
22 | 0 | String environmentProperty = SystemUtils.getEnvironmentPropertyIgnoreExceptions(key); |
23 | 0 | String property = super.retrieveValue(key); |
24 | 0 | String systemProperty = SystemUtils.getSystemPropertyIgnoreExceptions(key); |
25 | |
|
26 | 0 | if (isUseSystemProperty(systemProperty, property)) { |
27 | 0 | return systemProperty; |
28 | |
} |
29 | 0 | if (isUseEnvironmentProperty(environmentProperty, property)) { |
30 | 0 | return environmentProperty; |
31 | |
} |
32 | 0 | return property; |
33 | |
} |
34 | |
|
35 | |
protected boolean isUseSystemProperty(String systemProperty, String regularProperty) { |
36 | |
|
37 | 0 | if (systemProperty == null) { |
38 | 0 | return false; |
39 | |
} |
40 | |
|
41 | 0 | switch (mode) { |
42 | |
case SYSTEM_PROPERTIES_MODE_NEVER: |
43 | |
|
44 | 0 | return false; |
45 | |
case SYSTEM_PROPERTIES_MODE_OVERRIDE: |
46 | |
|
47 | 0 | return true; |
48 | |
case SYSTEM_PROPERTIES_MODE_FALLBACK: |
49 | |
|
50 | 0 | if (regularProperty == null) { |
51 | 0 | return true; |
52 | |
} else { |
53 | 0 | return false; |
54 | |
} |
55 | |
default: |
56 | |
|
57 | 0 | throw new IllegalArgumentException("Unknown system properties mode [" + mode + "] Available modes are " |
58 | |
+ SystemPropertiesMode.values()); |
59 | |
} |
60 | |
} |
61 | |
|
62 | |
protected boolean isUseEnvironmentProperty(String environmentProperty, String regularProperty) { |
63 | |
|
64 | 0 | if (!searchEnvironment) { |
65 | 0 | return false; |
66 | |
} |
67 | |
|
68 | |
|
69 | 0 | if (environmentProperty == null) { |
70 | 0 | return false; |
71 | |
} |
72 | |
|
73 | |
|
74 | 0 | if (regularProperty != null) { |
75 | 0 | return false; |
76 | |
} |
77 | |
|
78 | |
|
79 | 0 | return true; |
80 | |
} |
81 | |
|
82 | |
public boolean isSearchEnvironment() { |
83 | 0 | return searchEnvironment; |
84 | |
} |
85 | |
|
86 | |
public void setSearchEnvironment(boolean searchEnvironment) { |
87 | 0 | this.searchEnvironment = searchEnvironment; |
88 | 0 | } |
89 | |
|
90 | |
public SystemPropertiesMode getMode() { |
91 | 0 | return mode; |
92 | |
} |
93 | |
|
94 | |
public void setMode(SystemPropertiesMode mode) { |
95 | 0 | this.mode = mode; |
96 | 0 | } |
97 | |
|
98 | |
} |