Repository navigation
Expand file tree
/
Copy pathEnv.java
More file actions
295 lines (271 loc) · 9.61 KB
/
Copy pathEnv.java
File metadata and controls
295 lines (271 loc) · 9.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jooby;
import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;
import java.util.Locale;
import java.util.Optional;
import java.util.function.Predicate;
import org.jooby.util.ExSupplier;
import org.jooby.util.Switch;
import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.typesafe.config.Config;
/**
* Allows to optimize, customize or apply defaults values for services.
*
* <p>
* A env is represented by it's name. For example: <code>dev</code>, <code>prod</code>, etc... A
* <strong>dev</strong> env is special and a module provider could do some special configuration for
* development, like turning off a cache, reloading of resources, etc.
* </p>
* <p>
* Same is true for not <strong>dev</strong> environments. For example, a module provider might
* create a high performance connection pool, caches, etc.
* </p>
* <p>
* By default env is set to dev, but you can change it by setting the <code>application.env</code>
* property to anything else.
* </p>
*
* @author edgar
* @since 0.1.0
*/
public interface Env {
/**
* Build an jooby environment.
*
* @author edgar
*/
interface Builder {
/**
* Build a new environment from a {@link Config} object. The environment is created from the
* <code>application.env</code> property. If such property is missing, env's name must be:
* <code>dev</code>.
*
* @param config A config instance.
* @return A new environment.
*/
default Env build(final Config config) {
return build(config, Locale.getDefault());
}
/**
* Build a new environment from a {@link Config} object. The environment is created from the
* <code>application.env</code> property. If such property is missing, env's name must be:
* <code>dev</code>.
*
* @param config A config instance.
* @param locale App locale.
* @return A new environment.
*/
Env build(Config config, Locale locale);
}
/**
* Default builder.
*/
Env.Builder DEFAULT = (config, locale) -> {
requireNonNull(config, "A config is required.");
String name = config.hasPath("application.env") ? config.getString("application.env") : "dev";
return new Env() {
@Override
public String name() {
return name;
}
@Override
public Config config() {
return config;
}
@Override
public Locale locale() {
return locale;
}
@Override
public String toString() {
return name();
}
};
};
/**
* @return Env's name.
*/
String name();
/**
* @return environment properties.
*/
Config config();
Locale locale();
/**
* Returns a string with all substitutions (the <code>${foo.bar}</code> syntax,
* see <a href="https://github.com/typesafehub/config/blob/master/HOCON.md">the
* spec</a>) resolved. Substitutions are looked up using the {@link #config()} as the root object,
* that is, a substitution <code>${foo.bar}</code> will be replaced with
* the result of <code>getValue("foo.bar")</code>.
*
* @param text Text to process.
* @return A processed string.
*/
default String resolve(final String text) {
return resolve(text, config());
}
/**
* Returns a string with all substitutions (the <code>${foo.bar}</code> syntax,
* see <a href="https://github.com/typesafehub/config/blob/master/HOCON.md">the
* spec</a>) resolved. Substitutions are looked up using the {@link #config()} as the root object,
* that is, a substitution <code>${foo.bar}</code> will be replaced with
* the result of <code>getValue("foo.bar")</code>.
*
* @param text Text to process.
* @param startDelimiter Start delimiter.
* @param endDelimiter End delimiter.
* @return A processed string.
*/
default String resolve(final String text, final String startDelimiter,
final String endDelimiter) {
return resolve(text, config(), startDelimiter, endDelimiter);
}
/**
* Returns a string with all substitutions (the <code>${foo.bar}</code> syntax,
* see <a href="https://github.com/typesafehub/config/blob/master/HOCON.md">the
* spec</a>) resolved. Substitutions are looked up using the <code>source</code> param as the
* root object, that is, a substitution <code>${foo.bar}</code> will be replaced with
* the result of <code>getValue("foo.bar")</code>.
*
* @param text Text to process.
* @param source The source config to use.
* @return A processed string.
*/
default String resolve(final String text, final Config source) {
return resolve(text, source, "${", "}");
}
/**
* Returns a string with all substitutions (the <code>${foo.bar}</code> syntax,
* see <a href="https://github.com/typesafehub/config/blob/master/HOCON.md">the
* spec</a>) resolved. Substitutions are looked up using the <code>source</code> param as the
* root object, that is, a substitution <code>${foo.bar}</code> will be replaced with
* the result of <code>getValue("foo.bar")</code>.
*
* @param text Text to process.
* @param source The source config to use
* @param startDelimiter Start delimiter.
* @param endDelimiter End delimiter.
* @return A processed string.
*/
default String resolve(final String text, final Config source,
final String startDelimiter, final String endDelimiter) {
requireNonNull(text, "Text is required.");
requireNonNull(source, "A config source is required.");
checkArgument(!Strings.isNullOrEmpty(startDelimiter), "Start delimiter is required.");
checkArgument(!Strings.isNullOrEmpty(endDelimiter), "End delimiter is required.");
if (text.length() == 0) {
return "";
}
StringBuilder buffer = new StringBuilder();
int offset = 0;
int start = text.indexOf(startDelimiter);
while (start >= 0) {
int end = text.indexOf(endDelimiter, start + startDelimiter.length());
if (end == -1) {
throw new IllegalArgumentException("Unclosed placeholder: "
+ Splitter.on(CharMatcher.WHITESPACE)
.split(text.subSequence(start, text.length())).iterator().next());
}
buffer.append(text.substring(offset, start));
String key = text.substring(start + startDelimiter.length(), end);
buffer.append(source.getAnyRef(key));
offset = end + endDelimiter.length();
start = text.indexOf(startDelimiter, offset);
}
if (buffer.length() == 0) {
return text;
}
if (offset < text.length()) {
buffer.append(text.substring(offset));
}
return buffer.toString();
}
/**
* Runs the callback function if the current env matches the given name.
*
* @param name A name to test for.
* @param fn A callback function.
* @param <T> A resulting type.
* @return A resulting object.
* @throws Exception If something fails.
*/
default <T> Optional<T> ifMode(final String name, final ExSupplier<T> fn)
throws Exception {
return when(name, fn).value();
}
/**
* Produces a {@link Switch} of the current {@link Env}.
*
* <pre>
* String accessKey = env.when("dev", () {@literal ->} "1234")
* .when("stage", () {@literal ->} "4321")
* .when("prod", () {@literal ->} "abc")
* .get();
* </pre>
*
* @param name A name to test for.
* @param fn A callback function.
* @param <T> A resulting type.
* @return A new switch.
*/
default <T> Switch<String, T> when(final String name, final ExSupplier<T> fn) {
return Switch.<T> newSwitch(name()).when(name, fn);
}
/**
* Produces a {@link Switch} of the current {@link Env}.
*
* <pre>
* String accessKey = env.when("dev", "1234")
* .when("stage", "4321")
* .when("prod", "abc")
* .get();
* </pre>
*
* @param name A name to test for.
* @param result A constant value to return.
* @param <T> A resulting type.
* @return A new switch.
*/
default <T> Switch<String, T> when(final String name, final T result) {
return Switch.<T> newSwitch(name()).when(name, result);
}
/**
* Produces a {@link Switch} of the current {@link Env}.
*
* <pre>
* String accessKey = env.when("dev", () {@literal ->} "1234")
* .when("stage", () {@literal ->} "4321")
* .when("prod", () {@literal ->} "abc")
* .get();
* </pre>
*
* @param predicate A predicate to use.
* @param result A constant value to return.
* @param <T> A resulting type.
* @return A new switch.
*/
default <T> Switch<String, T> when(final Predicate<String> predicate,
final T result) {
return Switch.<T> newSwitch(name()).when(predicate, result);
}
}