Skip to content

Commit 507ef8f

Browse files
committed
Refactor application and context attributes. Add resource attributes. More javadoc
1 parent cae4641 commit 507ef8f

File tree

28 files changed

+400
-268
lines changed

28 files changed

+400
-268
lines changed

etc/source/jooby-style.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,9 @@
164164
<!-- <module name="HiddenField"/> -->
165165
<module name="IllegalInstantiation"/>
166166
<module name="InnerAssignment"/>
167-
<module name="MagicNumber"/>
167+
<module name="MagicNumber">
168+
<property name="ignoreNumbers" value="-1, 0, 1, 2, 3, 4, 5" />
169+
</module>
168170
<module name="MissingSwitchDefault"/>
169171
<module name="SimplifyBooleanExpression"/>
170172
<module name="SimplifyBooleanReturn"/>

jooby/src/main/java/io/jooby/AttributeMap.java

Lines changed: 0 additions & 115 deletions
This file was deleted.

jooby/src/main/java/io/jooby/Context.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,35 @@ public interface Context {
7474
*
7575
* @return Context attributes.
7676
*/
77-
@Nonnull AttributeMap getAttributes();
77+
@Nonnull Map<String, Object> getAttributes();
78+
79+
/**
80+
* Get an attribute by his key. This is just an utility method around {@link #getAttributes()}.
81+
* This method look first in current context and fallback to application attributes.
82+
*
83+
* @param key Attribute key.
84+
* @param <T> Attribute type.
85+
* @return Attribute value.
86+
*/
87+
@Nonnull default <T> T attribute(@Nonnull String key) {
88+
T attribute = (T) getAttributes().get(key);
89+
if (attribute == null) {
90+
attribute = getRouter().attribute(key);
91+
}
92+
return attribute;
93+
}
94+
95+
/**
96+
* Set an application attribute.
97+
*
98+
* @param key Attribute key.
99+
* @param value Attribute value.
100+
* @return This router.
101+
*/
102+
@Nonnull default Context attribute(@Nonnull String key, Object value) {
103+
getAttributes().put(key, value);
104+
return this;
105+
}
78106

79107
/**
80108
* Get the HTTP router (usually this represent an instance of {@link Jooby}.

jooby/src/main/java/io/jooby/Environment.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,17 @@ public class Environment {
5959
* @param actives Active environment names.
6060
*/
6161
public Environment(@Nonnull Config conf, @Nonnull String... actives) {
62-
this.actives = Stream.of(actives)
62+
this(conf, Arrays.asList(actives));
63+
}
64+
65+
/**
66+
* Creates a new environment.
67+
*
68+
* @param conf Application configuration.
69+
* @param actives Active environment names.
70+
*/
71+
public Environment(@Nonnull Config conf, @Nonnull List<String> actives) {
72+
this.actives = actives.stream()
6373
.map(String::trim)
6474
.map(String::toLowerCase)
6575
.collect(Collectors.toList());
@@ -172,7 +182,7 @@ private String configTree(final String[] sources, final int i) {
172182
Config sys = systemProperties()
173183
.withFallback(systemEnv());
174184

175-
String[] actives = options.getActiveNames();
185+
List<String> actives = options.getActiveNames();
176186
String filename = options.getFilename();
177187
String extension;
178188
int ext = filename.lastIndexOf('.');
@@ -186,11 +196,11 @@ private String configTree(final String[] sources, final int i) {
186196
Path userdir = Paths.get(System.getProperty("user.dir"));
187197
/** Application file: */
188198
Config application = ConfigFactory.empty();
189-
String[] names = new String[actives.length + 1];
190-
for (int i = 0; i < actives.length; i++) {
191-
names[i] = filename + "." + actives[i].trim().toLowerCase() + extension;
199+
String[] names = new String[actives.size() + 1];
200+
for (int i = 0; i < actives.size(); i++) {
201+
names[i] = filename + "." + actives.get(i).trim().toLowerCase() + extension;
192202
}
193-
names[actives.length] = filename + extension;
203+
names[actives.size()] = filename + extension;
194204
Path fsroot = Paths.get(basedir).toAbsolutePath();
195205
String[] cproot = basedir.split("/");
196206
for (String name : names) {

0 commit comments

Comments
 (0)