This is an automated email from the ASF dual-hosted git repository.
jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git
The following commit(s) were added to refs/heads/master by this push:
new 5e88643 Context API refactoring.
5e88643 is described below
commit 5e886432209dcff02710a08ff877e1fc3a7f4c47
Author: JamesBognar <[email protected]>
AuthorDate: Sat Sep 11 10:29:12 2021 -0400
Context API refactoring.
---
.../main/java/org/apache/juneau/svl/VarList.java | 116 ++++++++++++++++++--
.../java/org/apache/juneau/svl/VarResolver.java | 48 ++++----
.../java/org/apache/juneau/rest/RestContext.java | 122 +--------------------
.../org/apache/juneau/rest/RestContextBuilder.java | 77 +++++++++++--
.../apache/juneau/utils/StringVarResolverTest.java | 5 +-
5 files changed, 202 insertions(+), 166 deletions(-)
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/svl/VarList.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/svl/VarList.java
index d188c1f..fd7c85f 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/svl/VarList.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/svl/VarList.java
@@ -12,8 +12,12 @@
//
***************************************************************************************************************************
package org.apache.juneau.svl;
+import static java.util.Arrays.*;
+
import java.util.*;
+import org.apache.juneau.svl.vars.*;
+
/**
* Simple list of variables that can consist of either variable classes or
instances.
*/
@@ -22,13 +26,22 @@ public class VarList extends ArrayList<Object> {
private static final long serialVersionUID = 1L;
/**
+ * Returns an empty list of variables.
+ *
+ * @return A new empty list of variables.
+ */
+ public static VarList create() {
+ return new VarList();
+ }
+
+ /**
* Creates a new list of variables.
*
* @param vars The variables to create.
* @return A new list of variables.
*/
public static VarList of(Var...vars) {
- return new VarList().append(vars);
+ return create().append(vars);
}
/**
@@ -37,31 +50,112 @@ public class VarList extends ArrayList<Object> {
* @param vars The variables to create.
* @return A new list of variables.
*/
- @SuppressWarnings("unchecked")
- public static VarList of(Class<? extends Var>...vars) {
- return new VarList().append(vars);
+ @SafeVarargs
+ public static final VarList of(Class<? extends Var>...vars) {
+ return create().append(vars);
+ }
+
+ /**
+ * Constructor.
+ */
+ protected VarList() {
+ super();
+ }
+
+ /**
+ * Copy constructor.
+ *
+ * @param copyFrom The list to copy.
+ */
+ protected VarList(VarList copyFrom) {
+ super(copyFrom);
+ }
+
+ /**
+ * Adds a list of variables to this list.
+ *
+ * @param vars The variables to append to this list.
+ * @return This object (for method chaining).
+ */
+ public VarList append(Var...vars) {
+ addAll(asList(vars));
+ return this;
}
/**
- * Appends a list of variables to this list.
+ * Adds a list of variables to this list.
*
* @param vars The variables to append to this list.
* @return This object (for method chaining).
*/
- private VarList append(Var...vars) {
- addAll(Arrays.asList(vars));
+ public VarList append(VarList vars) {
+ addAll(vars);
return this;
}
/**
- * Appends a list of variables to this list.
+ * Adds a list of variables to this list.
*
* @param vars The variables to append to this list.
* @return This object (for method chaining).
*/
- @SuppressWarnings("unchecked")
- private VarList append(Class<? extends Var>...vars) {
- addAll(Arrays.asList(vars));
+ @SafeVarargs
+ public final VarList append(Class<? extends Var>...vars) {
+ addAll(asList(vars));
return this;
}
+
+ /**
+ * Adds the default variables to this list.
+ *
+ * <p>
+ * The default variables are:
+ * <ul>
+ * <li>{@link SystemPropertiesVar}
+ * <li>{@link EnvVariablesVar}
+ * <li>{@link ArgsVar}
+ * <li>{@link ManifestFileVar}
+ * <li>{@link SwitchVar}
+ * <li>{@link IfVar}
+ * <li>{@link CoalesceVar}
+ * <li>{@link PatternMatchVar}
+ * <li>{@link PatternReplaceVar}
+ * <li>{@link PatternExtractVar}
+ * <li>{@link UpperCaseVar}
+ * <li>{@link LowerCaseVar}
+ * <li>{@link NotEmptyVar}
+ * <li>{@link LenVar}
+ * <li>{@link SubstringVar}
+ * </ul>
+ *
+ * @return This object (for method chaining).
+ */
+ public VarList addDefault() {
+ return append(
+ SystemPropertiesVar.class,
+ EnvVariablesVar.class,
+ ManifestFileVar.class,
+ ArgsVar.class,
+ SwitchVar.class,
+ IfVar.class,
+ CoalesceVar.class,
+ PatternMatchVar.class,
+ PatternReplaceVar.class,
+ PatternExtractVar.class,
+ UpperCaseVar.class,
+ LowerCaseVar.class,
+ NotEmptyVar.class,
+ LenVar.class,
+ SubstringVar.class
+ );
+ }
+
+ /**
+ * Makes a copy of this list.
+ *
+ * @return A new copy of this list.
+ */
+ public VarList copy() {
+ return new VarList(this);
+ }
}
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/svl/VarResolver.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/svl/VarResolver.java
index bbdbab7..099a649 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/svl/VarResolver.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/svl/VarResolver.java
@@ -12,8 +12,6 @@
//
***************************************************************************************************************************
package org.apache.juneau.svl;
-import static org.apache.juneau.internal.ClassUtils.*;
-
import java.io.*;
import java.util.*;
import java.util.concurrent.*;
@@ -105,7 +103,7 @@ public class VarResolver {
* @param builder The builder for this object.
*/
protected VarResolver(Builder builder) {
- this.vars = builder.vars.toArray(new Var[builder.vars.size()]);
+ this.vars = builder.vars.stream().map(x ->
toVar(builder.beanStore,x)).toArray(Var[]::new);
Map<String,Var> m = new ConcurrentSkipListMap<>();
for (Var v : vars)
@@ -115,6 +113,13 @@ public class VarResolver {
this.beanStore = BeanStore.of(builder.beanStore);
}
+ @SuppressWarnings("unchecked")
+ private static Var toVar(BeanStore bs, Object o) {
+ if (o instanceof Class)
+ return bs.createBean(Var.class, (Class<? extends
Var>)o);
+ return (Var)o;
+ }
+
/**
* Returns a new builder object using the settings in this resolver as
a base.
*
@@ -129,7 +134,7 @@ public class VarResolver {
*/
public static class Builder {
- final List<Var> vars;
+ final VarList vars;
BeanStore beanStore;
VarResolver impl;
@@ -137,7 +142,7 @@ public class VarResolver {
* Constructor.
*/
protected Builder() {
- vars = AList.create();
+ vars = VarList.create();
beanStore = BeanStore.create().build();
}
@@ -147,7 +152,7 @@ public class VarResolver {
* @param copyFrom The bean to copy from.
*/
protected Builder(VarResolver copyFrom) {
- vars = new ArrayList<>(Arrays.asList(copyFrom.vars));
+ vars = VarList.of(copyFrom.vars);
beanStore = BeanStore.of(copyFrom.beanStore);
}
@@ -157,7 +162,7 @@ public class VarResolver {
* @param copyFrom The builder to copy from.
*/
protected Builder(Builder copyFrom) {
- vars = new ArrayList<>(copyFrom.vars);
+ vars = copyFrom.vars.copy();
beanStore = BeanStore.of(copyFrom.beanStore);
impl = copyFrom.impl;
}
@@ -170,9 +175,9 @@ public class VarResolver {
* These classes must subclass from {@link Var} and have
no-arg constructors.
* @return This object .
*/
- public Builder vars(Class<?>...values) {
- for (Class<?> v : values)
- vars.add(castOrCreate(Var.class, v));
+ @SafeVarargs
+ public final Builder vars(Class<? extends Var>...values) {
+ vars.append(values);
return this;
}
@@ -185,7 +190,7 @@ public class VarResolver {
* @return This object .
*/
public Builder vars(Var...values) {
- vars.addAll(Arrays.asList(values));
+ vars.append(values);
return this;
}
@@ -198,8 +203,7 @@ public class VarResolver {
* @return This object .
*/
public Builder vars(VarList values) {
- for (Object o : values)
- vars.add(castOrCreate(Var.class, o));
+ vars.append(values);
return this;
}
@@ -240,22 +244,8 @@ public class VarResolver {
* @return This object .
*/
public Builder defaultVars() {
- return vars(
- SystemPropertiesVar.class,
- EnvVariablesVar.class,
- ManifestFileVar.class,
- ArgsVar.class,
- SwitchVar.class,
- IfVar.class,
- CoalesceVar.class,
- PatternMatchVar.class,
- PatternReplaceVar.class,
- PatternExtractVar.class,
- UpperCaseVar.class,
- LowerCaseVar.class,
- NotEmptyVar.class,
- LenVar.class,
- SubstringVar.class);
+ vars.addDefault();
+ return this;
}
/**
diff --git
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContext.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContext.java
index c29107e..556cc2a 100644
---
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContext.java
+++
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContext.java
@@ -46,7 +46,6 @@ import org.apache.juneau.collections.*;
import org.apache.juneau.config.*;
import org.apache.juneau.cp.*;
import org.apache.juneau.dto.swagger.Swagger;
-import org.apache.juneau.html.*;
import org.apache.juneau.html.annotation.*;
import org.apache.juneau.http.annotation.Response;
import org.apache.juneau.httppart.*;
@@ -64,7 +63,6 @@ import org.apache.juneau.rest.logging.*;
import org.apache.juneau.http.header.*;
import org.apache.juneau.http.response.*;
import org.apache.juneau.rest.util.*;
-import org.apache.juneau.rest.vars.*;
import org.apache.juneau.svl.*;
import org.apache.juneau.utils.*;
@@ -272,7 +270,10 @@ public class RestContext extends Context {
Messages m = messages = createMessages(r, builder);
- VarResolver vr = varResolver = createVarResolver(r,
builder, bf, m);
+ VarResolver vr = varResolver = builder
+ .varResolver()
+ .bean(Messages.class, messages)
+ .build();
bf.addBean(VarResolver.class, vr);
config = builder.config.resolving(vr.createSession());
@@ -1296,7 +1297,7 @@ public class RestContext extends Context {
* <br>Created by {@link
#createBeanStore(Object,RestContextBuilder,RestContext)}.
* @param fileFinder The file finder configured on this bean created by
{@link #createFileFinder(Object,RestContextBuilder,BeanStore)}.
* @param messages The localized messages configured on this bean
created by {@link #createMessages(Object,RestContextBuilder)}.
- * @param varResolver The variable resolver configured on this bean
created by {@link
#createVarResolver(Object,RestContextBuilder,BeanStore,Messages)}.
+ * @param varResolver The variable resolver configured on this bean.
* @return The info provider for this REST resource.
* @throws Exception If info provider could not be instantiated.
*/
@@ -1340,7 +1341,7 @@ public class RestContext extends Context {
* <br>Created by {@link
#createBeanStore(Object,RestContextBuilder,RestContext)}.
* @param fileFinder The file finder configured on this bean created by
{@link #createFileFinder(Object,RestContextBuilder,BeanStore)}.
* @param messages The localized messages configured on this bean
created by {@link #createMessages(Object,RestContextBuilder)}.
- * @param varResolver The variable resolver configured on this bean
created by {@link
#createVarResolver(Object,RestContextBuilder,BeanStore,Messages)}.
+ * @param varResolver The variable resolver configured on this bean.
* @return The REST API builder for this REST resource.
* @throws Exception If REST API builder could not be instantiated.
*/
@@ -1370,117 +1371,6 @@ public class RestContext extends Context {
}
/**
- * Instantiates the variable resolver for this REST resource.
- *
- * <p>
- * Instantiates based on the following logic:
- * <ul>
- * <li>Looks for a static or non-static <c>createVarResolver()</>
method that returns <c>{@link VarResolver}</c> on the
- * resource class with any of the following arguments:
- * <ul>
- * <li>{@link RestContext}
- * <li>{@link BeanStore}
- * <li>Any {@doc RestInjection injected beans}.
- * </ul>
- * <li>Resolves it via the bean store registered in this context.
- * <li>Instantiates a new {@link VarResolver} using the variables
returned by {@link #createVars(Object,RestContextBuilder,BeanStore)}.
- * </ul>
- *
- * @param resource
- * The REST servlet or bean that this context defines.
- * @param builder
- * The builder for this object.
- * @param beanStore
- * The factory used for creating beans and retrieving injected
beans.
- * <br>Created by {@link
#createBeanStore(Object,RestContextBuilder,RestContext)}.
- * @param messages The localized messages of this bean.
- * @return The variable resolver for this REST resource.
- * @throws Exception If variable resolver could not be instantiated.
- */
- protected VarResolver createVarResolver(Object resource,
RestContextBuilder builder, BeanStore beanStore, Messages messages) throws
Exception {
-
- VarResolver x =
beanStore.getBean(VarResolver.class).orElse(null);
-
- if (x == null)
- x = builder.varResolver
- .vars(createVars(resource, builder, beanStore))
- .bean(Messages.class, messages)
- .build();
-
- x = BeanStore
- .of(beanStore, resource)
- .addBean(VarResolver.class, x)
- .beanCreateMethodFinder(VarResolver.class, resource)
- .find("createVarResolver")
- .withDefault(x)
- .run();
-
- return x;
- }
-
- /**
- * Instantiates the variable resolver variables for this REST resource.
- *
- * <p>
- * Instantiates based on the following logic:
- * <ul>
- * <li>Looks for a static or non-static <c>createVars()</> method
that returns <c>{@link VarList}</c> on the
- * resource class with any of the following arguments:
- * <ul>
- * <li>{@link RestContext}
- * <li>{@link BeanStore}
- * <li>Any {@doc RestInjection injected beans}.
- * </ul>
- * <li>Resolves it via the bean store registered in this context.
- * <li>Instantiates a new {@link VarList} using default variables.
- * </ul>
- *
- * @param resource
- * The REST servlet or bean that this context defines.
- * @param builder
- * The builder for this object.
- * @param beanStore
- * The factory used for creating beans and retrieving injected
beans.
- * <br>Created by {@link
#createBeanStore(Object,RestContextBuilder,RestContext)}.
- * @return The variable resolver variables for this REST resource.
- * @throws Exception If variable resolver variables could not be
instantiated.
- */
- @SuppressWarnings("unchecked")
- protected VarList createVars(Object resource, RestContextBuilder
builder, BeanStore beanStore) throws Exception {
-
- VarList x = beanStore.getBean(VarList.class).orElse(null);
-
- if (x == null)
- x = VarList.of(
- FileVar.class,
- LocalizationVar.class,
- RequestAttributeVar.class,
- RequestFormDataVar.class,
- RequestHeaderVar.class,
- RequestPathVar.class,
- RequestQueryVar.class,
- RequestVar.class,
- RequestSwaggerVar.class,
- SerializedRequestAttrVar.class,
- ServletInitParamVar.class,
- SwaggerVar.class,
- UrlVar.class,
- UrlEncodeVar.class,
- HtmlWidgetVar.class
- );
-
- x = BeanStore
- .of(beanStore, resource)
- .addBean(VarList.class, x)
- .beanCreateMethodFinder(VarList.class, resource)
- .find("createVars")
- .withDefault(x)
- .run();
-
- return x;
- }
-
- /**
* Instantiates the thrown exception store for this REST resource.
*
* <p>
diff --git
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContextBuilder.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContextBuilder.java
index 42500b6..89d12c9 100644
---
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContextBuilder.java
+++
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContextBuilder.java
@@ -37,6 +37,7 @@ import org.apache.juneau.config.*;
import org.apache.juneau.config.vars.*;
import org.apache.juneau.cp.*;
import org.apache.juneau.encoders.*;
+import org.apache.juneau.html.*;
import org.apache.juneau.http.header.*;
import org.apache.juneau.http.response.*;
import org.apache.juneau.httppart.*;
@@ -124,7 +125,7 @@ public class RestContextBuilder extends ContextBuilder
implements ServletConfig
ServletContext servletContext;
Config config;
- VarResolver.Builder varResolver;
+ private VarResolver.Builder varResolver;
String
allowedHeaderParams = env("RestContext.allowedHeaderParams",
"Accept,Content-Type"),
allowedMethodHeaders = env("RestContext.allowedMethodHeaders",
""),
@@ -405,18 +406,23 @@ public class RestContextBuilder extends ContextBuilder
implements ServletConfig
* <p>
* Instantiates based on the following logic:
* <ol>
- * <li>Looks for the following method on the resource class and
sets it as the implementation bean:
+ * <li>
+ * Looks for the following method on the resource class
and sets it as the implementation bean:
* <br><c><jk>public static</jk> VarResolver
createVarResolver(<any-bean-types-in-bean-store>) {}</c>
- * <li>Looks for bean of type {@link
org.apache.juneau.svl.VarResolver} in bean store and sets it as the
implementation bean.
- * <li>Looks for the following method on the resource class:
+ * <li>
+ * Looks for bean of type {@link
org.apache.juneau.svl.VarResolver} in bean store and sets it as the
implementation bean.
+ * <li>
+ * Looks for the following method on the resource class:
* <br><c><jk>public static</jk> VarResolver.Builder
createVarResolver(<any-bean-types-in-bean-store>) {}</c>
- * <li>Looks for bean of type {@link
org.apache.juneau.svl.VarResolver.Builder} in bean store and returns a copy of
it.
- * <li>Creates a default builder with default variables.
+ * <li>
+ * Looks for bean of type {@link
org.apache.juneau.svl.VarResolver.Builder} in bean store and returns a copy of
it.
+ * <li>
+ * Creates a default builder with default variables pulled
from {@link #createVars(Class,BeanStore)}.
* </ol>
*
* @param resourceClass The resource class.
* @param beanStore The bean store containing injected beans.
- * @return A new bean store.
+ * @return A new var resolver builder.
*/
protected VarResolver.Builder createVarResolver(Class<?> resourceClass,
BeanStore beanStore) {
@@ -433,7 +439,7 @@ public class RestContextBuilder extends ContextBuilder
implements ServletConfig
if (x == null) {
x = VarResolver.create()
.defaultVars()
- .vars(ConfigVar.class)
+ .vars(createVars(resourceClass, beanStore))
.vars(FileVar.class)
.bean(FileFinder.class,
FileFinder.create().cp(resourceClass,null,true).build());
}
@@ -444,6 +450,7 @@ public class RestContextBuilder extends ContextBuilder
implements ServletConfig
BeanStore
.of(beanStore)
+ .addBean(VarResolver.Builder.class, x)
.beanCreateMethodFinder(VarResolver.class,
resourceClass)
.find("createVarResolver")
.execute()
@@ -453,6 +460,60 @@ public class RestContextBuilder extends ContextBuilder
implements ServletConfig
}
/**
+ * Instantiates the variable resolver variables for this REST resource.
+ *
+ * <p>
+ * Instantiates based on the following logic:
+ * <ol>
+ * <li>
+ * Looks for the following method on the resource class:
+ * <br><c><jk>public static</jk> VarList
createVars(<any-bean-types-in-bean-store>) {}</c>
+ * <li>
+ * Looks for bean of type {@link
org.apache.juneau.svl.VarList} in bean store and returns a copy of it.
+ * <li>
+ * Creates a default builder with default variables.
+ * </ol>
+ *
+ * @param resourceClass The resource class.
+ * @param beanStore The bean store containing injected beans.
+ * @return A new var resolver variable list.
+ */
+ protected VarList createVars(Class<?> resourceClass, BeanStore
beanStore) {
+
+ VarList x = beanStore.getBean(VarList.class).map(y ->
y.copy()).orElse(null);
+
+ if (x == null)
+ x = VarList.of(
+ ConfigVar.class,
+ FileVar.class,
+ LocalizationVar.class,
+ RequestAttributeVar.class,
+ RequestFormDataVar.class,
+ RequestHeaderVar.class,
+ RequestPathVar.class,
+ RequestQueryVar.class,
+ RequestVar.class,
+ RequestSwaggerVar.class,
+ SerializedRequestAttrVar.class,
+ ServletInitParamVar.class,
+ SwaggerVar.class,
+ UrlVar.class,
+ UrlEncodeVar.class,
+ HtmlWidgetVar.class
+ ).addDefault();
+
+ x = BeanStore
+ .of(beanStore, resourceClass)
+ .addBean(VarList.class, x)
+ .beanCreateMethodFinder(VarList.class, resourceClass)
+ .find("createVars")
+ .withDefault(x)
+ .run();
+
+ return x;
+ }
+
+ /**
* Creates the config for this builder.
*
* @param resourceClass The resource class.
diff --git
a/juneau-utest/src/test/java/org/apache/juneau/utils/StringVarResolverTest.java
b/juneau-utest/src/test/java/org/apache/juneau/utils/StringVarResolverTest.java
index d0a0716..c85bc99 100755
---
a/juneau-utest/src/test/java/org/apache/juneau/utils/StringVarResolverTest.java
+++
b/juneau-utest/src/test/java/org/apache/juneau/utils/StringVarResolverTest.java
@@ -217,14 +217,15 @@ public class StringVarResolverTest {
//====================================================================================================
// Test false triggers.
//====================================================================================================
+ @SuppressWarnings("unchecked")
@Test
public void testFalseTriggers() throws Exception {
VarResolver.Builder vrb = VarResolver.create().defaultVars();
String in = null;
// Should reject names with characters outside A-Za-z
- for (Class<?> c : new Class[]{InvalidVar1.class,
InvalidVar2.class, InvalidVar3.class, InvalidVar4.class, InvalidVar5.class}) {
- assertThrown(()->vrb.vars(c)).exists();
+ for (Class<? extends Var> c : new Class[]{InvalidVar1.class,
InvalidVar2.class, InvalidVar3.class, InvalidVar4.class, InvalidVar5.class}) {
+ assertThrown(()->vrb.copy().vars(c).build()).exists();
}
VarResolver vr = vrb.build();