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 22b975f Context API refactoring.
22b975f is described below
commit 22b975f371e7ac7a14c41f1fd0000b4aff4284d8
Author: JamesBognar <[email protected]>
AuthorDate: Sat Oct 2 11:17:16 2021 -0400
Context API refactoring.
---
.../apache/juneau/dto/swagger/ui/SwaggerUI.java | 3 +-
.../src/main/java/org/apache/juneau/Context.java | 30 ++-
.../java/org/apache/juneau/SessionProperties.java | 247 ---------------------
.../apache/juneau/rest/client/ResponseBody.java | 2 +-
.../test/java/org/apache/juneau/rest/Nls_Test.java | 2 +-
.../juneau/rest/annotation/RestHook_Test.java | 15 +-
.../juneau/rest/annotation/Rest_RVars_Test.java | 8 +-
7 files changed, 36 insertions(+), 271 deletions(-)
diff --git
a/juneau-core/juneau-dto/src/main/java/org/apache/juneau/dto/swagger/ui/SwaggerUI.java
b/juneau-core/juneau-dto/src/main/java/org/apache/juneau/dto/swagger/ui/SwaggerUI.java
index 1264f54..5a12536 100644
---
a/juneau-core/juneau-dto/src/main/java/org/apache/juneau/dto/swagger/ui/SwaggerUI.java
+++
b/juneau-core/juneau-dto/src/main/java/org/apache/juneau/dto/swagger/ui/SwaggerUI.java
@@ -93,8 +93,7 @@ public class SwaggerUI extends PojoSwap<Swagger,Div> {
Session(BeanSession bs, Swagger swagger) {
this.swagger = swagger.copy();
- SessionProperties sp = bs.getSessionProperties();
- this.resolveRefsMaxDepth =
sp.getInteger(SWAGGERUI_resolveRefsMaxDepth).orElse(1);
+ this.resolveRefsMaxDepth =
bs.getSessionProperties().getInt(SWAGGERUI_resolveRefsMaxDepth, 1);
}
}
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/Context.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/Context.java
index 5a64ff9..1b2e8f1 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/Context.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/Context.java
@@ -55,7 +55,21 @@ import org.apache.juneau.xml.annotation.*;
/**
* Base class for all Context beans.
*
- * {@review}
+ * <p>
+ * Context beans follow the convention of having the following parts:
+ * <ul>
+ * <li>A {@link Builder} class for configuring the context bean.
+ * <ul>
+ * <li>This bean is non-thread-safe and meant for one-time use.
+ * </ul>
+ * <li>A {@link Context#Context(Builder)} constructor that takes in a
builder object.
+ * <ul>
+ * <li>This bean is thread-safe and cacheable/reusable.
+ * </ul>
+ * <li>A {@link Session} class for doing work.
+ * <ul>
+ * <li>This bean is non-thread-safe and meant for one-time use.
+ * </ul>
*/
public abstract class Context implements MetaProvider {
@@ -730,9 +744,9 @@ public abstract class Context implements MetaProvider {
*/
public static abstract class Session {
- private final SessionProperties properties;
+ private final OMap properties;
private Map<String,Object> cache;
- private List<String> warnings; // Any warnings
encountered.
+ private List<String> warnings; // Any warnings encountered.
private final Context ctx;
private final boolean debug;
@@ -749,7 +763,7 @@ public abstract class Context implements MetaProvider {
protected Session(Context ctx, Args args) {
this.ctx = ctx;
this.unmodifiable = args.unmodifiable;
- SessionProperties sp = args.properties;
+ OMap sp = args.properties;
if (args.unmodifiable)
sp = sp.unmodifiable();
properties = sp;
@@ -761,7 +775,7 @@ public abstract class Context implements MetaProvider {
*
* @return The session properties on this session. Never
<jk>null</jk>.
*/
- public final SessionProperties getSessionProperties() {
+ public final OMap getSessionProperties() {
return properties;
}
@@ -913,7 +927,7 @@ public abstract class Context implements MetaProvider {
@FluentSetters
public static class Args {
- SessionProperties properties = SessionProperties.create();
+ OMap properties = OMap.create();
boolean unmodifiable;
Boolean debug;
@@ -975,7 +989,7 @@ public abstract class Context implements MetaProvider {
*/
@FluentSetter
public Args properties(Map<String,Object> value) {
- this.properties = SessionProperties.create(value);
+ this.properties = OMap.of(value);
return this;
}
@@ -1013,7 +1027,7 @@ public abstract class Context implements MetaProvider {
.create()
.filtered()
.append("Args", OMap.create().filtered()
- .append("properties",
properties.asMap())
+ .append("properties", properties)
);
}
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/SessionProperties.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/SessionProperties.java
deleted file mode 100644
index b2c5353..0000000
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/SessionProperties.java
+++ /dev/null
@@ -1,247 +0,0 @@
-//
***************************************************************************************************************************
-// * 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.apache.juneau;
-
-import static org.apache.juneau.internal.ClassUtils.*;
-
-import java.lang.reflect.*;
-import java.util.*;
-
-import org.apache.juneau.collections.*;
-import org.apache.juneau.internal.*;
-
-/**
- * Contains the configurable properties for a single context session.
- */
-public class SessionProperties {
-
- private final OMap map;
-
- /**
- * Static creator.
- *
- * @return A new instance of this class.
- */
- public static SessionProperties create() {
- return new SessionProperties(null);
- }
-
- /**
- * Static creator.
- *
- * @param inner The initial contents of these properties.
- * @return A new instance of this class.
- */
- public static SessionProperties create(Map<String,Object> inner) {
- return new SessionProperties(inner);
- }
-
- /**
- * Constructor.
- */
- private SessionProperties(Map<String,Object> inner) {
- this(inner, false);
- }
-
- private SessionProperties(Map<String,Object> inner, boolean
unmodifiable) {
- OMap m = inner == null ? new OMap() : inner instanceof OMap ?
(OMap)inner : new OMap(inner);
- if (unmodifiable)
- m = m.unmodifiable();
- this.map = m;
- }
-
- /**
- * Returns an unmodifiable copy of these properties.
- *
- * @return An unmodifiable copy of these properties.
- */
- public final SessionProperties unmodifiable() {
- return new SessionProperties(map, true);
- }
-
- /**
- * Returns <jk>true</jk> if this session has the specified property
defined.
- *
- * @param key The property key.
- * @return <jk>true</jk> if this session has the specified property
defined.
- */
- public final boolean contains(String key) {
- return map.containsKey(key);
- }
-
- /**
- * Returns the property with the specified name.
- *
- * @param key The property name.
- * @return The property value. Never <jk>null</jk>.
- */
- public final Optional<Object> get(String key) {
- return Optional.ofNullable(map.get(key));
- }
-
- /**
- * Returns the session property with the specified key and type.
- *
- * @param key The property key.
- * @param type The type to convert the property to.
- * @return The session property.
- */
- public final <T> Optional<T> get(String key, Class<T> type) {
- return Optional.ofNullable(find(key, type));
- }
-
- /**
- * Shortcut for calling <code>get(key, String.<jk>class</jk>)</code>.
- *
- * @param key The property name.
- * @return The property value, never <jk>null</jk>.
- */
- public final Optional<String> getString(String key) {
- return Optional.ofNullable(find(key, String.class));
- }
-
- /**
- * Shortcut for calling <code>get(key, Boolean.<jk>class</jk>)</code>.
- *
- * @param key The property name.
- * @return The property value, never <jk>null</jk>.
- */
- public final Optional<Boolean> getBoolean(String key) {
- return Optional.ofNullable(find(key, Boolean.class));
- }
-
- /**
- * Shortcut for calling <code>get(key, Integer.<jk>class</jk>)</code>.
- *
- * @param key The property name.
- * @return The property value, never <jk>null</jk>.
- */
- public final Optional<Integer> getInteger(String key) {
- return Optional.ofNullable(find(key, Integer.class));
- }
-
- /**
- * Returns the session property as the specified instance type with the
specified key.
- *
- * @param key The property key.
- * @param type The type to convert the property to.
- * @return The session property.
- */
- public <T> Optional<T> getInstance(String key, Class<T> type) {
- return Optional.ofNullable(newInstance(type, map.get(key)));
- }
-
- /**
- * Returns the specified property as an array of instantiated objects.
- *
- * @param key The property name.
- * @param type The class type of the property.
- * @return A new property instance.
- */
- @SuppressWarnings("unchecked")
- public <T> Optional<T[]> getInstanceArray(String key, Class<T> type) {
- Object o = map.get(key);
- if (o == null)
- return Optional.empty();
- T[] t = null;
- if (o.getClass().isArray()) {
- if (o.getClass().getComponentType() == type)
- t = (T[])o;
- else {
- t = (T[])Array.newInstance(type,
Array.getLength(o));
- for (int i = 0; i < Array.getLength(o); i++)
- t[i] = newInstance(type, Array.get(o,
i));
- }
- } else if (o instanceof Collection) {
- Collection<?> c = (Collection<?>)o;
- t = (T[])Array.newInstance(type, c.size());
- int i = 0;
- for (Object o2 : c)
- t[i++] = newInstance(type, o2);
- }
- return Optional.ofNullable(t);
- }
-
- /**
- * Returns all the keys in these properties.
- *
- * @return All the keys in these properties.
- */
- public Set<String> keySet() {
- return map.keySet();
- }
-
- /**
- * Returns the contents of this session as an unmodifiable map.
- *
- * @return The contents of this session as an unmodifiable map.
- */
- public Map<String,Object> asMap() {
- return Collections.unmodifiableMap(map);
- }
-
- private <T> T find(String key, Class<T> c) {
- return newInstance(c, map.get(key));
- }
-
- @SuppressWarnings("unchecked")
- private <T> T newInstance(Class<T> type, Object o) {
- T t = null;
- if (o == null)
- return null;
- else if (type.isInstance(o))
- t = (T)o;
- else if (o.getClass() == Class.class)
- t = castOrCreate(type, o);
- else if (o.getClass() == String.class)
- t = ClassUtils.fromString(type, o.toString());
- if (t != null)
- return t;
- throw new ConfigException("Could not instantiate type ''{0}''
as type {1}", o, type);
- }
-
- /**
- * Removes a property from this store.
- *
- * @param key The property key.
- * @return This object (for method chaining).
- */
- public SessionProperties remove(String key) {
- map.remove(key);
- return this;
- }
-
- /**
- * Adds a property to this store.
- *
- * @param key The property key.
- * @param value The property value.
- * @return This object (for method chaining).
- */
- public SessionProperties put(String key, Object value) {
- map.put(key, value);
- return this;
- }
-
- /**
- * Adds multiple properties to this store.
- *
- * @param values The map containing the properties to add.
- * @return This object (for method chaining).
- */
- public SessionProperties putAll(Map<String,Object> values) {
- if (values != null)
- map.putAll(values);
- return this;
- }
-}
diff --git
a/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/ResponseBody.java
b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/ResponseBody.java
index 3a01bd0..3b043e4 100644
---
a/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/ResponseBody.java
+++
b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/ResponseBody.java
@@ -702,7 +702,7 @@ public class ResponseBody implements HttpEntity {
ParserSessionArgs pArgs =
ParserSessionArgs
.create()
- .properties(new
OMap().inner(request.getSessionProperties().asMap()))
+
.properties(OMap.create().inner(request.getSessionProperties()))
.locale(response.getLocale())
.mediaType(mt)
.schema(schema);
diff --git a/juneau-utest/src/test/java/org/apache/juneau/rest/Nls_Test.java
b/juneau-utest/src/test/java/org/apache/juneau/rest/Nls_Test.java
index b70e812..9fac9f3 100644
--- a/juneau-utest/src/test/java/org/apache/juneau/rest/Nls_Test.java
+++ b/juneau-utest/src/test/java/org/apache/juneau/rest/Nls_Test.java
@@ -52,7 +52,7 @@ public class Nls_Test {
}
public static String out(SerializerSession s) {
- return
s.getSessionProperties().getString("TestProperty").orElse(null);
+ return
s.getSessionProperties().getString("TestProperty",null);
}
}
diff --git
a/juneau-utest/src/test/java/org/apache/juneau/rest/annotation/RestHook_Test.java
b/juneau-utest/src/test/java/org/apache/juneau/rest/annotation/RestHook_Test.java
index b8a49a8..965a780 100644
---
a/juneau-utest/src/test/java/org/apache/juneau/rest/annotation/RestHook_Test.java
+++
b/juneau-utest/src/test/java/org/apache/juneau/rest/annotation/RestHook_Test.java
@@ -21,7 +21,6 @@ import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
-import org.apache.juneau.*;
import org.apache.juneau.collections.*;
import org.apache.juneau.http.annotation.Body;
import org.apache.juneau.http.header.*;
@@ -88,8 +87,8 @@ public class RestHook_Test {
}
private static Object in(ReaderParserSession session) {
- SessionProperties sp = session.getSessionProperties();
- return
"p1="+sp.get("p1").orElse(null)+",p2="+sp.get("p2").orElse(null)+",p3="+sp.get("p3").orElse(null)+",p4="+sp.get("p4").orElse(null)+",p5="+sp.get("p5").orElse(null);
+ OMap sp = session.getSessionProperties();
+ return
"p1="+sp.get("p1",null)+",p2="+sp.get("p2",null)+",p3="+sp.get("p3",null)+",p4="+sp.get("p4",null)+",p5="+sp.get("p5",null);
}
}
@@ -159,13 +158,13 @@ public class RestHook_Test {
super(b.produces("test/s1").accept("text/s1,text/s2,text/s3").function((s,o) ->
out(s)).headers(s->headers(s)));
}
public static String out(SerializerSession s) {
- SessionProperties sp = s.getSessionProperties();
- return
"p1="+sp.get("p1").orElse(null)+",p2="+sp.get("p2").orElse(null)+",p3="+sp.get("p3").orElse(null)+",p4="+sp.get("p4").orElse(null)+",p5="+sp.get("p5").orElse(null);
+ OMap sp = s.getSessionProperties();
+ return
"p1="+sp.get("p1",null)+",p2="+sp.get("p2",null)+",p3="+sp.get("p3",null)+",p4="+sp.get("p4",null)+",p5="+sp.get("p5",null);
}
public static Map<String,String> headers(SerializerSession s) {
- SessionProperties sp = s.getSessionProperties();
- if (sp.contains("Override-Content-Type"))
- return
AMap.of("Content-Type",sp.getString("Override-Content-Type").orElse(null));
+ OMap sp = s.getSessionProperties();
+ if (sp.containsKey("Override-Content-Type"))
+ return
AMap.of("Content-Type",sp.getString("Override-Content-Type",null));
return emptyMap();
}
}
diff --git
a/juneau-utest/src/test/java/org/apache/juneau/rest/annotation/Rest_RVars_Test.java
b/juneau-utest/src/test/java/org/apache/juneau/rest/annotation/Rest_RVars_Test.java
index 2fb0a4b..6b97714 100644
---
a/juneau-utest/src/test/java/org/apache/juneau/rest/annotation/Rest_RVars_Test.java
+++
b/juneau-utest/src/test/java/org/apache/juneau/rest/annotation/Rest_RVars_Test.java
@@ -15,7 +15,7 @@ package org.apache.juneau.rest.annotation;
import static java.lang.String.*;
import static org.junit.runners.MethodSorters.*;
-import org.apache.juneau.*;
+import org.apache.juneau.collections.*;
import org.apache.juneau.rest.RestResponse;
import org.apache.juneau.rest.client.*;
import org.apache.juneau.rest.mock.*;
@@ -65,10 +65,10 @@ public class Rest_RVars_Test {
super(b.produces("text/plain").accept("*/*").function((s,o) -> out(s)));
}
public static String out(SerializerSession s) {
- SessionProperties sp = s.getSessionProperties();
+ OMap sp = s.getSessionProperties();
return
format("A1=%s,A2=%s,B1=%s,B2=%s,C=%s,R1a=%s,R1b=%s,R2=%s,R3=%s,R4=%s,R5=%s,R6=%s",
- sp.get("A1").orElse(null),
sp.get("A2").orElse(null), sp.get("B1").orElse(null),
sp.get("B2").orElse(null), sp.get("C").orElse(null),
- sp.get("R1a").orElse(null),
sp.get("R1b").orElse(null), sp.get("R2").orElse(null),
sp.get("R3").orElse(null), sp.get("R4").orElse(null),
sp.get("R5").orElse(null), sp.get("R6").orElse(null));
+ sp.get("A1",null), sp.get("A2",null),
sp.get("B1",null), sp.get("B2",null), sp.get("C",null),
+ sp.get("R1a",null), sp.get("R1b",null),
sp.get("R2",null), sp.get("R3",null), sp.get("R4",null), sp.get("R5",null),
sp.get("R6",null));
}
}
}