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

commit 6d3c53ae6e8506cf7fde7be004f2aaa14218ceab
Author: JamesBognar <[email protected]>
AuthorDate: Mon Jan 11 17:22:43 2021 -0500

    REST refactoring.
---
 .../apache/juneau/rest/mock/MockRestClient.java    |  9 +++++----
 .../java/org/apache/juneau/rest/RestContext.java   |  7 +++----
 .../java/org/apache/juneau/rest/RestObject.java    | 23 +++++++++++++++-------
 .../java/org/apache/juneau/rest/RestServlet.java   | 14 ++++---------
 4 files changed, 28 insertions(+), 25 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 6543dfe..7562a03 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,6 +32,7 @@ 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.*;
@@ -281,13 +282,13 @@ public class MockRestClient extends RestClient implements 
HttpClientConnection {
                                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()) {
-                                               rs.setContext(rc);
+                                       if (! rs.isInitialized())
                                                rc.postInit();
-                                       }
-                                       rc = rs.getContext();
                                } else {
                                        rc.postInit();
                                }
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 aa8971c..a57f676 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
@@ -3632,11 +3632,10 @@ public class RestContext extends BeanContext {
                                }
 
                                childBuilder.init(r);
-                               if (r instanceof RestServlet)
-                                       
((RestServlet)r).innerInit(childBuilder);
                                RestContext rc2 = childBuilder.build();
-                               if (r instanceof RestServlet)
-                                       ((RestServlet)r).setContext(rc2);
+                               MethodInfo mi = 
ClassInfo.of(r).getMethod("setContext", RestContext.class);
+                               if (mi != null)
+                                       mi.accessible().invoke(r, rc2);
                                path = childBuilder.getPath();
                                childResources.put(path, rc2);
                        }
diff --git 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestObject.java
 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestObject.java
index 5d25abe..42c2aa8 100644
--- 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestObject.java
+++ 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestObject.java
@@ -15,6 +15,7 @@ package org.apache.juneau.rest;
 import static org.apache.juneau.rest.annotation.HookEvent.*;
 
 import java.text.*;
+import java.util.concurrent.atomic.*;
 import java.util.function.*;
 import java.util.logging.*;
 
@@ -39,21 +40,31 @@ import org.apache.juneau.rest.annotation.*;
 public abstract class RestObject {
 
        private Logger logger = Logger.getLogger(getClass().getName());
-       private volatile RestContext context;
+       private AtomicReference<RestContext> context = new AtomicReference<>();
 
        
//-----------------------------------------------------------------------------------------------------------------
        // Context methods.
        
//-----------------------------------------------------------------------------------------------------------------
 
        /**
+        * Sets the context object for this servlet.
+        *
+        * @param context Sets the context object on this servlet.
+        * @throws ServletException If error occurred during post-initialiation.
+        */
+       protected void setContext(RestContext context) throws ServletException {
+               this.context.set(context);
+       }
+
+       /**
         * Returns the read-only context object that contains all the 
configuration information about this resource.
         *
         * @return The context information on this servlet.
         */
-       protected synchronized RestContext getContext() {
-               if (context == null)
+       protected RestContext getContext() {
+               if (context.get() == null)
                        throw new InternalServerError("RestContext object not 
set on resource.");
-               return context;
+               return context.get();
        }
 
        
//-----------------------------------------------------------------------------------------------------------------
@@ -202,9 +213,7 @@ public abstract class RestObject {
         * @throws Exception Any exception thrown will cause servlet to fail 
startup.
         */
        @RestHook(POST_INIT)
-       public void onPostInit(RestContext context) throws Exception {
-               this.context = context;
-       }
+       public void onPostInit(RestContext context) throws Exception {}
 
        /**
         * Identical to {@link #onPostInit(RestContext)} except the order of 
execution is child-resources first.
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 ff4d28f..06d0e13 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
@@ -77,21 +77,15 @@ public abstract class RestServlet extends HttpServlet {
                return new BeanFactory(parent, this);
        }
 
-       /*
-        * Bypasses the init(ServletConfig) method and just calls the 
super.init(ServletConfig) method directly.
-        * Used when subclasses of RestServlet are attached as child resources.
-        */
-       synchronized void innerInit(ServletConfig servletConfig) throws 
ServletException {
-               super.init(servletConfig);
-       }
-
        /**
         * Sets the context object for this servlet.
         *
         * @param context Sets the context object on this servlet.
         * @throws ServletException If error occurred during post-initialiation.
         */
-       public synchronized void setContext(RestContext context) throws 
ServletException {
+       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);
        }
 
@@ -100,7 +94,7 @@ public abstract class RestServlet extends HttpServlet {
         *
         * @return <jk>true</jk> if this servlet has been initialized and 
{@link #getContext()} returns a value.
         */
-       public synchronized boolean isInitialized() {
+       public boolean isInitialized() {
                return context.get() != null;
        }
 

Reply via email to