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 f29b9f2  REST refactoring.
f29b9f2 is described below

commit f29b9f2478087cdf9cc82295bdd063b8097c690e
Author: JamesBognar <[email protected]>
AuthorDate: Tue Jan 12 10:41:24 2021 -0500

    REST refactoring.
---
 .../org/apache/juneau/rest/mock/MockRestClient.java | 21 +++++++--------------
 .../springboot/BasicSpringRestServletGroup.java     |  2 --
 .../java/org/apache/juneau/rest/RestContext.java    | 18 ++++++++++++++++++
 .../java/org/apache/juneau/rest/RestServlet.java    | 21 ++++++++-------------
 4 files changed, 33 insertions(+), 29 deletions(-)

diff --git 
a/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock/MockRestClient.java
 
b/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock/MockRestClient.java
index 7562a03..f47d9d5 100644
--- 
a/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock/MockRestClient.java
+++ 
b/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock/MockRestClient.java
@@ -32,7 +32,6 @@ import org.apache.http.message.*;
 import org.apache.juneau.*;
 import org.apache.juneau.http.remote.*;
 import org.apache.juneau.parser.*;
-import org.apache.juneau.reflect.*;
 import org.apache.juneau.rest.*;
 import org.apache.juneau.rest.annotation.*;
 import org.apache.juneau.rest.client.*;
@@ -280,19 +279,13 @@ public class MockRestClient extends RestClient implements 
HttpClientConnection {
                        if (! contexts.containsKey(c)) {
                                boolean isClass = restBean instanceof Class;
                                Object o = isClass ? 
((Class<?>)restBean).newInstance() : restBean;
-                               RestContextBuilder rcb = 
RestContext.create(o).callLoggerDefault(BasicTestRestLogger.class).debugDefault(CONDITIONAL);
-                               RestContext rc = rcb.build();
-                               MethodInfo mi = 
ClassInfo.of(o).getMethod("setContext", RestContext.class);
-                               if (mi != null)
-                                       mi.accessible().invoke(o, rc);
-                               if (o instanceof RestServlet) {
-                                       RestServlet rs = (RestServlet)o;
-                                       if (! rs.isInitialized())
-                                               rc.postInit();
-                               } else {
-                                       rc.postInit();
-                               }
-                               rc.postInitChildFirst();
+                               RestContext rc = RestContext
+                                       .create(o)
+                                       
.callLoggerDefault(BasicTestRestLogger.class)
+                                       .debugDefault(CONDITIONAL)
+                                       .build()
+                                       .postInit()
+                                       .postInitChildFirst();
                                contexts.put(c, rc);
                        }
                        RestContext restBeanCtx = contexts.get(c);
diff --git 
a/juneau-rest/juneau-rest-server-springboot/src/main/java/org/apache/juneau/rest/springboot/BasicSpringRestServletGroup.java
 
b/juneau-rest/juneau-rest-server-springboot/src/main/java/org/apache/juneau/rest/springboot/BasicSpringRestServletGroup.java
index 83a6361..a3ab546 100644
--- 
a/juneau-rest/juneau-rest-server-springboot/src/main/java/org/apache/juneau/rest/springboot/BasicSpringRestServletGroup.java
+++ 
b/juneau-rest/juneau-rest-server-springboot/src/main/java/org/apache/juneau/rest/springboot/BasicSpringRestServletGroup.java
@@ -11,8 +11,6 @@ package org.apache.juneau.rest.springboot;
 // * "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.                                              *
 // 
***************************************************************************************************************************
-
-
 import static org.apache.juneau.http.HttpMethod.*;
 
 import org.apache.juneau.rest.*;
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 8a8c3d4..32cb3b7 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
@@ -29,6 +29,7 @@ import java.nio.charset.*;
 import java.time.*;
 import java.util.*;
 import java.util.concurrent.*;
+import java.util.concurrent.atomic.*;
 import java.util.function.*;
 import java.util.logging.*;
 import java.util.stream.*;
@@ -3235,6 +3236,9 @@ public class RestContext extends BeanContext {
 
        private final ReflectionMap<Enablement> debugEnablement;
 
+       // Gets set when postInitChildFirst() gets called.
+       private final AtomicBoolean initialized = new AtomicBoolean(false);
+
        /**
         * Constructor.
         *
@@ -5540,6 +5544,17 @@ public class RestContext extends BeanContext {
         * @throws ServletException Error occurred.
         */
        public synchronized RestContext postInit() throws ServletException {
+               if (initialized.get())
+                       return this;
+               Object resource = getResource();
+               MethodInfo mi = 
ClassInfo.of(getResource()).getMethod("setContext", RestContext.class);
+               if (mi != null) {
+                       try {
+                               mi.accessible().invoke(resource, this);
+                       } catch (ExecutableException e) {
+                               throw new ServletException(e);
+                       }
+               }
                for (int i = 0; i < postInitMethods.length; i++)
                        postInitOrDestroy(getResource(), postInitMethods[i], 
postInitMethodParams[i]);
                for (RestContext childContext : this.childResources.values())
@@ -5554,10 +5569,13 @@ public class RestContext extends BeanContext {
         * @throws ServletException Error occurred.
         */
        public RestContext postInitChildFirst() throws ServletException {
+               if (initialized.get())
+                       return this;
                for (RestContext childContext : this.childResources.values())
                        childContext.postInitChildFirst();
                for (int i = 0; i < postInitChildFirstMethods.length; i++)
                        postInitOrDestroy(getResource(), 
postInitChildFirstMethods[i], postInitChildFirstMethodParams[i]);
+               initialized.set(true);
                return this;
        }
 
diff --git 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestServlet.java
 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestServlet.java
index 99d9dd8..8664f58 100644
--- 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestServlet.java
+++ 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestServlet.java
@@ -79,22 +79,17 @@ public abstract class RestServlet extends HttpServlet {
        /**
         * Sets the context object for this servlet.
         *
+        * <p>
+        * This method is effectively a no-op if {@link #init(ServletConfig)} 
has already been called.
+        *
         * @param context Sets the context object on this servlet.
-        * @throws ServletException If error occurred during post-initialiation.
+        * @throws ServletException If error occurred during initialization.
         */
        protected void setContext(RestContext context) throws ServletException {
-               // This only gets called when not created as a top-level 
servlet.
-               super.init(context.builder);
-               this.context.set(context);
-       }
-
-       /**
-        * Returns <jk>true</jk> if this servlet has been initialized and 
{@link #getContext()} returns a value.
-        *
-        * @return <jk>true</jk> if this servlet has been initialized and 
{@link #getContext()} returns a value.
-        */
-       public boolean isInitialized() {
-               return context.get() != null;
+               if (this.context.get() == null) {
+                       super.init(context.builder);
+                       this.context.set(context);
+               }
        }
 
        /**

Reply via email to