forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnvironmentOptions.java
More file actions
155 lines (137 loc) · 3.7 KB
/
EnvironmentOptions.java
File metadata and controls
155 lines (137 loc) · 3.7 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
/**
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
/**
* Available environment options.
*
* @author edgar
* @since 2.0.0
*/
public class EnvironmentOptions {
private static final String ENV = "application.env";
private String basedir;
private String filename;
private ClassLoader classLoader;
private String[] activeNames;
/**
* Creates environment options. Default application file name is: <code>application.conf</code>.
*/
public EnvironmentOptions() {
setFilename("application.conf");
}
/**
* Active environment names or fallback and read them from system property:
* <code>application.env</code>.
*
* @return Active environment names.
*/
public List<String> getActiveNames() {
return activeNames == null ? defaultEnvironmentNames() : Arrays.asList(activeNames);
}
/**
* Set active environment names.
*
* @param activeNames Active environment names.
* @return This options.
*/
public @Nonnull EnvironmentOptions setActiveNames(@Nonnull String... activeNames) {
this.activeNames = activeNames;
return this;
}
/**
* Set active environment names.
*
* @param activeNames Active environment names.
* @return This options.
*/
public @Nonnull EnvironmentOptions setActiveNames(@Nonnull List<String> activeNames) {
this.activeNames = activeNames.toArray(new String[0]);
return this;
}
private static @Nonnull List<String> defaultEnvironmentNames() {
return Arrays.asList(
System.getProperty(ENV, System.getenv().getOrDefault(ENV, "dev")).split("\\s*,\\s*"));
}
/**
* Class loader.
*
* @return Class loader.
*/
public @Nonnull ClassLoader getClassLoader() {
return classLoader == null ? getClass().getClassLoader() : classLoader;
}
/**
* Class loader.
*
* @param defaultClassLoader Default classloader is none was set.
* @return Class loader.
*/
public @Nonnull ClassLoader getClassLoader(@Nonnull ClassLoader defaultClassLoader) {
return classLoader == null ? defaultClassLoader : classLoader;
}
/**
* Set class loader.
*
* @param classLoader Class loader.
* @return This options.
*/
public @Nonnull EnvironmentOptions setClassLoader(@Nonnull ClassLoader classLoader) {
this.classLoader = classLoader;
return this;
}
/**
* Base directory to use or <code>null</code> for default.
*
* @return Base directory to use or <code>null</code> for default.s
*/
public @Nullable String getBasedir() {
return basedir;
}
/**
* Configuration file name.
*
* @return Configuration file name.
*/
public @Nonnull String getFilename() {
return filename;
}
/**
* Set base dir.
*
* @param basedir Base dir. Classpath folder or file system directory.
* @return This options.
*/
public @Nonnull EnvironmentOptions setBasedir(@Nullable String basedir) {
this.basedir = basedir;
return this;
}
/**
* Set base dir.
*
* @param basedir Base dir.
* @return This options.
*/
public @Nonnull EnvironmentOptions setBasedir(@Nullable Path basedir) {
this.basedir = basedir.toAbsolutePath().toString();
return this;
}
/**
* Set file name.
*
* @param filename File name with extension. Supported extensions are: <code>.properties</code>,
* <code>.conf</code> and <code>.json</code>.
* @return This environment.
*/
public @Nonnull EnvironmentOptions setFilename(@Nonnull String filename) {
this.filename = filename;
return this;
}
}