1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.util.spring.env.model;
17
18 import java.util.Properties;
19
20 import org.kuali.common.util.Assert;
21 import org.kuali.common.util.Mode;
22 import org.kuali.common.util.spring.env.EnvUtils;
23 import org.kuali.common.util.spring.env.PropertiesEnvironment;
24 import org.springframework.core.env.Environment;
25
26 public final class EnvironmentServiceContext {
27
28 public Environment getEnv() {
29 return env;
30 }
31
32 public boolean isCheckEnvironmentVariables() {
33 return checkEnvironmentVariables;
34 }
35
36 public boolean isResolveStrings() {
37 return resolveStrings;
38 }
39
40 public Mode getMissingPropertyMode() {
41 return missingPropertyMode;
42 }
43
44 private final Environment env;
45 private final boolean checkEnvironmentVariables;
46 private final boolean resolveStrings;
47 private final Mode missingPropertyMode;
48
49 private EnvironmentServiceContext(Builder builder) {
50 this.env = builder.env;
51 this.checkEnvironmentVariables = builder.checkEnvironmentVariables;
52 this.resolveStrings = builder.resolveStrings;
53 this.missingPropertyMode = builder.missingPropertyMode;
54 }
55
56 public static class Builder {
57
58 private Environment env = EnvUtils.getDefaultEnvironment();
59 private boolean checkEnvironmentVariables = true;
60 private boolean resolveStrings = true;
61 private Mode missingPropertyMode = Mode.ERROR;
62
63 private static final String CHECK_ENVIRONMENT_VARIABLES_KEY = "env.checkEnvironmentVariables";
64 private static final String RESOLVE_STRINGS_KEY = "env.resolveStrings";
65 private static final String MISSING_PROPERTY_MODE_KEY = "env.missingPropertyMode";
66
67 public Builder env(Properties properties) {
68 return env(new PropertiesEnvironment(properties));
69 }
70
71 public Builder env(Environment env) {
72 this.env = env;
73 return this;
74 }
75
76 public Builder checkEnvironmentVariables(boolean checkEnvironmentVariables) {
77 this.checkEnvironmentVariables = checkEnvironmentVariables;
78 return this;
79 }
80
81 public Builder resolveStrings(boolean resolveStrings) {
82 this.resolveStrings = resolveStrings;
83 return this;
84 }
85
86 public Builder missingPropertyMode(Mode missingPropertyMode) {
87 this.missingPropertyMode = missingPropertyMode;
88 return this;
89 }
90
91 private void override() {
92 Assert.noNulls(env);
93 checkEnvironmentVariables(env.getProperty(CHECK_ENVIRONMENT_VARIABLES_KEY, Boolean.class, checkEnvironmentVariables));
94 resolveStrings(env.getProperty(RESOLVE_STRINGS_KEY, Boolean.class, resolveStrings));
95 missingPropertyMode(env.getProperty(MISSING_PROPERTY_MODE_KEY, Mode.class, missingPropertyMode));
96 }
97
98 protected void validate(EnvironmentServiceContext ctx) {
99 Assert.notNull(ctx.getEnv(), "'env' cannot be null");
100 Assert.notNull(ctx.getMissingPropertyMode(), "'missingPropertyMode' cannot be null");
101 }
102
103 protected EnvironmentServiceContext getInstance() {
104 return new EnvironmentServiceContext(this);
105 }
106
107 public EnvironmentServiceContext build() {
108 override();
109 EnvironmentServiceContext ctx = new EnvironmentServiceContext(this);
110 validate(ctx);
111 return ctx;
112 }
113
114 }
115
116 }